73
56
if password is not None:
74
57
password = urllib.unquote(password)
76
password = ui.ui_factory.get_password(
77
prompt='HTTP %(user)s@%(host)s password',
78
user=username, host=host)
59
password = ui_factory.get_password(prompt='HTTP %(user)@%(host) password',
60
user=username, host=host)
79
61
password_manager.add_password(None, host, username, password)
80
62
url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
84
class HttpTransportBase(ConnectedTransport):
85
"""Base class for http implementations.
87
Does URL parsing, etc, but not any network IO.
89
The protocol can be given as e.g. http+urllib://host/ to use a particular
66
class Request(urllib2.Request):
67
"""Request object for urllib2 that allows the method to be overridden."""
72
if self.method is not None:
75
return urllib2.Request.get_method(self)
78
def get_url(url, method=None, ranges=None):
84
mutter("get_url %s [%s]", url, rangestring)
85
manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
86
url = extract_auth(url, manager)
87
auth_handler = urllib2.HTTPBasicAuthHandler(manager)
88
opener = urllib2.build_opener(auth_handler)
90
request = Request(url)
91
request.method = method
92
request.add_header('User-Agent', 'bzr/%s' % bzrlib.__version__)
94
request.add_header('Range', ranges)
95
response = opener.open(request)
99
class HttpTransport(Transport):
100
"""This is the transport agent for http:// access.
102
TODO: Implement pipelined versions of all of the *_multi() functions.
93
# _unqualified_scheme: "http" or "https"
94
# _scheme: may have "+pycurl", etc
96
def __init__(self, base, _impl_name, _from_transport=None):
105
def __init__(self, base):
97
106
"""Set the base path where files will be stored."""
98
proto_match = re.match(r'^(https?)(\+\w+)?://', base)
100
raise AssertionError("not a http url: %r" % base)
101
self._unqualified_scheme = proto_match.group(1)
102
self._impl_name = _impl_name
103
super(HttpTransportBase, self).__init__(base,
104
_from_transport=_from_transport)
106
# range hint is handled dynamically throughout the life
107
# of the transport object. We start by trying multi-range
108
# requests and if the server returns bogus results, we
109
# retry with single range requests and, finally, we
110
# forget about range if the server really can't
111
# understand. Once acquired, this piece of info is
112
# propagated to clones.
113
if _from_transport is not None:
114
self._range_hint = _from_transport._range_hint
116
self._range_hint = 'multi'
107
assert base.startswith('http://') or base.startswith('https://')
110
super(HttpTransport, self).__init__(base)
111
# In the future we might actually connect to the remote host
112
# rather than using get_url
113
# self._connection = None
114
(self._proto, self._host,
115
self._path, self._parameters,
116
self._query, self._fragment) = urlparse.urlparse(self.base)
118
def should_cache(self):
119
"""Return True if the data pulled across should be cached locally.
123
def clone(self, offset=None):
124
"""Return a new HttpTransport with root at self.base + offset
125
For now HttpTransport does not actually connect, so just return
126
a new HttpTransport object.
129
return HttpTransport(self.base)
131
return HttpTransport(self.abspath(offset))
133
def abspath(self, relpath):
134
"""Return the full url to the given relative path.
135
This can be supplied with a string or a list
137
assert isinstance(relpath, basestring)
138
if isinstance(relpath, basestring):
139
relpath_parts = relpath.split('/')
141
# TODO: Don't call this with an array - no magic interfaces
142
relpath_parts = relpath[:]
143
if len(relpath_parts) > 1:
144
if relpath_parts[0] == '':
145
raise ValueError("path %r within branch %r seems to be absolute"
146
% (relpath, self._path))
147
if relpath_parts[-1] == '':
148
raise ValueError("path %r within branch %r seems to be a directory"
149
% (relpath, self._path))
150
basepath = self._path.split('/')
151
if len(basepath) > 0 and basepath[-1] == '':
152
basepath = basepath[:-1]
153
for p in relpath_parts:
155
if len(basepath) == 0:
156
# In most filesystems, a request for the parent
157
# of root, just returns root.
160
elif p == '.' or p == '':
164
# Possibly, we could use urlparse.urljoin() here, but
165
# I'm concerned about when it chooses to strip the last
166
# portion of the path, and when it doesn't.
167
path = '/'.join(basepath)
168
return urlparse.urlunparse((self._proto,
169
self._host, path, '', '', ''))
118
171
def has(self, relpath):
119
raise NotImplementedError("has() is abstract on %r" % self)
121
def get(self, relpath):
172
"""Does the target location exist?
174
TODO: This should be changed so that we don't use
175
urllib2 and get an exception, the code path would be
176
cleaner if we just do an http HEAD request, and parse
181
path = self.abspath(relpath)
182
f = get_url(path, method='HEAD')
183
# Without the read and then close()
184
# we tend to have busy sockets.
188
except urllib2.HTTPError, e:
189
mutter('url error code: %s for has url: %r', e.code, path)
194
mutter('io error: %s %s for has url: %r',
195
e.errno, errno.errorcode.get(e.errno), path)
196
if e.errno == errno.ENOENT:
198
raise TransportError(orig_error=e)
200
def _get(self, relpath, decode=False, ranges=None):
203
path = self.abspath(relpath)
204
return get_url(path, ranges=ranges)
205
except urllib2.HTTPError, e:
206
mutter('url error code: %s for has url: %r', e.code, path)
208
raise NoSuchFile(path, extra=e)
210
except (BzrError, IOError), e:
211
if hasattr(e, 'errno'):
212
mutter('io error: %s %s for has url: %r',
213
e.errno, errno.errorcode.get(e.errno), path)
214
if e.errno == errno.ENOENT:
215
raise NoSuchFile(path, extra=e)
216
raise ConnectionError(msg = "Error retrieving %s: %s"
217
% (self.abspath(relpath), str(e)),
220
def get(self, relpath, decode=False):
122
221
"""Get the file at the given relative path.
124
223
:param relpath: The relative path to the file
126
code, response_file = self._get(relpath, None)
127
# FIXME: some callers want an iterable... One step forward, three steps
128
# backwards :-/ And not only an iterable, but an iterable that can be
129
# seeked backwards, so we will never be able to do that. One such
130
# known client is bzrlib.bundle.serializer.v4.get_bundle_reader. At the
131
# time of this writing it's even the only known client -- vila20071203
132
return StringIO(response_file.read())
134
def _get(self, relpath, ranges, tail_amount=0):
135
"""Get a file, or part of a file.
137
:param relpath: Path relative to transport base URL
138
:param ranges: None to get the whole file;
139
or a list of _CoalescedOffset to fetch parts of a file.
140
:param tail_amount: The amount to get from the end of the file.
142
:returns: (http_code, result_file)
144
raise NotImplementedError(self._get)
146
def _remote_path(self, relpath):
147
"""See ConnectedTransport._remote_path.
149
user and passwords are not embedded in the path provided to the server.
151
relative = urlutils.unescape(relpath).encode('utf-8')
152
path = self._combine_paths(self._path, relative)
153
return self._unsplit_url(self._unqualified_scheme,
154
None, None, self._host, self._port, path)
156
def _create_auth(self):
157
"""Returns a dict containing the credentials provided at build time."""
158
auth = dict(host=self._host, port=self._port,
159
user=self._user, password=self._password,
160
protocol=self._unqualified_scheme,
164
def get_smart_medium(self):
165
"""See Transport.get_smart_medium."""
166
if self._medium is None:
167
# Since medium holds some state (smart server probing at least), we
168
# need to keep it around. Note that this is needed because medium
169
# has the same 'base' attribute as the transport so it can't be
170
# shared between transports having different bases.
171
self._medium = SmartClientHTTPMedium(self)
174
def _degrade_range_hint(self, relpath, ranges, exc_info):
175
if self._range_hint == 'multi':
176
self._range_hint = 'single'
177
mutter('Retry "%s" with single range request' % relpath)
178
elif self._range_hint == 'single':
179
self._range_hint = None
180
mutter('Retry "%s" without ranges' % relpath)
182
# We tried all the tricks, but nothing worked. We re-raise the
183
# original exception; the 'mutter' calls above will indicate that
184
# further tries were unsuccessful
185
raise exc_info[0], exc_info[1], exc_info[2]
187
# _coalesce_offsets is a helper for readv, it try to combine ranges without
188
# degrading readv performances. _bytes_to_read_before_seek is the value
189
# used for the limit parameter and has been tuned for other transports. For
190
# HTTP, the name is inappropriate but the parameter is still useful and
191
# helps reduce the number of chunks in the response. The overhead for a
192
# chunk (headers, length, footer around the data itself is variable but
193
# around 50 bytes. We use 128 to reduce the range specifiers that appear in
194
# the header, some servers (notably Apache) enforce a maximum length for a
195
# header and issue a '400: Bad request' error when too much ranges are
197
_bytes_to_read_before_seek = 128
198
# No limit on the offset number that get combined into one, we are trying
199
# to avoid downloading the whole file.
200
_max_readv_combine = 0
201
# By default Apache has a limit of ~400 ranges before replying with a 400
202
# Bad Request. So we go underneath that amount to be safe.
203
_max_get_ranges = 200
204
# We impose no limit on the range size. But see _pycurl.py for a different
208
def _readv(self, relpath, offsets):
225
return self._get(relpath, decode=decode)
227
def readv(self, relpath, offsets):
209
228
"""Get parts of the file at the given relative path.
211
:param offsets: A list of (offset, size) tuples.
212
:param return: A list or generator of (offset, data) tuples
230
:offsets: A list of (offset, size) tuples.
231
:return: A list or generator of (offset, data) tuples
214
# offsets may be a generator, we will iterate it several times, so
216
offsets = list(offsets)
219
retried_offset = None
223
# Coalesce the offsets to minimize the GET requests issued
224
sorted_offsets = sorted(offsets)
225
coalesced = self._coalesce_offsets(
226
sorted_offsets, limit=self._max_readv_combine,
227
fudge_factor=self._bytes_to_read_before_seek,
228
max_size=self._get_max_size)
230
# Turn it into a list, we will iterate it several times
231
coalesced = list(coalesced)
232
if 'http' in debug.debug_flags:
233
mutter('http readv of %s offsets => %s collapsed %s',
234
relpath, len(offsets), len(coalesced))
236
# Cache the data read, but only until it's been used
238
# We will iterate on the data received from the GET requests and
239
# serve the corresponding offsets respecting the initial order. We
240
# need an offset iterator for that.
241
iter_offsets = iter(offsets)
242
cur_offset_and_size = iter_offsets.next()
245
for cur_coal, rfile in self._coalesce_readv(relpath, coalesced):
246
# Split the received chunk
247
for offset, size in cur_coal.ranges:
248
start = cur_coal.start + offset
250
data = rfile.read(size)
253
raise errors.ShortReadvError(relpath, start, size,
255
if (start, size) == cur_offset_and_size:
256
# The offset requested are sorted as the coalesced
257
# ones, no need to cache. Win !
258
yield cur_offset_and_size[0], data
259
cur_offset_and_size = iter_offsets.next()
261
# Different sorting. We need to cache.
262
data_map[(start, size)] = data
264
# Yield everything we can
265
while cur_offset_and_size in data_map:
266
# Clean the cached data since we use it
267
# XXX: will break if offsets contains duplicates --
269
this_data = data_map.pop(cur_offset_and_size)
270
yield cur_offset_and_size[0], this_data
271
cur_offset_and_size = iter_offsets.next()
273
except (errors.ShortReadvError, errors.InvalidRange,
274
errors.InvalidHttpRange), e:
275
mutter('Exception %r: %s during http._readv',e, e)
276
if (not isinstance(e, errors.ShortReadvError)
277
or retried_offset == cur_offset_and_size):
278
# We don't degrade the range hint for ShortReadvError since
279
# they do not indicate a problem with the server ability to
280
# handle ranges. Except when we fail to get back a required
281
# offset twice in a row. In that case, falling back to
282
# single range or whole file should help or end up in a
284
self._degrade_range_hint(relpath, coalesced, sys.exc_info())
285
# Some offsets may have been already processed, so we retry
286
# only the unsuccessful ones.
287
offsets = [cur_offset_and_size] + [o for o in iter_offsets]
288
retried_offset = cur_offset_and_size
291
def _coalesce_readv(self, relpath, coalesced):
292
"""Issue several GET requests to satisfy the coalesced offsets"""
294
def get_and_yield(relpath, coalesced):
296
# Note that the _get below may raise
297
# errors.InvalidHttpRange. It's the caller's responsibility to
298
# decide how to retry since it may provide different coalesced
300
code, rfile = self._get(relpath, coalesced)
301
for coal in coalesced:
304
if self._range_hint is None:
305
# Download whole file
306
for c, rfile in get_and_yield(relpath, coalesced):
309
total = len(coalesced)
310
if self._range_hint == 'multi':
311
max_ranges = self._max_get_ranges
312
elif self._range_hint == 'single':
233
# this is not quite regular enough to have a single driver routine and
234
# helper method in Transport.
235
def do_combined_read(combined_offsets):
236
# read one coalesced block
238
for offset, size in combined_offsets:
240
mutter('readv coalesced %d reads.', len(combined_offsets))
241
offset = combined_offsets[0][0]
242
ranges = 'bytes=%d-%d' % (offset, offset + total_size - 1)
243
response = self._get(relpath, ranges=ranges)
244
if response.code == 206:
245
for off, size in combined_offsets:
246
yield off, response.read(size)
247
elif response.code == 200:
248
data = response.read(offset + total_size)[offset:offset + total_size]
250
for offset, size in combined_offsets:
251
yield offset, data[pos:pos + size]
257
pending_offsets = deque(offsets)
258
combined_offsets = []
259
while len(pending_offsets):
260
offset, size = pending_offsets.popleft()
261
if not combined_offsets:
262
combined_offsets = [[offset, size]]
315
raise AssertionError("Unknown _range_hint %r"
316
% (self._range_hint,))
317
# TODO: Some web servers may ignore the range requests and return
318
# the whole file, we may want to detect that and avoid further
320
# Hint: test_readv_multiple_get_requests will fail once we do that
323
for coal in coalesced:
324
if ((self._get_max_size > 0
325
and cumul + coal.length > self._get_max_size)
326
or len(ranges) >= max_ranges):
327
# Get that much and yield
328
for c, rfile in get_and_yield(relpath, ranges):
330
# Restart with the current offset
264
if (len (combined_offsets) < 500 and
265
combined_offsets[-1][0] + combined_offsets[-1][1] == offset):
267
combined_offsets.append([offset, size])
336
# Get the rest and yield
337
for c, rfile in get_and_yield(relpath, ranges):
340
def recommended_page_size(self):
341
"""See Transport.recommended_page_size().
343
For HTTP we suggest a large page size to reduce the overhead
344
introduced by latency.
348
def _post(self, body_bytes):
349
"""POST body_bytes to .bzr/smart on this transport.
351
:returns: (response code, response body file-like object).
353
# TODO: Requiring all the body_bytes to be available at the beginning of
354
# the POST may require large client buffers. It would be nice to have
355
# an interface that allows streaming via POST when possible (and
356
# degrades to a local buffer when not).
357
raise NotImplementedError(self._post)
359
def put_file(self, relpath, f, mode=None):
360
"""Copy the file-like object into the location.
269
# incompatible, or over the threshold issue a read and yield
270
pending_offsets.appendleft((offset, size))
271
for result in do_combined_read(combined_offsets):
273
combined_offsets = []
274
# whatever is left is a single coalesced request
275
if len(combined_offsets):
276
for result in do_combined_read(combined_offsets):
279
def put(self, relpath, f, mode=None):
280
"""Copy the file-like or string object into the location.
362
282
:param relpath: Location to put the contents, relative to base.
363
:param f: File-like object.
283
:param f: File-like or string object.
365
raise errors.TransportNotPossible('http PUT not supported')
285
raise TransportNotPossible('http PUT not supported')
367
287
def mkdir(self, relpath, mode=None):
368
288
"""Create a directory at the given path."""
369
raise errors.TransportNotPossible('http does not support mkdir()')
289
raise TransportNotPossible('http does not support mkdir()')
371
291
def rmdir(self, relpath):
372
292
"""See Transport.rmdir."""
373
raise errors.TransportNotPossible('http does not support rmdir()')
293
raise TransportNotPossible('http does not support rmdir()')
375
def append_file(self, relpath, f, mode=None):
295
def append(self, relpath, f):
376
296
"""Append the text in the file-like object into the final
379
raise errors.TransportNotPossible('http does not support append()')
299
raise TransportNotPossible('http does not support append()')
381
301
def copy(self, rel_from, rel_to):
382
302
"""Copy the item at rel_from to the location at rel_to"""
383
raise errors.TransportNotPossible('http does not support copy()')
303
raise TransportNotPossible('http does not support copy()')
385
305
def copy_to(self, relpaths, other, mode=None, pb=None):
386
306
"""Copy a set of entries from self into another Transport.
450
359
:return: A lock object, which should be passed to Transport.unlock()
452
raise errors.TransportNotPossible('http does not support lock_write()')
454
def _attempted_range_header(self, offsets, tail_amount):
455
"""Prepare a HTTP Range header at a level the server should accept.
457
:return: the range header representing offsets/tail_amount or None if
458
no header can be built.
461
if self._range_hint == 'multi':
462
# Generate the header describing all offsets
463
return self._range_header(offsets, tail_amount)
464
elif self._range_hint == 'single':
465
# Combine all the requested ranges into a single
468
if tail_amount not in (0, None):
469
# Nothing we can do here to combine ranges with tail_amount
470
# in a single range, just returns None. The whole file
471
# should be downloaded.
474
start = offsets[0].start
476
end = last.start + last.length - 1
477
whole = self._coalesce_offsets([(start, end - start + 1)],
478
limit=0, fudge_factor=0)
479
return self._range_header(list(whole), 0)
481
# Only tail_amount, requested, leave range_header
483
return self._range_header(offsets, tail_amount)
488
def _range_header(ranges, tail_amount):
489
"""Turn a list of bytes ranges into a HTTP Range header value.
491
:param ranges: A list of _CoalescedOffset
492
:param tail_amount: The amount to get from the end of the file.
494
:return: HTTP range header string.
496
At least a non-empty ranges *or* a tail_amount must be
500
for offset in ranges:
501
strings.append('%d-%d' % (offset.start,
502
offset.start + offset.length - 1))
505
strings.append('-%d' % tail_amount)
507
return ','.join(strings)
509
def _redirected_to(self, source, target):
510
"""Returns a transport suitable to re-issue a redirected request.
512
:param source: The source url as returned by the server.
513
:param target: The target url as returned by the server.
515
The redirection can be handled only if the relpath involved is not
516
renamed by the redirection.
518
:returns: A transport or None.
520
def relpath(abspath):
521
"""Returns the path relative to our base.
523
The constraints are weaker than the real relpath method because the
524
abspath is coming from the server and may slightly differ from our
525
base. We don't check the scheme, host, port, user, password parts,
526
relying on the caller to give us a proper url (i.e. one returned by
527
the server mirroring the one we sent).
532
path) = self._split_url(abspath)
534
return path[pl:].strip('/')
536
relpath = relpath(source)
537
if not target.endswith(relpath):
538
# The final part of the url has been renamed, we can't handle the
545
path) = self._split_url(target)
546
# Recalculate base path. This is needed to ensure that when the
547
# redirected tranport will be used to re-try whatever request was
548
# redirected, we end up with the same url
549
base_path = path[:-len(relpath)]
550
if scheme in ('http', 'https'):
551
# Same protocol family (i.e. http[s]), we will preserve the same
552
# http client implementation when a redirection occurs from one to
553
# the other (otherwise users may be surprised that bzr switches
554
# from one implementation to the other, and devs may suffer
556
if (scheme == self._unqualified_scheme
557
and host == self._host
558
and port == self._port
559
and (user is None or user == self._user)):
560
# If a user is specified, it should match, we don't care about
561
# passwords, wrong passwords will be rejected anyway.
562
new_transport = self.clone(base_path)
564
# Rebuild the url preserving the scheme qualification and the
565
# credentials (if they don't apply, the redirected to server
566
# will tell us, but if they do apply, we avoid prompting the
568
redir_scheme = scheme + '+' + self._impl_name
569
new_url = self._unsplit_url(redir_scheme,
570
self._user, self._password,
573
new_transport = get_transport(new_url)
575
# Redirected to a different protocol
576
new_url = self._unsplit_url(scheme,
580
new_transport = get_transport(new_url)
584
# TODO: May be better located in smart/medium.py with the other
585
# SmartMedium classes
586
class SmartClientHTTPMedium(medium.SmartClientMedium):
588
def __init__(self, http_transport):
589
super(SmartClientHTTPMedium, self).__init__(http_transport.base)
590
# We don't want to create a circular reference between the http
591
# transport and its associated medium. Since the transport will live
592
# longer than the medium, the medium keep only a weak reference to its
594
self._http_transport_ref = weakref.ref(http_transport)
596
def get_request(self):
597
return SmartClientHTTPMediumRequest(self)
599
def should_probe(self):
602
def remote_path_from_transport(self, transport):
603
# Strip the optional 'bzr+' prefix from transport so it will have the
604
# same scheme as self.
605
transport_base = transport.base
606
if transport_base.startswith('bzr+'):
607
transport_base = transport_base[4:]
608
rel_url = urlutils.relative_url(self.base, transport_base)
609
return urllib.unquote(rel_url)
611
def send_http_smart_request(self, bytes):
613
# Get back the http_transport hold by the weak reference
614
t = self._http_transport_ref()
615
code, body_filelike = t._post(bytes)
617
raise InvalidHttpResponse(
618
t._remote_path('.bzr/smart'),
619
'Expected 200 response code, got %r' % (code,))
620
except (errors.InvalidHttpResponse, errors.ConnectionReset), e:
621
raise errors.SmartProtocolError(str(e))
624
def _report_activity(self, bytes, direction):
625
"""See SmartMedium._report_activity.
627
Does nothing; the underlying plain HTTP transport will report the
628
activity that this medium would report.
633
# TODO: May be better located in smart/medium.py with the other
634
# SmartMediumRequest classes
635
class SmartClientHTTPMediumRequest(medium.SmartClientMediumRequest):
636
"""A SmartClientMediumRequest that works with an HTTP medium."""
638
def __init__(self, client_medium):
639
medium.SmartClientMediumRequest.__init__(self, client_medium)
642
def _accept_bytes(self, bytes):
643
self._buffer += bytes
645
def _finished_writing(self):
646
data = self._medium.send_http_smart_request(self._buffer)
647
self._response_body = data
649
def _read_bytes(self, count):
650
"""See SmartClientMediumRequest._read_bytes."""
651
return self._response_body.read(count)
653
def _read_line(self):
654
line, excess = medium._get_line(self._response_body.read)
656
raise AssertionError(
657
'_get_line returned excess bytes, but this mediumrequest '
658
'cannot handle excess. (%r)' % (excess,))
661
def _finished_reading(self):
662
"""See SmartClientMediumRequest._finished_reading."""
361
raise TransportNotPossible('http does not support lock_write()')
364
#---------------- test server facilities ----------------
365
import BaseHTTPServer, SimpleHTTPServer, socket, time
369
class WebserverNotAvailable(Exception):
373
class BadWebserverPath(ValueError):
375
return 'path %s is not in %s' % self.args
378
class TestingHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
380
def log_message(self, format, *args):
381
self.server.test_case.log('webserver - %s - - [%s] %s "%s" "%s"',
382
self.address_string(),
383
self.log_date_time_string(),
385
self.headers.get('referer', '-'),
386
self.headers.get('user-agent', '-'))
388
def handle_one_request(self):
389
"""Handle a single HTTP request.
391
You normally don't need to override this method; see the class
392
__doc__ string for information on how to handle specific HTTP
393
commands such as GET and POST.
396
for i in xrange(1,11): # Don't try more than 10 times
398
self.raw_requestline = self.rfile.readline()
399
except socket.error, e:
400
if e.args[0] in (errno.EAGAIN, errno.EWOULDBLOCK):
401
# omitted for now because some tests look at the log of
402
# the server and expect to see no errors. see recent
403
# email thread. -- mbp 20051021.
404
## self.log_message('EAGAIN (%d) while reading from raw_requestline' % i)
410
if not self.raw_requestline:
411
self.close_connection = 1
413
if not self.parse_request(): # An error code has been sent, just exit
415
mname = 'do_' + self.command
416
if not hasattr(self, mname):
417
self.send_error(501, "Unsupported method (%r)" % self.command)
419
method = getattr(self, mname)
423
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
424
def __init__(self, server_address, RequestHandlerClass, test_case):
425
BaseHTTPServer.HTTPServer.__init__(self, server_address,
427
self.test_case = test_case
430
class HttpServer(Server):
431
"""A test server for http transports."""
433
def _http_start(self):
435
httpd = TestingHTTPServer(('localhost', 0),
436
TestingHTTPRequestHandler,
438
host, port = httpd.socket.getsockname()
439
self._http_base_url = 'http://localhost:%s/' % port
440
self._http_starting.release()
441
httpd.socket.settimeout(0.1)
443
while self._http_running:
445
httpd.handle_request()
446
except socket.timeout:
449
def _get_remote_url(self, path):
450
path_parts = path.split(os.path.sep)
451
if os.path.isabs(path):
452
if path_parts[:len(self._local_path_parts)] != \
453
self._local_path_parts:
454
raise BadWebserverPath(path, self.test_dir)
455
remote_path = '/'.join(path_parts[len(self._local_path_parts):])
457
remote_path = '/'.join(path_parts)
459
self._http_starting.acquire()
460
self._http_starting.release()
461
return self._http_base_url + remote_path
463
def log(self, format, *args):
464
"""Capture Server log output."""
465
self.logs.append(format % args)
468
"""See bzrlib.transport.Server.setUp."""
469
self._home_dir = os.getcwdu()
470
self._local_path_parts = self._home_dir.split(os.path.sep)
471
self._http_starting = threading.Lock()
472
self._http_starting.acquire()
473
self._http_running = True
474
self._http_base_url = None
475
self._http_thread = threading.Thread(target=self._http_start)
476
self._http_thread.setDaemon(True)
477
self._http_thread.start()
478
self._http_proxy = os.environ.get("http_proxy")
479
if self._http_proxy is not None:
480
del os.environ["http_proxy"]
484
"""See bzrlib.transport.Server.tearDown."""
485
self._http_running = False
486
self._http_thread.join()
487
if self._http_proxy is not None:
489
os.environ["http_proxy"] = self._http_proxy
492
"""See bzrlib.transport.Server.get_url."""
493
return self._get_remote_url(self._home_dir)
495
def get_bogus_url(self):
496
"""See bzrlib.transport.Server.get_bogus_url."""
497
return 'http://jasldkjsalkdjalksjdkljasd'
500
def get_test_permutations():
501
"""Return the permutations to be used in testing."""
502
warn("There are no HTTPS transport provider tests yet.")
503
return [(HttpTransport, HttpServer),