1
# Copyright (C) 2005 Canonical Ltd
 
 
3
# This program is free software; you can redistribute it and/or modify
 
 
4
# it under the terms of the GNU General Public License as published by
 
 
5
# the Free Software Foundation; either version 2 of the License, or
 
 
6
# (at your option) any later version.
 
 
8
# This program is distributed in the hope that it will be useful,
 
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
# GNU General Public License for more details.
 
 
13
# You should have received a copy of the GNU General Public License
 
 
14
# along with this program; if not, write to the Free Software
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
17
from cStringIO import StringIO
 
 
20
from SimpleHTTPServer import SimpleHTTPRequestHandler
 
 
28
from bzrlib.smart import protocol
 
 
29
from bzrlib.tests import TestCaseWithTransport
 
 
30
from bzrlib.tests.HttpServer import (
 
 
32
    TestingHTTPRequestHandler,
 
 
34
from bzrlib.transport import (
 
 
39
class WallRequestHandler(TestingHTTPRequestHandler):
 
 
40
    """Whatever request comes in, close the connection"""
 
 
42
    def handle_one_request(self):
 
 
43
        """Handle a single HTTP request, by abruptly closing the connection"""
 
 
44
        self.close_connection = 1
 
 
47
class BadStatusRequestHandler(TestingHTTPRequestHandler):
 
 
48
    """Whatever request comes in, returns a bad status"""
 
 
50
    def parse_request(self):
 
 
51
        """Fakes handling a single HTTP request, returns a bad status"""
 
 
52
        ignored = TestingHTTPRequestHandler.parse_request(self)
 
 
54
            self.send_response(0, "Bad status")
 
 
56
        except socket.error, e:
 
 
57
            # We don't want to pollute the test results with
 
 
58
            # spurious server errors while test succeed. In our
 
 
59
            # case, it may occur that the test has already read
 
 
60
            # the 'Bad Status' and closed the socket while we are
 
 
61
            # still trying to send some headers... So the test is
 
 
62
            # ok, but if we raise the exception, the output is
 
 
63
            # dirty. So we don't raise, but we close the
 
 
64
            # connection, just to be safe :)
 
 
65
            spurious = [errno.EPIPE,
 
 
69
            if (len(e.args) > 0) and (e.args[0] in spurious):
 
 
70
                self.close_connection = 1
 
 
77
class InvalidStatusRequestHandler(TestingHTTPRequestHandler):
 
 
78
    """Whatever request comes in, returns am invalid status"""
 
 
80
    def parse_request(self):
 
 
81
        """Fakes handling a single HTTP request, returns a bad status"""
 
 
82
        ignored = TestingHTTPRequestHandler.parse_request(self)
 
 
83
        self.wfile.write("Invalid status line\r\n")
 
 
87
class BadProtocolRequestHandler(TestingHTTPRequestHandler):
 
 
88
    """Whatever request comes in, returns a bad protocol version"""
 
 
90
    def parse_request(self):
 
 
91
        """Fakes handling a single HTTP request, returns a bad status"""
 
 
92
        ignored = TestingHTTPRequestHandler.parse_request(self)
 
 
93
        # Returns an invalid protocol version, but curl just
 
 
94
        # ignores it and those cannot be tested.
 
 
95
        self.wfile.write("%s %d %s\r\n" % ('HTTP/0.0',
 
 
97
                                           'Look at my protocol version'))
 
 
101
class ForbiddenRequestHandler(TestingHTTPRequestHandler):
 
 
102
    """Whatever request comes in, returns a 403 code"""
 
 
104
    def parse_request(self):
 
 
105
        """Handle a single HTTP request, by replying we cannot handle it"""
 
 
106
        ignored = TestingHTTPRequestHandler.parse_request(self)
 
 
111
class HTTPServerWithSmarts(HttpServer):
 
 
112
    """HTTPServerWithSmarts extends the HttpServer with POST methods that will
 
 
113
    trigger a smart server to execute with a transport rooted at the rootdir of
 
 
118
        HttpServer.__init__(self, SmartRequestHandler)
 
 
121
class SmartRequestHandler(TestingHTTPRequestHandler):
 
 
122
    """Extend TestingHTTPRequestHandler to support smart client POSTs."""
 
 
125
        """Hand the request off to a smart server instance."""
 
 
126
        self.send_response(200)
 
 
127
        self.send_header("Content-type", "application/octet-stream")
 
 
128
        transport = get_transport(self.server.test_case_server._home_dir)
 
 
129
        # TODO: We might like to support streaming responses.  1.0 allows no
 
 
130
        # Content-length in this case, so for integrity we should perform our
 
 
131
        # own chunking within the stream.
 
 
132
        # 1.1 allows chunked responses, and in this case we could chunk using
 
 
133
        # the HTTP chunking as this will allow HTTP persistence safely, even if
 
 
134
        # we have to stop early due to error, but we would also have to use the
 
 
135
        # HTTP trailer facility which may not be widely available.
 
 
136
        out_buffer = StringIO()
 
 
137
        smart_protocol_request = protocol.SmartServerRequestProtocolOne(
 
 
138
                transport, out_buffer.write)
 
 
139
        # if this fails, we should return 400 bad request, but failure is
 
 
140
        # failure for now - RBC 20060919
 
 
141
        data_length = int(self.headers['Content-Length'])
 
 
142
        # Perhaps there should be a SmartServerHTTPMedium that takes care of
 
 
143
        # feeding the bytes in the http request to the smart_protocol_request,
 
 
144
        # but for now it's simpler to just feed the bytes directly.
 
 
145
        smart_protocol_request.accept_bytes(self.rfile.read(data_length))
 
 
146
        assert smart_protocol_request.next_read_size() == 0, (
 
 
147
            "not finished reading, but all data sent to protocol.")
 
 
148
        self.send_header("Content-Length", str(len(out_buffer.getvalue())))
 
 
150
        self.wfile.write(out_buffer.getvalue())
 
 
153
class SingleRangeRequestHandler(TestingHTTPRequestHandler):
 
 
154
    """Always reply to range request as if they were single.
 
 
156
    Don't be explicit about it, just to annoy the clients.
 
 
159
    def get_multiple_ranges(self, file, file_size, ranges):
 
 
160
        """Answer as if it was a single range request and ignores the rest"""
 
 
161
        (start, end) = ranges[0]
 
 
162
        return self.get_single_range(file, file_size, start, end)
 
 
165
class SingleOnlyRangeRequestHandler(TestingHTTPRequestHandler):
 
 
166
    """Only reply to simple range requests, errors out on multiple"""
 
 
168
    def get_multiple_ranges(self, file, file_size, ranges):
 
 
169
        """Refuses the multiple ranges request"""
 
 
172
            self.send_error(416, "Requested range not satisfiable")
 
 
174
        (start, end) = ranges[0]
 
 
175
        return self.get_single_range(file, file_size, start, end)
 
 
178
class NoRangeRequestHandler(TestingHTTPRequestHandler):
 
 
179
    """Ignore range requests without notice"""
 
 
181
    # Just bypass the range handling done by TestingHTTPRequestHandler
 
 
182
    do_GET = SimpleHTTPRequestHandler.do_GET
 
 
185
class TestCaseWithWebserver(TestCaseWithTransport):
 
 
186
    """A support class that provides readonly urls that are http://.
 
 
188
    This is done by forcing the readonly server to be an http
 
 
189
    one. This will currently fail if the primary transport is not
 
 
190
    backed by regular disk files.
 
 
193
        super(TestCaseWithWebserver, self).setUp()
 
 
194
        self.transport_readonly_server = HttpServer
 
 
197
class TestCaseWithTwoWebservers(TestCaseWithWebserver):
 
 
198
    """A support class providing readonly urls on two servers that are http://.
 
 
200
    We set up two webservers to allows various tests involving
 
 
201
    proxies or redirections from one server to the other.
 
 
204
        super(TestCaseWithTwoWebservers, self).setUp()
 
 
205
        self.transport_secondary_server = HttpServer
 
 
206
        self.__secondary_server = None
 
 
208
    def create_transport_secondary_server(self):
 
 
209
        """Create a transport server from class defined at init.
 
 
211
        This is mostly a hook for daughter classes.
 
 
213
        return self.transport_secondary_server()
 
 
215
    def get_secondary_server(self):
 
 
216
        """Get the server instance for the secondary transport."""
 
 
217
        if self.__secondary_server is None:
 
 
218
            self.__secondary_server = self.create_transport_secondary_server()
 
 
219
            self.__secondary_server.setUp()
 
 
220
            self.addCleanup(self.__secondary_server.tearDown)
 
 
221
        return self.__secondary_server
 
 
224
class ProxyServer(HttpServer):
 
 
225
    """A proxy test server for http transports."""
 
 
227
    proxy_requests = True
 
 
230
class RedirectRequestHandler(TestingHTTPRequestHandler):
 
 
231
    """Redirect all request to the specified server"""
 
 
233
    def parse_request(self):
 
 
234
        """Redirect a single HTTP request to another host"""
 
 
235
        valid = TestingHTTPRequestHandler.parse_request(self)
 
 
237
            tcs = self.server.test_case_server
 
 
238
            code, target = tcs.is_redirected(self.path)
 
 
239
            if code is not None and target is not None:
 
 
240
                # Redirect as instructed
 
 
241
                self.send_response(code)
 
 
242
                self.send_header('Location', target)
 
 
244
                return False # The job is done
 
 
246
                # We leave the parent class serve the request
 
 
251
class HTTPServerRedirecting(HttpServer):
 
 
252
    """An HttpServer redirecting to another server """
 
 
254
    def __init__(self, request_handler=RedirectRequestHandler):
 
 
255
        HttpServer.__init__(self, request_handler)
 
 
256
        # redirections is a list of tuples (source, target, code)
 
 
257
        # - source is a regexp for the paths requested
 
 
258
        # - target is a replacement for re.sub describing where
 
 
259
        #   the request will be redirected
 
 
260
        # - code is the http error code associated to the
 
 
261
        #   redirection (301 permanent, 302 temporarry, etc
 
 
262
        self.redirections = []
 
 
264
    def redirect_to(self, host, port):
 
 
265
        """Redirect all requests to a specific host:port"""
 
 
266
        self.redirections = [('(.*)',
 
 
267
                              r'http://%s:%s\1' % (host, port) ,
 
 
270
    def is_redirected(self, path):
 
 
271
        """Is the path redirected by this server.
 
 
273
        :param path: the requested relative path
 
 
275
        :returns: a tuple (code, target) if a matching
 
 
276
             redirection is found, (None, None) otherwise.
 
 
280
        for (rsource, rtarget, rcode) in self.redirections:
 
 
281
            target, match = re.subn(rsource, rtarget, path)
 
 
284
                break # The first match wins
 
 
290
class TestCaseWithRedirectedWebserver(TestCaseWithTwoWebservers):
 
 
291
   """A support class providing redirections from one server to another.
 
 
293
   We set up two webservers to allows various tests involving
 
 
295
   The 'old' server is redirected to the 'new' server.
 
 
298
   def create_transport_secondary_server(self):
 
 
299
       """Create the secondary server redirecting to the primary server"""
 
 
300
       new = self.get_readonly_server()
 
 
301
       redirecting = HTTPServerRedirecting()
 
 
302
       redirecting.redirect_to(new.host, new.port)
 
 
306
       super(TestCaseWithRedirectedWebserver, self).setUp()
 
 
307
       # The redirections will point to the new server
 
 
308
       self.new_server = self.get_readonly_server()
 
 
309
       # The requests to the old server will be redirected
 
 
310
       self.old_server = self.get_secondary_server()
 
 
313
class AuthRequestHandler(TestingHTTPRequestHandler):
 
 
314
    """Requires an authentication to process requests.
 
 
316
    This is intended to be used with a server that always and
 
 
317
    only use one authentication scheme (implemented by daughter
 
 
321
    # The following attributes should be defined in the server
 
 
322
    # - auth_header_sent: the header name sent to require auth
 
 
323
    # - auth_header_recv: the header received containing auth
 
 
324
    # - auth_error_code: the error code to indicate auth required
 
 
327
        if self.authorized():
 
 
328
            return TestingHTTPRequestHandler.do_GET(self)
 
 
330
            # Note that we must update test_case_server *before*
 
 
331
            # sending the error or the client may try to read it
 
 
332
            # before we have sent the whole error back.
 
 
333
            tcs = self.server.test_case_server
 
 
334
            tcs.auth_required_errors += 1
 
 
335
            self.send_response(tcs.auth_error_code)
 
 
336
            self.send_header_auth_reqed()
 
 
340
        TestingHTTPRequestHandler.do_GET(self)
 
 
343
class BasicAuthRequestHandler(AuthRequestHandler):
 
 
344
    """Implements the basic authentication of a request"""
 
 
346
    def authorized(self):
 
 
347
        tcs = self.server.test_case_server
 
 
348
        if tcs.auth_scheme != 'basic':
 
 
351
        auth_header = self.headers.get(tcs.auth_header_recv, None)
 
 
353
            scheme, raw_auth = auth_header.split(' ', 1)
 
 
354
            if scheme.lower() == tcs.auth_scheme:
 
 
355
                user, password = raw_auth.decode('base64').split(':')
 
 
356
                return tcs.authorized(user, password)
 
 
360
    def send_header_auth_reqed(self):
 
 
361
        tcs = self.server.test_case_server
 
 
362
        self.send_header(tcs.auth_header_sent,
 
 
363
                         'Basic realm="%s"' % tcs.auth_realm)
 
 
366
# FIXME: We could send an Authentication-Info header too when
 
 
367
# the authentication is succesful
 
 
369
class DigestAuthRequestHandler(AuthRequestHandler):
 
 
370
    """Implements the digest authentication of a request.
 
 
372
    We need persistence for some attributes and that can't be
 
 
373
    achieved here since we get instantiated for each request. We
 
 
374
    rely on the DigestAuthServer to take care of them.
 
 
377
    def authorized(self):
 
 
378
        tcs = self.server.test_case_server
 
 
379
        if tcs.auth_scheme != 'digest':
 
 
382
        auth_header = self.headers.get(tcs.auth_header_recv, None)
 
 
383
        if auth_header is None:
 
 
385
        scheme, auth = auth_header.split(None, 1)
 
 
386
        if scheme.lower() == tcs.auth_scheme:
 
 
387
            auth_dict = urllib2.parse_keqv_list(urllib2.parse_http_list(auth))
 
 
389
            return tcs.digest_authorized(auth_dict, self.command)
 
 
393
    def send_header_auth_reqed(self):
 
 
394
        tcs = self.server.test_case_server
 
 
395
        header = 'Digest realm="%s", ' % tcs.auth_realm
 
 
396
        header += 'nonce="%s", algorithm="%s", qop="auth"' % (tcs.auth_nonce,
 
 
398
        self.send_header(tcs.auth_header_sent,header)
 
 
401
class AuthServer(HttpServer):
 
 
402
    """Extends HttpServer with a dictionary of passwords.
 
 
404
    This is used as a base class for various schemes which should
 
 
405
    all use or redefined the associated AuthRequestHandler.
 
 
407
    Note that no users are defined by default, so add_user should
 
 
408
    be called before issuing the first request.
 
 
411
    # The following attributes should be set dy daughter classes
 
 
412
    # and are used by AuthRequestHandler.
 
 
413
    auth_header_sent = None
 
 
414
    auth_header_recv = None
 
 
415
    auth_error_code = None
 
 
416
    auth_realm = "Thou should not pass"
 
 
418
    def __init__(self, request_handler, auth_scheme):
 
 
419
        HttpServer.__init__(self, request_handler)
 
 
420
        self.auth_scheme = auth_scheme
 
 
421
        self.password_of = {}
 
 
422
        self.auth_required_errors = 0
 
 
424
    def add_user(self, user, password):
 
 
425
        """Declare a user with an associated password.
 
 
427
        password can be empty, use an empty string ('') in that
 
 
430
        self.password_of[user] = password
 
 
432
    def authorized(self, user, password):
 
 
433
        """Check that the given user provided the right password"""
 
 
434
        expected_password = self.password_of.get(user, None)
 
 
435
        return expected_password is not None and password == expected_password
 
 
438
# FIXME: There is some code duplication with
 
 
439
# _urllib2_wrappers.py.DigestAuthHandler. If that duplciation
 
 
440
# grows, it may require a refactoring. Also, we don't implement
 
 
441
# SHA algorithm nor MD5-sess here, but that does not seem worth
 
 
443
class DigestAuthServer(AuthServer):
 
 
444
    """A digest authentication server"""
 
 
448
    def __init__(self, request_handler, auth_scheme):
 
 
449
        AuthServer.__init__(self, request_handler, auth_scheme)
 
 
451
    def digest_authorized(self, auth, command):
 
 
452
        nonce = auth['nonce']
 
 
453
        if nonce != self.auth_nonce:
 
 
455
        realm = auth['realm']
 
 
456
        if realm != self.auth_realm:
 
 
458
        user = auth['username']
 
 
459
        if not self.password_of.has_key(user):
 
 
461
        algorithm= auth['algorithm']
 
 
462
        if algorithm != 'MD5':
 
 
468
        password = self.password_of[user]
 
 
470
        # Recalculate the response_digest to compare with the one
 
 
472
        A1 = '%s:%s:%s' % (user, realm, password)
 
 
473
        A2 = '%s:%s' % (command, auth['uri'])
 
 
475
        H = lambda x: md5.new(x).hexdigest()
 
 
476
        KD = lambda secret, data: H("%s:%s" % (secret, data))
 
 
478
        nonce_count = int(auth['nc'], 16)
 
 
480
        ncvalue = '%08x' % nonce_count
 
 
482
        cnonce = auth['cnonce']
 
 
483
        noncebit = '%s:%s:%s:%s:%s' % (nonce, ncvalue, cnonce, qop, H(A2))
 
 
484
        response_digest = KD(H(A1), noncebit)
 
 
486
        return response_digest == auth['response']
 
 
488
class HTTPAuthServer(AuthServer):
 
 
489
    """An HTTP server requiring authentication"""
 
 
491
    def init_http_auth(self):
 
 
492
        self.auth_header_sent = 'WWW-Authenticate'
 
 
493
        self.auth_header_recv = 'Authorization'
 
 
494
        self.auth_error_code = 401
 
 
497
class ProxyAuthServer(AuthServer):
 
 
498
    """A proxy server requiring authentication"""
 
 
500
    def init_proxy_auth(self):
 
 
501
        self.proxy_requests = True
 
 
502
        self.auth_header_sent = 'Proxy-Authenticate'
 
 
503
        self.auth_header_recv = 'Proxy-Authorization'
 
 
504
        self.auth_error_code = 407
 
 
507
class HTTPBasicAuthServer(HTTPAuthServer):
 
 
508
    """An HTTP server requiring basic authentication"""
 
 
511
        HTTPAuthServer.__init__(self, BasicAuthRequestHandler, 'basic')
 
 
512
        self.init_http_auth()
 
 
515
class HTTPDigestAuthServer(DigestAuthServer, HTTPAuthServer):
 
 
516
    """An HTTP server requiring digest authentication"""
 
 
519
        DigestAuthServer.__init__(self, DigestAuthRequestHandler, 'digest')
 
 
520
        self.init_http_auth()
 
 
523
class ProxyBasicAuthServer(ProxyAuthServer):
 
 
524
    """A proxy server requiring basic authentication"""
 
 
527
        ProxyAuthServer.__init__(self, BasicAuthRequestHandler, 'basic')
 
 
528
        self.init_proxy_auth()
 
 
531
class ProxyDigestAuthServer(DigestAuthServer, ProxyAuthServer):
 
 
532
    """A proxy server requiring basic authentication"""
 
 
535
        ProxyAuthServer.__init__(self, DigestAuthRequestHandler, 'digest')
 
 
536
        self.init_proxy_auth()