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
19
from SimpleHTTPServer import SimpleHTTPRequestHandler
26
from bzrlib.osutils import md5
27
from bzrlib.smart import protocol
28
from bzrlib.tests import TestCaseWithTransport
29
from bzrlib.tests.http_server import (
31
TestingHTTPRequestHandler,
33
from bzrlib.transport import (
38
class WallRequestHandler(TestingHTTPRequestHandler):
39
"""Whatever request comes in, close the connection"""
41
def handle_one_request(self):
42
"""Handle a single HTTP request, by abruptly closing the connection"""
43
self.close_connection = 1
46
class BadStatusRequestHandler(TestingHTTPRequestHandler):
47
"""Whatever request comes in, returns a bad status"""
49
def parse_request(self):
50
"""Fakes handling a single HTTP request, returns a bad status"""
51
ignored = TestingHTTPRequestHandler.parse_request(self)
53
self.send_response(0, "Bad status")
55
except socket.error, e:
56
# We don't want to pollute the test results with
57
# spurious server errors while test succeed. In our
58
# case, it may occur that the test has already read
59
# the 'Bad Status' and closed the socket while we are
60
# still trying to send some headers... So the test is
61
# ok, but if we raise the exception, the output is
62
# dirty. So we don't raise, but we close the
63
# connection, just to be safe :)
64
spurious = [errno.EPIPE,
68
if (len(e.args) > 0) and (e.args[0] in spurious):
69
self.close_connection = 1
76
class InvalidStatusRequestHandler(TestingHTTPRequestHandler):
77
"""Whatever request comes in, returns am invalid status"""
79
def parse_request(self):
80
"""Fakes handling a single HTTP request, returns a bad status"""
81
ignored = TestingHTTPRequestHandler.parse_request(self)
82
self.wfile.write("Invalid status line\r\n")
86
class BadProtocolRequestHandler(TestingHTTPRequestHandler):
87
"""Whatever request comes in, returns a bad protocol version"""
89
def parse_request(self):
90
"""Fakes handling a single HTTP request, returns a bad status"""
91
ignored = TestingHTTPRequestHandler.parse_request(self)
92
# Returns an invalid protocol version, but curl just
93
# ignores it and those cannot be tested.
94
self.wfile.write("%s %d %s\r\n" % ('HTTP/0.0',
96
'Look at my protocol version'))
100
class ForbiddenRequestHandler(TestingHTTPRequestHandler):
101
"""Whatever request comes in, returns a 403 code"""
103
def parse_request(self):
104
"""Handle a single HTTP request, by replying we cannot handle it"""
105
ignored = TestingHTTPRequestHandler.parse_request(self)
110
class HTTPServerWithSmarts(HttpServer):
111
"""HTTPServerWithSmarts extends the HttpServer with POST methods that will
112
trigger a smart server to execute with a transport rooted at the rootdir of
117
HttpServer.__init__(self, SmartRequestHandler)
120
class SmartRequestHandler(TestingHTTPRequestHandler):
121
"""Extend TestingHTTPRequestHandler to support smart client POSTs."""
124
"""Hand the request off to a smart server instance."""
125
self.send_response(200)
126
self.send_header("Content-type", "application/octet-stream")
127
transport = get_transport(self.server.test_case_server._home_dir)
128
# TODO: We might like to support streaming responses. 1.0 allows no
129
# Content-length in this case, so for integrity we should perform our
130
# own chunking within the stream.
131
# 1.1 allows chunked responses, and in this case we could chunk using
132
# the HTTP chunking as this will allow HTTP persistence safely, even if
133
# we have to stop early due to error, but we would also have to use the
134
# HTTP trailer facility which may not be widely available.
135
out_buffer = StringIO()
136
smart_protocol_request = protocol.SmartServerRequestProtocolOne(
137
transport, out_buffer.write)
138
# if this fails, we should return 400 bad request, but failure is
139
# failure for now - RBC 20060919
140
data_length = int(self.headers['Content-Length'])
141
# Perhaps there should be a SmartServerHTTPMedium that takes care of
142
# feeding the bytes in the http request to the smart_protocol_request,
143
# but for now it's simpler to just feed the bytes directly.
144
smart_protocol_request.accept_bytes(self.rfile.read(data_length))
145
assert smart_protocol_request.next_read_size() == 0, (
146
"not finished reading, but all data sent to protocol.")
147
self.send_header("Content-Length", str(len(out_buffer.getvalue())))
149
self.wfile.write(out_buffer.getvalue())
152
class LimitedRangeRequestHandler(TestingHTTPRequestHandler):
153
"""Errors out when range specifiers exceed the limit"""
155
def get_multiple_ranges(self, file, file_size, ranges):
156
"""Refuses the multiple ranges request"""
157
tcs = self.server.test_case_server
158
if tcs.range_limit is not None and len(ranges) > tcs.range_limit:
160
# Emulate apache behavior
161
self.send_error(400, "Bad Request")
163
return TestingHTTPRequestHandler.get_multiple_ranges(self, file,
167
tcs = self.server.test_case_server
168
tcs.GET_request_nb += 1
169
return TestingHTTPRequestHandler.do_GET(self)
172
class LimitedRangeHTTPServer(HttpServer):
173
"""An HttpServer erroring out on requests with too much range specifiers"""
175
def __init__(self, request_handler=LimitedRangeRequestHandler,
177
HttpServer.__init__(self, request_handler)
178
self.range_limit = range_limit
179
self.GET_request_nb = 0
182
class SingleRangeRequestHandler(TestingHTTPRequestHandler):
183
"""Always reply to range request as if they were single.
185
Don't be explicit about it, just to annoy the clients.
188
def get_multiple_ranges(self, file, file_size, ranges):
189
"""Answer as if it was a single range request and ignores the rest"""
190
(start, end) = ranges[0]
191
return self.get_single_range(file, file_size, start, end)
194
class SingleOnlyRangeRequestHandler(TestingHTTPRequestHandler):
195
"""Only reply to simple range requests, errors out on multiple"""
197
def get_multiple_ranges(self, file, file_size, ranges):
198
"""Refuses the multiple ranges request"""
201
self.send_error(416, "Requested range not satisfiable")
203
(start, end) = ranges[0]
204
return self.get_single_range(file, file_size, start, end)
207
class NoRangeRequestHandler(TestingHTTPRequestHandler):
208
"""Ignore range requests without notice"""
210
# Just bypass the range handling done by TestingHTTPRequestHandler
211
do_GET = SimpleHTTPRequestHandler.do_GET
214
class TestCaseWithWebserver(TestCaseWithTransport):
215
"""A support class that provides readonly urls that are http://.
217
This is done by forcing the readonly server to be an http
218
one. This will currently fail if the primary transport is not
219
backed by regular disk files.
222
super(TestCaseWithWebserver, self).setUp()
223
self.transport_readonly_server = HttpServer
226
class TestCaseWithTwoWebservers(TestCaseWithWebserver):
227
"""A support class providing readonly urls on two servers that are http://.
229
We set up two webservers to allows various tests involving
230
proxies or redirections from one server to the other.
233
super(TestCaseWithTwoWebservers, self).setUp()
234
self.transport_secondary_server = HttpServer
235
self.__secondary_server = None
237
def create_transport_secondary_server(self):
238
"""Create a transport server from class defined at init.
240
This is mostly a hook for daughter classes.
242
return self.transport_secondary_server()
244
def get_secondary_server(self):
245
"""Get the server instance for the secondary transport."""
246
if self.__secondary_server is None:
247
self.__secondary_server = self.create_transport_secondary_server()
248
self.__secondary_server.setUp()
249
self.addCleanup(self.__secondary_server.tearDown)
250
return self.__secondary_server
253
class ProxyServer(HttpServer):
254
"""A proxy test server for http transports."""
256
proxy_requests = True
259
class RedirectRequestHandler(TestingHTTPRequestHandler):
260
"""Redirect all request to the specified server"""
262
def parse_request(self):
263
"""Redirect a single HTTP request to another host"""
264
valid = TestingHTTPRequestHandler.parse_request(self)
266
tcs = self.server.test_case_server
267
code, target = tcs.is_redirected(self.path)
268
if code is not None and target is not None:
269
# Redirect as instructed
270
self.send_response(code)
271
self.send_header('Location', target)
273
return False # The job is done
275
# We leave the parent class serve the request
280
class HTTPServerRedirecting(HttpServer):
281
"""An HttpServer redirecting to another server """
283
def __init__(self, request_handler=RedirectRequestHandler):
284
HttpServer.__init__(self, request_handler)
285
# redirections is a list of tuples (source, target, code)
286
# - source is a regexp for the paths requested
287
# - target is a replacement for re.sub describing where
288
# the request will be redirected
289
# - code is the http error code associated to the
290
# redirection (301 permanent, 302 temporarry, etc
291
self.redirections = []
293
def redirect_to(self, host, port):
294
"""Redirect all requests to a specific host:port"""
295
self.redirections = [('(.*)',
296
r'http://%s:%s\1' % (host, port) ,
299
def is_redirected(self, path):
300
"""Is the path redirected by this server.
302
:param path: the requested relative path
304
:returns: a tuple (code, target) if a matching
305
redirection is found, (None, None) otherwise.
309
for (rsource, rtarget, rcode) in self.redirections:
310
target, match = re.subn(rsource, rtarget, path)
313
break # The first match wins
319
class TestCaseWithRedirectedWebserver(TestCaseWithTwoWebservers):
320
"""A support class providing redirections from one server to another.
322
We set up two webservers to allows various tests involving
324
The 'old' server is redirected to the 'new' server.
327
def create_transport_secondary_server(self):
328
"""Create the secondary server redirecting to the primary server"""
329
new = self.get_readonly_server()
330
redirecting = HTTPServerRedirecting()
331
redirecting.redirect_to(new.host, new.port)
335
super(TestCaseWithRedirectedWebserver, self).setUp()
336
# The redirections will point to the new server
337
self.new_server = self.get_readonly_server()
338
# The requests to the old server will be redirected
339
self.old_server = self.get_secondary_server()
342
class AuthRequestHandler(TestingHTTPRequestHandler):
343
"""Requires an authentication to process requests.
345
This is intended to be used with a server that always and
346
only use one authentication scheme (implemented by daughter
350
# The following attributes should be defined in the server
351
# - auth_header_sent: the header name sent to require auth
352
# - auth_header_recv: the header received containing auth
353
# - auth_error_code: the error code to indicate auth required
356
if self.authorized():
357
return TestingHTTPRequestHandler.do_GET(self)
359
# Note that we must update test_case_server *before*
360
# sending the error or the client may try to read it
361
# before we have sent the whole error back.
362
tcs = self.server.test_case_server
363
tcs.auth_required_errors += 1
364
self.send_response(tcs.auth_error_code)
365
self.send_header_auth_reqed()
370
class BasicAuthRequestHandler(AuthRequestHandler):
371
"""Implements the basic authentication of a request"""
373
def authorized(self):
374
tcs = self.server.test_case_server
375
if tcs.auth_scheme != 'basic':
378
auth_header = self.headers.get(tcs.auth_header_recv, None)
380
scheme, raw_auth = auth_header.split(' ', 1)
381
if scheme.lower() == tcs.auth_scheme:
382
user, password = raw_auth.decode('base64').split(':')
383
return tcs.authorized(user, password)
387
def send_header_auth_reqed(self):
388
tcs = self.server.test_case_server
389
self.send_header(tcs.auth_header_sent,
390
'Basic realm="%s"' % tcs.auth_realm)
393
# FIXME: We could send an Authentication-Info header too when
394
# the authentication is succesful
396
class DigestAuthRequestHandler(AuthRequestHandler):
397
"""Implements the digest authentication of a request.
399
We need persistence for some attributes and that can't be
400
achieved here since we get instantiated for each request. We
401
rely on the DigestAuthServer to take care of them.
404
def authorized(self):
405
tcs = self.server.test_case_server
406
if tcs.auth_scheme != 'digest':
409
auth_header = self.headers.get(tcs.auth_header_recv, None)
410
if auth_header is None:
412
scheme, auth = auth_header.split(None, 1)
413
if scheme.lower() == tcs.auth_scheme:
414
auth_dict = urllib2.parse_keqv_list(urllib2.parse_http_list(auth))
416
return tcs.digest_authorized(auth_dict, self.command)
420
def send_header_auth_reqed(self):
421
tcs = self.server.test_case_server
422
header = 'Digest realm="%s", ' % tcs.auth_realm
423
header += 'nonce="%s", algorithm="%s", qop="auth"' % (tcs.auth_nonce,
425
self.send_header(tcs.auth_header_sent,header)
428
class AuthServer(HttpServer):
429
"""Extends HttpServer with a dictionary of passwords.
431
This is used as a base class for various schemes which should
432
all use or redefined the associated AuthRequestHandler.
434
Note that no users are defined by default, so add_user should
435
be called before issuing the first request.
438
# The following attributes should be set dy daughter classes
439
# and are used by AuthRequestHandler.
440
auth_header_sent = None
441
auth_header_recv = None
442
auth_error_code = None
443
auth_realm = "Thou should not pass"
445
def __init__(self, request_handler, auth_scheme):
446
HttpServer.__init__(self, request_handler)
447
self.auth_scheme = auth_scheme
448
self.password_of = {}
449
self.auth_required_errors = 0
451
def add_user(self, user, password):
452
"""Declare a user with an associated password.
454
password can be empty, use an empty string ('') in that
457
self.password_of[user] = password
459
def authorized(self, user, password):
460
"""Check that the given user provided the right password"""
461
expected_password = self.password_of.get(user, None)
462
return expected_password is not None and password == expected_password
465
# FIXME: There is some code duplication with
466
# _urllib2_wrappers.py.DigestAuthHandler. If that duplication
467
# grows, it may require a refactoring. Also, we don't implement
468
# SHA algorithm nor MD5-sess here, but that does not seem worth
470
class DigestAuthServer(AuthServer):
471
"""A digest authentication server"""
475
def __init__(self, request_handler, auth_scheme):
476
AuthServer.__init__(self, request_handler, auth_scheme)
478
def digest_authorized(self, auth, command):
479
nonce = auth['nonce']
480
if nonce != self.auth_nonce:
482
realm = auth['realm']
483
if realm != self.auth_realm:
485
user = auth['username']
486
if not self.password_of.has_key(user):
488
algorithm= auth['algorithm']
489
if algorithm != 'MD5':
495
password = self.password_of[user]
497
# Recalculate the response_digest to compare with the one
499
A1 = '%s:%s:%s' % (user, realm, password)
500
A2 = '%s:%s' % (command, auth['uri'])
502
H = lambda x: md5(x).hexdigest()
503
KD = lambda secret, data: H("%s:%s" % (secret, data))
505
nonce_count = int(auth['nc'], 16)
507
ncvalue = '%08x' % nonce_count
509
cnonce = auth['cnonce']
510
noncebit = '%s:%s:%s:%s:%s' % (nonce, ncvalue, cnonce, qop, H(A2))
511
response_digest = KD(H(A1), noncebit)
513
return response_digest == auth['response']
515
class HTTPAuthServer(AuthServer):
516
"""An HTTP server requiring authentication"""
518
def init_http_auth(self):
519
self.auth_header_sent = 'WWW-Authenticate'
520
self.auth_header_recv = 'Authorization'
521
self.auth_error_code = 401
524
class ProxyAuthServer(AuthServer):
525
"""A proxy server requiring authentication"""
527
def init_proxy_auth(self):
528
self.proxy_requests = True
529
self.auth_header_sent = 'Proxy-Authenticate'
530
self.auth_header_recv = 'Proxy-Authorization'
531
self.auth_error_code = 407
534
class HTTPBasicAuthServer(HTTPAuthServer):
535
"""An HTTP server requiring basic authentication"""
538
HTTPAuthServer.__init__(self, BasicAuthRequestHandler, 'basic')
539
self.init_http_auth()
542
class HTTPDigestAuthServer(DigestAuthServer, HTTPAuthServer):
543
"""An HTTP server requiring digest authentication"""
546
DigestAuthServer.__init__(self, DigestAuthRequestHandler, 'digest')
547
self.init_http_auth()
550
class ProxyBasicAuthServer(ProxyAuthServer):
551
"""A proxy server requiring basic authentication"""
554
ProxyAuthServer.__init__(self, BasicAuthRequestHandler, 'basic')
555
self.init_proxy_auth()
558
class ProxyDigestAuthServer(DigestAuthServer, ProxyAuthServer):
559
"""A proxy server requiring basic authentication"""
562
ProxyAuthServer.__init__(self, DigestAuthRequestHandler, 'digest')
563
self.init_proxy_auth()