~ubuntu-branches/debian/stretch/foxyproxy/stretch

« back to all changes in this revision

Viewing changes to src/components/proxy.js

  • Committer: Bazaar Package Importer
  • Author(s): Yaroslav Halchenko
  • Date: 2010-02-22 08:34:53 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20100222083453-23zgkfv5c7ybcj54
Tags: 2.19.1-1
New upstream release with a security fix on top of 2.19.

Show diffs side-by-side

added added

removed removed

Lines of Context:
88
88
  colorString: "nmbado",
89
89
  _proxyDNS: true,
90
90
  fp: null,
 
91
  readOnlyProperties : ["lastresort", "fp", "wrappedJSObject", "matches", /* from ManualConf */ "owner",
 
92
                        /* from AutoConf */ "timer", /* from AutoConf */  "_resolver"],
91
93
 
92
94
  QueryInterface: function(aIID) {
93
95
    if (!aIID.equals(CI.nsISupports))
155
157
    return e;
156
158
  },
157
159
  
 
160
  /**
 
161
   * Merge |src| into this, using the keys of the |nameValuePairs|
 
162
   * associative array as the properties to overwrite in |this|.
 
163
   */
 
164
  merge : function(src, nameValuePairs) {
 
165
    for (var propertyName in nameValuePairs) {
 
166
      // Simple sanity check on our input
 
167
      var obj = this.propertyBelongsTo(propertyName, this, this.manualconf, this.autoconf);
 
168
      // If obj == null then we don't understand this property, so ignore it.
 
169
      if (obj)
 
170
        obj[propertyName] = nameValuePairs[propertyName];
 
171
    }
 
172
  },
 
173
  
 
174
  getPropertyValue : function(propertyName) {
 
175
    var obj = this.propertyBelongsTo(propertyName, this, this.manualconf, this.autoconf);
 
176
    return obj ? obj[propertyName] : "";
 
177
  },
 
178
  
 
179
  /**
 
180
   * Checks if |propertyName| is a known writable property of the classes Proxy,
 
181
   * ManualConf, or AutoConf (excepting readOnly properties). |x|, |y|, or |z|
 
182
   * is returned, respectively, based on |propertyName's| membership in one of those
 
183
   * classes, If the property is unknown or read-only, null is returned.
 
184
   */
 
185
  propertyBelongsTo : function(propertyName, x, y, z) {
 
186
    function validType(str) {
 
187
      return str == "string" || str == "boolean" || str == "number";
 
188
    }    
 
189
    if (this.readOnlyProperties.indexOf(propertyName) > -1) return null;
 
190
    
 
191
    if (validType(typeof(this[propertyName])))
 
192
      return x;
 
193
    else if (validType(typeof(this.manualconf[propertyName])))
 
194
      return y;
 
195
    else if (validType(typeof(this.autoconf[propertyName])))
 
196
      return z;
 
197
    return null;    
 
198
  },
 
199
  
 
200
  /**
 
201
   * Use as a static-style method on this class.
 
202
   * Returns a |Proxy| instance based on the |nameValuePairs|
 
203
   * associative array. Each key in the array is expected to be
 
204
   * a property of either Proxy, ManualConf, or AutoConf, otherwise
 
205
   * it is ignored.
 
206
   */
 
207
  fromAssociateArray : function(nameValuePairs) {
 
208
    var doc = CC["@mozilla.org/xml/xml-document;1"].createInstance(CI.nsIDOMDocument),
 
209
      proxyElem = doc.createElement("proxy"),
 
210
      manualConfElem = doc.createElement("manualconf"),
 
211
      autoConfElem = doc.createElement("autoconf");
 
212
    
 
213
    proxyElem.appendChild(manualConfElem);
 
214
    proxyElem.appendChild(autoConfElem);
 
215
    for (var i in nameValuePairs) {
 
216
      // Simple sanity check on our input
 
217
      var elem = this.propertyBelongsTo(i, proxyElem, manualConfElem, autoConfElem);
 
218
      /* If elem == null then we don't understand this property, so ignore it.
 
219
         However, even if an unrecognized property were to slip by us here,
 
220
         the |Proxy.fromDOM()| code would ignore it anyway. This check just prevents us from
 
221
         building an arbitrarily large DOM element.
 
222
      */
 
223
      if (elem)
 
224
        elem.setAttribute(i, nameValuePairs[i] == null ? "" : nameValuePairs[i]);         
 
225
    }
 
226
    // Turn it on by default
 
227
    if (!nameValuePairs["enabled"])
 
228
      proxyElem.setAttribute("enabled", "true");
 
229
    
 
230
    // If a socks version was specified and either isSocks is true or no isSocks parameter was specified,
 
231
    // then enable socks
 
232
    if (nameValuePairs["socksversion"] && (nameValuePairs["isSocks"] == "true" || !nameValuePairs["isSocks"])) {
 
233
      nameValuePairs["isSocks"] = true;
 
234
      manualConfElem.setAttribute("isSocks", "true");
 
235
      manualConfElem.setAttribute("socksversion", parseInt(nameValuePairs["socksversion"]));
 
236
    }    
 
237
    // If a URL was specified and either mode was specified as auto or no mode was specified,
 
238
    // then set mode to auto
 
239
    if (nameValuePairs["url"] && (nameValuePairs["mode"] == "auto" || !nameValuePairs["mode"])) {
 
240
      nameValuePairs["mode"] = "auto";
 
241
      proxyElem.setAttribute("mode", "auto");
 
242
    }
 
243
    // Change the mode to "direct" if we don't have enough info for "manual" and "auto"
 
244
    var noManual = (nameValuePairs["host"] && !nameValuePairs["port"]) ||
 
245
      (!nameValuePairs["host"] && nameValuePairs["port"]);
 
246
    if (!nameValuePairs["url"] && noManual) {
 
247
      nameValuePairs["mode"] = "direct";      
 
248
      proxyElem.setAttribute("mode", "direct");
 
249
      dump("No host and port specified, and no PAC URL specified; setting proxy mode to direct.\n");
 
250
    }
 
251
 
 
252
    // Set a default name if one wasn't specified. Don't set the default name based
 
253
    // on the mode because proxies like these will end up with different names:
 
254
    // proxy:host=my.proxy.server.com&port=999
 
255
    // proxy:host=my.proxy.server.com&port=999&mode=direct
 
256
    if (!nameValuePairs["name"]) {
 
257
      if (noManual)
 
258
        nameValuePairs["name"] = nameValuePairs["url"] ? nameValuePairs["url"] : this.fp.getMessage("new.proxy");
 
259
      else
 
260
        nameValuePairs["name"] = nameValuePairs["host"] + ":" + nameValuePairs["port"];
 
261
      proxyElem.setAttribute("name", nameValuePairs["name"]);
 
262
    }
 
263
    this.fromDOM(proxyElem, true);
 
264
  },  
 
265
  
158
266
  set proxyDNS(e) {
159
267
    this._proxyDNS = e;
160
268
    this.manualconf._makeProxy();