diff options
author | Patrick Brunschwig <[email protected]> | 2021-06-12 11:01:51 +0200 |
---|---|---|
committer | Patrick Brunschwig <[email protected]> | 2021-06-13 08:45:19 +0200 |
commit | c67cdbc511f0727321c7d60b76b28e76b288680c (patch) | |
tree | 25723cb809562fc9f85c90fd6797cdec54f346d8 | |
parent | b590bc6cdf8283d246bb3c8f7fd08c0b64a4df3d (diff) | |
download | enigmail-c67cdbc511f0727321c7d60b76b28e76b288680c.tar.gz enigmail-c67cdbc511f0727321c7d60b76b28e76b288680c.tar.bz2 enigmail-c67cdbc511f0727321c7d60b76b28e76b288680c.zip |
fixed reading proxy settings and added test case
-rw-r--r-- | package/httpProxy.jsm | 54 | ||||
-rw-r--r-- | package/tests/httpProxy-test.js | 48 | ||||
-rw-r--r-- | package/tests/main.js | 1 |
3 files changed, 58 insertions, 45 deletions
diff --git a/package/httpProxy.jsm b/package/httpProxy.jsm index d791bbe7..6ab5af5e 100644 --- a/package/httpProxy.jsm +++ b/package/httpProxy.jsm @@ -8,37 +8,8 @@ var EXPORTED_SYMBOLS = ["EnigmailHttpProxy"]; - - - - const EnigmailPrefs = ChromeUtils.import("chrome://enigmail/content/modules/prefs.jsm").EnigmailPrefs; -const NS_PREFS_SERVICE_CID = "@mozilla.org/preferences-service;1"; - -function getPasswdForHost(hostname, userObj, passwdObj) { - var loginmgr = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager); - - // search HTTP password 1st - var logins = loginmgr.findLogins({}, "http://" + hostname, "", ""); - if (logins.length > 0) { - userObj.value = logins[0].username; - passwdObj.value = logins[0].password; - return true; - } - - // look for any other password for same host - logins = loginmgr.getAllLogins({}); - for (var i = 0; i < logins.lenth; i++) { - if (hostname == logins[i].hostname.replace(/^.*:\/\//, "")) { - userObj.value = logins[i].username; - passwdObj.value = logins[i].password; - return true; - } - } - return false; -} - var EnigmailHttpProxy = { /** * get Proxy for a given hostname as configured in Mozilla @@ -49,16 +20,16 @@ var EnigmailHttpProxy = { * null if no proxy required */ getHttpProxy: function(hostName) { - var proxyHost = null; + let proxyHost = null; if (((typeof hostName) !== 'undefined') && EnigmailPrefs.getPref("respectHttpProxy")) { // determine proxy host - var prefsSvc = Cc[NS_PREFS_SERVICE_CID].getService(Ci.nsIPrefService); - var prefRoot = prefsSvc.getBranch(null); - var useProxy = prefRoot.getIntPref("network.proxy.type"); - if (useProxy == 1) { - var proxyHostName = prefRoot.getCharPref("network.proxy.http"); - var proxyHostPort = prefRoot.getIntPref("network.proxy.http_port"); - var noProxy = prefRoot.getCharPref("network.proxy.no_proxies_on").split(/[ ,]/); + let prefsSvc = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService); + let prefRoot = prefsSvc.getBranch(null); + let useProxy = prefRoot.getIntPref("network.proxy.type"); + if (useProxy === 1) { + let proxyHostName = prefRoot.getCharPref("network.proxy.http"); + let proxyHostPort = prefRoot.getIntPref("network.proxy.http_port"); + let noProxy = prefRoot.getCharPref("network.proxy.no_proxies_on").split(/[ ,]/); for (let host of noProxy) { // Replace regex chars, except star. @@ -66,19 +37,12 @@ var EnigmailHttpProxy = { // Make star match anything. host = host.replace(/\*/g, ".*"); let proxySearch = new RegExp(host + "$", "i"); - if (host && hostName.test(proxySearch)) { + if (host && proxySearch.test(hostName)) { proxyHostName = null; break; } } - if (proxyHostName) { - var userObj = {}; - var passwdObj = {}; - if (getPasswdForHost(proxyHostName, userObj, passwdObj)) { - proxyHostName = userObj.value + ":" + passwdObj.value + "@" + proxyHostName; - } - } if (proxyHostName && proxyHostPort) { proxyHost = "http://" + proxyHostName + ":" + proxyHostPort; } diff --git a/package/tests/httpProxy-test.js b/package/tests/httpProxy-test.js new file mode 100644 index 00000000..eee9061d --- /dev/null +++ b/package/tests/httpProxy-test.js @@ -0,0 +1,48 @@ +/*global do_load_module: false, do_get_file: false, do_get_cwd: false, testing: false, test: false, Assert: false, resetting: false */ +/*global do_test_pending: false, do_test_finished: false */ + +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +"use strict"; + +do_load_module("file://" + do_get_cwd().path + "/testHelper.js"); + +testing("httpProxy.jsm"); /*global EnigmailHttpProxy: false */ + + +function resetProxy() { + const prefRoot = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch(null); + + prefRoot.setIntPref("network.proxy.type", 0); + prefRoot.setCharPref("network.proxy.http", ""); + prefRoot.setIntPref("network.proxy.http_port", null); + prefRoot.setCharPref("network.proxy.no_proxies_on", ""); +} + +test(function proxyTest() { + const prefRoot = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch(null); + + resetProxy(); + let px = EnigmailHttpProxy.getHttpProxy("somewhere"); + Assert.equal(px, null); + + prefRoot.setIntPref("network.proxy.type", 1); + prefRoot.setCharPref("network.proxy.http", "enigmail-proxy.host"); + prefRoot.setIntPref("network.proxy.http_port", 1234); + prefRoot.setCharPref("network.proxy.no_proxies_on", "noproxy.host"); + + px = EnigmailHttpProxy.getHttpProxy("somewhere"); + Assert.equal(px, "http://enigmail-proxy.host:1234"); + + px = EnigmailHttpProxy.getHttpProxy("https://noproxy.host"); + Assert.equal(px, null); + + px = EnigmailHttpProxy.getHttpProxy("sub.noproxy.host"); + Assert.equal(px, null); + + resetProxy(); +}); diff --git a/package/tests/main.js b/package/tests/main.js index 66c1e440..e40eabf1 100644 --- a/package/tests/main.js +++ b/package/tests/main.js @@ -48,6 +48,7 @@ execTest("keyserver-test.js"); execTest("keyserverUris-test.js"); execTest("locale-test.js"); execTest("log-test.js"); +execTest("httpProxy-test.js"); execTest("mime-test.js"); execTest("os-test.js"); execTest("prefs-test.js"); |