home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / components / nsProxyAutoConfig.js < prev    next >
Text File  |  2001-09-21  |  14KB  |  427 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. const nsIRequest = Components.interfaces.nsIRequest;
  38.  
  39. // implementor of nsIProxyAutoConfig
  40. function nsProxyAutoConfig() {};
  41.  
  42. // global variable that will hold the downloaded js 
  43. var pac = null;
  44. //hold PAC's URL, used in evalAsCodebase()
  45. var pacURL;
  46. // ptr to eval'ed FindProxyForURL function
  47. var LocalFindProxyForURL=null;
  48. // sendbox in which we eval loaded autoconfig js file
  49. var ProxySandBox = null;
  50.  
  51. nsProxyAutoConfig.prototype = {
  52.     sis: null,
  53.     done: false,
  54.  
  55.     ProxyForURL: function(url, host, port, type) {
  56.         /* If we're not done loading the pac yet, wait (ideally). For
  57.            now, just return DIRECT to avoid loops. A simple mutex
  58.            between ProxyForURL and LoadPACFromURL locks-up the
  59.            browser. */
  60.         if (!this.done) {
  61.             host.value = null;
  62.             type.value = "direct";
  63.             return;
  64.         }
  65.  
  66.         var uri = url.QueryInterface(Components.interfaces.nsIURI);
  67.         // Call the original function-
  68.         var proxy = LocalFindProxyForURL(uri.spec, uri.host);
  69.         if (proxy == "DIRECT") {
  70.             host.value = null;
  71.             type.value = "direct";
  72.         }
  73.         else {
  74.             // TODO warn about SOCKS
  75.  
  76.             // we ignore everything else past the first proxy. 
  77.             // we could theoretically check isResolvable now and continue 
  78.             // parsing. but for now...
  79.             var hostport = /^PROXY ([^:]+):(\d+)/(proxy);
  80.             host.value = hostport[1];
  81.             port.value = hostport[2];
  82.             type.value = "http"; //proxy (http, socks, direct, etc)
  83.         }
  84.     },
  85.  
  86.     LoadPACFromURL: function(uri, ioService) {
  87.         this.done = false;
  88.         var channel = ioService.newChannelFromURI(uri);
  89.         // don't cache the PAC content
  90.         channel.loadFlags |= nsIRequest.LOAD_BYPASS_CACHE;
  91.         pacURL = uri.spec;
  92.         channel.asyncOpen(this, null);
  93.     },
  94.  
  95.     // nsIStreamListener interface
  96.     onStartRequest: function(request, ctxt) { 
  97.         pac = '';
  98.         LocalFindProxyForURL=null;
  99.         this.sis = 
  100.         Components.Constructor('@mozilla.org/scriptableinputstream;1',
  101.                                'nsIScriptableInputStream', 
  102.                                'init');
  103.     },
  104.  
  105.     onStopRequest: function(request, ctxt, status, errorMsg) {
  106.         if(!ProxySandBox) {
  107.            ProxySandBox = new Sandbox();
  108.         }
  109.         // add predefined functions to pac
  110.         var mypac = pacUtils + pac;
  111.         // evaluate loded js file
  112.         evalInSandbox(mypac, ProxySandBox, pacURL);
  113.         try {
  114.             ProxySandBox.myIP = dns.myIPAddress;
  115.         } catch (e) {
  116.             // Well, theres nothing better.
  117.             // see bugs 80363 and 92516.
  118.             ProxySandBox.myIP = "127.0.0.1";
  119.         }
  120.         ProxySandBox.dnsResolve = dnsResolve;
  121.         LocalFindProxyForURL=ProxySandBox.FindProxyForURL;
  122.         this.done = true;
  123.     },
  124.  
  125.     onDataAvailable: function(request, ctxt, inStream, sourceOffset, count) {
  126.         var ins = new this.sis(inStream);
  127.         pac += ins.read(count);
  128.     }
  129. }
  130.  
  131. // Synchronous calls to nsDNSService::Resolve ignore the cache! (bug 97097)
  132. // Keep a simple one of our own.
  133. var dnsResolveCachedHost = null;
  134. var dnsResolveCachedIp = null;
  135.  
  136. // wrapper for dns.resolve to catch exception on failure
  137. function dnsResolve(host) {
  138.     if (host == dnsResolveCachedHost) {
  139.         return dnsResolveCachedIp;
  140.     }
  141.     dnsResolveCachedHost = host;
  142.     try {
  143.         dnsResolveCachedIp = dns.resolve(host);
  144.     }
  145.     catch (e) {
  146.         dnsResolveCachedIp = null;
  147.     }
  148.     return dnsResolveCachedIp;
  149. }
  150.  
  151. var pacModule = new Object();
  152.  
  153. pacModule.registerSelf =
  154.     function (compMgr, fileSpec, location, type) {
  155.         compMgr.registerComponentWithType(kPAC_CID,
  156.             "Proxy Auto Config",
  157.             kPAC_CONTRACTID,
  158.             fileSpec, location, 
  159.             true, true, type);
  160.     }
  161.  
  162. pacModule.getClassObject =
  163. function (compMgr, cid, iid) {
  164.         if (!cid.equals(kPAC_CID))
  165.             throw Components.results.NS_ERROR_NO_INTERFACE;
  166.  
  167.         if (!iid.equals(Components.interfaces.nsIFactory))
  168.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  169.  
  170.         return pacFactory;
  171.     }
  172.  
  173. pacModule.canUnload =
  174.     function (compMgr) {
  175.         return true;
  176.     }
  177.  
  178. var pacFactory = new Object();
  179. pacFactory.createInstance =
  180.     function (outer, iid) {
  181.         if (outer != null)
  182.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  183.  
  184.         if (!iid.equals(nsIProxyAutoConfig) &&
  185.             !!iid.equals(Components.interfaces.nsIStreamListener) &&
  186.             !iid.equals(Components.interfaces.nsISupports)) {
  187.             // shouldn't this be NO_INTERFACE?
  188.             throw Components.results.NS_ERROR_INVALID_ARG;
  189.         }
  190.         return PacMan;
  191.     }
  192.  
  193. function NSGetModule(compMgr, fileSpec) {
  194.     return pacModule;
  195. }
  196.  
  197. var PacMan = new nsProxyAutoConfig() ;
  198.  
  199. var ios = Components.classes[kIOSERVICE_CONTRACTID].getService(nsIIOService);
  200. var dns = Components.classes[kDNS_CONTRACTID].getService(nsIDNSService);
  201.  
  202. var pacUtils = 
  203. "function dnsDomainIs(host, domain) {\n" +
  204. "    return (host.length >= domain.length &&\n" +
  205. "            host.substring(host.length - domain.length) == domain);\n" +
  206. "}\n" +
  207.  
  208. "function dnsDomainLevels(host) {\n" +
  209. "    return host.split('.').length-1;\n" +
  210. "}\n" +
  211.  
  212. "function convert_addr(ipchars) {\n"+
  213. "    var bytes = ipchars.split('.');\n"+
  214. "    var result = ((bytes[0] & 0xff) << 24) |\n"+
  215. "                 ((bytes[1] & 0xff) << 16) |\n"+
  216. "                 ((bytes[2] & 0xff) <<  8) |\n"+
  217. "                  (bytes[3] & 0xff);\n"+
  218. "    return result;\n"+
  219. "}\n"+
  220.  
  221. "function isInNet(ipaddr, pattern, maskstr) {\n"+
  222. "    var test = /^(\\d{1,4})\\.(\\d{1,4})\\.(\\d{1,4})\\.(\\d{1,4})$/(ipaddr);\n"+
  223. "    if (test == null) {\n"+
  224. "        ipaddr = dnsResolve(ipaddr);\n"+
  225. "        if (ipaddr == null)\n"+
  226. "            return false;\n"+
  227. "    } else if (test[1] > 255 || test[2] > 255 || \n"+
  228. "               test[3] > 255 || test[4] > 255) {\n"+
  229. "        return false;    // not an IP address\n"+
  230. "    }\n"+
  231. "    var host = convert_addr(ipaddr);\n"+
  232. "    var pat  = convert_addr(pattern);\n"+
  233. "    var mask = convert_addr(maskstr);\n"+
  234. "    return ((host & mask) == (pat & mask));\n"+
  235. "    \n"+
  236. "}\n"+
  237.  
  238. "function isPlainHostName(host) {\n" +
  239. "    return (host.search('\\\\.') == -1);\n" +
  240. "}\n" +
  241.  
  242. "function isResolvable(host) {\n" +
  243. "    var ip = dnsResolve(host);\n" +
  244. "    return (ip != null);\n" +
  245. "}\n" +
  246.  
  247. "function localHostOrDomainIs(host, hostdom) {\n" +
  248. "    if (isPlainHostName(host)) {\n" +
  249. "        return (hostdom.search('/^' + host + '/') != -1);\n" +
  250. "    }\n" +
  251. "    else {\n" +
  252. "        return (host == hostdom); //TODO check \n" +
  253. "    }\n" +
  254. "}\n" +
  255.  
  256. " var myIP;\n" +
  257. "function myIpAddress() {\n" +
  258. "    return (myIP) ? myIP : '127.0.0.1';\n" +
  259. "}\n" +
  260.  
  261. "function shExpMatch(url, pattern) {\n" +
  262. "   pattern = pattern.replace(/\\./g, '\\\\.');\n" +
  263. "   pattern = pattern.replace(/\\*/g, '.*');\n" +
  264. "   pattern = pattern.replace(/\\?/g, '.');\n" +
  265. "   var newRe = new RegExp(pattern);\n" +
  266. "   return newRe.test(url);\n" +
  267. "}\n" +
  268.  
  269. "var wdays = new Array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT');\n" +
  270.  
  271. "var monthes = new Array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');\n"+
  272.  
  273. "function weekdayRange() {\n" +
  274. "    function getDay(weekday) {\n" +
  275. "        for (var i = 0; i < 6; i++) {\n" +
  276. "            if (weekday == wdays[i]) \n" +
  277. "                return i;\n" +
  278. "        }\n" +
  279. "        return -1;\n" +
  280. "    }\n" +
  281. "    var date = new Date();\n" +
  282. "    var argc = arguments.length;\n" +
  283. "    var wday;\n" +
  284. "    if (argc < 1)\n" +
  285. "        return false;\n" +
  286. "    if (arguments[argc - 1] == 'GMT') {\n" +
  287. "        argc--;\n" +
  288. "        wday = date.getUTCDay();\n" +
  289. "    } else {\n" +
  290. "        wday = date.getDay();\n" +
  291. "    }\n" +
  292. "    var wd1 = getDay(arguments[0]);\n" +
  293. "    var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n" +
  294. "    return (wd1 == -1 || wd2 == -1) ? false\n" +
  295. "                                    : (wd1 <= wday && wday <= wd2);\n" +
  296. "}\n" +
  297.  
  298. "function dateRange() {\n" +
  299. "    function getMonth(name) {\n" +
  300. "        for (var i = 0; i < 6; i++) {\n" +
  301. "            if (name == monthes[i])\n" +
  302. "                return i;\n" +
  303. "        }\n" +
  304. "        return -1;\n" +
  305. "    }\n" +
  306. "    var date = new Date();\n" +
  307. "    var argc = arguments.length;\n" +
  308. "    if (argc < 1) {\n" +
  309. "        return false;\n" +
  310. "    }\n" +
  311. "    var isGMT = (arguments[argc - 1] == 'GMT');\n" +
  312. "\n" +
  313. "    if (isGMT) {\n" +
  314. "        argc--;\n" +
  315. "    }\n" +
  316. "    // function will work even without explict handling of this case\n" +
  317. "    if (argc == 1) {\n" +
  318. "        var tmp = parseInt(arguments[0]);\n" +
  319. "        if (isNaN(tmp)) {\n" +
  320. "            return ((isGMT ? date.getUTCMonth() : date.getMonth()) ==\n" +
  321. "getMonth(arguments[0]));\n" +
  322. "        } else if (tmp < 32) {\n" +
  323. "            return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);\n" +
  324. "        } else { \n" +
  325. "            return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==\n" +
  326. "tmp);\n" +
  327. "        }\n" +
  328. "    }\n" +
  329. "    var year = date.getFullYear();\n" +
  330. "    var date1, date2;\n" +
  331. "    date1 = new Date(year,  0,  1,  0,  0,  0);\n" +
  332. "    date2 = new Date(year, 11, 31, 23, 59, 59);\n" +
  333. "    var adjustMonth = false;\n" +
  334. "    for (var i = 0; i < (argc >> 1); i++) {\n" +
  335. "        var tmp = parseInt(arguments[i]);\n" +
  336. "        if (isNaN(tmp)) {\n" +
  337. "            var mon = getMonth(arguments[i]);\n" +
  338. "            date1.setMonth(mon);\n" +
  339. "        } else if (tmp < 32) {\n" +
  340. "            adjustMonth = (argc <= 2);\n" +
  341. "            date1.setDate(tmp);\n" +
  342. "        } else {\n" +
  343. "            date1.setFullYear(tmp);\n" +
  344. "        }\n" +
  345. "    }\n" +
  346. "    for (var i = (argc >> 1); i < argc; i++) {\n" +
  347. "        var tmp = parseInt(arguments[i]);\n" +
  348. "        if (isNaN(tmp)) {\n" +
  349. "            var mon = getMonth(arguments[i]);\n" +
  350. "            date2.setMonth(mon);\n" +
  351. "        } else if (tmp < 32) {\n" +
  352. "            date2.setDate(tmp);\n" +
  353. "        } else {\n" +
  354. "            date2.setFullYear(tmp);\n" +
  355. "        }\n" +
  356. "    }\n" +
  357. "    if (adjustMonth) {\n" +
  358. "        date1.setMonth(date.getMonth());\n" +
  359. "        date2.setMonth(date.getMonth());\n" +
  360. "    }\n" +
  361. "    if (isGMT) {\n" +
  362. "    var tmp = date;\n" +
  363. "        tmp.setFullYear(date.getUTCFullYear());\n" +
  364. "        tmp.setMonth(date.getUTCMonth());\n" +
  365. "        tmp.setDate(date.getUTCDate());\n" +
  366. "        tmp.setHours(date.getUTCHours());\n" +
  367. "        tmp.setMinutes(date.getUTCMinutes());\n" +
  368. "        tmp.setSeconds(date.getUTCSeconds());\n" +
  369. "        date = tmp;\n" +
  370. "    }\n" +
  371. "    return ((date1 <= date) && (date <= date2));\n" +
  372. "}\n" +
  373.  
  374. "function timeRange() {\n" +
  375. "    var argc = arguments.length;\n" +
  376. "    var date = new Date();\n" +
  377. "    var isGMT= false;\n"+
  378. "\n" +
  379. "    if (argc < 1) {\n" +
  380. "        return false;\n" +
  381. "    }\n" +
  382. "    if (arguments[argc - 1] == 'GMT') {\n" +
  383. "        isGMT = true;\n" +
  384. "        argc--;\n" +
  385. "    }\n" +
  386. "\n" +
  387. "    var hour = isGMT ? date.getUTCHours() : date.getHours();\n" +
  388. "    var date1, date2;\n" +
  389. "    date1 = new Date();\n" +
  390. "    date2 = new Date();\n" +
  391. "\n" +
  392. "    if (argc == 1) {\n" +
  393. "        return (hour == arguments[0]);\n" +
  394. "    } else if (argc == 2) {\n" +
  395. "        return ((arguments[0] <= hour) && (hour <= arguments[1]));\n" +
  396. "    } else {\n" +
  397. "        switch (argc) {\n" +
  398. "        case 6:\n" +
  399. "            date1.setSeconds(arguments[2]);\n" +
  400. "            date2.setSeconds(arguments[5]);\n" +
  401. "        case 4:\n" +
  402. "            var middle = argc >> 1;\n" +
  403. "            date1.setHours(arguments[0]);\n" +
  404. "            date1.setMinutes(arguments[1]);\n" +
  405. "            date2.setHours(arguments[middle]);\n" +
  406. "            date2.setMinutes(arguments[middle + 1]);\n" +
  407. "            if (middle == 2) {\n" +
  408. "                date2.setSeconds(59);\n" +
  409. "            }\n" +
  410. "            break;\n" +
  411. "        default:\n" +
  412. "          throw 'timeRange: bad number of arguments'\n" +
  413. "        }\n" +
  414. "    }\n" +
  415. "\n" +
  416. "    if (isGMT) {\n" +
  417. "        date.setFullYear(date.getUTCFullYear());\n" +
  418. "        date.setMonth(date.getUTCMonth());\n" +
  419. "        date.setDate(date.getUTCDate());\n" +
  420. "        date.setHours(date.getUTCHours());\n" +
  421. "        date.setMinutes(date.getUTCMinutes());\n" +
  422. "        date.setSeconds(date.getUTCSeconds());\n" +
  423. "    }\n" +
  424. "    return ((date1 <= date) && (date <= date2));\n" +
  425. "}\n"
  426.  
  427.