~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Alexander Belchenko
  • Date: 2006-07-30 16:43:12 UTC
  • mto: (1711.2.111 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: bialix@ukr.net-20060730164312-b025fd3ff0cee59e
rename  gpl.txt => COPYING.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
from cStringIO import StringIO
18
 
import urllib
19
 
import urlparse
20
 
 
21
 
from bzrlib import (
22
 
    errors,
23
 
    trace,
24
 
    urlutils,
25
 
    )
26
 
from bzrlib.transport import http
27
 
# TODO: handle_response should be integrated into the http/__init__.py
28
 
from bzrlib.transport.http.response import handle_response
29
 
from bzrlib.transport.http._urllib2_wrappers import (
30
 
    Opener,
31
 
    Request,
32
 
    )
33
 
 
34
 
 
35
 
class HttpTransport_urllib(http.HttpTransportBase):
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
import errno
 
18
import urllib, urllib2
 
19
import errno
 
20
from StringIO import StringIO
 
21
 
 
22
import bzrlib  # for the version
 
23
from bzrlib.errors import (TransportNotPossible, NoSuchFile, BzrError,
 
24
                           TransportError, ConnectionError)
 
25
from bzrlib.trace import mutter
 
26
from bzrlib.transport import register_urlparse_netloc_protocol
 
27
from bzrlib.transport.http import (HttpTransportBase, HttpServer,
 
28
                                   extract_auth, response)
 
29
 
 
30
register_urlparse_netloc_protocol('http+urllib')
 
31
 
 
32
 
 
33
class Request(urllib2.Request):
 
34
    """Request object for urllib2 that allows the method to be overridden."""
 
35
 
 
36
    method = None
 
37
 
 
38
    def get_method(self):
 
39
        if self.method is not None:
 
40
            return self.method
 
41
        else:
 
42
            return urllib2.Request.get_method(self)
 
43
 
 
44
 
 
45
class HttpTransport_urllib(HttpTransportBase):
36
46
    """Python urllib transport for http and https."""
37
47
 
38
 
    # In order to debug we have to issue our traces in sync with
39
 
    # httplib, which use print :(
40
 
    _debuglevel = 0
41
 
 
42
 
    _opener_class = Opener
43
 
 
44
 
    def __init__(self, base, _from_transport=None):
45
 
        super(HttpTransport_urllib, self).__init__(
46
 
            base, 'urllib', _from_transport=_from_transport)
47
 
        if _from_transport is not None:
48
 
            self._opener = _from_transport._opener
49
 
        else:
50
 
            self._opener = self._opener_class(
51
 
                report_activity=self._report_activity)
52
 
 
53
 
    def _perform(self, request):
54
 
        """Send the request to the server and handles common errors.
55
 
 
56
 
        :returns: urllib2 Response object
57
 
        """
58
 
        connection = self._get_connection()
59
 
        if connection is not None:
60
 
            # Give back shared info
61
 
            request.connection = connection
62
 
            (auth, proxy_auth) = self._get_credentials()
63
 
            # Clean the httplib.HTTPConnection pipeline in case the previous
64
 
            # request couldn't do it
65
 
            connection.cleanup_pipe()
66
 
        else:
67
 
            # First request, initialize credentials.
68
 
            # scheme and realm will be set by the _urllib2_wrappers.AuthHandler
69
 
            auth = self._create_auth()
70
 
            # Proxy initialization will be done by the first proxied request
71
 
            proxy_auth = dict()
72
 
        # Ensure authentication info is provided
73
 
        request.auth = auth
74
 
        request.proxy_auth = proxy_auth
75
 
 
76
 
        if self._debuglevel > 0:
77
 
            print 'perform: %s base: %s, url: %s' % (request.method, self.base,
78
 
                                                     request.get_full_url())
79
 
        response = self._opener.open(request)
80
 
        if self._get_connection() is not request.connection:
81
 
            # First connection or reconnection
82
 
            self._set_connection(request.connection,
83
 
                                 (request.auth, request.proxy_auth))
84
 
        else:
85
 
            # http may change the credentials while keeping the
86
 
            # connection opened
87
 
            self._update_credentials((request.auth, request.proxy_auth))
88
 
 
89
 
        code = response.code
90
 
        if (request.follow_redirections is False
91
 
            and code in (301, 302, 303, 307)):
92
 
            raise errors.RedirectRequested(request.get_full_url(),
93
 
                                           request.redirected_to,
94
 
                                           is_permanent=(code == 301))
95
 
 
96
 
        if request.redirected_to is not None:
97
 
            trace.mutter('redirected from: %s to: %s' % (request.get_full_url(),
98
 
                                                         request.redirected_to))
99
 
 
100
 
        return response
101
 
 
102
 
    def disconnect(self):
103
 
        connection = self._get_connection()
104
 
        if connection is not None:
105
 
            connection.close()
106
 
 
107
 
    def _get(self, relpath, offsets, tail_amount=0):
108
 
        """See HttpTransport._get"""
109
 
        abspath = self._remote_path(relpath)
110
 
        headers = {}
111
 
        accepted_errors = [200, 404]
112
 
        if offsets or tail_amount:
113
 
            range_header = self._attempted_range_header(offsets, tail_amount)
114
 
            if range_header is not None:
115
 
                accepted_errors.append(206)
116
 
                accepted_errors.append(400)
117
 
                accepted_errors.append(416)
118
 
                bytes = 'bytes=' + range_header
119
 
                headers = {'Range': bytes}
120
 
 
121
 
        request = Request('GET', abspath, None, headers,
122
 
                          accepted_errors=accepted_errors)
123
 
        response = self._perform(request)
124
 
 
125
 
        code = response.code
126
 
        if code == 404: # not found
127
 
            raise errors.NoSuchFile(abspath)
128
 
        elif code in (400, 416):
129
 
            # We don't know which, but one of the ranges we specified was
130
 
            # wrong.
131
 
            raise errors.InvalidHttpRange(abspath, range_header,
132
 
                                          'Server return code %d' % code)
133
 
 
134
 
        data = handle_response(abspath, code, response.info(), response)
135
 
        return code, data
136
 
 
137
 
    def _post(self, body_bytes):
138
 
        abspath = self._remote_path('.bzr/smart')
139
 
        # We include 403 in accepted_errors so that send_http_smart_request can
140
 
        # handle a 403.  Otherwise a 403 causes an unhandled TransportError.
141
 
        response = self._perform(Request('POST', abspath, body_bytes,
142
 
                                         accepted_errors=[200, 403]))
143
 
        code = response.code
144
 
        data = handle_response(abspath, code, response.info(), response)
145
 
        return code, data
146
 
 
147
 
    def _head(self, relpath):
148
 
        """Request the HEAD of a file.
149
 
 
150
 
        Performs the request and leaves callers handle the results.
151
 
        """
152
 
        abspath = self._remote_path(relpath)
153
 
        request = Request('HEAD', abspath,
154
 
                          accepted_errors=[200, 404])
155
 
        response = self._perform(request)
156
 
 
157
 
        return response
 
48
    # TODO: Implement pipelined versions of all of the *_multi() functions.
 
49
 
 
50
    def __init__(self, base, from_transport=None):
 
51
        """Set the base path where files will be stored."""
 
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
 
55
 
 
56
    def _get(self, relpath, ranges, tail_amount=0):
 
57
        path = relpath
 
58
        try:
 
59
            path = self._real_abspath(relpath)
 
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)
 
