[Python-checkins] r53203 - in sandbox/branches/setuptools-0.6: EasyInstall.txt setuptools/package_index.py

phillip.eby python-checkins at python.org
Sat Dec 30 17:05:41 CET 2006


Author: phillip.eby
Date: Sat Dec 30 17:05:41 2006
New Revision: 53203

Modified:
   sandbox/branches/setuptools-0.6/EasyInstall.txt
   sandbox/branches/setuptools-0.6/setuptools/package_index.py
Log:
Add Basic Auth support for http URLs with embedded credentials.  If an
authenticated page contains links to the same protocol and host, those
links should inherit the same credentials.  (backport from trunk)


Modified: sandbox/branches/setuptools-0.6/EasyInstall.txt
==============================================================================
--- sandbox/branches/setuptools-0.6/EasyInstall.txt	(original)
+++ sandbox/branches/setuptools-0.6/EasyInstall.txt	Sat Dec 30 17:05:41 2006
@@ -1195,6 +1195,11 @@
 ============================
 
 0.6c4
+ * Added support for HTTP "Basic" authentication using ``http://user:pass@host``
+   URLs.  If a password-protected page contains links to the same host (and
+   protocol), those links will inherit the credentials used to access the
+   original page.
+
  * Removed all special support for Sourceforge mirrors, as Sourceforge's
    mirror system now works well for non-browser downloads.
 

Modified: sandbox/branches/setuptools-0.6/setuptools/package_index.py
==============================================================================
--- sandbox/branches/setuptools-0.6/setuptools/package_index.py	(original)
+++ sandbox/branches/setuptools-0.6/setuptools/package_index.py	Sat Dec 30 17:05:41 2006
@@ -576,9 +576,7 @@
         if url.startswith('file:'):
             return local_open(url)
         try:
-            request = urllib2.Request(url)
-            request.add_header('User-Agent', user_agent)
-            return urllib2.urlopen(request)
+            return open_with_auth(url)
         except urllib2.HTTPError, v:
             return v
         except urllib2.URLError, v:
@@ -613,6 +611,8 @@
     def scan_url(self, url):
         self.process_url(url, True)
 
+
+
     def _attempt_download(self, url, filename):
         headers = self._download_to(url, filename)
         if 'html' in headers['content-type'].lower():
@@ -654,6 +654,47 @@
 
 
 
+def open_with_auth(url):
+    """Open a urllib2 request, handling HTTP authentication"""
+
+    scheme, netloc, path, params, query, frag = urlparse.urlparse(url)
+
+    if scheme in ('http', 'https'):
+        auth, host = urllib2.splituser(netloc)
+    else:
+        auth = None
+
+    if auth:
+        auth = "Basic " + urllib2.unquote(auth).encode('base64').strip()
+        new_url = urlparse.urlunparse((scheme,host,path,params,query,frag))
+        request = urllib2.Request(new_url)
+        request.add_header("Authorization", auth)
+    else:
+        request = urllib2.Request(url)
+
+    request.add_header('User-Agent', user_agent)
+    fp = urllib2.urlopen(request)
+
+    if auth:
+        # Put authentication info back into request URL if same host,
+        # so that links found on the page will work
+        s2, h2, path2, param2, query2, frag2 = urlparse.urlparse(fp.url)
+        if s2==scheme and h2==host:
+            fp.url = urlparse.urlunparse((s2,netloc,path2,param2,query2,frag2))
+
+    return fp
+
+
+
+
+
+
+
+
+
+
+
+
 def fix_sf_url(url):
     return url      # backward compatibility
 


More information about the Python-checkins mailing list