~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2006-03-10 06:29:53 UTC
  • mfrom: (1608 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060310062953-bc1c7ade75c89a7a
[merge] bzr.dev; pycurl not updated for readv yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
            return urllib2.Request.get_method(self)
37
37
 
38
38
 
39
 
class HttpTransport(HttpTransportBase):
 
39
class HttpTransport_urllib(HttpTransportBase):
40
40
    """Python urllib transport for http and https.
41
41
    """
42
42
 
44
44
 
45
45
    def __init__(self, base):
46
46
        """Set the base path where files will be stored."""
47
 
        super(HttpTransport, self).__init__(base)
48
 
 
49
 
    def _get_url(self, url, method=None):
50
 
        mutter("get_url %s" % url)
 
47
        super(HttpTransport_urllib, self).__init__(base)
 
48
 
 
49
    def get(self, relpath):
 
50
        """Get the file at the given relative path.
 
51
 
 
52
        :param relpath: The relative path to the file
 
53
        """
 
54
        return self._get(relpath, [])
 
55
 
 
56
    def _get(self, relpath, ranges):
 
57
        path = relpath
 
58
        try:
 
59
            path = self._real_abspath(relpath)
 
60
            return self._get_url_impl(path, method=method, ranges=ranges)
 
61
        except urllib2.HTTPError, e:
 
62
            mutter('url error code: %s for has url: %r', e.code, path)
 
63
            if e.code == 404:
 
64
                raise NoSuchFile(path, extra=e)
 
65
            raise
 
66
        except (BzrError, IOError), e:
 
67
            if hasattr(e, 'errno'):
 
68
                mutter('io error: %s %s for has url: %r',
 
69
                    e.errno, errno.errorcode.get(e.errno), path)
 
70
                if e.errno == errno.ENOENT:
 
71
                    raise NoSuchFile(path, extra=e)
 
72
            raise ConnectionError(msg = "Error retrieving %s: %s" 
 
73
                             % (self.abspath(relpath), str(e)),
 
74
                             orig_error=e)
 
75
 
 
76
    def _get_url_impl(self, url, method, ranges):
 
77
        if ranges:
 
78
            range_string = ranges
 
79
        else:
 
80
            range_string = 'all'
 
81
        mutter("get_url %s [%s]" % (url, range_string))
51
82
        manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
52
83
        url = extract_auth(url, manager)
53
84
        auth_handler = urllib2.HTTPBasicAuthHandler(manager)
55
86
        request = Request(url)
56
87
        request.method = method
57
88
        request.add_header('User-Agent', 'bzr/%s' % bzrlib.__version__)
 
89
        if ranges:
 
90
            request.add_header('Range', ranges)
58
91
        response = opener.open(request)
59
92
        return response
60
93
 
66
99
    def has(self, relpath):
67
100
        """Does the target location exist?
68
101
        """
69
 
        path = relpath
 
102
        abspath = self._real_abspath(relpath)
70
103
        try:
71
 
            path = self._real_abspath(relpath)
72
 
            f = self._get_url(path, 'HEAD')
 
104
            f = self._get_url_impl(abspath, 'HEAD', [])
73
105
            # Without the read and then close()
74
106
            # we tend to have busy sockets.
75
107
            f.read()
76
108
            f.close()
77
109
            return True
78
110
        except urllib2.URLError, e:
79
 
            mutter('url error code: %s for has url: %r', e.code, path)
 
111
            mutter('url error code: %s for has url: %r', e.code, abspath)
80
112
            if e.code == 404:
81
113
                return False
82
114
            raise
83
115
        except IOError, e:
84
 
            mutter('io error: %s %s for has url: %r', 
85
 
                e.errno, errno.errorcode.get(e.errno), path)
 
116
            mutter('io error: %s %s for has url: %r',
 
117
                e.errno, errno.errorcode.get(e.errno), abspath)
86
118
            if e.errno == errno.ENOENT:
87
119
                return False
88
120
            raise TransportError(orig_error=e)
89
121
 
90
 
    def get(self, relpath):
91
 
        """Get the file at the given relative path.
92
 
 
93
 
        :param relpath: The relative path to the file
94
 
        """
95
 
        path = relpath
96
 
        try:
97
 
            path = self._real_abspath(relpath)
98
 
            return self._get_url(path)
99
 
        except urllib2.HTTPError, e:
100
 
            mutter('url error code: %s for has url: %r', e.code, path)
101
 
            if e.code == 404:
102
 
                raise NoSuchFile(path, extra=e)
103
 
            raise
104
 
        except (BzrError, IOError), e:
105
 
            if hasattr(e, 'errno'):
106
 
                mutter('io error: %s %s for has url: %r', 
107
 
                    e.errno, errno.errorcode.get(e.errno), path)
108
 
                if e.errno == errno.ENOENT:
109
 
                    raise NoSuchFile(path, extra=e)
110
 
            raise ConnectionError(msg = "Error retrieving %s: %s" 
111
 
                             % (self._real_abspath(relpath), str(e)),
112
 
                             orig_error=e)
113
 
 
114
122
    def copy_to(self, relpaths, other, mode=None, pb=None):
115
123
        """Copy a set of entries from self into another Transport.
116
124
 
119
127
        TODO: if other is LocalTransport, is it possible to
120
128
              do better than put(get())?
121
129
        """
122
 
        # At this point HttpTransport might be able to check and see if
 
130
        # At this point HttpTransport_urllib might be able to check and see if
123
131
        # the remote location is the same, and rather than download, and
124
132
        # then upload, it could just issue a remote copy_this command.
125
 
        if isinstance(other, HttpTransport):
 
133
        if isinstance(other, HttpTransport_urllib):
126
134
            raise TransportNotPossible('http cannot be the target of copy_to()')
127
135
        else:
128
 
            return super(HttpTransport, self).copy_to(relpaths, other, mode=mode, pb=pb)
 
136
            return super(HttpTransport_urllib, self).copy_to(relpaths, other, mode=mode, pb=pb)
129
137
 
130
138
    def move(self, rel_from, rel_to):
131
139
        """Move the item at rel_from to the location at rel_to"""
149
157
 
150
158
def get_test_permutations():
151
159
    """Return the permutations to be used in testing."""
152
 
    return [(HttpTransport, HttpServer_urllib),
 
160
    return [(HttpTransport_urllib, HttpServer_urllib),
153
161
            ]