Commit bbe00513 authored by Jason R. Coombs's avatar Jason R. Coombs

Fix issue #262 - package_index.open_with_auth no longer throws LookupError on Python 3.

--HG--
branch : distribute
extra : rebase_source : ae4a0886ff89d3679f495f51171f94d3770e5d47
parent 70177237
...@@ -9,6 +9,8 @@ CHANGES ...@@ -9,6 +9,8 @@ CHANGES
* Issue #258: Workaround a cache issue * Issue #258: Workaround a cache issue
* Issue #260: distribute_setup.py now accepts the --user parameter for * Issue #260: distribute_setup.py now accepts the --user parameter for
Python 2.6 and later. Python 2.6 and later.
* Issue #262: package_index.open_with_auth no longer throws LookupError
on Python 3.
------ ------
0.6.24 0.6.24
......
"""PyPI and direct package downloading""" """PyPI and direct package downloading"""
import sys, os.path, re, urlparse, urllib, urllib2, shutil, random, socket, cStringIO import sys, os.path, re, urlparse, urllib, urllib2, shutil, random, socket, cStringIO
import base64
import httplib import httplib
from pkg_resources import * from pkg_resources import *
from distutils import log from distutils import log
...@@ -756,6 +757,22 @@ def socket_timeout(timeout=15): ...@@ -756,6 +757,22 @@ def socket_timeout(timeout=15):
return _socket_timeout return _socket_timeout
return _socket_timeout return _socket_timeout
def _encode_auth(auth):
"""
A function compatible with Python 2.3-3.3 that will encode
auth from a URL suitable for an HTTP header.
>>> _encode_auth('username%3Apassword')
u'dXNlcm5hbWU6cGFzc3dvcmQ='
"""
auth_s = urllib2.unquote(auth)
# convert to bytes
auth_bytes = auth_s.encode()
# use the legacy interface for Python 2.3 support
encoded_bytes = base64.encodestring(auth_bytes)
# convert back to a string
encoded = encoded_bytes.decode()
# strip the trailing carriage return
return encoded.rstrip()
def open_with_auth(url): def open_with_auth(url):
"""Open a urllib2 request, handling HTTP authentication""" """Open a urllib2 request, handling HTTP authentication"""
...@@ -768,7 +785,7 @@ def open_with_auth(url): ...@@ -768,7 +785,7 @@ def open_with_auth(url):
auth = None auth = None
if auth: if auth:
auth = "Basic " + urllib2.unquote(auth).encode('base64').strip() auth = "Basic " + _encode_auth(auth)
new_url = urlparse.urlunparse((scheme,host,path,params,query,frag)) new_url = urlparse.urlunparse((scheme,host,path,params,query,frag))
request = urllib2.Request(new_url) request = urllib2.Request(new_url)
request.add_header("Authorization", auth) request.add_header("Authorization", auth)
......
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