summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPJ Eby <distutils-sig@python.org>2006-12-30 16:05:41 +0000
committerPJ Eby <distutils-sig@python.org>2006-12-30 16:05:41 +0000
commit5e2fb1dcd218f4dab91a0beb3c5ce5fe7b89dd81 (patch)
tree1797273ece4fd1ed3e87bd1f17439a5b8b62e59e
parent981bc8e3da37dda70f06d59b81b4dc694f1c48df (diff)
downloadpython-setuptools-git-5e2fb1dcd218f4dab91a0beb3c5ce5fe7b89dd81.tar.gz
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) --HG-- branch : setuptools-0.6 extra : convert_revision : svn%3A6015fed2-1504-0410-9fe1-9d1591cc4771/sandbox/branches/setuptools-0.6%4053203
-rwxr-xr-xEasyInstall.txt5
-rwxr-xr-xsetuptools/package_index.py47
2 files changed, 49 insertions, 3 deletions
diff --git a/EasyInstall.txt b/EasyInstall.txt
index ada765fb..2cd7a7cd 100755
--- a/EasyInstall.txt
+++ b/EasyInstall.txt
@@ -1195,6 +1195,11 @@ Release Notes/Change History
============================
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.
diff --git a/setuptools/package_index.py b/setuptools/package_index.py
index 7e290d0c..f6bc45f5 100755
--- a/setuptools/package_index.py
+++ b/setuptools/package_index.py
@@ -576,9 +576,7 @@ class PackageIndex(Environment):
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 @@ class PackageIndex(Environment):
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 @@ class PackageIndex(Environment):
+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