64
        except urllib2.HTTPError, e:
 
65
            mutter('url error code: %s for has url: %r', e.code, path)
 
66
            if e.code == 404:
 
67
                raise NoSuchFile(path, extra=e)
 
68
            raise
 
69
        except (BzrError, IOError), e:
 
70
            if hasattr(e, 'errno'):
 
71
                mutter('io error: %s %s for has url: %r',
 
72
                    e.errno, errno.errorcode.get(e.errno), path)
 
73
                if e.errno == errno.ENOENT:
 
74
                    raise NoSuchFile(path, extra=e)
 
75
            raise ConnectionError(msg = "Error retrieving %s: %s" 
 
76
                             % (self.abspath(relpath), str(e)),
 
77
                             orig_error=e)
 
78
 
 
79
    def _get_url_impl(self, url, method, ranges, tail_amount=0):
 
80
        """Actually pass get request into urllib
 
81
 
 
82
        :returns: urllib Response object
 
83
        """
 
84
        manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
 
85
        url = extract_auth(url, manager)
 
86
        auth_handler = urllib2.HTTPBasicAuthHandler(manager)
 
87
        opener = urllib2.build_opener(auth_handler)
 
88
        request = Request(url)
 
89
        request.method = method
 
90
        request.add_header('Pragma', 'no-cache')
 
