~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/http/_urllib.py

Merge Tree.changes_from work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import errno
18
18
import urllib, urllib2
 
19
import errno
 
20
from StringIO import StringIO
19
21
 
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
 
 
 
27
from bzrlib.transport.http import (HttpTransportBase, HttpServer,
 
28
                                   extract_auth, response)
28
29
 
29
30
register_urlparse_netloc_protocol('http+urllib')
30
31
 
42
43
 
43
44
 
44
45
class HttpTransport_urllib(HttpTransportBase):
45
 
    """Python urllib transport for http and https.
46
 
    """
 
46
    """Python urllib transport for http and https."""
47
47
 
48
48
    # TODO: Implement pipelined versions of all of the *_multi() functions.
49
49
 
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
53
55
 
54
 
    def _get(self, relpath, ranges):
 
56
    def _get(self, relpath, ranges, tail_amount=0):
55
57
        path = relpath
56
58
        try:
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)
62
66
            if e.code == 404:
72
76
                             % (self.abspath(relpath), str(e)),
73
77
                             orig_error=e)
74
78
 
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
77
81
 
78
82
        :returns: urllib Response object
79
83
        """
80
 
        if ranges:
81
 
            range_string = ranges
82
 
        else:
83
 
            range_string = 'all'
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__)
94
 
        if ranges:
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)
98
98
        return response
99
99