home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / components / nsProxyAutoConfig.js < prev    next >
Text File  |  2001-03-23  |  14KB  |  421 lines

  1. /* -*- Mode: Java; tab-width: 4; c-basic-offset: 4; -*-
  2.  * 
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s): 
  21.  *   Akhil Arora <akhil.arora@sun.com>
  22.  *   Tomi Leppikangas <Tomi.Leppikangas@oulu.fi>
  23.  */
  24.  
  25. /*
  26.    Script for Proxy Auto Config in the new world order.
  27.        - Gagan Saksena 04/24/00 
  28. */
  29.  
  30. const kPAC_CONTRACTID = "@mozilla.org/network/proxy_autoconfig;1";
  31. const kIOSERVICE_CONTRACTID = "@mozilla.org/network/io-service;1";
  32. const kDNS_CONTRACTID = "@mozilla.org/network/dns-service;1";
  33. const kPAC_CID = Components.ID("{63ac8c66-1dd2-11b2-b070-84d00d3eaece}");
  34. const nsIProxyAutoConfig = Components.interfaces.nsIProxyAutoConfig;
  35. const nsIIOService = Components.interfaces['nsIIOService'];
  36. const nsIDNSService = Components.interfaces.nsIDNSService;
  37.  
  38. function debug(msg)
  39. {
  40.     dump("\nPAC:" + msg + "\n\n");
  41. }
  42.  
  43. // implementor of nsIProxyAutoConfig
  44. function nsProxyAutoConfig() {};
  45.  
  46. // global variable that will hold the downloaded js 
  47. var pac = null;
  48. // ptr to eval'ed FindProxyForURL function
  49. var LocalFindProxyForURL=null;
  50. // sendbox in which we eval loaded autoconfig js file
  51. var ProxySandBox = null;
  52.  
  53. nsProxyAutoConfig.prototype = {
  54.     sis: null,
  55.     done: false,
  56.  
  57.     ProxyForURL: function(url, host, port, type) {
  58.         /* If we're not done loading the pac yet, wait (ideally). For
  59.            now, just return DIRECT to avoid loops. A simple mutex
  60.            between ProxyForURL and LoadPACFromURL locks-up the
  61.            browser. */
  62.         if (!this.done) {
  63.             host.value = null;
  64.             type.value = "direct";
  65.             return;
  66.         }
  67.  
  68.         var uri = url.QueryInterface(Components.interfaces.nsIURI);
  69.         // Call the original function-
  70.         var proxy = LocalFindProxyForURL(uri.spec, uri.host);
  71.         debug("Proxy = " + proxy);
  72.         if (proxy == "DIRECT") {
  73.             host.value = null;
  74.             type.value = "direct";
  75.         }
  76.         else {
  77.             // TODO warn about SOCKS
  78.             
  79.             // we ignore everything else past the first proxy. 
  80.             // we could theoretically check isResolvable now and continue 
  81.             // parsing. but for now...
  82.             var hostport = /^PROXY ([^:]+):(\d+)/(proxy);
  83.             host.value = hostport[1];
  84.             port.value = hostport[2];
  85.             type.value = "http"; //proxy (http, socks, direct, etc)
  86.         }
  87.     },
  88.  
  89.     LoadPACFromURL: function(uri, ioService) {
  90.         debug("Loading PAC from " + uri.spec);
  91.         this.done = false;
  92.         var channel = ioService.newChannelFromURI(uri);
  93.         channel.asyncRead(this, null);
  94.     },
  95.  
  96.     // nsIStreamListener interface
  97.     onStartRequest: function(channel, ctxt) { 
  98.         pac = '';
  99.         LocalFindProxyForURL=null;
  100.         this.sis = 
  101.         Components.Constructor('@mozilla.org/scriptableinputstream;1',
  102.                                'nsIScriptableInputStream', 
  103.                                'init');
  104.     },
  105.  
  106.     onStopRequest: function(channel, ctxt, status, errorMsg) {
  107.         this.done = true;
  108.         if(!ProxySandBox) {
  109.            ProxySandBox = new Sandbox();
  110.         }
  111.         // add predefined functions to pac
  112.         var mypac = pacUtils + pac;
  113.         // evaluate loded js file
  114.         EvalAsCodebase(ProxySandBox, mypac, channel.URI.spec);
  115.         ProxySandBox.setDNS(dns);
  116.         LocalFindProxyForURL=ProxySandBox.FindProxyForURL;
  117.     },
  118.  
  119.     onDataAvailable: function(channel, ctxt, inStream, sourceOffset, count) {
  120.         var ins = new this.sis(inStream);
  121.         pac += ins.read(count);
  122.     }
  123. }
  124.  
  125. var pacModule = new Object();
  126.  
  127. pacModule.registerSelf =
  128.     function (compMgr, fileSpec, location, type) {
  129.         dump("*** Registering Proxy Auto Config (a Javascript module!) \n");
  130.         compMgr.registerComponentWithType(kPAC_CID,
  131.             "Proxy Auto Config",
  132.             kPAC_CONTRACTID,
  133.             fileSpec, location, 
  134.             true, true, type);
  135.     }
  136.  
  137. pacModule.getClassObject =
  138. function (compMgr, cid, iid) {
  139.         if (!cid.equals(kPAC_CID))
  140.             throw Components.results.NS_ERROR_NO_INTERFACE;
  141.  
  142.         if (!iid.equals(Components.interfaces.nsIFactory))
  143.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  144.  
  145.         return pacFactory;
  146.     }
  147.  
  148. pacModule.canUnload =
  149.     function (compMgr) {
  150.         dump("*** Unloading Proxy Auto Config...\n");
  151.         return true;
  152.     }
  153.  
  154. var pacFactory = new Object();
  155. pacFactory.createInstance =
  156.     function (outer, iid) {
  157.         if (outer != null)
  158.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  159.  
  160.         if (!iid.equals(nsIProxyAutoConfig) &&
  161.             !!iid.equals(Components.interfaces.nsIStreamListener) &&
  162.             !iid.equals(Components.interfaces.nsISupports)) {
  163.             // shouldn't this be NO_INTERFACE?
  164.             throw Components.results.NS_ERROR_INVALID_ARG;
  165.         }
  166.         return PacMan;
  167.     }
  168.  
  169. function NSGetModule(compMgr, fileSpec) {
  170.     return pacModule;
  171. }
  172.  
  173. var PacMan = new nsProxyAutoConfig() ;
  174.  
  175. var ios = Components.classes[kIOSERVICE_CONTRACTID].getService(nsIIOService);
  176. var dns = Components.classes[kDNS_CONTRACTID].getService(nsIDNSService);
  177.  
  178. var pacUtils = 
  179. "function dnsDomainIs(host, domain) {\n" +
  180. "    return (host.replace(/\\w*/, '') == domain);\n" +
  181. "}\n" +
  182.  
  183. "function dnsDomainLevels(host) {\n" +
  184. "    return host.split('.').length-1;\n" +
  185. "}\n" +
  186.  
  187. "var dns;\n" +
  188. "function setDNS(dnsservice) {\n" +
  189. "    dns = dnsservice;\n" +
  190. "}\n" +
  191.  
  192. "function dnsResolve(host) {\n" +
  193. "    try {\n" +
  194. "        var addr =  dns.resolve(host);\n" +
  195. "        return addr;\n" +
  196. "    }\n" +
  197. "    catch (ex) {\n" +
  198. "        return null;\n" +
  199. "    }\n" +
  200. "}\n" +
  201.  
  202. "function convert_addr(ipchars) {\n"+
  203. "    var bytes = ipchars.split('.');\n"+
  204. "    var result = ((bytes[0] & 0xff) << 24) |\n"+
  205. "                 ((bytes[1] & 0xff) << 16) |\n"+
  206. "                 ((bytes[2] & 0xff) <<  8) |\n"+
  207. "                  (bytes[3] & 0xff);\n"+
  208. "    return result;\n"+
  209. "}\n"+
  210.  
  211. "function isInNet(ipaddr, pattern, maskstr) {\n"+
  212. "    var test = /^(\\d{1,4})\\.(\\d{1,4})\\.(\\d{1,4})\\.(\\d{1,4})$/(ipaddr);\n"+
  213. "    if (test == null) {\n"+
  214. "        ipaddr = dnsResolve(ipaddr);\n"+
  215. "    } else if (test[1] > 255 || test[2] > 255 || \n"+
  216. "               test[3] > 255 || test[4] > 255) {\n"+
  217. "        return false;    // not an IP address\n"+
  218. "    }\n"+
  219. "    var host = convert_addr(ipaddr);\n"+
  220. "    var pat  = convert_addr(pattern);\n"+
  221. "    var mask = convert_addr(maskstr);\n"+
  222. "    return ((host & mask) == (pat & mask));\n"+
  223. "    \n"+
  224. "}\n"+
  225.  
  226. "function isPlainHostName(host) {\n" +
  227. "    return (host.search('\\.') == -1);\n" +
  228. "}\n" +
  229.  
  230. "function isResolvable(host) {\n" +
  231. "    var ip = dnsResolve(host);\n" +
  232. "    return (ip != null);\n" +
  233. "}\n" +
  234.  
  235. "function localHostOrDomainIs(host, hostdom) {\n" +
  236. "    if (isPlainHostName(host)) {\n" +
  237. "        return (hostdom.search('/^' + host + '/') != -1);\n" +
  238. "    }\n" +
  239. "    else {\n" +
  240. "        return (host == hostdom); //TODO check \n" +
  241. "    }\n" +
  242. "}\n" +
  243.  
  244. "function myIpAddress() {\n" +
  245. "    try {\n" +
  246. "        // for now...\n" +
  247. "        var ip = dnsResolve('localhost');\n" +
  248. "        return ip;\n" +
  249. "    }\n" +
  250. "    catch (ex) {\n" +
  251. "    }\n" +
  252. "    return null;\n" +
  253. "}\n" +
  254.  
  255. "function shExpMatch(url, pattern) {\n" +
  256. "   pattern = pattern.replace(/\\./g, '\\\\.');\n" +
  257. "   pattern = pattern.replace(/\\*/g, '.*');\n" +
  258. "   pattern = pattern.replace(/\\?/g, '.');\n" +
  259. "   var newRe = new RegExp(pattern);\n" +
  260. "   return newRe.test(url);\n" +
  261. "}\n" +
  262.  
  263. "var wdays = new Array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT');\n" +
  264.  
  265. "var monthes = new Array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');\n"+
  266.  
  267. "function weekdayRange() {\n" +
  268. "    function getDay(weekday) {\n" +
  269. "        for (var i = 0; i < 6; i++) {\n" +
  270. "            if (weekday == wdays[i]) \n" +
  271. "                return i;\n" +
  272. "        }\n" +
  273. "        return -1;\n" +
  274. "    }\n" +
  275. "    var date = new Date();\n" +
  276. "    var argc = arguments.length;\n" +
  277. "    var wday;\n" +
  278. "    if (argc < 1)\n" +
  279. "        return false;\n" +
  280. "    if (arguments[argc - 1] == 'GMT') {\n" +
  281. "        argc--;\n" +
  282. "        wday = date.getUTCDay();\n" +
  283. "    } else {\n" +
  284. "        wday = date.getDay();\n" +
  285. "    }\n" +
  286. "    var wd1 = getDay(arguments[0]);\n" +
  287. "    var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n" +
  288. "    return (wd1 == -1 || wd2 == -1) ? false\n" +
  289. "                                    : (wd1 <= wday && wday <= wd2);\n" +
  290. "}\n" +
  291.  
  292. "function dateRange() {\n" +
  293. "    function getMonth(name) {\n" +
  294. "        for (var i = 0; i < 6; i++) {\n" +
  295. "            if (name == monthes[i])\n" +
  296. "                return i;\n" +
  297. "        }\n" +
  298. "        return -1;\n" +
  299. "    }\n" +
  300. "    var date = new Date();\n" +
  301. "    var argc = arguments.length;\n" +
  302. "    if (argc < 1) {\n" +
  303. "        return false;\n" +
  304. "    }\n" +
  305. "    var isGMT = (arguments[argc - 1] == 'GMT');\n" +
  306. "\n" +
  307. "    if (isGMT) {\n" +
  308. "        argc--;\n" +
  309. "    }\n" +
  310. "    // function will work even without explict handling of this case\n" +
  311. "    if (argc == 1) {\n" +
  312. "        var tmp = parseInt(arguments[0]);\n" +
  313. "        if (isNaN(tmp)) {\n" +
  314. "            return ((isGMT ? date.getUTCMonth() : date.getMonth()) ==\n" +
  315. "getMonth(arguments[0]));\n" +
  316. "        } else if (tmp < 32) {\n" +
  317. "            return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);\n" +
  318. "        } else { \n" +
  319. "            return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==\n" +
  320. "tmp);\n" +
  321. "        }\n" +
  322. "    }\n" +
  323. "    var year = date.getFullYear();\n" +
  324. "    var date1, date2;\n" +
  325. "    date1 = new Date(year,  0,  1,  0,  0,  0);\n" +
  326. "    date2 = new Date(year, 11, 31, 23, 59, 59);\n" +
  327. "    var adjustMonth = false;\n" +
  328. "    for (var i = 0; i < (argc >> 1); i++) {\n" +
  329. "        var tmp = parseInt(arguments[i]);\n" +
  330. "        if (isNaN(tmp)) {\n" +
  331. "            var mon = getMonth(arguments[i]);\n" +
  332. "            date1.setMonth(mon);\n" +
  333. "        } else if (tmp < 32) {\n" +
  334. "            adjustMonth = (argc <= 2);\n" +
  335. "            date1.setDate(tmp);\n" +
  336. "        } else {\n" +
  337. "            date1.setFullYear(tmp);\n" +
  338. "        }\n" +
  339. "    }\n" +
  340. "    for (var i = (argc >> 1); i < argc; i++) {\n" +
  341. "        var tmp = parseInt(arguments[i]);\n" +
  342. "        if (isNaN(tmp)) {\n" +
  343. "            var mon = getMonth(arguments[i]);\n" +
  344. "            date2.setMonth(mon);\n" +
  345. "        } else if (tmp < 32) {\n" +
  346. "            date2.setDate(tmp);\n" +
  347. "        } else {\n" +
  348. "            date2.setFullYear(tmp);\n" +
  349. "        }\n" +
  350. "    }\n" +
  351. "    if (adjustMonth) {\n" +
  352. "        date1.setMonth(date.getMonth());\n" +
  353. "        date2.setMonth(date.getMonth());\n" +
  354. "    }\n" +
  355. "    if (isGMT) {\n" +
  356. "    var tmp = date;\n" +
  357. "        tmp.setFullYear(date.getUTCFullYear());\n" +
  358. "        tmp.setMonth(date.getUTCMonth());\n" +
  359. "        tmp.setDate(date.getUTCDate());\n" +
  360. "        tmp.setHours(date.getUTCHours());\n" +
  361. "        tmp.setMinutes(date.getUTCMinutes());\n" +
  362. "        tmp.setSeconds(date.getUTCSeconds());\n" +
  363. "        date = tmp;\n" +
  364. "    }\n" +
  365. "    return ((date1 <= date) && (date <= date2));\n" +
  366. "}\n" +
  367.  
  368. "function timeRange() {\n" +
  369. "    var argc = arguments.length;\n" +
  370. "    var date = new Date();\n" +
  371. "    var isGMT= false;\n"+
  372. "\n" +
  373. "    if (argc < 1) {\n" +
  374. "        return false;\n" +
  375. "    }\n" +
  376. "    if (arguments[argc - 1] == 'GMT') {\n" +
  377. "        isGMT = true;\n" +
  378. "        argc--;\n" +
  379. "    }\n" +
  380. "\n" +
  381. "    var hour = isGMT ? date.getUTCHours() : date.getHours();\n" +
  382. "    var date1, date2;\n" +
  383. "    date1 = new Date();\n" +
  384. "    date2 = new Date();\n" +
  385. "\n" +
  386. "    if (argc == 1) {\n" +
  387. "        return (hour == arguments[0]);\n" +
  388. "    } else if (argc == 2) {\n" +
  389. "        return ((arguments[0] <= hour) && (hour <= arguments[1]));\n" +
  390. "    } else {\n" +
  391. "        switch (argc) {\n" +
  392. "        case 6:\n" +
  393. "            date1.setSeconds(arguments[2]);\n" +
  394. "            date2.setSeconds(arguments[5]);\n" +
  395. "        case 4:\n" +
  396. "            var middle = argc >> 1;\n" +
  397. "            date1.setHours(arguments[0]);\n" +
  398. "            date1.setMinutes(arguments[1]);\n" +
  399. "            date2.setHours(arguments[middle]);\n" +
  400. "            date2.setMinutes(arguments[middle + 1]);\n" +
  401. "            if (middle == 2) {\n" +
  402. "                date2.setSeconds(59);\n" +
  403. "            }\n" +
  404. "            break;\n" +
  405. "        default:\n" +
  406. "          throw 'timeRange: bad number of arguments'\n" +
  407. "        }\n" +
  408. "    }\n" +
  409. "\n" +
  410. "    if (isGMT) {\n" +
  411. "        date.setFullYear(date.getUTCFullYear());\n" +
  412. "        date.setMonth(date.getUTCMonth());\n" +
  413. "        date.setDate(date.getUTCDate());\n" +
  414. "        date.setHours(date.getUTCHours());\n" +
  415. "        date.setMinutes(date.getUTCMinutes());\n" +
  416. "        date.setSeconds(date.getUTCSeconds());\n" +
  417. "    }\n" +
  418. "    return ((date1 <= date) && (date <= date2));\n" +
  419. "}\n"
  420.  
  421.