Bug 998372 - part 3 - remove swathes of conditional logic from FindProxyForURL; r=jmaher

This commit is contained in:
Nathan Froyd 2014-04-18 12:28:31 -04:00
parent 0031dec99d
commit b9371fb908
2 changed files with 28 additions and 22 deletions

View File

@ -316,34 +316,40 @@ var uriRegex = new RegExp('^([a-z][-a-z0-9+.]*)' +
'(?:[^/@]*@)?' +
'(.*?)' +
'(?::(\\\\d+))?/');
var defaultPortsForScheme = {
'http': 80,
'ws': 80,
'https': 443,
'wss': 443
};
var originSchemesRemap = {
'ws': 'http',
'wss': 'https'
};
var proxyForScheme = {
'http': 'PROXY %(remote)s:%(http)s',
'https': 'PROXY %(remote)s:%(https)s',
'ws': 'PROXY %(remote)s:%(ws)s',
'wss': 'PROXY %(remote)s:%(wss)s'
};
function FindProxyForURL(url, host)
{
var matches = uriRegex.exec(url);
if (!matches)
return 'DIRECT';
var isHttp = matches[1] == 'http';
var isHttps = matches[1] == 'https';
var isWebSocket = matches[1] == 'ws';
var isWebSocketSSL = matches[1] == 'wss';
if (!matches[3])
{
if (isHttp | isWebSocket) matches[3] = '80';
if (isHttps | isWebSocketSSL) matches[3] = '443';
var originalScheme = matches[1];
var host = matches[2];
var port = matches[3];
if (!port && originalScheme in defaultPortsForScheme) {
port = defaultPortsForScheme[originalScheme];
}
if (isWebSocket)
matches[1] = 'http';
if (isWebSocketSSL)
matches[1] = 'https';
var schemeForOriginChecking = originSchemesRemap[originalScheme] || originalScheme;
var origin = matches[1] + '://' + matches[2] + ':' + matches[3];
var origin = schemeForOriginChecking + '://' + host + ':' + port;
if (!(origin in knownOrigins))
return 'DIRECT';
if (isHttp) return 'PROXY %(remote)s:%(http)s';
if (isHttps) return 'PROXY %(remote)s:%(https)s';
if (isWebSocket) return 'PROXY %(remote)s:%(ws)s';
if (isWebSocketSSL) return 'PROXY %(remote)s:%(wss)s';
return 'DIRECT';
return proxyForScheme[originalScheme] || 'DIRECT';
}""" % proxy
pacURL = "".join(pacURL.splitlines())

View File

@ -126,10 +126,10 @@ http://127.0.0.1:8888 privileged
origins_decl = "var knownOrigins = (function () { return ['http://mochi.test:8888', 'http://127.0.0.1:80', 'http://127.0.0.1:8888'].reduce"
self.assertTrue(origins_decl in user_prefs[1][1])
proxy_check = ("if (isHttp) return 'PROXY mochi.test:8888';",
"if (isHttps) return 'PROXY mochi.test:4443';",
"if (isWebSocket) return 'PROXY mochi.test:4443';",
"if (isWebSocketSSL) return 'PROXY mochi.test:4443';")
proxy_check = ("'http': 'PROXY mochi.test:8888'",
"'https': 'PROXY mochi.test:4443'",
"'ws': 'PROXY mochi.test:4443'",
"'wss': 'PROXY mochi.test:4443'")
self.assertTrue(all(c in user_prefs[1][1] for c in proxy_check))
def verify_user_version(self, version):