19
19
from bzrlib.transport import Transport, register_transport
20
20
from bzrlib.errors import (TransportNotPossible, NoSuchFile,
21
TransportError, ConnectionError)
21
NonRelativePath, TransportError)
23
23
from cStringIO import StringIO
24
import urllib, urllib2
27
27
from bzrlib.errors import BzrError, BzrCheckError
28
28
from bzrlib.branch import Branch
29
29
from bzrlib.trace import mutter
32
def extract_auth(url, password_manager):
34
Extract auth parameters from am HTTP/HTTPS url and add them to the given
35
password manager. Return the url, minus those auth parameters (which
38
assert url.startswith('http://') or url.startswith('https://')
39
scheme, host = url.split('//', 1)
41
host, path = host.split('/', 1)
47
auth, host = host.split('@', 1)
49
username, password = auth.split(':', 1)
51
username, password = auth, None
53
host, port = host.split(':', 1)
55
# FIXME: if password isn't given, should we ask for it?
56
if password is not None:
57
username = urllib.unquote(username)
58
password = urllib.unquote(password)
59
password_manager.add_password(None, host, username, password)
60
url = scheme + '//' + host + port + path
31
# velocitynet.com.au transparently proxies connections and thereby
32
# breaks keep-alive -- sucks!
65
37
mutter("get_url %s" % url)
66
manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
67
url = extract_auth(url, manager)
68
auth_handler = urllib2.HTTPBasicAuthHandler(manager)
69
opener = urllib2.build_opener(auth_handler)
70
url_f = opener.open(url)
38
url_f = urllib2.urlopen(url)
41
class HttpTransportError(TransportError):
73
44
class HttpTransport(Transport):
74
45
"""This is the transport agent for http:// access.
106
77
"""Return the full url to the given relative path.
107
78
This can be supplied with a string or a list
109
assert isinstance(relpath, basestring)
110
80
if isinstance(relpath, basestring):
111
relpath_parts = relpath.split('/')
113
# TODO: Don't call this with an array - no magic interfaces
114
relpath_parts = relpath[:]
115
if len(relpath_parts) > 1:
116
if relpath_parts[0] == '':
117
raise ValueError("path %r within branch %r seems to be absolute"
118
% (relpath, self._path))
119
if relpath_parts[-1] == '':
120
raise ValueError("path %r within branch %r seems to be a directory"
121
% (relpath, self._path))
122
82
basepath = self._path.split('/')
123
83
if len(basepath) > 0 and basepath[-1] == '':
124
84
basepath = basepath[:-1]
125
for p in relpath_parts:
127
if len(basepath) == 0:
128
89
# In most filesystems, a request for the parent
129
90
# of root, just returns root.
132
elif p == '.' or p == '':
135
96
basepath.append(p)
136
98
# Possibly, we could use urlparse.urljoin() here, but
137
99
# I'm concerned about when it chooses to strip the last
138
100
# portion of the path, and when it doesn't.
151
120
cleaner if we just do an http HEAD request, and parse
156
path = self.abspath(relpath)
124
f = get_url(self.abspath(relpath))
158
125
# Without the read and then close()
159
126
# we tend to have busy sockets.
163
except urllib2.URLError, e:
164
mutter('url error code: %s for has url: %r', e.code, path)
132
except urllib2.URLError:
168
134
except IOError, e:
169
mutter('io error: %s %s for has url: %r',
170
e.errno, errno.errorcode.get(e.errno), path)
171
135
if e.errno == errno.ENOENT:
173
raise TransportError(orig_error=e)
137
raise HttpTransportError(orig_error=e)
175
139
def get(self, relpath, decode=False):
176
140
"""Get the file at the given relative path.
178
142
:param relpath: The relative path to the file
182
path = self.abspath(relpath)
184
except urllib2.HTTPError, e:
185
mutter('url error code: %s for has url: %r', e.code, path)
187
raise NoSuchFile(path, extra=e)
189
except (BzrError, IOError), e:
190
if hasattr(e, 'errno'):
191
mutter('io error: %s %s for has url: %r',
192
e.errno, errno.errorcode.get(e.errno), path)
193
if e.errno == errno.ENOENT:
194
raise NoSuchFile(path, extra=e)
195
raise ConnectionError(msg = "Error retrieving %s: %s"
196
% (self.abspath(relpath), str(e)),
199
def put(self, relpath, f, mode=None):
145
return get_url(self.abspath(relpath))
146
except (BzrError, urllib2.URLError, IOError), e:
147
raise NoSuchFile(orig_error=e)
149
raise HttpTransportError(orig_error=e)
151
def get_partial(self, relpath, start, length=None):
152
"""Get just part of a file.
154
:param relpath: Path to the file, relative to base
155
:param start: The starting position to read from
156
:param length: The length to read. A length of None indicates
157
read to the end of the file.
158
:return: A file-like object containing at least the specified bytes.
159
Some implementations may return objects which can be read
160
past this length, but this is not guaranteed.
162
# TODO: You can make specialized http requests for just
163
# a portion of the file. Figure out how to do that.
164
# For now, urllib2 returns files that cannot seek() so
165
# we just read bytes off the beginning, until we
166
# get to the point that we care about.
167
f = self.get(relpath)
168
# TODO: read in smaller chunks, in case things are
169
# buffered internally.
173
def put(self, relpath, f):
200
174
"""Copy the file-like or string object into the location.
202
176
:param relpath: Location to put the contents, relative to base.
232
206
if isinstance(other, HttpTransport):
233
207
raise TransportNotPossible('http cannot be the target of copy_to()')
235
return super(HttpTransport, self).copy_to(relpaths, other, mode=mode, pb=pb)
209
return super(HttpTransport, self).copy_to(relpaths, other, pb=pb)
237
211
def move(self, rel_from, rel_to):
238
212
"""Move the item at rel_from to the location at rel_to"""