This file is indexed.

/usr/share/xul-ext/foxyproxy-standard/components/proxy.js is in xul-ext-foxyproxy-standard 3.4-1.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/**
  FoxyProxy
  Copyright (C) 2006-2011 Eric H. Jung and FoxyProxy, Inc.
  http://getfoxyproxy.org/
  eric.jung@yahoo.com

  This source code is released under the GPL license,
  available in the LICENSE file at the root of this installation
  and also online at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
**/
//dump("proxy.js\n");
if (!CI) {
  // XPCOM module initialization
  var NSGetModule = function() { return ProxyModule; }

  var CI = Components.interfaces, CC = Components.classes, CR = Components.results, self,
    fileProtocolHandler = CC["@mozilla.org/network/protocol;1?name=file"].getService(CI["nsIFileProtocolHandler"]);
  if ("undefined" != typeof(__LOCATION__)) {
    // preferred way
    self = __LOCATION__;
  }
  else {
    self = fileProtocolHandler.getFileFromURLSpec(Components.Exception().filename);
  }
  var componentDir = self.parent; // the directory this file is in

  // Get attribute from node if it exists, otherwise return |def|.
  // No exceptions, no errors, no null returns.
  var gGetSafeAttr = function(n, name, def) {
    n.QueryInterface(CI.nsIDOMElement);
    return n ? (n.hasAttribute(name) ? n.getAttribute(name) : def) : def;
  }
  // Boolean version of GetSafe
  var gGetSafeAttrB = function(n, name, def) {
    n.QueryInterface(CI.nsIDOMElement);
    return n ? (n.hasAttribute(name) ? n.getAttribute(name)=="true" : def) : def;
  }

  var loadComponentScript = function(filename) {
    try {
      var filePath = componentDir.clone();
      filePath.append(filename);
      loader.loadSubScript(fileProtocolHandler.getURLSpecFromFile(filePath));
    }
    catch (e) {
      dump("Error loading component " + filename + ": " + e + "\n" + e.stack + "\n");
      throw(e);
    }
  }
  var self,
    fileProtocolHandler = CC["@mozilla.org/network/protocol;1?name=file"].getService(CI["nsIFileProtocolHandler"]);
  if ("undefined" != typeof(__LOCATION__)) {
    // preferred way
    self = __LOCATION__;
  }
  else {
    self = fileProtocolHandler.getFileFromURLSpec(Components.Exception().filename);
  }
  var dir = self.parent, // the directory this file is in
    loader = CC["@mozilla.org/moz/jssubscript-loader;1"].getService(CI["mozIJSSubScriptLoader"]);
}

loadComponentScript("autoconf.js");
loadComponentScript("match.js");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

var proxyService = CC["@mozilla.org/network/protocol-proxy-service;1"].getService(CI.nsIProtocolProxyService),
  DEFAULT_COLOR = "#0055E5"; /* blue */
///////////////////////////// Proxy class ///////////////////////
function Proxy(fp) {
  this.wrappedJSObject = this;
  this.fp = fp || CC["@leahscape.org/foxyproxy/service;1"].getService().
    wrappedJSObject;
  this.sysProxyService = CC["@mozilla.org/system-proxy-settings;1"].
    getService(CI.nsISystemProxySettings);
  this.iOService = CC["@mozilla.org/network/io-service;1"].
    getService(CI.nsIIOService);
  this.matches = [];
  this.name = this.notes = "";
  this.manualconf = new ManualConf(this, this.fp);
  this.autoconf = new AutoConf(this, this.fp);
  // An own object for the WPAD feature...
  this.wpad = new AutoConf(this, this.fp);
  // We set a URL to the proxy file which cannot changed. The rationale for
  // this is:
  // "We diverge from the WPAD spec here in that we don't walk the
  // hosts's FQDN, stripping components until we hit a TLD.  Doing so
  // is dangerous in the face of an incomplete list of TLDs, and TLDs
  // get added over time.  We could consider doing only a single
  // substitution of the first component, if that proves to help
  // compatibility." 
  // See: http://mxr.mozilla.org/mozilla2.0/source/netwerk/base/src/
  // nsProtocolProxyService.cpp#488 
  this.wpad.url = "http://wpad/wpad.dat";
  this._mode = "manual"; // manual, auto, direct, random
  this._autoconfMode = "pac";
  this._enabled = true;
  this.selectedTabIndex = 1; /* default tab is the proxy details tab */
  this.lastresort = false;
  this.id = this.fp.proxies.uniqueRandom();
}

