1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_PROXY_PROXY_RESOLVER_SCRIPT_H_
6 #define NET_PROXY_PROXY_RESOLVER_SCRIPT_H_
8 /* ***** BEGIN LICENSE BLOCK *****
9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
11 * The contents of this file are subject to the Mozilla Public License Version
12 * 1.1 (the "License"); you may not use this file except in compliance with
13 * the License. You may obtain a copy of the License at
14 * http://www.mozilla.org/MPL/
16 * Software distributed under the License is distributed on an "AS IS" basis,
17 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
18 * for the specific language governing rights and limitations under the
21 * The Original Code is mozilla.org code.
23 * The Initial Developer of the Original Code is
24 * Netscape Communications Corporation.
25 * Portions created by the Initial Developer are Copyright (C) 1998
26 * the Initial Developer. All Rights Reserved.
29 * Akhil Arora <akhil.arora@sun.com>
30 * Tomi Leppikangas <Tomi.Leppikangas@oulu.fi>
31 * Darin Fisher <darin@meer.net>
33 * Alternatively, the contents of this file may be used under the terms of
34 * either the GNU General Public License Version 2 or later (the "GPL"), or
35 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
36 * in which case the provisions of the GPL or the LGPL are applicable instead
37 * of those above. If you wish to allow use of your version of this file only
38 * under the terms of either the GPL or the LGPL, and not to allow others to
39 * use your version of this file under the terms of the MPL, indicate your
40 * decision by deleting the provisions above and replace them with the notice
41 * and other provisions required by the GPL or the LGPL. If you do not delete
42 * the provisions above, a recipient may use your version of this file under
43 * the terms of any one of the MPL, the GPL or the LGPL.
45 * ***** END LICENSE BLOCK ***** */
47 // The following code was formatted from:
48 // 'mozilla/netwerk/base/src/nsProxyAutoConfig.js' (1.55)
51 // $ cat nsProxyAutoConfig.js |
52 // awk '/var pacUtils/,/EOF/' |
53 // sed -e 's/^\s*$/""/g' |
54 // sed -e 's/"\s*[+]\s*$/"/g' |
55 // sed -e 's/"$/" \\/g' |
56 // sed -e 's/\/(ipaddr);/\/.exec(ipaddr);/g' |
57 // grep -v '^var pacUtils ='
59 // isPlainHost() was removed.
60 #define PROXY_RESOLVER_SCRIPT \
61 "function dnsDomainIs(host, domain) {\n" \
62 " return (host.length >= domain.length &&\n" \
63 " host.substring(host.length - domain.length) == domain);\n" \
66 "function dnsDomainLevels(host) {\n" \
67 " return host.split('.').length-1;\n" \
70 "function convert_addr(ipchars) {\n" \
71 " var bytes = ipchars.split('.');\n" \
72 " var result = ((bytes[0] & 0xff) << 24) |\n" \
73 " ((bytes[1] & 0xff) << 16) |\n" \
74 " ((bytes[2] & 0xff) << 8) |\n" \
75 " (bytes[3] & 0xff);\n" \
79 "function isInNet(ipaddr, pattern, maskstr) {\n" \
80 " var test = /^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/.exec(ipaddr);\n" \
81 " if (test == null) {\n" \
82 " ipaddr = dnsResolve(ipaddr);\n" \
83 " if (ipaddr == null)\n" \
85 " } else if (test[1] > 255 || test[2] > 255 || \n" \
86 " test[3] > 255 || test[4] > 255) {\n" \
87 " return false; // not an IP address\n" \
89 " var host = convert_addr(ipaddr);\n" \
90 " var pat = convert_addr(pattern);\n" \
91 " var mask = convert_addr(maskstr);\n" \
92 " return ((host & mask) == (pat & mask));\n" \
96 "function isResolvable(host) {\n" \
97 " var ip = dnsResolve(host);\n" \
98 " return (ip != null);\n" \
101 "function localHostOrDomainIs(host, hostdom) {\n" \
102 " return (host == hostdom) ||\n" \
103 " (hostdom.lastIndexOf(host + '.', 0) == 0);\n" \
106 "function shExpMatch(url, pattern) {\n" \
107 " pattern = pattern.replace(/\\./g, '\\\\.');\n" \
108 " pattern = pattern.replace(/\\*/g, '.*');\n" \
109 " pattern = pattern.replace(/\\?/g, '.');\n" \
110 " var newRe = new RegExp('^'+pattern+'$');\n" \
111 " return newRe.test(url);\n" \
114 "var wdays = {SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6};\n" \
116 "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" \
118 "function weekdayRange() {\n" \
119 " function getDay(weekday) {\n" \
120 " if (weekday in wdays) {\n" \
121 " return wdays[weekday];\n" \
125 " var date = new Date();\n" \
126 " var argc = arguments.length;\n" \
130 " if (arguments[argc - 1] == 'GMT') {\n" \
132 " wday = date.getUTCDay();\n" \
134 " wday = date.getDay();\n" \
136 " var wd1 = getDay(arguments[0]);\n" \
137 " var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n" \
138 " return (wd1 == -1 || wd2 == -1) ? false\n" \
139 " : (wd1 <= wday && wday <= wd2);\n" \
142 "function dateRange() {\n" \
143 " function getMonth(name) {\n" \
144 " if (name in months) {\n" \
145 " return months[name];\n" \
149 " var date = new Date();\n" \
150 " var argc = arguments.length;\n" \
151 " if (argc < 1) {\n" \
154 " var isGMT = (arguments[argc - 1] == 'GMT');\n" \
159 " // function will work even without explict handling of this case\n" \
160 " if (argc == 1) {\n" \
161 " var tmp = parseInt(arguments[0]);\n" \
162 " if (isNaN(tmp)) {\n" \
163 " return ((isGMT ? date.getUTCMonth() : date.getMonth()) ==\n" \
164 "getMonth(arguments[0]));\n" \
165 " } else if (tmp < 32) {\n" \
166 " return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);\n" \
168 " return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==\n" \
172 " var year = date.getFullYear();\n" \
173 " var date1, date2;\n" \
174 " date1 = new Date(year, 0, 1, 0, 0, 0);\n" \
175 " date2 = new Date(year, 11, 31, 23, 59, 59);\n" \
176 " var adjustMonth = false;\n" \
177 " for (var i = 0; i < (argc >> 1); i++) {\n" \
178 " var tmp = parseInt(arguments[i]);\n" \
179 " if (isNaN(tmp)) {\n" \
180 " var mon = getMonth(arguments[i]);\n" \
181 " date1.setMonth(mon);\n" \
182 " } else if (tmp < 32) {\n" \
183 " adjustMonth = (argc <= 2);\n" \
184 " date1.setDate(tmp);\n" \
186 " date1.setFullYear(tmp);\n" \
189 " for (var i = (argc >> 1); i < argc; i++) {\n" \
190 " var tmp = parseInt(arguments[i]);\n" \
191 " if (isNaN(tmp)) {\n" \
192 " var mon = getMonth(arguments[i]);\n" \
193 " date2.setMonth(mon);\n" \
194 " } else if (tmp < 32) {\n" \
195 " date2.setDate(tmp);\n" \
197 " date2.setFullYear(tmp);\n" \
200 " if (adjustMonth) {\n" \
201 " date1.setMonth(date.getMonth());\n" \
202 " date2.setMonth(date.getMonth());\n" \
205 " var tmp = date;\n" \
206 " tmp.setFullYear(date.getUTCFullYear());\n" \
207 " tmp.setMonth(date.getUTCMonth());\n" \
208 " tmp.setDate(date.getUTCDate());\n" \
209 " tmp.setHours(date.getUTCHours());\n" \
210 " tmp.setMinutes(date.getUTCMinutes());\n" \
211 " tmp.setSeconds(date.getUTCSeconds());\n" \
214 " return ((date1 <= date) && (date <= date2));\n" \
217 "function timeRange() {\n" \
218 " var argc = arguments.length;\n" \
219 " var date = new Date();\n" \
220 " var isGMT= false;\n" \
222 " if (argc < 1) {\n" \
225 " if (arguments[argc - 1] == 'GMT') {\n" \
230 " var hour = isGMT ? date.getUTCHours() : date.getHours();\n" \
231 " var date1, date2;\n" \
232 " date1 = new Date();\n" \
233 " date2 = new Date();\n" \
235 " if (argc == 1) {\n" \
236 " return (hour == arguments[0]);\n" \
237 " } else if (argc == 2) {\n" \
238 " return ((arguments[0] <= hour) && (hour <= arguments[1]));\n" \
240 " switch (argc) {\n" \
242 " date1.setSeconds(arguments[2]);\n" \
243 " date2.setSeconds(arguments[5]);\n" \
245 " var middle = argc >> 1;\n" \
246 " date1.setHours(arguments[0]);\n" \
247 " date1.setMinutes(arguments[1]);\n" \
248 " date2.setHours(arguments[middle]);\n" \
249 " date2.setMinutes(arguments[middle + 1]);\n" \
250 " if (middle == 2) {\n" \
251 " date2.setSeconds(59);\n" \
255 " throw 'timeRange: bad number of arguments'\n" \
260 " date.setFullYear(date.getUTCFullYear());\n" \
261 " date.setMonth(date.getUTCMonth());\n" \
262 " date.setDate(date.getUTCDate());\n" \
263 " date.setHours(date.getUTCHours());\n" \
264 " date.setMinutes(date.getUTCMinutes());\n" \
265 " date.setSeconds(date.getUTCSeconds());\n" \
267 " return ((date1 <= date) && (date <= date2));\n" \
270 // This is a Microsoft extension to PAC for IPv6, see:
271 // http://blogs.msdn.com/b/wndp/archive/2006/07/13/ipv6-pac-extensions-v0-9.aspx
272 #define PROXY_RESOLVER_SCRIPT_EX \
273 "function isResolvableEx(host) {\n" \
274 " var ipList = dnsResolveEx(host);\n" \
275 " return (ipList != '');\n" \
278 #endif // NET_PROXY_PROXY_RESOLVER_SCRIPT_H_