46
48
def __init__(self, base, from_transport=None):
47
49
"""Set the base path where files will be stored."""
48
super(HttpTransport_urllib, self).__init__(base, from_transport)
49
50
if from_transport is not None:
51
super(HttpTransport_urllib, self).__init__(base, from_transport)
50
52
self._connection = from_transport._connection
53
self._auth = from_transport._auth
51
54
self._user = from_transport._user
52
55
self._password = from_transport._password
53
56
self._opener = from_transport._opener
58
# urllib2 will be confused if it find
59
# authentification infos in the urls. So we handle
60
# them separatly. Note that we don't need to do that
61
# when cloning (as above) since the cloned base is
63
clean_base, user, password = self._extract_auth(base)
64
super(HttpTransport_urllib, self).__init__(clean_base,
55
66
self._connection = None
67
self._auth = None # We have to wait the 401 to know
69
self._password = password
58
70
self._opener = self._opener_class()
71
if password is not None: # '' is a valid password
72
# Make the (user, password) available to urllib2
73
pm = self._opener.password_manager
74
pm.add_password(None, self.base, self._user, self._password)
60
def ask_password(self, request):
61
"""Ask for a password if none is already provided in the request"""
76
def _ask_password(self):
77
"""Ask for a password if none is already available"""
62
78
# TODO: jam 20060915 There should be a test that asserts we ask
63
79
# for a password at the right time.
64
host = request.get_host()
65
password_manager = self._opener.password_manager
66
if request.password is None:
80
if self._password is None:
67
81
# We can't predict realm, let's try None, we'll get a
68
82
# 401 if we are wrong anyway
70
84
# Query the password manager first
71
user, password = password_manager.find_user_password(None, host)
72
if user == request.user and password is not None:
73
request.password = password
86
pm = self._opener.password_manager
87
user, password = pm.find_user_password(None, authuri)
88
if user == self._user and password is not None:
89
self._password = password
75
91
# Ask the user if we MUST
76
92
http_pass = 'HTTP %(user)s@%(host)s password'
77
request.password = ui.ui_factory.get_password(prompt=http_pass,
80
password_manager.add_password(None, host,
81
request.user, request.password)
93
self._password = ui.ui_factory.get_password(prompt=http_pass,
96
pm.add_password(None, authuri, self._user, self._password)
98
def _extract_auth(self, url):
99
"""Extracts authentification information from url.
101
Get user and password from url of the form: http://user:pass@host/path
102
:returns: (clean_url, user, password)
104
scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
107
auth, netloc = netloc.split('@', 1)
109
user, password = auth.split(':', 1)
111
user, password = auth, None
112
user = urllib.unquote(user)
113
if password is not None:
114
password = urllib.unquote(password)
119
url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
121
return url, user, password
83
123
def _perform(self, request):
84
124
"""Send the request to the server and handles common errors.