From be12f6cce6ffa0f4520e1ee5ab707d712ea30b72 Mon Sep 17 00:00:00 2001 From: Valentin Gosu Date: Tue, 25 Jan 2022 10:02:06 +0000 Subject: [PATCH] Bug 1751698 - Include ascii_pac_utils.js as a separate file r=necko-reviewers,kershaw Differential Revision: https://phabricator.services.mozilla.com/D136728 --- netwerk/base/ProxyAutoConfig.cpp | 235 +------------------------------ netwerk/base/ascii_pac_utils.js | 221 +++++++++++++++++++++++++++++ netwerk/base/makecppstring.py | 17 +++ netwerk/base/moz.build | 4 + 4 files changed, 244 insertions(+), 233 deletions(-) create mode 100644 netwerk/base/ascii_pac_utils.js create mode 100644 netwerk/base/makecppstring.py diff --git a/netwerk/base/ProxyAutoConfig.cpp b/netwerk/base/ProxyAutoConfig.cpp index 5da9baff8299..acb61b7334d0 100644 --- a/netwerk/base/ProxyAutoConfig.cpp +++ b/netwerk/base/ProxyAutoConfig.cpp @@ -56,239 +56,8 @@ namespace net { // Per ProxyAutoConfig::Init, this data must be ASCII. static const char sAsciiPacUtils[] = - "function dnsDomainIs(host, domain) {\n" - " return (host.length >= domain.length &&\n" - " host.substring(host.length - domain.length) == domain);\n" - "}\n" - "" - "function dnsDomainLevels(host) {\n" - " return host.split('.').length - 1;\n" - "}\n" - "" - "function isValidIpAddress(ipchars) {\n" - " var matches = " - "/^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/.exec(ipchars);\n" - " if (matches == null) {\n" - " return false;\n" - " } else if (matches[1] > 255 || matches[2] > 255 || \n" - " matches[3] > 255 || matches[4] > 255) {\n" - " return false;\n" - " }\n" - " return true;\n" - "}\n" - "" - "function convert_addr(ipchars) {\n" - " var bytes = ipchars.split('.');\n" - " var result = ((bytes[0] & 0xff) << 24) |\n" - " ((bytes[1] & 0xff) << 16) |\n" - " ((bytes[2] & 0xff) << 8) |\n" - " (bytes[3] & 0xff);\n" - " return result;\n" - "}\n" - "" - "function isInNet(ipaddr, pattern, maskstr) {\n" - " if (!isValidIpAddress(pattern) || !isValidIpAddress(maskstr)) {\n" - " return false;\n" - " }\n" - " if (!isValidIpAddress(ipaddr)) {\n" - " ipaddr = dnsResolve(ipaddr);\n" - " if (ipaddr == null) {\n" - " return false;\n" - " }\n" - " }\n" - " var host = convert_addr(ipaddr);\n" - " var pat = convert_addr(pattern);\n" - " var mask = convert_addr(maskstr);\n" - " return ((host & mask) == (pat & mask));\n" - " \n" - "}\n" - "" - "function isPlainHostName(host) {\n" - " return (host.search('(\\\\.)|:') == -1);\n" - "}\n" - "" - "function isResolvable(host) {\n" - " var ip = dnsResolve(host);\n" - " return (ip != null);\n" - "}\n" - "" - "function localHostOrDomainIs(host, hostdom) {\n" - " return (host == hostdom) ||\n" - " (hostdom.lastIndexOf(host + '.', 0) == 0);\n" - "}\n" - "" - "function shExpMatch(url, pattern) {\n" - " pattern = pattern.replace(/\\./g, '\\\\.');\n" - " pattern = pattern.replace(/\\*/g, '.*');\n" - " pattern = pattern.replace(/\\?/g, '.');\n" - " var newRe = new RegExp('^'+pattern+'$');\n" - " return newRe.test(url);\n" - "}\n" - "" - "var wdays = {SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6};\n" - "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" - "" - "function weekdayRange() {\n" - " function getDay(weekday) {\n" - " if (weekday in wdays) {\n" - " return wdays[weekday];\n" - " }\n" - " return -1;\n" - " }\n" - " var date = new Date();\n" - " var argc = arguments.length;\n" - " var wday;\n" - " if (argc < 1)\n" - " return false;\n" - " if (arguments[argc - 1] == 'GMT') {\n" - " argc--;\n" - " wday = date.getUTCDay();\n" - " } else {\n" - " wday = date.getDay();\n" - " }\n" - " var wd1 = getDay(arguments[0]);\n" - " var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n" - " return (wd1 == -1 || wd2 == -1) ? false\n" - " : (wd1 <= wd2) ? (wd1 <= wday && wday " - "<= wd2)\n" - " : (wd2 >= wday || wday " - ">= wd1);\n" - "}\n" - "" - "function dateRange() {\n" - " function getMonth(name) {\n" - " if (name in months) {\n" - " return months[name];\n" - " }\n" - " return -1;\n" - " }\n" - " var date = new Date();\n" - " var argc = arguments.length;\n" - " if (argc < 1) {\n" - " return false;\n" - " }\n" - " var isGMT = (arguments[argc - 1] == 'GMT');\n" - "\n" - " if (isGMT) {\n" - " argc--;\n" - " }\n" - " // function will work even without explict handling of this case\n" - " if (argc == 1) {\n" - " var tmp = parseInt(arguments[0]);\n" - " if (isNaN(tmp)) {\n" - " return ((isGMT ? date.getUTCMonth() : date.getMonth()) ==\n" - " getMonth(arguments[0]));\n" - " } else if (tmp < 32) {\n" - " return ((isGMT ? date.getUTCDate() : date.getDate()) == " - "tmp);\n" - " } else { \n" - " return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) " - "==\n" - " tmp);\n" - " }\n" - " }\n" - " var year = date.getFullYear();\n" - " var date1, date2;\n" - " date1 = new Date(year, 0, 1, 0, 0, 0);\n" - " date2 = new Date(year, 11, 31, 23, 59, 59);\n" - " var adjustMonth = false;\n" - " for (var i = 0; i < (argc >> 1); i++) {\n" - " var tmp = parseInt(arguments[i]);\n" - " if (isNaN(tmp)) {\n" - " var mon = getMonth(arguments[i]);\n" - " date1.setMonth(mon);\n" - " } else if (tmp < 32) {\n" - " adjustMonth = (argc <= 2);\n" - " date1.setDate(tmp);\n" - " } else {\n" - " date1.setFullYear(tmp);\n" - " }\n" - " }\n" - " for (var i = (argc >> 1); i < argc; i++) {\n" - " var tmp = parseInt(arguments[i]);\n" - " if (isNaN(tmp)) {\n" - " var mon = getMonth(arguments[i]);\n" - " date2.setMonth(mon);\n" - " } else if (tmp < 32) {\n" - " date2.setDate(tmp);\n" - " } else {\n" - " date2.setFullYear(tmp);\n" - " }\n" - " }\n" - " if (adjustMonth) {\n" - " date1.setMonth(date.getMonth());\n" - " date2.setMonth(date.getMonth());\n" - " }\n" - " if (isGMT) {\n" - " var tmp = date;\n" - " tmp.setFullYear(date.getUTCFullYear());\n" - " tmp.setMonth(date.getUTCMonth());\n" - " tmp.setDate(date.getUTCDate());\n" - " tmp.setHours(date.getUTCHours());\n" - " tmp.setMinutes(date.getUTCMinutes());\n" - " tmp.setSeconds(date.getUTCSeconds());\n" - " date = tmp;\n" - " }\n" - " return (date1 <= date2) ? (date1 <= date) && (date <= date2)\n" - " : (date2 >= date) || (date >= date1);\n" - "}\n" - "" - "function timeRange() {\n" - " var argc = arguments.length;\n" - " var date = new Date();\n" - " var isGMT= false;\n" - "" - " if (argc < 1) {\n" - " return false;\n" - " }\n" - " if (arguments[argc - 1] == 'GMT') {\n" - " isGMT = true;\n" - " argc--;\n" - " }\n" - "\n" - " var hour = isGMT ? date.getUTCHours() : date.getHours();\n" - " var date1, date2;\n" - " date1 = new Date();\n" - " date2 = new Date();\n" - "\n" - " if (argc == 1) {\n" - " return (hour == arguments[0]);\n" - " } else if (argc == 2) {\n" - " return ((arguments[0] <= hour) && (hour <= arguments[1]));\n" - " } else {\n" - " switch (argc) {\n" - " case 6:\n" - " date1.setSeconds(arguments[2]);\n" - " date2.setSeconds(arguments[5]);\n" - " case 4:\n" - " var middle = argc >> 1;\n" - " date1.setHours(arguments[0]);\n" - " date1.setMinutes(arguments[1]);\n" - " date2.setHours(arguments[middle]);\n" - " date2.setMinutes(arguments[middle + 1]);\n" - " if (middle == 2) {\n" - " date2.setSeconds(59);\n" - " }\n" - " break;\n" - " default:\n" - " throw 'timeRange: bad number of arguments'\n" - " }\n" - " }\n" - "\n" - " if (isGMT) {\n" - " date.setFullYear(date.getUTCFullYear());\n" - " date.setMonth(date.getUTCMonth());\n" - " date.setDate(date.getUTCDate());\n" - " date.setHours(date.getUTCHours());\n" - " date.setMinutes(date.getUTCMinutes());\n" - " date.setSeconds(date.getUTCSeconds());\n" - " }\n" - " return (date1 <= date2) ? (date1 <= date) && (date <= date2)\n" - " : (date2 >= date) || (date >= date1);\n" - "\n" - "}\n" - ""; +#include "ascii_pac_utils.inc" + ; // sRunning is defined for the helper functions only while the // Javascript engine is running and the PAC object cannot be deleted diff --git a/netwerk/base/ascii_pac_utils.js b/netwerk/base/ascii_pac_utils.js new file mode 100644 index 000000000000..77e0a5331c2b --- /dev/null +++ b/netwerk/base/ascii_pac_utils.js @@ -0,0 +1,221 @@ +function dnsDomainIs(host, domain) { + return (host.length >= domain.length && + host.substring(host.length - domain.length) == domain); +} + +function dnsDomainLevels(host) { + return host.split('.').length - 1; +} + +function isValidIpAddress(ipchars) { + var matches = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(ipchars); + if (matches == null) { + return false; + } else if (matches[1] > 255 || matches[2] > 255 || + matches[3] > 255 || matches[4] > 255) { + return false; + } + return true; +} + +function convert_addr(ipchars) { + var bytes = ipchars.split('.'); + var result = ((bytes[0] & 0xff) << 24) | + ((bytes[1] & 0xff) << 16) | + ((bytes[2] & 0xff) << 8) | + (bytes[3] & 0xff); + return result; +} + +function isInNet(ipaddr, pattern, maskstr) { + if (!isValidIpAddress(pattern) || !isValidIpAddress(maskstr)) { + return false; + } + if (!isValidIpAddress(ipaddr)) { + ipaddr = dnsResolve(ipaddr); + if (ipaddr == null) { + return false; + } + } + var host = convert_addr(ipaddr); + var pat = convert_addr(pattern); + var mask = convert_addr(maskstr); + return ((host & mask) == (pat & mask)); +} + +function isPlainHostName(host) { + return (host.search('(\\.)|:') == -1); +} + +function isResolvable(host) { + var ip = dnsResolve(host); + return (ip != null); +} + +function localHostOrDomainIs(host, hostdom) { + return (host == hostdom) || + (hostdom.lastIndexOf(host + '.', 0) == 0); +} + +function shExpMatch(url, pattern) { + pattern = pattern.replace(/\./g, '\\.'); + pattern = pattern.replace(/\*/g, '.*'); + pattern = pattern.replace(/\?/g, '.'); + var newRe = new RegExp('^'+pattern+'$'); + return newRe.test(url); +} + +var wdays = {SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6}; +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}; +function weekdayRange() { + function getDay(weekday) { + if (weekday in wdays) { + return wdays[weekday]; + } + return -1; + } + var date = new Date(); + var argc = arguments.length; + var wday; + if (argc < 1) + return false; + if (arguments[argc - 1] == 'GMT') { + argc--; + wday = date.getUTCDay(); + } else { + wday = date.getDay(); + } + var wd1 = getDay(arguments[0]); + var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1; + return (wd1 == -1 || wd2 == -1) ? false + : (wd1 <= wd2) ? (wd1 <= wday && wday <= wd2) + : (wd2 >= wday || wday >= wd1); +} + +function dateRange() { + function getMonth(name) { + if (name in months) { + return months[name]; + } + return -1; + } + var date = new Date(); + var argc = arguments.length; + if (argc < 1) { + return false; + } + var isGMT = (arguments[argc - 1] == 'GMT'); + + if (isGMT) { + argc--; + } + // function will work even without explict handling of this case + if (argc == 1) { + var tmp = parseInt(arguments[0]); + if (isNaN(tmp)) { + return ((isGMT ? date.getUTCMonth() : date.getMonth()) == + getMonth(arguments[0])); + } else if (tmp < 32) { + return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp); + } else { + return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) == tmp); + } + } + var year = date.getFullYear(); + var date1, date2; + date1 = new Date(year, 0, 1, 0, 0, 0); + date2 = new Date(year, 11, 31, 23, 59, 59); + var adjustMonth = false; + for (var i = 0; i < (argc >> 1); i++) { + var tmp = parseInt(arguments[i]); + if (isNaN(tmp)) { + var mon = getMonth(arguments[i]); + date1.setMonth(mon); + } else if (tmp < 32) { + adjustMonth = (argc <= 2); + date1.setDate(tmp); + } else { + date1.setFullYear(tmp); + } + } + for (var i = (argc >> 1); i < argc; i++) { + var tmp = parseInt(arguments[i]); + if (isNaN(tmp)) { + var mon = getMonth(arguments[i]); + date2.setMonth(mon); + } else if (tmp < 32) { + date2.setDate(tmp); + } else { + date2.setFullYear(tmp); + } + } + if (adjustMonth) { + date1.setMonth(date.getMonth()); + date2.setMonth(date.getMonth()); + } + if (isGMT) { + var tmp = date; + tmp.setFullYear(date.getUTCFullYear()); + tmp.setMonth(date.getUTCMonth()); + tmp.setDate(date.getUTCDate()); + tmp.setHours(date.getUTCHours()); + tmp.setMinutes(date.getUTCMinutes()); + tmp.setSeconds(date.getUTCSeconds()); + date = tmp; + } + return (date1 <= date2) ? (date1 <= date) && (date <= date2) + : (date2 >= date) || (date >= date1); +} + +function timeRange() { + var argc = arguments.length; + var date = new Date(); + var isGMT= false; + if (argc < 1) { + return false; + } + if (arguments[argc - 1] == 'GMT') { + isGMT = true; + argc--; + } + + var hour = isGMT ? date.getUTCHours() : date.getHours(); + var date1, date2; + date1 = new Date(); + date2 = new Date(); + + if (argc == 1) { + return (hour == arguments[0]); + } else if (argc == 2) { + return ((arguments[0] <= hour) && (hour <= arguments[1])); + } else { + switch (argc) { + case 6: + date1.setSeconds(arguments[2]); + date2.setSeconds(arguments[5]); + case 4: + var middle = argc >> 1; + date1.setHours(arguments[0]); + date1.setMinutes(arguments[1]); + date2.setHours(arguments[middle]); + date2.setMinutes(arguments[middle + 1]); + if (middle == 2) { + date2.setSeconds(59); + } + break; + default: + throw 'timeRange: bad number of arguments' + } + } + + if (isGMT) { + date.setFullYear(date.getUTCFullYear()); + date.setMonth(date.getUTCMonth()); + date.setDate(date.getUTCDate()); + date.setHours(date.getUTCHours()); + date.setMinutes(date.getUTCMinutes()); + date.setSeconds(date.getUTCSeconds()); + } + return (date1 <= date2) ? (date1 <= date) && (date <= date2) + : (date2 >= date) || (date >= date1); +} diff --git a/netwerk/base/makecppstring.py b/netwerk/base/makecppstring.py new file mode 100644 index 000000000000..330fcfb04c9e --- /dev/null +++ b/netwerk/base/makecppstring.py @@ -0,0 +1,17 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +import sys + + +def main(output, filename): + file = open(filename, "r") + output.write('R"(') # insert literal start + for line in file: + output.write(line) + output.write(')"') # insert literal end + + +if __name__ == "__main__": + main(sys.stdout, sys.argv[1]) diff --git a/netwerk/base/moz.build b/netwerk/base/moz.build index d6605f6fb98d..4d5f6acc53e3 100644 --- a/netwerk/base/moz.build +++ b/netwerk/base/moz.build @@ -323,4 +323,8 @@ if CONFIG["MOZ_WIDGET_TOOLKIT"] == "cocoa": "/xpcom/base", ] +GeneratedFile( + "ascii_pac_utils.inc", script="makecppstring.py", inputs=["ascii_pac_utils.js"] +) + REQUIRES_UNIFIED_BUILD = True