TransRead: support URLs with user name and password
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Fri, 8 Mar 2013 08:35:52 +0000 (10:35 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Fri, 8 Mar 2013 12:57:04 +0000 (14:57 +0200)
Markus Lehtonen reported that bmaptool does not support URLs which contain user
name and password, e.g., https://marquiz:qwerty@server.com/nice.image.bz2.

This patch adds the corresponding support. What we do is we first parse the URL
and try to figure out if it contains user name and password, and if it does,
open the URL with a specially build opener which supports authentication.

Change-Id: I517eb9ffd21ff5de6e9deac4c0cacffe82e53ad5
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptools/TransRead.py

index eb16efe..834f6c1 100644 (file)
@@ -202,11 +202,39 @@ class TransRead:
 
         import urllib2
         import httplib
+        import urlparse
+        import re
+
+        # Unfortunately, in order to handle URLs which contain user name and
+        # password (e.g., http://user:password@my.site.org), we need to do
+        # things a bit differently. The following code tries to find out if
+        # the URL contains user name and password.
+
+        parsed_url = urlparse.urlparse(url)
+        username = parsed_url.username
+        password = parsed_url.password
+
+        if username and password:
+            # Construct a new URL without user name and password
+            new_url = list(parsed_url)
+            if parsed_url.port:
+                new_url[1] = "%s:%s" % (parsed_url.hostname, parsed_url.port)
+            else:
+                new_url[1] = parsed_url.hostname
+            url = urlparse.urlunparse(new_url)
+
+            # Build an URL opener which will do the authentication
+            password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
+            password_manager.add_password(None, url, username, password)
+            auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
+            opener = urllib2.build_opener(auth_handler)
+        else:
+            opener = urllib2.build_opener()
+
+        opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
+        urllib2.install_opener(opener)
 
         try:
-            opener = urllib2.build_opener()
-            opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
-            urllib2.install_opener(opener)
             self._file_obj = opener.open(url)
             self.is_url = True
         except (IOError, ValueError, httplib.InvalidURL) as err: