32
32
from bzrlib.errors import BzrError, BzrCheckError
33
33
from bzrlib.branch import Branch
34
34
from bzrlib.trace import mutter
36
35
# TODO: load these only when running http tests
37
36
import BaseHTTPServer, SimpleHTTPServer, socket, time
38
from bzrlib.ui import ui_factory
41
41
def extract_auth(url, password_manager):
44
44
password manager. Return the url, minus those auth parameters (which
47
assert url.startswith('http://') or url.startswith('https://')
48
scheme, host = url.split('//', 1)
50
host, path = host.split('/', 1)
56
auth, host = host.split('@', 1)
47
scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
48
assert (scheme == 'http') or (scheme == 'https')
51
auth, netloc = netloc.split('@', 1)
58
53
username, password = auth.split(':', 1)
60
55
username, password = auth, None
62
host, port = host.split(':', 1)
64
# FIXME: if password isn't given, should we ask for it?
57
host = netloc.split(':', 1)[0]
60
username = urllib.unquote(username)
65
61
if password is not None:
66
username = urllib.unquote(username)
67
62
password = urllib.unquote(password)
68
password_manager.add_password(None, host, username, password)
69
url = scheme + '//' + host + port + path
64
password = ui_factory.get_password(prompt='HTTP %(user)@%(host) password',
65
user=username, host=host)
66
password_manager.add_password(None, host, username, password)
67
url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
125
123
return urlparse.urlunparse((self._proto,
126
124
self._host, path, '', '', ''))
126
def get(self, relpath):
127
raise NotImplementedError("has() is abstract on %r" % self)
129
def has(self, relpath):
130
raise NotImplementedError("has() is abstract on %r" % self)
128
132
def stat(self, relpath):
129
133
"""Return the stat information for a file.
266
270
class TestingHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
268
272
def log_message(self, format, *args):
269
self.server.test_case.log("webserver - %s - - [%s] %s",
273
self.server.test_case.log('webserver - %s - - [%s] %s "%s" "%s"',
270
274
self.address_string(),
271
275
self.log_date_time_string(),
277
self.headers.get('referer', '-'),
278
self.headers.get('user-agent', '-'))
274
280
def handle_one_request(self):
275
281
"""Handle a single HTTP request.
305
311
method = getattr(self, mname)
308
315
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
309
316
def __init__(self, server_address, RequestHandlerClass, test_case):
310
317
BaseHTTPServer.HTTPServer.__init__(self, server_address,
315
322
class HttpServer(Server):
316
323
"""A test server for http transports."""
318
_HTTP_PORTS = range(13000, 0x8000)
320
325
def _http_start(self):
322
for port in self._HTTP_PORTS:
324
httpd = TestingHTTPServer(('localhost', port),
325
TestingHTTPRequestHandler,
327
except socket.error, e:
328
if e.args[0] == errno.EADDRINUSE:
330
print >>sys.stderr, "Cannot run webserver :-("
336
raise WebserverNotAvailable("Cannot run webserver :-( "
337
"no free ports in range %s..%s" %
338
(_HTTP_PORTS[0], _HTTP_PORTS[-1]))
327
httpd = TestingHTTPServer(('localhost', 0),
328
TestingHTTPRequestHandler,
330
host, port = httpd.socket.getsockname()
340
331
self._http_base_url = 'http://localhost:%s/' % port
341
332
self._http_starting.release()
342
333
httpd.socket.settimeout(0.1)
361
352
self._http_starting.release()
362
353
return self._http_base_url + remote_path
364
def log(self, *args, **kwargs):
355
def log(self, format, *args):
365
356
"""Capture Server log output."""
366
self.logs.append(args[3])
357
self.logs.append(format % args)
369
360
"""See bzrlib.transport.Server.setUp."""