91
        request.add_header('Cache-control', 'max-age=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
        response = opener.open(request)
 
98
        return response
 
99
 
 
100
    def should_cache(self):
 
101
        """Return True if the data pulled across should be cached locally.
 
102
        """
 
103
        return True
158
104
 
159
105
    def has(self, relpath):
160
106
        """Does the target location exist?
161
107
        """
162
 
        response = self._head(relpath)
163
 
 
164
 
        code = response.code
165
 
        if code == 200: # "ok",
 
108
        abspath = self._real_abspath(relpath)
 
109
        try:
 
110
            f = self._get_url_impl(abspath, 'HEAD', [])
 
111
            # Without the read and then close()
 
112
            # we tend to have busy sockets.
 
113
            f.read()
 
114
            f.close()
166
115
            return True
 
116
        except urllib2.URLError, e:
 
117
            mutter('url error code: %s for has url: %r', e.code, abspath)
 
118
            if e.code == 404:
 
119
                return False
 
120
            raise
 
121
        except IOError, e:
 
122
            mutter('io error: %s %s for has url: %r',
 
123
                e.errno, errno.errorcode.get(e.errno), abspath)
 
124
            if e.errno == errno.ENOENT:
 
125
                return False
 
126
            raise TransportError(orig_error=e)
 
127
 
 
128
    def copy_to(self, relpaths, other, mode=None, pb=None):
 
129
        """Copy a set of entries from self into another Transport.
 
130
 
 
131
        :param relpaths: A list/generator of entries to be copied.
 
132
 
 
133
        TODO: if other is LocalTransport, is it possible to
 
134
              do better than put(get())?
 
135
        """
 
136
        # At this point HttpTransport_urllib might be able to check and see if
 
137
        # the remote location is the same, and rather than download, and
 
138
        # then upload, it could just issue a remote copy_this command.
 
139
        if isinstance(other, HttpTransport_urllib):
 
140
            raise TransportNotPossible('http cannot be the target of copy_to()')
167
141
        else:
168
 
            return False
 
142
            return super(HttpTransport_urllib, self).copy_to(relpaths, other, mode=mode, pb=pb)
 
143
 
 
144
    def move(self, rel_from, rel_to):
 
145
        """Move the item at rel_from to the location at rel_to"""
 
146
        raise TransportNotPossible('http does not support move()')
 
147
 
 
148
    def delete(self, relpath):
 
149
        """Delete the item at relpath"""
 
150
        raise TransportNotPossible('http does not support delete()')
 
151
 
 
152
 
 
153
class HttpServer_urllib(HttpServer):
 
154
    """Subclass of HttpServer that gives http+urllib urls.
 
155
 
 
156
    This is for use in testing: connections to this server will always go
 
157
    through urllib where possible.
 
158
    """
 
159
 
 
160
    # urls returned by this server should require the urllib client impl
 
161
    _url_protocol = 'http+urllib'
169
162
 
170
163
 
171
164
def get_test_permutations():
172
165
    """Return the permutations to be used in testing."""
173
 
    from bzrlib import tests
174
 
    from bzrlib.tests import http_server
175
 
    permutations = [(HttpTransport_urllib, http_server.HttpServer_urllib),]
176
 
    if tests.HTTPSServerFeature.available():
177
 
        from bzrlib.tests import https_server
178
 
        permutations.append((HttpTransport_urllib,
179
 
                             https_server.HTTPSServer_urllib))
180
 
    return permutations
 
166
    return [(HttpTransport_urllib, HttpServer_urllib),
 
167
            ]