Add UpgradeToHttps() to reliably and forcibly upgrade all urls to https.

Enable it for git-cl and gcl.

R=nsylvain@chromium.org
BUG=107838
TEST=New connections go through https://


Review URL: http://codereview.chromium.org/9214004

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@117857 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/gclient_utils.py b/gclient_utils.py
index e5784ac..35f8b3b 100644
--- a/gclient_utils.py
+++ b/gclient_utils.py
@@ -14,6 +14,7 @@
 import tempfile
 import threading
 import time
+import urlparse
 
 import subprocess2
 
@@ -734,6 +735,30 @@
     os.remove(filename)
 
 
+def UpgradeToHttps(url):
+  """Upgrades random urls to https://.
+
+  Do not touch unknown urls like ssh:// or git://.
+  Do not touch http:// urls with a port number,
+  Fixes invalid GAE url.
+  """
+  if not url:
+    return url
+  if not re.match(r'[a-z\-]+\://.*', url):
+    # Make sure it is a valid uri. Otherwise, urlparse() will consider it a
+    # relative url and will use http:///foo. Note that it defaults to http://
+    # for compatibility with naked url like "localhost:8080".
+    url = 'http://%s' % url
+  parsed = list(urlparse.urlparse(url))
+  # Do not automatically upgrade http to https if a port number is provided.
+  if parsed[0] == 'http' and not re.match(r'^.+?\:\d+$', parsed[1]):
+    parsed[0] = 'https'
+  # Until GAE supports SNI, manually convert the url.
+  if parsed[1] == 'codereview.chromium.org':
+    parsed[1] = 'chromiumcodereview.appspot.com'
+  return urlparse.urlunparse(parsed)
+
+
 def ParseCodereviewSettingsContent(content):
   """Process a codereview.settings file properly."""
   lines = (l for l in content.splitlines() if not l.strip().startswith("#"))
@@ -742,5 +767,9 @@
   except ValueError:
     raise Error(
         'Failed to process settings, please fix. Content:\n\n%s' % content)
-  # TODO(maruel): Post-process
+  def fix_url(key):
+    if keyvals.get(key):
+      keyvals[key] = UpgradeToHttps(keyvals[key])
+  fix_url('CODE_REVIEW_SERVER')
+  fix_url('VIEW_VC')
   return keyvals