Commit 5e2fb1dc authored by PJ Eby's avatar PJ Eby

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
parent 981bc8e3
......@@ -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.
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment