Home | History | Annotate | Download | only in proxy
      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 (at) sun.com>
     23  *   Tomi Leppikangas <Tomi.Leppikangas (at) oulu.fi>
     24  *   Darin Fisher <darin (at) 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 #pragma once
     43 
     44 // The following code was formatted from:
     45 //   'mozilla/netwerk/base/src/nsProxyAutoConfig.js' (1.55)
     46 //
     47 // Using the command:
     48 //   $ cat nsProxyAutoConfig.js |
     49 //       awk '/var pacUtils/,/EOF/' |
     50 //       sed -e 's/^\s*$/""/g' |
     51 //       sed -e 's/"\s*[+]\s*$/"/g' |
     52 //       sed -e 's/"$/" \\/g' |
     53 //       sed -e 's/\/(ipaddr);/\/.exec(ipaddr);/g' |
     54 //       grep -v '^var pacUtils ='
     55 #define PROXY_RESOLVER_SCRIPT \
     56   "function dnsDomainIs(host, domain) {\n" \
     57   "    return (host.length >= domain.length &&\n" \
     58   "            host.substring(host.length - domain.length) == domain);\n" \
     59   "}\n" \
     60   "" \
     61   "function dnsDomainLevels(host) {\n" \
     62   "    return host.split('.').length-1;\n" \
     63   "}\n" \
     64   "" \
     65   "function convert_addr(ipchars) {\n" \
     66   "    var bytes = ipchars.split('.');\n" \
     67   "    var result = ((bytes[0] & 0xff) << 24) |\n" \
     68   "                 ((bytes[1] & 0xff) << 16) |\n" \
     69   "                 ((bytes[2] & 0xff) <<  8) |\n" \
     70   "                  (bytes[3] & 0xff);\n" \
     71   "    return result;\n" \
     72   "}\n" \
     73   "" \
     74   "function isInNet(ipaddr, pattern, maskstr) {\n" \
     75   "    var test = /^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/.exec(ipaddr);\n" \
     76   "    if (test == null) {\n" \
     77   "        ipaddr = dnsResolve(ipaddr);\n" \
     78   "        if (ipaddr == null)\n" \
     79   "            return false;\n" \
     80   "    } else if (test[1] > 255 || test[2] > 255 || \n" \
     81   "               test[3] > 255 || test[4] > 255) {\n" \
     82   "        return false;    // not an IP address\n" \
     83   "    }\n" \
     84   "    var host = convert_addr(ipaddr);\n" \
     85   "    var pat  = convert_addr(pattern);\n" \
     86   "    var mask = convert_addr(maskstr);\n" \
     87   "    return ((host & mask) == (pat & mask));\n" \
     88   "    \n" \
     89   "}\n" \
     90   "" \
     91   "function isPlainHostName(host) {\n" \
     92   "    return (host.search('\\\\.') == -1);\n" \
     93   "}\n" \
     94   "" \
     95   "function isResolvable(host) {\n" \
     96   "    var ip = dnsResolve(host);\n" \
     97   "    return (ip != null);\n" \
     98   "}\n" \
     99   "" \
    100   "function localHostOrDomainIs(host, hostdom) {\n" \
    101   "    return (host == hostdom) ||\n" \
    102   "           (hostdom.lastIndexOf(host + '.', 0) == 0);\n" \
    103   "}\n" \
    104   "" \
    105   "function shExpMatch(url, pattern) {\n" \
    106   "   pattern = pattern.replace(/\\./g, '\\\\.');\n" \
    107   "   pattern = pattern.replace(/\\*/g, '.*');\n" \
    108   "   pattern = pattern.replace(/\\?/g, '.');\n" \
    109   "   var newRe = new RegExp('^'+pattern+'$');\n" \
    110   "   return newRe.test(url);\n" \
    111   "}\n" \
    112   "" \
    113   "var wdays = {SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6};\n" \
    114   "" \
    115   "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" \
    116   "" \
    117   "function weekdayRange() {\n" \
    118   "    function getDay(weekday) {\n" \
    119   "        if (weekday in wdays) {\n" \
    120   "            return wdays[weekday];\n" \
    121   "        }\n" \
    122   "        return -1;\n" \
    123   "    }\n" \
    124   "    var date = new Date();\n" \
    125   "    var argc = arguments.length;\n" \
    126   "    var wday;\n" \
    127   "    if (argc < 1)\n" \
    128   "        return false;\n" \
    129   "    if (arguments[argc - 1] == 'GMT') {\n" \
    130   "        argc--;\n" \
    131   "        wday = date.getUTCDay();\n" \
    132   "    } else {\n" \
    133   "        wday = date.getDay();\n" \
    134   "    }\n" \
    135   "    var wd1 = getDay(arguments[0]);\n" \
    136   "    var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n" \
    137   "    return (wd1 == -1 || wd2 == -1) ? false\n" \
    138   "                                    : (wd1 <= wday && wday <= wd2);\n" \
    139   "}\n" \
    140   "" \
    141   "function dateRange() {\n" \
    142   "    function getMonth(name) {\n" \
    143   "        if (name in months) {\n" \
    144   "            return months[name];\n" \
    145   "        }\n" \
    146   "        return -1;\n" \
    147   "    }\n" \
    148   "    var date = new Date();\n" \
    149   "    var argc = arguments.length;\n" \
    150   "    if (argc < 1) {\n" \
    151   "        return false;\n" \
    152   "    }\n" \
    153   "    var isGMT = (arguments[argc - 1] == 'GMT');\n" \
    154   "\n" \
    155   "    if (isGMT) {\n" \
    156   "        argc--;\n" \
    157   "    }\n" \
    158   "    // function will work even without explict handling of this case\n" \
    159   "    if (argc == 1) {\n" \
    160   "        var tmp = parseInt(arguments[0]);\n" \
    161   "        if (isNaN(tmp)) {\n" \
    162   "            return ((isGMT ? date.getUTCMonth() : date.getMonth()) ==\n" \
    163   "getMonth(arguments[0]));\n" \
    164   "        } else if (tmp < 32) {\n" \
    165   "            return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);\n" \
    166   "        } else { \n" \
    167   "            return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==\n" \
    168   "tmp);\n" \
    169   "        }\n" \
    170   "    }\n" \
    171   "    var year = date.getFullYear();\n" \
    172   "    var date1, date2;\n" \
    173   "    date1 = new Date(year,  0,  1,  0,  0,  0);\n" \
    174   "    date2 = new Date(year, 11, 31, 23, 59, 59);\n" \
    175   "    var adjustMonth = false;\n" \
    176   "    for (var i = 0; i < (argc >> 1); i++) {\n" \
    177   "        var tmp = parseInt(arguments[i]);\n" \
    178   "        if (isNaN(tmp)) {\n" \
    179   "            var mon = getMonth(arguments[i]);\n" \
    180   "            date1.setMonth(mon);\n" \
    181   "        } else if (tmp < 32) {\n" \
    182   "            adjustMonth = (argc <= 2);\n" \
    183   "            date1.setDate(tmp);\n" \
    184   "        } else {\n" \
    185   "            date1.setFullYear(tmp);\n" \
    186   "        }\n" \
    187   "    }\n" \
    188   "    for (var i = (argc >> 1); i < argc; i++) {\n" \
    189   "        var tmp = parseInt(arguments[i]);\n" \
    190   "        if (isNaN(tmp)) {\n" \
    191   "            var mon = getMonth(arguments[i]);\n" \
    192   "            date2.setMonth(mon);\n" \
    193   "        } else if (tmp < 32) {\n" \
    194   "            date2.setDate(tmp);\n" \
    195   "        } else {\n" \
    196   "            date2.setFullYear(tmp);\n" \
    197   "        }\n" \
    198   "    }\n" \
    199   "    if (adjustMonth) {\n" \
    200   "        date1.setMonth(date.getMonth());\n" \
    201   "        date2.setMonth(date.getMonth());\n" \
    202   "    }\n" \
    203   "    if (isGMT) {\n" \
    204   "    var tmp = date;\n" \
    205   "        tmp.setFullYear(date.getUTCFullYear());\n" \
    206   "        tmp.setMonth(date.getUTCMonth());\n" \
    207   "        tmp.setDate(date.getUTCDate());\n" \
    208   "        tmp.setHours(date.getUTCHours());\n" \
    209   "        tmp.setMinutes(date.getUTCMinutes());\n" \
    210   "        tmp.setSeconds(date.getUTCSeconds());\n" \
    211   "        date = tmp;\n" \
    212   "    }\n" \
    213   "    return ((date1 <= date) && (date <= date2));\n" \
    214   "}\n" \
    215   "" \
    216   "function timeRange() {\n" \
    217   "    var argc = arguments.length;\n" \
    218   "    var date = new Date();\n" \
    219   "    var isGMT= false;\n" \
    220   "\n" \
    221   "    if (argc < 1) {\n" \
    222   "        return false;\n" \
    223   "    }\n" \
    224   "    if (arguments[argc - 1] == 'GMT') {\n" \
    225   "        isGMT = true;\n" \
    226   "        argc--;\n" \
    227   "    }\n" \
    228   "\n" \
    229   "    var hour = isGMT ? date.getUTCHours() : date.getHours();\n" \
    230   "    var date1, date2;\n" \
    231   "    date1 = new Date();\n" \
    232   "    date2 = new Date();\n" \
    233   "\n" \
    234   "    if (argc == 1) {\n" \
    235   "        return (hour == arguments[0]);\n" \
    236   "    } else if (argc == 2) {\n" \
    237   "        return ((arguments[0] <= hour) && (hour <= arguments[1]));\n" \
    238   "    } else {\n" \
    239   "        switch (argc) {\n" \
    240   "        case 6:\n" \
    241   "            date1.setSeconds(arguments[2]);\n" \
    242   "            date2.setSeconds(arguments[5]);\n" \
    243   "        case 4:\n" \
    244   "            var middle = argc >> 1;\n" \
    245   "            date1.setHours(arguments[0]);\n" \
    246   "            date1.setMinutes(arguments[1]);\n" \
    247   "            date2.setHours(arguments[middle]);\n" \
    248   "            date2.setMinutes(arguments[middle + 1]);\n" \
    249   "            if (middle == 2) {\n" \
    250   "                date2.setSeconds(59);\n" \
    251   "            }\n" \
    252   "            break;\n" \
    253   "        default:\n" \
    254   "          throw 'timeRange: bad number of arguments'\n" \
    255   "        }\n" \
    256   "    }\n" \
    257   "\n" \
    258   "    if (isGMT) {\n" \
    259   "        date.setFullYear(date.getUTCFullYear());\n" \
    260   "        date.setMonth(date.getUTCMonth());\n" \
    261   "        date.setDate(date.getUTCDate());\n" \
    262   "        date.setHours(date.getUTCHours());\n" \
    263   "        date.setMinutes(date.getUTCMinutes());\n" \
    264   "        date.setSeconds(date.getUTCSeconds());\n" \
    265   "    }\n" \
    266   "    return ((date1 <= date) && (date <= date2));\n" \
    267   "}\n"
    268 
    269 // This is a Microsoft extension to PAC for IPv6, see:
    270 // http://blogs.msdn.com/b/wndp/archive/2006/07/13/ipv6-pac-extensions-v0-9.aspx
    271 #define PROXY_RESOLVER_SCRIPT_EX \
    272   "function isResolvableEx(host) {\n" \
    273   "    var ipList = dnsResolveEx(host);\n" \
    274   "    return (ipList != '');\n" \
    275   "}\n"
    276 
    277 #endif  // NET_PROXY_PROXY_RESOLVER_SCRIPT_H_
    278