18
18
import urllib, urllib2
20
from StringIO import StringIO
20
22
import bzrlib # for the version
21
from bzrlib.errors import BzrError
23
from bzrlib.errors import (TransportNotPossible, NoSuchFile, BzrError,
24
TransportError, ConnectionError)
22
25
from bzrlib.trace import mutter
23
26
from bzrlib.transport import register_urlparse_netloc_protocol
24
from bzrlib.transport.http import HttpTransportBase, extract_auth, HttpServer
25
from bzrlib.errors import (TransportNotPossible, NoSuchFile,
26
TransportError, ConnectionError)
27
from bzrlib.transport.http import (HttpTransportBase, HttpServer,
28
extract_auth, response)
29
30
register_urlparse_netloc_protocol('http+urllib')
44
45
class HttpTransport_urllib(HttpTransportBase):
45
"""Python urllib transport for http and https.
46
"""Python urllib transport for http and https."""
48
48
# TODO: Implement pipelined versions of all of the *_multi() functions.
50
def __init__(self, base):
50
def __init__(self, base, from_transport=None):
51
51
"""Set the base path where files will be stored."""
52
52
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
54
def _get(self, relpath, ranges):
56
def _get(self, relpath, ranges, tail_amount=0):
57
59
path = self._real_abspath(relpath)
58
response = self._get_url_impl(path, method='GET', ranges=ranges)
59
return response.code, response
60
resp = self._get_url_impl(path, method='GET', ranges=ranges,
61
tail_amount=tail_amount)
62
return resp.code, response.handle_response(path,
63
resp.code, resp.headers, resp)
60
64
except urllib2.HTTPError, e:
61
65
mutter('url error code: %s for has url: %r', e.code, path)
72
76
% (self.abspath(relpath), str(e)),
75
def _get_url_impl(self, url, method, ranges):
79
def _get_url_impl(self, url, method, ranges, tail_amount=0):
76
80
"""Actually pass get request into urllib
78
82
:returns: urllib Response object
84
mutter("get_url %s [%s]" % (url, range_string))
85
84
manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
86
85
url = extract_auth(url, manager)
87
86
auth_handler = urllib2.HTTPBasicAuthHandler(manager)
90
89
request.method = method
91
90
request.add_header('Pragma', 'no-cache')
92
91
request.add_header('Cache-control', 'max-age=0')
93
request.add_header('User-Agent', 'bzr/%s (urllib)' % bzrlib.__version__)
95
assert len(ranges) == 1
96
request.add_header('Range', 'bytes=%d-%d' % ranges[0])
92
request.add_header('User-Agent',
93
'bzr/%s (urllib)' % (bzrlib.__version__,))
94
if ranges or tail_amount:
95
bytes = 'bytes=' + self.range_header(ranges, tail_amount)
96
request.add_header('Range', bytes)
97
97
response = opener.open(request)