Proxy.prototype = {
  direct: proxyService.newProxyInfo("direct", "", -1, 0, 0, null),
  animatedIcons: true,
  includeInCycle: true,
  noInternalIPs: false,
  _color: DEFAULT_COLOR,
  colorString: "nmbado",
  _proxyDNS: true,
  fp: null,
  iOService: null,
  sysProxyService: null,
  // Eventually, we need one object to store the PAC settings of the proxy
  // specified in the system settings.
  systemProxyPAC: null,
  readOnlyProperties : ["lastresort", "fp", "wrappedJSObject", "matches", /* from ManualConf */ "owner",
                        /* from AutoConf */ "timer", /* from AutoConf */  "_resolver"],

  fromDOM : function(node, includeTempPatterns) {
    this.name = node.getAttribute("name");
    this.id = node.getAttribute("id") || this.fp.proxies.uniqueRandom();
    this.notes = node.getAttribute("notes");
    this._enabled = node.getAttribute("enabled") == "true";
    this.autoconf.fromDOM(node.getElementsByTagName("autoconf").item(0));
    let wpadNode = node.getElementsByTagName("autoconf").item(1);
    if (wpadNode) { 
      this.wpad.fromDOM(wpadNode); 
    } else {
      this.wpad = new AutoConf(this, this.fp);
      this.wpad.url = "http://wpad/wpad.dat";
    }
    this._proxyDNS = gGetSafeAttrB(node, "proxyDNS", true);
    this.manualconf.fromDOM(node.getElementsByTagName("manualconf").item(0));
    // 1.1 used "manual" instead of "mode" and was true/false only (for manual or auto)
    this._mode = node.hasAttribute("manual") ?
      (node.getAttribute("manual") == "true" ? "manual" : "auto") :
      node.getAttribute("mode");
    this._mode = this._mode || "manual";
    // New for 3.3. If the proxy had "wpad" as its mode select "wpad" as
    // autoconfMode otherwise the default, "pac", is used.
    if (this._mode !== "wpad") {
      this._autoconfMode = gGetSafeAttr(node, "autoconfMode", "pac");
    } else {
      // The mode was WPAD but that is not available anymore starting with 3.3.
      // There is only "auto" as proxy mode (we choose it) and two autoconf
      // modes, "wpad" and "pac", now (we choose former). 
      this._mode = "auto";
      this._autoconfMode = gGetSafeAttr(node, "autoconfMode", "wpad"); 
    }
    this.selectedTabIndex = node.getAttribute("selectedTabIndex") || "0";
    if (this.fp.isFoxyProxySimple() && this.selectedTabIndex > 1)
      this.selectedTabIndex = 1; /* FoxyProxy Simple only has 2 tabs */
	  this.lastresort = node.hasAttribute("lastresort") ? node.getAttribute("lastresort") == "true" : false; // new for 2.0
    this.animatedIcons = node.hasAttribute("animatedIcons") ? node.getAttribute("animatedIcons") == "true" : !this.lastresort; // new for 2.4
    this.includeInCycle = node.hasAttribute("includeInCycle") ? node.getAttribute("includeInCycle") == "true" : !this.lastresort; // new for 2.5
    this.color = gGetSafeAttr(node, "color", DEFAULT_COLOR);    
    
    this.noInternalIPs = node.hasAttribute("noInternalIPs") ?
      node.getAttribute("noInternalIPs") == "true" : false;
    for (var i=0,temp=node.getElementsByTagName("match"); i<temp.length; i++) {
      var j = this.matches.length;
      this.matches[j] = new Match();
      this.matches[j].fromDOM(temp.item(i), includeTempPatterns);
    }
    // Were we disabled due to a bad/missing PAC file? If so, try enabling ourselves again.
    //if (this.autoconf.disabledDueToBadPAC) {
      //this._enabled = true;
    //}
    this.afterPropertiesSet();
  },
  
  /**
   * |includeTempPatterns| is only true when the user is copying a proxy and all its data
   */
  toDOM : function(doc, includeTempPatterns) {
    var e = doc.createElement("proxy");
    e.setAttribute("name", this.name);
    e.setAttribute("id", this.id);
    e.setAttribute("notes", this.notes);
    e.setAttribute("enabled", this.enabled);
    e.setAttribute("mode", this.mode);
    e.setAttribute("selectedTabIndex", this.selectedTabIndex);
    e.setAttribute("lastresort", this.lastresort);
    e.setAttribute("animatedIcons", this.animatedIcons);
    e.setAttribute("includeInCycle", this.includeInCycle);
    e.setAttribute("color", this._color);
    e.setAttribute("proxyDNS", this._proxyDNS);
    e.setAttribute("noInternalIPs", this.noInternalIPs);
    e.setAttribute("autoconfMode", this._autoconfMode);

    var matchesElem = doc.createElement("matches");
    e.appendChild(matchesElem);
    for (var j=0, m; j<this.matches.length && (m=this.matches[j]); j++)
      if (!m.temp || (includeTempPatterns && m.temp)) matchesElem.appendChild(m.toDOM(doc, includeTempPatterns));

    e.appendChild(this.autoconf.toDOM(doc));
    e.appendChild(this.wpad.toDOM(doc)); 
    e.appendChild(this.manualconf.toDOM(doc));
    return e;
  },
  
  /**
   * If this proxy requires network.dns.disablePrefetch to be false,
   * return true. network.dns.disablePrefetch must be false when the
   * user wants to proxy DNS requests through this proxy. Otherwise,
   * Firefox won't always use this proxy for DNS lookups.
   */
  shouldDisableDNSPrefetch : function() {
    return this._mode != "direct" && this._enabled && this._proxyDNS;
  },
  
  /**
   * Merge |src| into this, using the keys of the |nameValuePairs|
   * associative array as the properties to overwrite in |this|.
   */
  merge : function(src, nameValuePairs) {
    for (var propertyName in nameValuePairs) {
      // Simple sanity check on our input
      var obj = this.propertyBelongsTo(propertyName, this, this.manualconf, this.autoconf);
      // If obj == null then we don't understand this property, so ignore it.
      if (obj)
        obj[propertyName] = nameValuePairs[propertyName];
    }
  },
  
  getPropertyValue : function(propertyName) {
    var obj = this.propertyBelongsTo(propertyName, this, this.manualconf, this.autoconf);
    return obj ? obj[propertyName] : "";
  },
  
  /**
   * Checks if |propertyName| is a known writable property of the classes Proxy,
   * ManualConf, or AutoConf (excepting readOnly properties). |x|, |y|, or |z|
   * is returned, respectively, based on |propertyName's| membership in one of those
   * classes, If the property is unknown or read-only, null is returned.
   */
  propertyBelongsTo : function(propertyName, x, y, z) {
    function validType(str) {
      return str == "string" || str == "boolean" || str == "number";
    }    
    if (this.readOnlyProperties.indexOf(propertyName) > -1) return null;
    
    if (validType(typeof(this[propertyName])))
      return x;
    else if (validType(typeof(this.manualconf[propertyName])))
      return y;
    else if (validType(typeof(this.autoconf[propertyName])))
      return z;
    return null;    
  },
  
  /**
   * Use as a static-style method on this class.
   * Returns a |Proxy| instance based on the |nameValuePairs|
   * associative array. Each key in the array is expected to be
   * a property of either Proxy, ManualConf, or AutoConf, otherwise
   * it is ignored.
   */
  fromAssociateArray : function(nameValuePairs) {
    var doc = CC["@mozilla.org/xml/xml-document;1"].createInstance(CI.nsIDOMDocument),
      proxyElem = doc.createElement("proxy"),
      manualConfElem = doc.createElement("manualconf"),
      autoConfElem = doc.createElement("autoconf");
    
    proxyElem.appendChild(manualConfElem);
    proxyElem.appendChild(autoConfElem);
    for (var i in nameValuePairs) {
      // Simple sanity check on our input
      var elem = this.propertyBelongsTo(i, proxyElem, manualConfElem, autoConfElem);
      /* If elem == null then we don't understand this property, so ignore it.
         However, even if an unrecognized property were to slip by us here,
         the |Proxy.fromDOM()| code would ignore it anyway. This check just prevents us from
         building an arbitrarily large DOM element.
      */
      if (elem)
        elem.setAttribute(i, nameValuePairs[i] == null ? "" : nameValuePairs[i]);         
    }
    // Turn it on by default
    if (!nameValuePairs["enabled"])
      proxyElem.setAttribute("enabled", "true");
    
    // If a socks version was specified and either isSocks is true or no isSocks parameter was specified,
    // then enable socks
    if (nameValuePairs["socksversion"] && (nameValuePairs["isSocks"] == "true" || !nameValuePairs["isSocks"])) {
      nameValuePairs["isSocks"] = true;
      manualConfElem.setAttribute("isSocks", "true");
      manualConfElem.setAttribute("socksversion", parseInt(nameValuePairs["socksversion"]));
    }    
    // If a URL was specified and either mode was specified as auto or no mode was specified,
    // then set mode to auto
    if (nameValuePairs["url"] && (nameValuePairs["mode"] == "auto" || !nameValuePairs["mode"])) {
      nameValuePairs["mode"] = "auto";
      proxyElem.setAttribute("mode", "auto");
    }
    // Change the mode to "direct" if we don't have enough info for "manual" and "auto"
    var noManual = (nameValuePairs["host"] && !nameValuePairs["port"]) ||
      (!nameValuePairs["host"] && nameValuePairs["port"]);
    if (!nameValuePairs["url"] && noManual) {
      nameValuePairs["mode"] = "direct";      
      proxyElem.setAttribute("mode", "direct");
      dump("No host and port specified, and no PAC URL specified; setting proxy mode to direct.\n");
    }

    // Set a default name if one wasn't specified. Don't set the default name based
    // on the mode because proxies like these will end up with different names:
    // proxy:host=my.proxy.server.com&port=999
    // proxy:host=my.proxy.server.com&port=999&mode=direct
    if (!nameValuePairs["name"]) {
      if (noManual)
        nameValuePairs["name"] = nameValuePairs["url"] ? nameValuePairs["url"] : this.fp.getMessage("new.proxy");
      else
        nameValuePairs["name"] = nameValuePairs["host"] + ":" + nameValuePairs["port"];
      proxyElem.setAttribute("name", nameValuePairs["name"]);
    }
    this.fromDOM(proxyElem, true);
  },  

  set autoconfMode(e) {
    this._autoconfMode = e;
  },

  get autoconfMode() {
    return this._autoconfMode;
  },

  set proxyDNS(e) {
    this._proxyDNS = e;
    this.manualconf._makeProxy();
  },

  get proxyDNS() {return this._proxyDNS;},  
  
  /**
   * Create a variable that represents the color but as all letters.
   * This is because the CSS style treechildren::-moz-tree-cell(x) requires
   * x to be all letters (no numbers or symbols)
   */
  set color(n) {
    this._color = n;
    var str = new String(n); /* ensure it's a String type, not a Number type */
    str = str.toLowerCase();
    var temp = ["g", "h", "i", "j", "k", "m", "n", "o", "p", "q"];
    temp["a"] = "a";
    temp["b"] = "b";
    temp["c"] = "c";
    temp["d"] = "d";
    temp["e"] = "e";
    temp["f"] = "f";
    temp["#"] = ""; 
    this.colorString = "";
    for (var i=0, len = str.length; i<len; i++)
      this.colorString += temp[str[i]];
  },
  
  get color() {
    return this._color;
  },

  set enabled(e) {
    if (this.lastresort && !e) return; // can't ever disable this guy
    this._enabled = e;
    if (this.shouldLoadPAC()) {
      this.preparePACLoading();
    } 
  },

  get enabled() {return this._enabled;},

  shouldLoadPAC : function() {
    let pacURI; 
    if (this._mode === "system") {
      // We need the try-catch block here as on Mac OS X an exception is thrown
      // if no PACURI is available. On Unix an empty string is given back.
      try {
        pacURI = this.sysProxyService.PACURI;
      } catch (e) {
        pacURI = null;
      }
    }
         
    if ((this._mode == "auto" || (this._mode == "system" &&
         this.sysProxyService && pacURI)) && this._enabled) {
      var m = this.fp.mode;
      return m == this.id || m == "patterns" || m == "random" ||
        m == "roundrobin";
    }
  },

  set mode(m) {
    this._mode = m;
    if (this.shouldLoadPAC()) {
      this.preparePACLoading();
    }
  },

  preparePACLoading: function() {
    if (this._mode === "auto") {
      if (this._autoconfMode === "pac") {
        this.autoconf.loadPAC();
      } else if (this._autoconfMode === "wpad") {
        this.wpad.loadPAC();
      }
      this.handleTimer();
    } else if (this._mode === "system") {
      if (this.systemProxyPAC === null) {
        this.systemProxyPAC = new AutoConf(this, this.fp);
      }
      this.systemProxyPAC.url = this.sysProxyService.PACURI;
      this.systemProxyPAC.loadPAC();
    }
  },

  afterPropertiesSet : function() {
    // Some integrity maintenance: if this is a manual proxy and
    // this.manualconf.proxy wasn't created during deserialization, disable us.
    if (this._enabled && this._mode == "manual" && !this.manualconf.proxy) {
      if (this.lastresort) {
        // Switch lastresort to DIRECT since manualconf is corrupt--someone
        // changed foxyproxy.xml manually, outside our GUI
        this._mode = "direct";
      } else {
        this._enabled = false;
      }
      !this._enabled &&
        // (proxy, isBeingDeleted, isBeingDisabled, isBecomingDIRECT)  
        this.fp.proxies.maintainIntegrity(this, false, true, false); 
    }
  },

  handleTimer : function() {
    let ac;
    if (this._autoconfMode === "pac") {
      ac = this.autoconf;
    } else if (this._autoconfMode === "wpad") {
      ac = this.wpad;
    } 
    // always always always cancel first before doing anything 
    if (ac) {
      ac.timer.cancel();
    } else {
      // We should never reach this code path.
      return;
    }
    if (ac._autoReload) {
      ac.timer.initWithCallback(ac, ac._reloadFreqMins*60000,
        CI.nsITimer.TYPE_REPEATING_SLACK);
    }
  },

  get mode() {return this._mode;},

  /**
   * Check if any white patterns already match uriStr. As a shortcut,
   * we first check if the existing white patterns (as strings) equal |patStr|
   * before performing regular expression matches.
   *
   * Black pattern matches take precendence over white pattern matches.
   * 
   * Note patStr is sometimes null when this method is called.
   */
  isWhiteMatch : function(patStr, uriStr) {
    var white = -1;
    for (var i=0,sz=this.matches.length; i<sz; i++) {
      var m = this.matches[i];
      if (m.enabled) {
        if ((patStr && m.pattern == patStr) || m.regex.test(uriStr)) {
          if (m.isBlackList) {
            // Black takes priority over white
            return false;
          }
          else if (white == -1) {
            white = i; // continue checking for blacklist matches!
          }
        }
      }
    }
    return white == -1 ? false : this.matches[white];
  },

  isBlackMatch : function(patStr, uriStr) {
    for (var i=0,sz=this.matches.length; i<sz; i++) {
      var m = this.matches[i];
      if (m.enabled && m.isBlackList && (m.pattern == patStr || m.regex.test(uriStr)))
        return m;
    }
  },
  
  removeURLPattern : function(removeMe) {
    this.matches = this.matches.filter(function(e) {return e != removeMe;});
  },
  
  resolve : function(spec, host, mp, mode) {
    function _notifyUserOfError(spec) {
      /*this.autoconf.errorNotification &&*/
      this.fp.notifier.alert(this.fp.getMessage("foxyproxy"),
        this.fp.getMessage("proxy.error.for.url", [spec]));
      return null;
    }
    // See http://wp.netscape.com/eng/mozilla/2.0/relnotes/demo/proxy-live.html
    if (mode === "wpad") {
      var str = mp.pacResult = this.wpad._resolver.getProxyForURI(spec, host);
    } else if (mode === "pac") {
      var str = mp.pacResult = this.autoconf._resolver.getProxyForURI(spec,
        host);
    } else {
      // we have the system proxy settings here
      var str = mp.pacResult = this.systemProxyPAC._resolver.
        getProxyForURI(spec, host); 
    }
    if (str && str != "") {
      str = str.toLowerCase();
      var tokens = str.split(/\s*;\s*/), // Trim and split
      proxies = [];
      // In case final token ends with semi-colon 
      if (tokens[tokens.length-1] == "")
        tokens.length--;
      for (var i=0; i<tokens.length; i++) {
        if (mode === "wpad") {
          var components = this.wpad.parser.exec(tokens[i]);
        } else if (mode === "pac") {
          var components = this.autoconf.parser.exec(tokens[i]); 
        } else {
          var components = this.systemProxyPAC.parser.exec(tokens[i]);
        }
        if (!components) continue;
        var tmp = this._proxyDNS && components[1].indexOf("socks") === 0 ?
          CI.nsIProxyInfo.TRANSPARENT_PROXY_RESOLVES_HOST : 0;
        switch (components[1]) {
          case "proxy":
            proxies.push(proxyService.newProxyInfo("http", components[2],
              components[3], tmp, 0, null));
            break;
          case "socks":
          case "socks5":
            proxies.push(proxyService.newProxyInfo("socks", components[2],
              components[3], tmp, 0, null));
            break;
          case "socks4":
            proxies.push(proxyService.newProxyInfo("socks4", components[2],
              components[3], tmp, 0, null));
            break;
          case "direct":
            proxies.push(this.direct);
           break;
          default:
            return _notifyUserOfError(spec);
        }
      }
      // Build a proxy list for proxy for failover support
      for (var i=1; i<=proxies.length-1; i++) {
        proxies[i-1].failoverTimeout = 1800;
        proxies[i-1].failoverProxy = proxies[i];
      }
      if (proxies[0] == null) {
        return _notifyUserOfError(spec);
      }
      else if (proxies[1]) {
        proxies[0].failoverTimeout = 1800;
        proxies[0].failoverProxy = proxies[1];
      }
      return proxies[0];
    } else {
      // Resolver did not find a proxy, but this isn't an error condition
      return null;
    }
  },

  createProxyInfo: function(aProxyString) {
    let proxyInfo = aProxyString.slice(6).split(":");
    if (aProxyString.indexOf("PROXY") === 0) {
      return proxyService.newProxyInfo("http", proxyInfo[0], proxyInfo[1], 0,
        0, null);
    } else if (aProxyString.indexOf("SOCKS") === 0) {
      let remoteResolve;
      if (this._proxyDNS) {
        remoteResolve = CI.nsIProxyInfo.TRANSPARENT_PROXY_RESOLVES_HOST;
      } else {
        remoteResolve = 0;
      }
      return proxyService.newProxyInfo("socks", proxyInfo[0], proxyInfo[1],
        remoteResolve, 0, null); 
    } else {
      dump("Unknown proxy type!\n");
      return this.direct; 
    }
  },

  getSystemProxy: function(spec, host, mp) {
    // If system proxy settings are not supported on the system we give
    // "direct" back as Mozilla does.
    if (!this.sysProxyService) {
      return this.direct;
    } else {
      // We need the try-catch block here as on Mac OS X an exception is thrown
      // if no PACURI is available. On Unix an empty string is given back.
      let pacURI;
      try {
        pacURI = this.sysProxyService.PACURI;
      } catch (e) {
        pacURI = null;
      }
      if (pacURI) {
        // The user wants to use a PAC file. Let's check what we have to do.
        if (!this.systemProxyPAC.url || this.systemProxyPAC.url != pacURI) {
          // This case means the user ether changed the system proxy settings
          // from direct or manual proxy to PAC mode for the first time in the
          // session. Or the PAC URI changes meanwhile in the system proxy
          // settings while still having the proxy in use. In both cases we
          // have to reload the PAC.
          this.systemProxyPAC.url = pacURI;
          this.systemProxyPAC.loadPAC();
          return this.resolve(spec, host, mp, "system");
        } else if (this.systemProxyPAC.url == pacURI) {
          // The easiest case: We just take the already loaded PAC
          return this.resolve(spec, host, mp, "system"); 
        }
        return this.direct;
      } else {
        let uri = this.iOService.newURI(spec, null, null);
        let proxyString = this.sysProxyService.getProxyForURI(uri);
        if (proxyString == "DIRECT") {
          return this.direct;
        } else {
          // We have to construct a proxyInfo object out of the manual settings
          // we got back.
          return this.createProxyInfo(proxyString);
        }
      }
    }
  },

  getProxy : function(spec, host, mp) {
    switch (this._mode) {
      case "manual": return this.manualconf.proxy;
      case "auto":
        if (this._autoconfMode === "pac") {
          return this.resolve(spec, host, mp, "pac");
        } else {
          // WPAD
          return this.resolve(spec, host, mp, "wpad");
        }
      // TODO: We should consider using a timer to periodically re-call
      // getSystemProxy(). But the main problem is that we could miss changes in
      // the global blacklist as this one is considered while determining the
      // proxy for every request. That could be especially painful for users in
      // China or other restrictive countries, a thing we may want to avoid.
      case "system": return this.getSystemProxy(spec, host, mp);
      case "direct": return this.direct;
    }
  },
  
  QueryInterface: XPCOMUtils.generateQI([CI.nsISupports]),
  classDescription: "FoxyProxy Proxy Component",
  classID: Components.ID("{51b469a0-edc1-11da-8ad9-0800200c9a66}"),
  contractID: "@leahscape.org/foxyproxy/proxy;1"    
};

