From 5e2fb1dcd218f4dab91a0beb3c5ce5fe7b89dd81 Mon Sep 17 00:00:00 2001 From: PJ Eby Date: Sat, 30 Dec 2006 16:05:41 +0000 Subject: 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 --- setuptools/package_index.py | 47 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'setuptools/package_index.py') 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 -- cgit v1.2.3