Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / netwerk / base / src / nsProxyAutoConfig.js
blob2afa6b218041083b8345f9b0d4c72c9820187dc0
1 /* -*- Mode: Java; tab-width: 4; c-basic-offset: 4; -*- */
2 /* vim:set ts=4 sw=4 sts=4 et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is mozilla.org code.
17  *
18  * The Initial Developer of the Original Code is
19  * Netscape Communications Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 1998
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  *   Akhil Arora <akhil.arora@sun.com>
25  *   Tomi Leppikangas <Tomi.Leppikangas@oulu.fi>
26  *   Darin Fisher <darin@meer.net>
27  *
28  * Alternatively, the contents of this file may be used under the terms of
29  * either the GNU General Public License Version 2 or later (the "GPL"), or
30  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31  * in which case the provisions of the GPL or the LGPL are applicable instead
32  * of those above. If you wish to allow use of your version of this file only
33  * under the terms of either the GPL or the LGPL, and not to allow others to
34  * use your version of this file under the terms of the MPL, indicate your
35  * decision by deleting the provisions above and replace them with the notice
36  * and other provisions required by the GPL or the LGPL. If you do not delete
37  * the provisions above, a recipient may use your version of this file under
38  * the terms of any one of the MPL, the GPL or the LGPL.
39  *
40  * ***** END LICENSE BLOCK ***** */
43    Script for Proxy Auto Config in the new world order.
44        - Gagan Saksena 04/24/00 
47 const kDNS_CONTRACTID = "@mozilla.org/network/dns-service;1";
48 const kPAC_CONTRACTID = "@mozilla.org/network/proxy-auto-config;1";
49 const kPAC_CID = Components.ID("{63ac8c66-1dd2-11b2-b070-84d00d3eaece}");
51 const nsISupports        = Components.interfaces.nsISupports;
52 const nsIProxyAutoConfig = Components.interfaces.nsIProxyAutoConfig;
53 const nsIDNSService      = Components.interfaces.nsIDNSService;
55 // implementor of nsIProxyAutoConfig
56 function nsProxyAutoConfig() {};
58 nsProxyAutoConfig.prototype = {
59     // sandbox in which we eval loaded autoconfig js file
60     _sandBox: null, 
62     QueryInterface: function(iid) {
63         if (iid.Equals(nsIProxyAutoConfig) ||
64             iid.Equals(nsISupports))
65             return this;
66         throw Components.results.NS_ERROR_NO_INTERFACE;
67     },
69     init: function(pacURI, pacText) {
70         // remove PAC configuration if requested
71         if (pacURI == "" || pacText == "") {
72             this._sandBox = null;
73             return;
74         }
76         // allocate a fresh Sandbox to clear global scope for new PAC script
77         this._sandBox = new Components.utils.Sandbox(pacURI);
78         Components.utils.evalInSandbox(pacUtils, this._sandBox);
80         // add predefined functions to pac
81         this._sandBox.importFunction(myIpAddress);
82         this._sandBox.importFunction(dnsResolve);
83         this._sandBox.importFunction(proxyAlert, "alert");
85         // evaluate loaded js file
86         Components.utils.evalInSandbox(pacText, this._sandBox);
88         // We can no longer trust this._sandBox. Touching it directly can
89         // cause all sorts of pain, so wrap it in an XPCSafeJSObjectWrapper
90         // and do all of our work through there.
91         this._sandBox = new XPCSafeJSObjectWrapper(this._sandBox);
92     },
94     getProxyForURI: function(testURI, testHost) {
95         if (!("FindProxyForURL" in this._sandBox))
96             return null;
98         // Call the original function
99         try {
100             var rval = this._sandBox.FindProxyForURL(testURI, testHost);
101         } catch (e) {
102             throw XPCSafeJSObjectWrapper(e);
103         }
104         return rval;
105     }
108 function proxyAlert(msg) {
109     msg = XPCSafeJSObjectWrapper(msg);
110     try {
111         // It would appear that the console service is threadsafe.
112         var cns = Components.classes["@mozilla.org/consoleservice;1"]
113                             .getService(Components.interfaces.nsIConsoleService);
114         cns.logStringMessage("PAC-alert: "+msg);
115     } catch (e) {
116         dump("PAC: proxyAlert ERROR: "+e+"\n");
117     }
120 // wrapper for getting local IP address called by PAC file
121 function myIpAddress() {
122     try {
123         return dns.resolve(dns.myHostName, 0).getNextAddrAsString();
124     } catch (e) {
125         return '127.0.0.1';
126     }
129 // wrapper for resolving hostnames called by PAC file
130 function dnsResolve(host) {
131     host = XPCSafeJSObjectWrapper(host);
132     try {
133         return dns.resolve(host, 0).getNextAddrAsString();
134     } catch (e) {
135         return null;
136     }
139 var pacModule = new Object();
141 pacModule.registerSelf =
142     function (compMgr, fileSpec, location, type) {
143         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
144         compMgr.registerFactoryLocation(kPAC_CID,
145                                         "nsProxyAutoConfig",
146                                         kPAC_CONTRACTID,
147                                         fileSpec, 
148                                         location, 
149                                         type);
150     }
152 pacModule.getClassObject =
153 function (compMgr, cid, iid) {
154         if (!cid.equals(kPAC_CID))
155             throw Components.results.NS_ERROR_NO_INTERFACE;
157         if (!iid.equals(Components.interfaces.nsIFactory))
158             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
160         return pacFactory;
161     }
163 pacModule.canUnload =
164     function (compMgr) {
165         return true;
166     }
168 var pacFactory = new Object();
169 pacFactory.createInstance =
170     function (outer, iid) {
171         if (outer != null)
172             throw Components.results.NS_ERROR_NO_AGGREGATION;
174         if (!iid.equals(nsIProxyAutoConfig) &&
175             !iid.equals(Components.interfaces.nsISupports)) {
176             throw Components.results.NS_ERROR_NO_INTERFACE;
177         }
178         return pac;
179     }
181 function NSGetModule(compMgr, fileSpec) {
182     return pacModule;
185 var pac = new nsProxyAutoConfig() ;
186 var dns = Components.classes[kDNS_CONTRACTID].getService(nsIDNSService);
188 var pacUtils = 
189 "function dnsDomainIs(host, domain) {\n" +
190 "    return (host.length >= domain.length &&\n" +
191 "            host.substring(host.length - domain.length) == domain);\n" +
192 "}\n" +
194 "function dnsDomainLevels(host) {\n" +
195 "    return host.split('.').length-1;\n" +
196 "}\n" +
198 "function convert_addr(ipchars) {\n"+
199 "    var bytes = ipchars.split('.');\n"+
200 "    var result = ((bytes[0] & 0xff) << 24) |\n"+
201 "                 ((bytes[1] & 0xff) << 16) |\n"+
202 "                 ((bytes[2] & 0xff) <<  8) |\n"+
203 "                  (bytes[3] & 0xff);\n"+
204 "    return result;\n"+
205 "}\n"+
207 "function isInNet(ipaddr, pattern, maskstr) {\n"+
208 "    var test = /^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/(ipaddr);\n"+
209 "    if (test == null) {\n"+
210 "        ipaddr = dnsResolve(ipaddr);\n"+
211 "        if (ipaddr == null)\n"+
212 "            return false;\n"+
213 "    } else if (test[1] > 255 || test[2] > 255 || \n"+
214 "               test[3] > 255 || test[4] > 255) {\n"+
215 "        return false;    // not an IP address\n"+
216 "    }\n"+
217 "    var host = convert_addr(ipaddr);\n"+
218 "    var pat  = convert_addr(pattern);\n"+
219 "    var mask = convert_addr(maskstr);\n"+
220 "    return ((host & mask) == (pat & mask));\n"+
221 "    \n"+
222 "}\n"+
224 "function isPlainHostName(host) {\n" +
225 "    return (host.search('\\\\.') == -1);\n" +
226 "}\n" +
228 "function isResolvable(host) {\n" +
229 "    var ip = dnsResolve(host);\n" +
230 "    return (ip != null);\n" +
231 "}\n" +
233 "function localHostOrDomainIs(host, hostdom) {\n" +
234 "    return (host == hostdom) ||\n" +
235 "           (hostdom.lastIndexOf(host + '.', 0) == 0);\n" +
236 "}\n" +
238 "function shExpMatch(url, pattern) {\n" +
239 "   pattern = pattern.replace(/\\./g, '\\\\.');\n" +
240 "   pattern = pattern.replace(/\\*/g, '.*');\n" +
241 "   pattern = pattern.replace(/\\?/g, '.');\n" +
242 "   var newRe = new RegExp('^'+pattern+'$');\n" +
243 "   return newRe.test(url);\n" +
244 "}\n" +
246 "var wdays = {SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6};\n" +
248 "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"+
250 "function weekdayRange() {\n" +
251 "    function getDay(weekday) {\n" +
252 "        if (weekday in wdays) {\n" +
253 "            return wdays[weekday];\n" +
254 "        }\n" +
255 "        return -1;\n" +
256 "    }\n" +
257 "    var date = new Date();\n" +
258 "    var argc = arguments.length;\n" +
259 "    var wday;\n" +
260 "    if (argc < 1)\n" +
261 "        return false;\n" +
262 "    if (arguments[argc - 1] == 'GMT') {\n" +
263 "        argc--;\n" +
264 "        wday = date.getUTCDay();\n" +
265 "    } else {\n" +
266 "        wday = date.getDay();\n" +
267 "    }\n" +
268 "    var wd1 = getDay(arguments[0]);\n" +
269 "    var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n" +
270 "    return (wd1 == -1 || wd2 == -1) ? false\n" +
271 "                                    : (wd1 <= wday && wday <= wd2);\n" +
272 "}\n" +
274 "function dateRange() {\n" +
275 "    function getMonth(name) {\n" +
276 "        if (name in months) {\n" +
277 "            return months[name];\n" +
278 "        }\n" +
279 "        return -1;\n" +
280 "    }\n" +
281 "    var date = new Date();\n" +
282 "    var argc = arguments.length;\n" +
283 "    if (argc < 1) {\n" +
284 "        return false;\n" +
285 "    }\n" +
286 "    var isGMT = (arguments[argc - 1] == 'GMT');\n" +
287 "\n" +
288 "    if (isGMT) {\n" +
289 "        argc--;\n" +
290 "    }\n" +
291 "    // function will work even without explict handling of this case\n" +
292 "    if (argc == 1) {\n" +
293 "        var tmp = parseInt(arguments[0]);\n" +
294 "        if (isNaN(tmp)) {\n" +
295 "            return ((isGMT ? date.getUTCMonth() : date.getMonth()) ==\n" +
296 "getMonth(arguments[0]));\n" +
297 "        } else if (tmp < 32) {\n" +
298 "            return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);\n" +
299 "        } else { \n" +
300 "            return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==\n" +
301 "tmp);\n" +
302 "        }\n" +
303 "    }\n" +
304 "    var year = date.getFullYear();\n" +
305 "    var date1, date2;\n" +
306 "    date1 = new Date(year,  0,  1,  0,  0,  0);\n" +
307 "    date2 = new Date(year, 11, 31, 23, 59, 59);\n" +
308 "    var adjustMonth = false;\n" +
309 "    for (var i = 0; i < (argc >> 1); i++) {\n" +
310 "        var tmp = parseInt(arguments[i]);\n" +
311 "        if (isNaN(tmp)) {\n" +
312 "            var mon = getMonth(arguments[i]);\n" +
313 "            date1.setMonth(mon);\n" +
314 "        } else if (tmp < 32) {\n" +
315 "            adjustMonth = (argc <= 2);\n" +
316 "            date1.setDate(tmp);\n" +
317 "        } else {\n" +
318 "            date1.setFullYear(tmp);\n" +
319 "        }\n" +
320 "    }\n" +
321 "    for (var i = (argc >> 1); i < argc; i++) {\n" +
322 "        var tmp = parseInt(arguments[i]);\n" +
323 "        if (isNaN(tmp)) {\n" +
324 "            var mon = getMonth(arguments[i]);\n" +
325 "            date2.setMonth(mon);\n" +
326 "        } else if (tmp < 32) {\n" +
327 "            date2.setDate(tmp);\n" +
328 "        } else {\n" +
329 "            date2.setFullYear(tmp);\n" +
330 "        }\n" +
331 "    }\n" +
332 "    if (adjustMonth) {\n" +
333 "        date1.setMonth(date.getMonth());\n" +
334 "        date2.setMonth(date.getMonth());\n" +
335 "    }\n" +
336 "    if (isGMT) {\n" +
337 "    var tmp = date;\n" +
338 "        tmp.setFullYear(date.getUTCFullYear());\n" +
339 "        tmp.setMonth(date.getUTCMonth());\n" +
340 "        tmp.setDate(date.getUTCDate());\n" +
341 "        tmp.setHours(date.getUTCHours());\n" +
342 "        tmp.setMinutes(date.getUTCMinutes());\n" +
343 "        tmp.setSeconds(date.getUTCSeconds());\n" +
344 "        date = tmp;\n" +
345 "    }\n" +
346 "    return ((date1 <= date) && (date <= date2));\n" +
347 "}\n" +
349 "function timeRange() {\n" +
350 "    var argc = arguments.length;\n" +
351 "    var date = new Date();\n" +
352 "    var isGMT= false;\n"+
353 "\n" +
354 "    if (argc < 1) {\n" +
355 "        return false;\n" +
356 "    }\n" +
357 "    if (arguments[argc - 1] == 'GMT') {\n" +
358 "        isGMT = true;\n" +
359 "        argc--;\n" +
360 "    }\n" +
361 "\n" +
362 "    var hour = isGMT ? date.getUTCHours() : date.getHours();\n" +
363 "    var date1, date2;\n" +
364 "    date1 = new Date();\n" +
365 "    date2 = new Date();\n" +
366 "\n" +
367 "    if (argc == 1) {\n" +
368 "        return (hour == arguments[0]);\n" +
369 "    } else if (argc == 2) {\n" +
370 "        return ((arguments[0] <= hour) && (hour <= arguments[1]));\n" +
371 "    } else {\n" +
372 "        switch (argc) {\n" +
373 "        case 6:\n" +
374 "            date1.setSeconds(arguments[2]);\n" +
375 "            date2.setSeconds(arguments[5]);\n" +
376 "        case 4:\n" +
377 "            var middle = argc >> 1;\n" +
378 "            date1.setHours(arguments[0]);\n" +
379 "            date1.setMinutes(arguments[1]);\n" +
380 "            date2.setHours(arguments[middle]);\n" +
381 "            date2.setMinutes(arguments[middle + 1]);\n" +
382 "            if (middle == 2) {\n" +
383 "                date2.setSeconds(59);\n" +
384 "            }\n" +
385 "            break;\n" +
386 "        default:\n" +
387 "          throw 'timeRange: bad number of arguments'\n" +
388 "        }\n" +
389 "    }\n" +
390 "\n" +
391 "    if (isGMT) {\n" +
392 "        date.setFullYear(date.getUTCFullYear());\n" +
393 "        date.setMonth(date.getUTCMonth());\n" +
394 "        date.setDate(date.getUTCDate());\n" +
395 "        date.setHours(date.getUTCHours());\n" +
396 "        date.setMinutes(date.getUTCMinutes());\n" +
397 "        date.setSeconds(date.getUTCSeconds());\n" +
398 "    }\n" +
399 "    return ((date1 <= date) && (date <= date2));\n" +
400 "}\n"