/**
 * XPCOMUtils.generateNSGetFactory was introduced in Mozilla 2 (Firefox 4)
 * XPCOMUtils.generateNSGetModule is for Mozilla 1.9.2 and earlier (Firefox 3.6)
 */
if (XPCOMUtils.generateNSGetFactory)
  var NSGetFactory = XPCOMUtils.generateNSGetFactory([Proxy]);
else
  var NSGetModule = XPCOMUtils.generateNSGetModule([Proxy]);

///////////////////////////// ManualConf class ///////////////////////
function ManualConf(owner, fp) {
  this.owner = owner;
  this.fp = fp;
}

ManualConf.prototype = {
  _host: "",
  _port: "",
  _socksversion: "5",
  _isSocks: false,
  fp : null,
  owner: null,

  fromDOM : function(n) {
    this._host = gGetSafeAttr(n, "host", null) || gGetSafeAttr(n, "http", null) ||
      gGetSafeAttr(n, "socks", null) || gGetSafeAttr(n, "ssl", null) ||
      gGetSafeAttr(n, "ftp", null) || gGetSafeAttr(n, "gopher", ""); //"host" is new for 2.5

    this._port = gGetSafeAttr(n, "port", null) || gGetSafeAttr(n, "httpport", null) ||
      gGetSafeAttr(n, "socksport", null) || gGetSafeAttr(n, "sslport", null) ||
      gGetSafeAttr(n, "ftpport", null) || gGetSafeAttr(n, "gopherport", ""); // "port" is new for 2.5

    this._socksversion = gGetSafeAttr(n, "socksversion", "5");

    this._isSocks = n.hasAttribute("isSocks") ? n.getAttribute("isSocks") == "true" :
      n.getAttribute("http") ? false:
      n.getAttribute("ssl") ? false:
      n.getAttribute("ftp") ? false:
      n.getAttribute("gopher") ? false:
      n.getAttribute("socks") ? true : false; // new for 2.5

    this._makeProxy();
  },

  toDOM : function(doc)  {
    var e = doc.createElement("manualconf");
    e.setAttribute("host", this._host);
    e.setAttribute("port", this._port);
    e.setAttribute("socksversion", this._socksversion);
    e.setAttribute("isSocks", this._isSocks); 
    return e;
  },

  _makeProxy : function() {
    if (!this._host || !this._port)
      return;
    this.proxy = this._isSocks ? proxyService.newProxyInfo(this._socksversion == "5"?"socks":"socks4", this._host, this._port,
          this.owner._proxyDNS ? CI.nsIProxyInfo.TRANSPARENT_PROXY_RESOLVES_HOST : 0, 0, null): // never ignore, never failover
          proxyService.newProxyInfo("http", this._host, this._port, 0, 0, null);
  },

  get host() {return this._host;},
  set host(e) {
    this._host = e;
    this._makeProxy();
  },

  get port() {return this._port;},
  set port(e) {
    this._port = e;
    this._makeProxy();
  },

  get isSocks() {return this._isSocks;},
  set isSocks(e) {
    this._isSocks = e;
    this._makeProxy();
  },

  get socksversion() {return this._socksversion;},
  set socksversion(e) {
    this._socksversion = e;
    this._makeProxy();
  }
};