14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
import urllib, urllib2
20
from StringIO import StringIO
17
from cStringIO import StringIO
22
import bzrlib # for the version
23
from bzrlib.errors import (TransportNotPossible, NoSuchFile, BzrError,
24
TransportError, ConnectionError)
20
from bzrlib.errors import NoSuchFile
25
21
from bzrlib.trace import mutter
26
22
from bzrlib.transport import register_urlparse_netloc_protocol
27
from bzrlib.transport.http import (HttpTransportBase, HttpServer,
28
extract_auth, response)
23
from bzrlib.transport.http import HttpTransportBase
24
# TODO: handle_response should be integrated into the _urllib2_wrappers
25
from bzrlib.transport.http.response import handle_response
26
from bzrlib.transport.http._urllib2_wrappers import (
30
32
register_urlparse_netloc_protocol('http+urllib')
33
class Request(urllib2.Request):
34
"""Request object for urllib2 that allows the method to be overridden."""
39
if self.method is not None:
42
return urllib2.Request.get_method(self)
45
35
class HttpTransport_urllib(HttpTransportBase):
46
36
"""Python urllib transport for http and https."""
48
# TODO: Implement pipelined versions of all of the *_multi() functions.
38
# In order to debug we have to issue our traces in sync with
39
# httplib, which use print :(
42
_opener_class = Opener
50
44
def __init__(self, base, from_transport=None):
51
45
"""Set the base path where files will be stored."""
52
46
super(HttpTransport_urllib, self).__init__(base)
53
# HttpTransport_urllib doesn't maintain any per-transport state yet
54
# so nothing to do with from_transport
47
if from_transport is not None:
48
self._connection = from_transport._connection
49
self._user = from_transport._user
50
self._password = from_transport._password
51
self._opener = from_transport._opener
53
self._connection = None
56
self._opener = self._opener_class()
58
def ask_password(self, request):
59
"""Ask for a password if none is already provided in the request"""
60
# TODO: jam 20060915 There should be a test that asserts we ask
61
# for a password at the right time.
62
if request.password is None:
63
# We can't predict realm, let's try None, we'll get a
64
# 401 if we are wrong anyway
66
host = request.get_host()
67
password_manager = self._opener.password_manager
68
# Query the password manager first
69
user, password = password_manager.find_user_password(None, host)
70
if user == request.user and password is not None:
71
request.password = password
73
# Ask the user if we MUST
74
http_pass = 'HTTP %(user)s@%(host)s password'
75
request.password = ui.ui_factory.get_password(prompt=http_pass,
78
password_manager.add_password(None, host,
79
request.user, request.password)
81
def _perform(self, request):
82
"""Send the request to the server and handles common errors.
84
:returns: urllib2 Response object
86
if self._connection is not None:
87
# Give back shared info
88
request.connection = self._connection
89
if self._user is not None:
90
request.user = self._user
91
request.password = self._password
92
elif request.user is not None:
93
# We will issue our first request, time to ask for a
95
self.ask_password(request)
97
mutter('%s: [%s]' % (request.method, request.get_full_url()))
98
if self._debuglevel > 0:
99
print 'perform: %s base: %s, url: %s' % (request.method, self.base,
100
request.get_full_url())
102
response = self._opener.open(request)
103
if self._connection is None:
104
# Acquire connection when the first request is able
105
# to connect to the server
106
self._connection = request.connection
107
self._user = request.user
108
self._password = request.password
110
if request.redirected_to is not None:
111
# TODO: Update the transport so that subsequent
112
# requests goes directly to the right host
113
mutter('redirected from: %s to: %s' % (request.get_full_url(),
114
request.redirected_to))
56
118
def _get(self, relpath, ranges, tail_amount=0):
57
return self._request(relpath, 'GET', ranges, tail_amount=tail_amount)
59
def _request(self, relpath, method, ranges, tail_amount=0, body=None):
62
path = self._real_abspath(relpath)
63
resp = self._get_url_impl(path, method=method, ranges=ranges,
64
tail_amount=tail_amount, body=body)
65
return resp.code, response.handle_response(path,
66
resp.code, resp.headers, resp)
67
except urllib2.HTTPError, e:
68
mutter('url error code: %s for has url: %r', e.code, path)
70
raise NoSuchFile(path, extra=e)
72
except (BzrError, IOError), e:
73
if getattr(e, 'errno', None) is not None:
74
mutter('io error: %s %s for has url: %r',
75
e.errno, errno.errorcode.get(e.errno), path)
76
if e.errno == errno.ENOENT:
77
raise NoSuchFile(path, extra=e)
78
raise ConnectionError(msg = "Error retrieving %s: %s"
79
% (self.abspath(relpath), str(e)),
82
def _get_url_impl(self, url, method, ranges, tail_amount=0, body=None):
83
"""Actually pass get request into urllib
85
:returns: urllib Response object
87
manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
88
url = extract_auth(url, manager)
89
auth_handler = urllib2.HTTPBasicAuthHandler(manager)
90
opener = urllib2.build_opener(auth_handler)
91
request = Request(url)
92
request.method = method
94
request.add_data(body)
95
request.add_header('Pragma', 'no-cache')
96
request.add_header('Cache-control', 'max-age=0')
97
request.add_header('User-Agent',
98
'bzr/%s (urllib)' % (bzrlib.__version__,))
119
"""See HttpTransport._get"""
121
abspath = self._real_abspath(relpath)
99
123
if ranges or tail_amount:
100
bytes = 'bytes=' + self.range_header(ranges, tail_amount)
101
request.add_header('Range', bytes)
102
response = opener.open(request)
124
range_header = self.attempted_range_header(ranges, tail_amount)
125
if range_header is not None:
126
bytes = 'bytes=' + range_header
127
headers = {'Range': bytes}
129
request = Request('GET', abspath, None, headers)
130
response = self._perform(request)
133
if code == 404: # not found
134
self._connection.fake_close()
135
raise NoSuchFile(abspath)
137
data = handle_response(abspath, code, response.headers, response)
138
# Close response to free the httplib.HTTPConnection pipeline
139
self._connection.fake_close()
105
142
def _post(self, body_bytes):
106
return self._request('.bzr/smart', 'POST', [], body=body_bytes)
143
abspath = self._real_abspath('.bzr/smart')
144
response = self._perform(Request('POST', abspath, body_bytes))
146
data = handle_response(abspath, code, response.headers, response)
147
# Close response to free the httplib.HTTPConnection pipeline
148
self._connection.fake_close()
108
151
def should_cache(self):
109
152
"""Return True if the data pulled across should be cached locally.
156
def _head(self, relpath):
157
"""Request the HEAD of a file.
159
Performs the request and leaves callers handle the results.
161
abspath = self._real_abspath(relpath)
162
request = Request('HEAD', abspath)
163
response = self._perform(request)
165
self._connection.fake_close()
113
168
def has(self, relpath):
114
169
"""Does the target location exist?
116
abspath = self._real_abspath(relpath)
118
f = self._get_url_impl(abspath, 'HEAD', [])
119
# Without the read and then close()
120
# we tend to have busy sockets.
171
response = self._head(relpath)
174
# FIXME: 302 MAY have been already processed by the
175
# redirection handler
176
if code in (200, 302): # "ok", "found"
124
except urllib2.HTTPError, e:
125
mutter('url error code: %s, for has url: %r', e.code, abspath)
129
except urllib2.URLError, e:
130
mutter('url error: %s, for has url: %r', e.reason, abspath)
133
mutter('io error: %s %s for has url: %r',
134
e.errno, errno.errorcode.get(e.errno), abspath)
135
if e.errno == errno.ENOENT:
137
raise TransportError(orig_error=e)
139
def copy_to(self, relpaths, other, mode=None, pb=None):
140
"""Copy a set of entries from self into another Transport.
142
:param relpaths: A list/generator of entries to be copied.
144
TODO: if other is LocalTransport, is it possible to
145
do better than put(get())?
147
# At this point HttpTransport_urllib might be able to check and see if
148
# the remote location is the same, and rather than download, and
149
# then upload, it could just issue a remote copy_this command.
150
if isinstance(other, HttpTransport_urllib):
151
raise TransportNotPossible('http cannot be the target of copy_to()')
153
return super(HttpTransport_urllib, self).copy_to(relpaths, other, mode=mode, pb=pb)
155
def move(self, rel_from, rel_to):
156
"""Move the item at rel_from to the location at rel_to"""
157
raise TransportNotPossible('http does not support move()')
159
def delete(self, relpath):
160
"""Delete the item at relpath"""
161
raise TransportNotPossible('http does not support delete()')
164
class HttpServer_urllib(HttpServer):
165
"""Subclass of HttpServer that gives http+urllib urls.
167
This is for use in testing: connections to this server will always go
168
through urllib where possible.
171
# urls returned by this server should require the urllib client impl
172
_url_protocol = 'http+urllib'
179
assert(code == 404, 'Only 200, 404 or may be 302 are correct')
175
183
def get_test_permutations():
176
184
"""Return the permutations to be used in testing."""
185
from bzrlib.tests.HttpServer import HttpServer_urllib
177
186
return [(HttpTransport_urllib, HttpServer_urllib),