| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef NET_PROXY_PROXY_RESOLVER_SCRIPT_H_ | |
| 6 #define NET_PROXY_PROXY_RESOLVER_SCRIPT_H_ | |
| 7 | |
| 8 /* ***** BEGIN LICENSE BLOCK ***** | |
| 9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | |
| 10 * | |
| 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/ | |
| 15 * | |
| 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 | |
| 19 * License. | |
| 20 * | |
| 21 * The Original Code is mozilla.org code. | |
| 22 * | |
| 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. | |
| 27 * | |
| 28 * Contributor(s): | |
| 29 * Akhil Arora <akhil.arora@sun.com> | |
| 30 * Tomi Leppikangas <Tomi.Leppikangas@oulu.fi> | |
| 31 * Darin Fisher <darin@meer.net> | |
| 32 * | |
| 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. | |
| 44 * | |
| 45 * ***** END LICENSE BLOCK ***** */ | |
| 46 | |
| 47 // The following code was formatted from: | |
| 48 // 'mozilla/netwerk/base/src/nsProxyAutoConfig.js' (1.55) | |
| 49 // | |
| 50 // Using the command: | |
| 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 =' | |
| 58 // | |
| 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" \ | |
| 64 "}\n" \ | |
| 65 "" \ | |
| 66 "function dnsDomainLevels(host) {\n" \ | |
| 67 " return host.split('.').length-1;\n" \ | |
| 68 "}\n" \ | |
| 69 "" \ | |
| 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" \ | |
| 76 " return result;\n" \ | |
| 77 "}\n" \ | |
| 78 "" \ | |
| 79 "function isInNet(ipaddr, pattern, maskstr) {\n" \ | |
| 80 " var test = /^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/.exec(ipa
ddr);\n" \ | |
| 81 " if (test == null) {\n" \ | |
| 82 " ipaddr = dnsResolve(ipaddr);\n" \ | |
| 83 " if (ipaddr == null)\n" \ | |
| 84 " return false;\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" \ | |
| 88 " }\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" \ | |
| 93 " \n" \ | |
| 94 "}\n" \ | |
| 95 "" \ | |
| 96 "function isResolvable(host) {\n" \ | |
| 97 " var ip = dnsResolve(host);\n" \ | |
| 98 " return (ip != null);\n" \ | |
| 99 "}\n" \ | |
| 100 "" \ | |
| 101 "function localHostOrDomainIs(host, hostdom) {\n" \ | |
| 102 " return (host == hostdom) ||\n" \ | |
| 103 " (hostdom.lastIndexOf(host + '.', 0) == 0);\n" \ | |
| 104 "}\n" \ | |
| 105 "" \ | |
| 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" \ | |
| 112 "}\n" \ | |
| 113 "" \ | |
| 114 "var wdays = {SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6};\n" \ | |
| 115 "" \ | |
| 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" \ | |
| 117 "" \ | |
| 118 "function weekdayRange() {\n" \ | |
| 119 " function getDay(weekday) {\n" \ | |
| 120 " if (weekday in wdays) {\n" \ | |
| 121 " return wdays[weekday];\n" \ | |
| 122 " }\n" \ | |
| 123 " return -1;\n" \ | |
| 124 " }\n" \ | |
| 125 " var date = new Date();\n" \ | |
| 126 " var argc = arguments.length;\n" \ | |
| 127 " var wday;\n" \ | |
| 128 " if (argc < 1)\n" \ | |
| 129 " return false;\n" \ | |
| 130 " if (arguments[argc - 1] == 'GMT') {\n" \ | |
| 131 " argc--;\n" \ | |
| 132 " wday = date.getUTCDay();\n" \ | |
| 133 " } else {\n" \ | |
| 134 " wday = date.getDay();\n" \ | |
| 135 " }\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" \ | |
| 140 "}\n" \ | |
| 141 "" \ | |
| 142 "function dateRange() {\n" \ | |
| 143 " function getMonth(name) {\n" \ | |
| 144 " if (name in months) {\n" \ | |
| 145 " return months[name];\n" \ | |
| 146 " }\n" \ | |
| 147 " return -1;\n" \ | |
| 148 " }\n" \ | |
| 149 " var date = new Date();\n" \ | |
| 150 " var argc = arguments.length;\n" \ | |
| 151 " if (argc < 1) {\n" \ | |
| 152 " return false;\n" \ | |
| 153 " }\n" \ | |
| 154 " var isGMT = (arguments[argc - 1] == 'GMT');\n" \ | |
| 155 "\n" \ | |
| 156 " if (isGMT) {\n" \ | |
| 157 " argc--;\n" \ | |
| 158 " }\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"
\ | |
| 167 " } else { \n" \ | |
| 168 " return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==\n
" \ | |
| 169 "tmp);\n" \ | |
| 170 " }\n" \ | |
| 171 " }\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" \ | |
| 185 " } else {\n" \ | |
| 186 " date1.setFullYear(tmp);\n" \ | |
| 187 " }\n" \ | |
| 188 " }\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" \ | |
| 196 " } else {\n" \ | |
| 197 " date2.setFullYear(tmp);\n" \ | |
| 198 " }\n" \ | |
| 199 " }\n" \ | |
| 200 " if (adjustMonth) {\n" \ | |
| 201 " date1.setMonth(date.getMonth());\n" \ | |
| 202 " date2.setMonth(date.getMonth());\n" \ | |
| 203 " }\n" \ | |
| 204 " if (isGMT) {\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" \ | |
| 212 " date = tmp;\n" \ | |
| 213 " }\n" \ | |
| 214 " return ((date1 <= date) && (date <= date2));\n" \ | |
| 215 "}\n" \ | |
| 216 "" \ | |
| 217 "function timeRange() {\n" \ | |
| 218 " var argc = arguments.length;\n" \ | |
| 219 " var date = new Date();\n" \ | |
| 220 " var isGMT= false;\n" \ | |
| 221 "\n" \ | |
| 222 " if (argc < 1) {\n" \ | |
| 223 " return false;\n" \ | |
| 224 " }\n" \ | |
| 225 " if (arguments[argc - 1] == 'GMT') {\n" \ | |
| 226 " isGMT = true;\n" \ | |
| 227 " argc--;\n" \ | |
| 228 " }\n" \ | |
| 229 "\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" \ | |
| 234 "\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" \ | |
| 239 " } else {\n" \ | |
| 240 " switch (argc) {\n" \ | |
| 241 " case 6:\n" \ | |
| 242 " date1.setSeconds(arguments[2]);\n" \ | |
| 243 " date2.setSeconds(arguments[5]);\n" \ | |
| 244 " case 4:\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" \ | |
| 252 " }\n" \ | |
| 253 " break;\n" \ | |
| 254 " default:\n" \ | |
| 255 " throw 'timeRange: bad number of arguments'\n" \ | |
| 256 " }\n" \ | |
| 257 " }\n" \ | |
| 258 "\n" \ | |
| 259 " if (isGMT) {\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" \ | |
| 266 " }\n" \ | |
| 267 " return ((date1 <= date) && (date <= date2));\n" \ | |
| 268 "}\n" | |
| 269 | |
| 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" \ | |
| 276 "}\n" | |
| 277 | |
| 278 #endif // NET_PROXY_PROXY_RESOLVER_SCRIPT_H_ | |
| OLD | NEW |