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
29
from bzrlib.smart import protocol
30
from bzrlib.tests import TestCaseWithTransport
31
from bzrlib.tests.http_server import (
33
TestingHTTPRequestHandler,
35
from bzrlib.transport import (
40
class WallRequestHandler(TestingHTTPRequestHandler):
41
"""Whatever request comes in, close the connection"""
43
def handle_one_request(self):
44
"""Handle a single HTTP request, by abruptly closing the connection"""
45
self.close_connection = 1
48
class BadStatusRequestHandler(TestingHTTPRequestHandler):
49
"""Whatever request comes in, returns a bad status"""
51
def parse_request(self):
52
"""Fakes handling a single HTTP request, returns a bad status"""
53
ignored = TestingHTTPRequestHandler.parse_request(self)
55
self.send_response(0, "Bad status")
57
except socket.error, e:
58
# We don't want to pollute the test results with
59
# spurious server errors while test succeed. In our
60
# case, it may occur that the test has already read
61
# the 'Bad Status' and closed the socket while we are
62
# still trying to send some headers... So the test is
63
# ok, but if we raise the exception, the output is
64
# dirty. So we don't raise, but we close the
65
# connection, just to be safe :)
66
spurious = [errno.EPIPE,
70
if (len(e.args) > 0) and (e.args[0] in spurious):
71
self.close_connection = 1
78
class InvalidStatusRequestHandler(TestingHTTPRequestHandler):
79
"""Whatever request comes in, returns am invalid status"""
81
def parse_request(self):
82
"""Fakes handling a single HTTP request, returns a bad status"""
83
ignored = TestingHTTPRequestHandler.parse_request(self)
84
self.wfile.write("Invalid status line\r\n")
88
class BadProtocolRequestHandler(TestingHTTPRequestHandler):
89
"""Whatever request comes in, returns a bad protocol version"""
91
def parse_request(self):
92
"""Fakes handling a single HTTP request, returns a bad status"""
93
ignored = TestingHTTPRequestHandler.parse_request(self)
94
# Returns an invalid protocol version, but curl just
95
# ignores it and those cannot be tested.
96
self.wfile.write("%s %d %s\r\n" % ('HTTP/0.0',
98
'Look at my protocol version'))
102
class ForbiddenRequestHandler(TestingHTTPRequestHandler):
103
"""Whatever request comes in, returns a 403 code"""
105
def parse_request(self):
106
"""Handle a single HTTP request, by replying we cannot handle it"""
107
ignored = TestingHTTPRequestHandler.parse_request(self)
112
class HTTPServerWithSmarts(HttpServer):
113
"""HTTPServerWithSmarts extends the HttpServer with POST methods that will
114
trigger a smart server to execute with a transport rooted at the rootdir of
119
HttpServer.__init__(self, SmartRequestHandler)
122
class SmartRequestHandler(TestingHTTPRequestHandler):
123
"""Extend TestingHTTPRequestHandler to support smart client POSTs."""
126
"""Hand the request off to a smart server instance."""
127
self.send_response(200)
128
self.send_header("Content-type", "application/octet-stream")
129
transport = get_transport(self.server.test_case_server._home_dir)
130
# TODO: We might like to support streaming responses. 1.0 allows no
131
# Content-length in this case, so for integrity we should perform our
132
# own chunking within the stream.
133
# 1.1 allows chunked responses, and in this case we could chunk using
134
# the HTTP chunking as this will allow HTTP persistence safely, even if
135
# we have to stop early due to error, but we would also have to use the
136
# HTTP trailer facility which may not be widely available.
137
out_buffer = StringIO()
138
smart_protocol_request = protocol.SmartServerRequestProtocolOne(
139
transport, out_buffer.write)
140
# if this fails, we should return 400 bad request, but failure is
141
# failure for now - RBC 20060919
142
data_length = int(self.headers['Content-Length'])
143
# Perhaps there should be a SmartServerHTTPMedium that takes care of
144
# feeding the bytes in the http request to the smart_protocol_request,
145
# but for now it's simpler to just feed the bytes directly.
146
smart_protocol_request.accept_bytes(self.rfile.read(data_length))
147
assert smart_protocol_request.next_read_size() == 0, (
148
"not finished reading, but all data sent to protocol.")
149
self.send_header("Content-Length", str(len(out_buffer.getvalue())))
151
self.wfile.write(out_buffer.getvalue())
154
class LimitedRangeRequestHandler(TestingHTTPRequestHandler):
155
"""Errors out when range specifiers exceed the limit"""
157
def get_multiple_ranges(self, file, file_size, ranges):
158
"""Refuses the multiple ranges request"""
159
tcs = self.server.test_case_server
160
if tcs.range_limit is not None and len(ranges) > tcs.range_limit:
162
# Emulate apache behavior
163
self.send_error(400, "Bad Request")
165
return TestingHTTPRequestHandler.get_multiple_ranges(self, file,
169
class LimitedRangeHTTPServer(HttpServer):
170
"""An HttpServer erroring out on requests with too much range specifiers"""
172
def __init__(self, request_handler=LimitedRangeRequestHandler,
174
HttpServer.__init__(self, request_handler)
175
self.range_limit = range_limit
178
class SingleRangeRequestHandler(TestingHTTPRequestHandler):
179
"""Always reply to range request as if they were single.
181
Don't be explicit about it, just to annoy the clients.
184
def get_multiple_ranges(self, file, file_size, ranges):
185
"""Answer as if it was a single range request and ignores the rest"""
186
(start, end) = ranges[0]
187
return self.get_single_range(file, file_size, start, end)
190
class SingleOnlyRangeRequestHandler(TestingHTTPRequestHandler):
191
"""Only reply to simple range requests, errors out on multiple"""
193
def get_multiple_ranges(self, file, file_size, ranges):
194
"""Refuses the multiple ranges request"""
197
self.send_error(416, "Requested range not satisfiable")
199
(start, end) = ranges[0]
200
return self.get_single_range(file, file_size, start, end)
203
class NoRangeRequestHandler(TestingHTTPRequestHandler):
204
"""Ignore range requests without notice"""
207
# Update the statistics
208
self.server.test_case_server.GET_request_nb += 1
209
# Just bypass the range handling done by TestingHTTPRequestHandler
210
return SimpleHTTPRequestHandler.do_GET(self)
213
class TestCaseWithWebserver(TestCaseWithTransport):
214
"""A support class that provides readonly urls that are http://.
216
This is done by forcing the readonly server to be an http
217
one. This will currently fail if the primary transport is not
218
backed by regular disk files.
221
super(TestCaseWithWebserver, self).setUp()
222
self.transport_readonly_server = HttpServer
225
class TestCaseWithTwoWebservers(TestCaseWithWebserver):
226
"""A support class providing readonly urls on two servers that are http://.
228
We set up two webservers to allows various tests involving
229
proxies or redirections from one server to the other.
232
super(TestCaseWithTwoWebservers, self).setUp()
233
self.transport_secondary_server = HttpServer
234
self.__secondary_server = None
236
def create_transport_secondary_server(self):
237
"""Create a transport server from class defined at init.
239
This is mostly a hook for daughter classes.
241
return self.transport_secondary_server()
243
def get_secondary_server(self):
244
"""Get the server instance for the secondary transport."""
245
if self.__secondary_server is None:
246
self.__secondary_server = self.create_transport_secondary_server()
247
self.__secondary_server.setUp()
248
self.addCleanup(self.__secondary_server.tearDown)
249
return self.__secondary_server
252
class ProxyServer(HttpServer):
253
"""A proxy test server for http transports."""
255
proxy_requests = True
258
class RedirectRequestHandler(TestingHTTPRequestHandler):
259
"""Redirect all request to the specified server"""
261
def parse_request(self):
262
"""Redirect a single HTTP request to another host"""
263
valid = TestingHTTPRequestHandler.parse_request(self)
265
tcs = self.server.test_case_server
266
code, target = tcs.is_redirected(self.path)
267
if code is not None and target is not None:
268
# Redirect as instructed
269
self.send_response(code)
270
self.send_header('Location', target)
272
return False # The job is done
274
# We leave the parent class serve the request
279
class HTTPServerRedirecting(HttpServer):
280
"""An HttpServer redirecting to another server """
282
def __init__(self, request_handler=RedirectRequestHandler):
283
HttpServer.__init__(self, request_handler)
284
# redirections is a list of tuples (source, target, code)
285
# - source is a regexp for the paths requested
286
# - target is a replacement for re.sub describing where
287
# the request will be redirected
288
# - code is the http error code associated to the
289
# redirection (301 permanent, 302 temporarry, etc
290
self.redirections = []
292
def redirect_to(self, host, port):
293
"""Redirect all requests to a specific host:port"""
294
self.redirections = [('(.*)',
295
r'http://%s:%s\1' % (host, port) ,
298
def is_redirected(self, path):
299
"""Is the path redirected by this server.
301
:param path: the requested relative path
303
:returns: a tuple (code, target) if a matching
304
redirection is found, (None, None) otherwise.
308
for (rsource, rtarget, rcode) in self.redirections:
309
target, match = re.subn(rsource, rtarget, path)
312
break # The first match wins
318
class TestCaseWithRedirectedWebserver(TestCaseWithTwoWebservers):
319
"""A support class providing redirections from one server to another.
321
We set up two webservers to allows various tests involving
323
The 'old' server is redirected to the 'new' server.
326
def create_transport_secondary_server(self):
327
"""Create the secondary server redirecting to the primary server"""
328
new = self.get_readonly_server()
329
redirecting = HTTPServerRedirecting()
330
redirecting.redirect_to(new.host, new.port)
334
super(TestCaseWithRedirectedWebserver, self).setUp()
335
# The redirections will point to the new server
336
self.new_server = self.get_readonly_server()
337
# The requests to the old server will be redirected
338
self.old_server = self.get_secondary_server()
341
class AuthRequestHandler(TestingHTTPRequestHandler):
342
"""Requires an authentication to process requests.
344
This is intended to be used with a server that always and
345
only use one authentication scheme (implemented by daughter
349
# The following attributes should be defined in the server
350
# - auth_header_sent: the header name sent to require auth
351
# - auth_header_recv: the header received containing auth
352
# - auth_error_code: the error code to indicate auth required
355
if self.authorized():
356
return TestingHTTPRequestHandler.do_GET(self)
358
# Note that we must update test_case_server *before*
359
# sending the error or the client may try to read it
360
# before we have sent the whole error back.
361
tcs = self.server.test_case_server
362
tcs.auth_required_errors += 1
363
self.send_response(tcs.auth_error_code)
364
self.send_header_auth_reqed()
369
class BasicAuthRequestHandler(AuthRequestHandler):
370
"""Implements the basic authentication of a request"""
372
def authorized(self):
373
tcs = self.server.test_case_server
374
if tcs.auth_scheme != 'basic':
377
auth_header = self.headers.get(tcs.auth_header_recv, None)
379
scheme, raw_auth = auth_header.split(' ', 1)
380
if scheme.lower() == tcs.auth_scheme:
381
user, password = raw_auth.decode('base64').split(':')
382
return tcs.authorized(user, password)
386
def send_header_auth_reqed(self):
387
tcs = self.server.test_case_server
388
self.send_header(tcs.auth_header_sent,
389
'Basic realm="%s"' % tcs.auth_realm)
392
# FIXME: We could send an Authentication-Info header too when
393
# the authentication is succesful
395
class DigestAuthRequestHandler(AuthRequestHandler):
396
"""Implements the digest authentication of a request.
398
We need persistence for some attributes and that can't be
399
achieved here since we get instantiated for each request. We
400
rely on the DigestAuthServer to take care of them.
403
def authorized(self):
404
tcs = self.server.test_case_server
405
if tcs.auth_scheme != 'digest':
408
auth_header = self.headers.get(tcs.auth_header_recv, None)
409
if auth_header is None:
411
scheme, auth = auth_header.split(None, 1)
412
if scheme.lower() == tcs.auth_scheme:
413
auth_dict = urllib2.parse_keqv_list(urllib2.parse_http_list(auth))
415
return tcs.digest_authorized(auth_dict, self.command)
419
def send_header_auth_reqed(self):
420
tcs = self.server.test_case_server
421
header = 'Digest realm="%s", ' % tcs.auth_realm
422
header += 'nonce="%s", algorithm="%s", qop="auth"' % (tcs.auth_nonce,
424
self.send_header(tcs.auth_header_sent,header)
427
class AuthServer(HttpServer):
428
"""Extends HttpServer with a dictionary of passwords.
430
This is used as a base class for various schemes which should
431
all use or redefined the associated AuthRequestHandler.
433
Note that no users are defined by default, so add_user should
434
be called before issuing the first request.
437
# The following attributes should be set dy daughter classes
438
# and are used by AuthRequestHandler.
439
auth_header_sent = None
440
auth_header_recv = None
441
auth_error_code = None
442
auth_realm = "Thou should not pass"
444
def __init__(self, request_handler, auth_scheme):
445
HttpServer.__init__(self, request_handler)
446
self.auth_scheme = auth_scheme
447
self.password_of = {}
448
self.auth_required_errors = 0
450
def add_user(self, user, password):
451
"""Declare a user with an associated password.
453
password can be empty, use an empty string ('') in that
456
self.password_of[user] = password
458
def authorized(self, user, password):
459
"""Check that the given user provided the right password"""
460
expected_password = self.password_of.get(user, None)
461
return expected_password is not None and password == expected_password
464
# FIXME: There is some code duplication with
465
# _urllib2_wrappers.py.DigestAuthHandler. If that duplication
466
# grows, it may require a refactoring. Also, we don't implement
467
# SHA algorithm nor MD5-sess here, but that does not seem worth
469
class DigestAuthServer(AuthServer):
470
"""A digest authentication server"""
474
def __init__(self, request_handler, auth_scheme):
475
AuthServer.__init__(self, request_handler, auth_scheme)
477
def digest_authorized(self, auth, command):
478
nonce = auth['nonce']
479
if nonce != self.auth_nonce:
481
realm = auth['realm']
482
if realm != self.auth_realm:
484
user = auth['username']
485
if not self.password_of.has_key(user):
487
algorithm= auth['algorithm']
488
if algorithm != 'MD5':
494
password = self.password_of[user]
496
# Recalculate the response_digest to compare with the one
498
A1 = '%s:%s:%s' % (user, realm, password)
499
A2 = '%s:%s' % (command, auth['uri'])
501
H = lambda x: md5.new(x).hexdigest()
502
KD = lambda secret, data: H("%s:%s" % (secret, data))
504
nonce_count = int(auth['nc'], 16)
506
ncvalue = '%08x' % nonce_count
508
cnonce = auth['cnonce']
509
noncebit = '%s:%s:%s:%s:%s' % (nonce, ncvalue, cnonce, qop, H(A2))
510
response_digest = KD(H(A1), noncebit)
512
return response_digest == auth['response']
514
class HTTPAuthServer(AuthServer):
515
"""An HTTP server requiring authentication"""
517
def init_http_auth(self):
518
self.auth_header_sent = 'WWW-Authenticate'
519
self.auth_header_recv = 'Authorization'
520
self.auth_error_code = 401
523
class ProxyAuthServer(AuthServer):
524
"""A proxy server requiring authentication"""
526
def init_proxy_auth(self):
527
self.proxy_requests = True
528
self.auth_header_sent = 'Proxy-Authenticate'
529
self.auth_header_recv = 'Proxy-Authorization'
530
self.auth_error_code = 407
533
class HTTPBasicAuthServer(HTTPAuthServer):
534
"""An HTTP server requiring basic authentication"""
537
HTTPAuthServer.__init__(self, BasicAuthRequestHandler, 'basic')
538
self.init_http_auth()
541
class HTTPDigestAuthServer(DigestAuthServer, HTTPAuthServer):
542
"""An HTTP server requiring digest authentication"""
545
DigestAuthServer.__init__(self, DigestAuthRequestHandler, 'digest')
546
self.init_http_auth()
549
class ProxyBasicAuthServer(ProxyAuthServer):
550
"""A proxy server requiring basic authentication"""
553
ProxyAuthServer.__init__(self, BasicAuthRequestHandler, 'basic')
554
self.init_proxy_auth()
557
class ProxyDigestAuthServer(DigestAuthServer, ProxyAuthServer):
558
"""A proxy server requiring basic authentication"""
561
ProxyAuthServer.__init__(self, DigestAuthRequestHandler, 'digest')
562
self.init_proxy_auth()
565
class RecordingServer(object):
566
"""A fake HTTP server.
568
It records the bytes sent to it, and replies with a 200.
571
def __init__(self, expect_body_tail=None):
574
:type expect_body_tail: str
575
:param expect_body_tail: a reply won't be sent until this string is
578
self._expect_body_tail = expect_body_tail
581
self.received_bytes = ''
584
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
585
self._sock.bind(('127.0.0.1', 0))
586
self.host, self.port = self._sock.getsockname()
587
self._ready = threading.Event()
588
self._thread = threading.Thread(target=self._accept_read_and_reply)
589
self._thread.setDaemon(True)
593
def _accept_read_and_reply(self):
596
self._sock.settimeout(5)
598
conn, address = self._sock.accept()
599
# On win32, the accepted connection will be non-blocking to start
600
# with because we're using settimeout.
601
conn.setblocking(True)
602
while not self.received_bytes.endswith(self._expect_body_tail):
603
self.received_bytes += conn.recv(4096)
604
conn.sendall('HTTP/1.1 200 OK\r\n')
605
except socket.timeout:
606
# Make sure the client isn't stuck waiting for us to e.g. accept.
609
# The client may have already closed the socket.
616
# We might have already closed it. We don't care.