1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is mozilla.org code.
15  *
16  * The Initial Developer of the Original Code is
17  * Netscape Communications Corporation.
18  * Portions created by the Initial Developer are Copyright (C) 1998
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Akhil Arora <akhil.arora@sun.com>
23  *   Tomi Leppikangas <Tomi.Leppikangas@oulu.fi>
24  *   Darin Fisher <darin@meer.net>
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either the GNU General Public License Version 2 or later (the "GPL"), or
28  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 
40 #ifndef NET_PROXY_PROXY_RESOLVER_SCRIPT_H_
41 #define NET_PROXY_PROXY_RESOLVER_SCRIPT_H_
42 
43 // The following code was formatted from:
44 //   'mozilla/netwerk/base/src/nsProxyAutoConfig.js' (1.55)
45 //
46 // Using the command:
47 //   $ cat nsProxyAutoConfig.js |
48 //       awk '/var pacUtils/,/EOF/' |
49 //       sed -e 's/^\s*$/""/g' |
50 //       sed -e 's/"\s*[+]\s*$/"/g' |
51 //       sed -e 's/"$/" \\/g' |
52 //       sed -e 's/\/(ipaddr);/\/.exec(ipaddr);/g' |
53 //       grep -v '^var pacUtils ='
54 #define PROXY_RESOLVER_SCRIPT \
55   "function dnsDomainIs(host, domain) {\n" \
56   "    return (host.length >= domain.length &&\n" \
57   "            host.substring(host.length - domain.length) == domain);\n" \
58   "}\n" \
59   "" \
60   "function dnsDomainLevels(host) {\n" \
61   "    return host.split('.').length-1;\n" \
62   "}\n" \
63   "" \
64   "function convert_addr(ipchars) {\n" \
65   "    var bytes = ipchars.split('.');\n" \
66   "    var result = ((bytes[0] & 0xff) << 24) |\n" \
67   "                 ((bytes[1] & 0xff) << 16) |\n" \
68   "                 ((bytes[2] & 0xff) <<  8) |\n" \
69   "                  (bytes[3] & 0xff);\n" \
70   "    return result;\n" \
71   "}\n" \
72   "" \
73   "function isInNet(ipaddr, pattern, maskstr) {\n" \
74   "    var test = /^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/.exec(ipaddr);\n" \
75   "    if (test == null) {\n" \
76   "        ipaddr = dnsResolve(ipaddr);\n" \
77   "        if (ipaddr == null)\n" \
78   "            return false;\n" \
79   "    } else if (test[1] > 255 || test[2] > 255 || \n" \
80   "               test[3] > 255 || test[4] > 255) {\n" \
81   "        return false;    // not an IP address\n" \
82   "    }\n" \
83   "    var host = convert_addr(ipaddr);\n" \
84   "    var pat  = convert_addr(pattern);\n" \
85   "    var mask = convert_addr(maskstr);\n" \
86   "    return ((host & mask) == (pat & mask));\n" \
87   "    \n" \
88   "}\n" \
89   "" \
90   "function isPlainHostName(host) {\n" \
91   "    return (host.search('\\\\.') == -1);\n" \
92   "}\n" \
93   "" \
94   "function isResolvable(host) {\n" \
95   "    var ip = dnsResolve(host);\n" \
96   "    return (ip != null);\n" \
97   "}\n" \
98   "" \
99   "function localHostOrDomainIs(host, hostdom) {\n" \
100   "    return (host == hostdom) ||\n" \
101   "           (hostdom.lastIndexOf(host + '.', 0) == 0);\n" \
102   "}\n" \
103   "" \
104   "function shExpMatch(url, pattern) {\n" \
105   "   pattern = pattern.replace(/\\./g, '\\\\.');\n" \
106   "   pattern = pattern.replace(/\\*/g, '.*');\n" \
107   "   pattern = pattern.replace(/\\?/g, '.');\n" \
108   "   var newRe = new RegExp('^'+pattern+'$');\n" \
109   "   return newRe.test(url);\n" \
110   "}\n" \
111   "" \
112   "var wdays = {SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6};\n" \
113   "" \
114   "var months = {JAN: 0, FEB: 1, MAR: 2, APR: 3, MAY: 4, JUN: 5, JUL: 6, AUG: 7, SEP: 8, OCT: 9, NOV: 10, DEC: 11};\n" \
115   "" \
116   "function weekdayRange() {\n" \
117   "    function getDay(weekday) {\n" \
118   "        if (weekday in wdays) {\n" \
119   "            return wdays[weekday];\n" \
120   "        }\n" \
121   "        return -1;\n" \
122   "    }\n" \
123   "    var date = new Date();\n" \
124   "    var argc = arguments.length;\n" \
125   "    var wday;\n" \
126   "    if (argc < 1)\n" \
127   "        return false;\n" \
128   "    if (arguments[argc - 1] == 'GMT') {\n" \
129   "        argc--;\n" \
130   "        wday = date.getUTCDay();\n" \
131   "    } else {\n" \
132   "        wday = date.getDay();\n" \
133   "    }\n" \
134   "    var wd1 = getDay(arguments[0]);\n" \
135   "    var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n" \
136   "    return (wd1 == -1 || wd2 == -1) ? false\n" \
137   "                                    : (wd1 <= wday && wday <= wd2);\n" \
138   "}\n" \
139   "" \
140   "function dateRange() {\n" \
141   "    function getMonth(name) {\n" \
142   "        if (name in months) {\n" \
143   "            return months[name];\n" \
144   "        }\n" \
145   "        return -1;\n" \
146   "    }\n" \
147   "    var date = new Date();\n" \
148   "    var argc = arguments.length;\n" \
149   "    if (argc < 1) {\n" \
150   "        return false;\n" \
151   "    }\n" \
152   "    var isGMT = (arguments[argc - 1] == 'GMT');\n" \
153   "\n" \
154   "    if (isGMT) {\n" \
155   "        argc--;\n" \
156   "    }\n" \
157   "    // function will work even without explict handling of this case\n" \
158   "    if (argc == 1) {\n" \
159   "        var tmp = parseInt(arguments[0]);\n" \
160   "        if (isNaN(tmp)) {\n" \
161   "            return ((isGMT ? date.getUTCMonth() : date.getMonth()) ==\n" \
162   "getMonth(arguments[0]));\n" \
163   "        } else if (tmp < 32) {\n" \
164   "            return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);\n" \
165   "        } else { \n" \
166   "            return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==\n" \
167   "tmp);\n" \
168   "        }\n" \
169   "    }\n" \
170   "    var year = date.getFullYear();\n" \
171   "    var date1, date2;\n" \
172   "    date1 = new Date(year,  0,  1,  0,  0,  0);\n" \
173   "    date2 = new Date(year, 11, 31, 23, 59, 59);\n" \
174   "    var adjustMonth = false;\n" \
175   "    for (var i = 0; i < (argc >> 1); i++) {\n" \
176   "        var tmp = parseInt(arguments[i]);\n" \
177   "        if (isNaN(tmp)) {\n" \
178   "            var mon = getMonth(arguments[i]);\n" \
179   "            date1.setMonth(mon);\n" \
180   "        } else if (tmp < 32) {\n" \
181   "            adjustMonth = (argc <= 2);\n" \
182   "            date1.setDate(tmp);\n" \
183   "        } else {\n" \
184   "            date1.setFullYear(tmp);\n" \
185   "        }\n" \
186   "    }\n" \
187   "    for (var i = (argc >> 1); i < argc; i++) {\n" \
188   "        var tmp = parseInt(arguments[i]);\n" \
189   "        if (isNaN(tmp)) {\n" \
190   "            var mon = getMonth(arguments[i]);\n" \
191   "            date2.setMonth(mon);\n" \
192   "        } else if (tmp < 32) {\n" \
193   "            date2.setDate(tmp);\n" \
194   "        } else {\n" \
195   "            date2.setFullYear(tmp);\n" \
196   "        }\n" \
197   "    }\n" \
198   "    if (adjustMonth) {\n" \
199   "        date1.setMonth(date.getMonth());\n" \
200   "        date2.setMonth(date.getMonth());\n" \
201   "    }\n" \
202   "    if (isGMT) {\n" \
203   "    var tmp = date;\n" \
204   "        tmp.setFullYear(date.getUTCFullYear());\n" \
205   "        tmp.setMonth(date.getUTCMonth());\n" \
206   "        tmp.setDate(date.getUTCDate());\n" \
207   "        tmp.setHours(date.getUTCHours());\n" \
208   "        tmp.setMinutes(date.getUTCMinutes());\n" \
209   "        tmp.setSeconds(date.getUTCSeconds());\n" \
210   "        date = tmp;\n" \
211   "    }\n" \
212   "    return ((date1 <= date) && (date <= date2));\n" \
213   "}\n" \
214   "" \
215   "function timeRange() {\n" \
216   "    var argc = arguments.length;\n" \
217   "    var date = new Date();\n" \
218   "    var isGMT= false;\n" \
219   "\n" \
220   "    if (argc < 1) {\n" \
221   "        return false;\n" \
222   "    }\n" \
223   "    if (arguments[argc - 1] == 'GMT') {\n" \
224   "        isGMT = true;\n" \
225   "        argc--;\n" \
226   "    }\n" \
227   "\n" \
228   "    var hour = isGMT ? date.getUTCHours() : date.getHours();\n" \
229   "    var date1, date2;\n" \
230   "    date1 = new Date();\n" \
231   "    date2 = new Date();\n" \
232   "\n" \
233   "    if (argc == 1) {\n" \
234   "        return (hour == arguments[0]);\n" \
235   "    } else if (argc == 2) {\n" \
236   "        return ((arguments[0] <= hour) && (hour <= arguments[1]));\n" \
237   "    } else {\n" \
238   "        switch (argc) {\n" \
239   "        case 6:\n" \
240   "            date1.setSeconds(arguments[2]);\n" \
241   "            date2.setSeconds(arguments[5]);\n" \
242   "        case 4:\n" \
243   "            var middle = argc >> 1;\n" \
244   "            date1.setHours(arguments[0]);\n" \
245   "            date1.setMinutes(arguments[1]);\n" \
246   "            date2.setHours(arguments[middle]);\n" \
247   "            date2.setMinutes(arguments[middle + 1]);\n" \
248   "            if (middle == 2) {\n" \
249   "                date2.setSeconds(59);\n" \
250   "            }\n" \
251   "            break;\n" \
252   "        default:\n" \
253   "          throw 'timeRange: bad number of arguments'\n" \
254   "        }\n" \
255   "    }\n" \
256   "\n" \
257   "    if (isGMT) {\n" \
258   "        date.setFullYear(date.getUTCFullYear());\n" \
259   "        date.setMonth(date.getUTCMonth());\n" \
260   "        date.setDate(date.getUTCDate());\n" \
261   "        date.setHours(date.getUTCHours());\n" \
262   "        date.setMinutes(date.getUTCMinutes());\n" \
263   "        date.setSeconds(date.getUTCSeconds());\n" \
264   "    }\n" \
265   "    return ((date1 <= date) && (date <= date2));\n" \
266   "}\n"
267 
268 // This is a Microsoft extension to PAC for IPv6, see:
269 // http://blogs.msdn.com/b/wndp/archive/2006/07/13/ipv6-pac-extensions-v0-9.aspx
270 #define PROXY_RESOLVER_SCRIPT_EX \
271   "function isResolvableEx(host) {\n" \
272   "    var ipList = dnsResolveEx(host);\n" \
273   "    return (ipList != '');\n" \
274   "}\n"
275 
276 #endif  // NET_PROXY_PROXY_RESOLVER_SCRIPT_H_
277