Bug 998372 - part 3 - remove swathes of conditional logic from FindProxyForURL; r=jmaher
authorNathan Froyd <froydnj@mozilla.com>
Fri, 18 Apr 2014 12:28:31 -0400
changeset 179615 cbbf7905937be8859e933efbef16394888606bde
parent 179614 43c5d06184bba1ac42cd0e7ae5c69d30a34a7bd4
child 179616 3c39d43169c04459a2342d45b5a9c88c668d277c
push id26632
push userkwierso@gmail.com
push dateWed, 23 Apr 2014 00:54:42 +0000
treeherdermozilla-central@02515cf4fcfd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjmaher
bugs998372
milestone31.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 998372 - part 3 - remove swathes of conditional logic from FindProxyForURL; r=jmaher
testing/mozbase/mozprofile/mozprofile/permissions.py
testing/mozbase/mozprofile/tests/permissions.py
--- a/testing/mozbase/mozprofile/mozprofile/permissions.py
+++ b/testing/mozbase/mozprofile/mozprofile/permissions.py
@@ -311,44 +311,50 @@ class Permissions(object):
 var knownOrigins = (function () {
   return [%(origins)s].reduce(function(t, h) { t[h] = true; return t; }, {})
 })();
 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())
 
         prefs = []
         prefs.append(("network.proxy.type", 2))
         prefs.append(("network.proxy.autoconfig_url", pacURL))
 
         return prefs
--- a/testing/mozbase/mozprofile/tests/permissions.py
+++ b/testing/mozbase/mozprofile/tests/permissions.py
@@ -121,20 +121,20 @@ http://127.0.0.1:8888           privileg
         prefs, user_prefs = perms.network_prefs(True)
         self.assertEqual(len(user_prefs), 2)
         self.assertEqual(user_prefs[0], ('network.proxy.type', 2))
         self.assertEqual(user_prefs[1][0], 'network.proxy.autoconfig_url')
 
         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):
         """Verifies that we call INSERT statements using the correct number
         of columns for existing databases.
         """
         self.write_perm_db(version=version)
         Permissions(self.profile_dir, self.locations_file.name)