~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2006-03-06 11:20:10 UTC
  • mfrom: (1593 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060306112010-17c0170dde5d1eea
[merge] large merge to sync with bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
from bzrlib.errors import BzrError, BzrCheckError
33
33
from bzrlib.branch import Branch
34
34
from bzrlib.trace import mutter
35
 
 
36
35
# TODO: load these only when running http tests
37
36
import BaseHTTPServer, SimpleHTTPServer, socket, time
38
37
import threading
 
38
from bzrlib.ui import ui_factory
39
39
 
40
40
 
41
41
def extract_auth(url, password_manager):
44
44
    password manager.  Return the url, minus those auth parameters (which
45
45
    confuse urllib2).
46
46
    """
47
 
    assert url.startswith('http://') or url.startswith('https://')
48
 
    scheme, host = url.split('//', 1)
49
 
    if '/' in host:
50
 
        host, path = host.split('/', 1)
51
 
        path = '/' + path
52
 
    else:
53
 
        path = ''
54
 
    port = ''
55
 
    if '@' in host:
56
 
        auth, host = host.split('@', 1)
 
47
    scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
 
48
    assert (scheme == 'http') or (scheme == 'https')
 
49
    
 
50
    if '@' in netloc:
 
51
        auth, netloc = netloc.split('@', 1)
57
52
        if ':' in auth:
58
53
            username, password = auth.split(':', 1)
59
54
        else:
60
55
            username, password = auth, None
61
 
        if ':' in host:
62
 
            host, port = host.split(':', 1)
63
 
            port = ':' + port
64
 
        # FIXME: if password isn't given, should we ask for it?
 
56
        if ':' in netloc:
 
57
            host = netloc.split(':', 1)[0]
 
58
        else:
 
59
            host = netloc
 
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
 
63
        else:
 
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))
70
68
    return url
71
69
    
72
70
 
125
123
        return urlparse.urlunparse((self._proto,
126
124
                self._host, path, '', '', ''))
127
125
 
 
126
    def get(self, relpath):
 
127
        raise NotImplementedError("has() is abstract on %r" % self)
 
128
 
 
129
    def has(self, relpath):
 
130
        raise NotImplementedError("has() is abstract on %r" % self)
 
131
 
128
132
    def stat(self, relpath):
129
133
        """Return the stat information for a file.
130
134
        """
266
270
class TestingHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
267
271
 
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(),
272
 
                                  format%args)
 
276
                                  format % args,
 
277
                                  self.headers.get('referer', '-'),
 
278
                                  self.headers.get('user-agent', '-'))
273
279
 
274
280
    def handle_one_request(self):
275
281
        """Handle a single HTTP request.
305
311
        method = getattr(self, mname)
306
312
        method()
307
313
 
 
314
 
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."""
317
324
 
318
 
    _HTTP_PORTS = range(13000, 0x8000)
319
 
 
320
325
    def _http_start(self):
321
326
        httpd = None
322
 
        for port in self._HTTP_PORTS:
323
 
            try:
324
 
                httpd = TestingHTTPServer(('localhost', port),
325
 
                                          TestingHTTPRequestHandler,
326
 
                                          self)
327
 
            except socket.error, e:
328
 
                if e.args[0] == errno.EADDRINUSE:
329
 
                    continue
330
 
                print >>sys.stderr, "Cannot run webserver :-("
331
 
                raise
332
 
            else:
333
 
                break
334
 
 
335
 
        if httpd is None:
336
 
            raise WebserverNotAvailable("Cannot run webserver :-( "
337
 
                                        "no free ports in range %s..%s" %
338
 
                                        (_HTTP_PORTS[0], _HTTP_PORTS[-1]))
339
 
 
 
327
        httpd = TestingHTTPServer(('localhost', 0),
 
328
                                  TestingHTTPRequestHandler,
 
329
                                  self)
 
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
363
354
 
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)
367
358
 
368
359
    def setUp(self):
369
360
        """See bzrlib.transport.Server.setUp."""
396
387
    def get_bogus_url(self):
397
388
        """See bzrlib.transport.Server.get_bogus_url."""
398
389
        return 'http://jasldkjsalkdjalksjdkljasd'
399
 
 
400