1
# Copyright (C) 2005 Canonical Ltd
1
# Copyright (C) 2005 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from cStringIO import StringIO
19
from SimpleHTTPServer import SimpleHTTPRequestHandler
23
from bzrlib.tests import TestCaseWithTransport
24
from bzrlib.tests.HttpServer import (
26
TestingHTTPRequestHandler,
28
from bzrlib.transport import (
34
class WallRequestHandler(TestingHTTPRequestHandler):
35
"""Whatever request comes in, close the connection"""
17
import BaseHTTPServer, SimpleHTTPServer, socket, errno, time
18
from bzrlib.selftest import TestCaseInTempDir
21
class WebserverNotAvailable(Exception):
24
class BadWebserverPath(ValueError):
26
return 'path %s is not in %s' % self.args
28
class TestingHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
29
def log_message(self, format, *args):
30
self.server.test_case.log("webserver - %s - - [%s] %s" %
31
(self.address_string(),
32
self.log_date_time_string(),
37
35
def handle_one_request(self):
38
"""Handle a single HTTP request, by abruptly closing the connection"""
39
self.close_connection = 1
42
class BadStatusRequestHandler(TestingHTTPRequestHandler):
43
"""Whatever request comes in, returns a bad status"""
45
def parse_request(self):
46
"""Fakes handling a single HTTP request, returns a bad status"""
47
ignored = TestingHTTPRequestHandler.parse_request(self)
49
self.send_response(0, "Bad status")
51
except socket.error, e:
52
# We don't want to pollute the test results with
53
# spurious server errors while test succeed. In our
54
# case, it may occur that the test has already read
55
# the 'Bad Status' and closed the socket while we are
56
# still trying to send some headers... So the test is
57
# ok, but if we raise the exception, the output is
58
# dirty. So we don't raise, but we close the
59
# connection, just to be safe :)
60
spurious = [errno.EPIPE,
64
if (len(e.args) > 0) and (e.args[0] in spurious):
65
self.close_connection = 1
36
"""Handle a single HTTP request.
38
You normally don't need to override this method; see the class
39
__doc__ string for information on how to handle specific HTTP
40
commands such as GET and POST.
43
for i in xrange(1,11): # Don't try more than 10 times
45
self.raw_requestline = self.rfile.readline()
46
except socket.error, e:
47
if e.args[0] == errno.EAGAIN:
48
self.log_message('EAGAIN (%d) while reading from raw_requestline' % i)
54
if not self.raw_requestline:
55
self.close_connection = 1
57
if not self.parse_request(): # An error code has been sent, just exit
59
mname = 'do_' + self.command
60
if not hasattr(self, mname):
61
self.send_error(501, "Unsupported method (%r)" % self.command)
63
method = getattr(self, mname)
66
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
67
def __init__(self, server_address, RequestHandlerClass, test_case):
68
BaseHTTPServer.HTTPServer.__init__(self, server_address,
70
self.test_case = test_case
73
class TestCaseWithWebserver(TestCaseInTempDir):
74
"""Derived class that starts a localhost-only webserver
75
(in addition to what TestCaseInTempDir does).
77
This is useful for testing RemoteBranch.
80
_HTTP_PORTS = range(13000, 0x8000)
82
def _http_start(self):
83
import SimpleHTTPServer, BaseHTTPServer, socket, errno
85
for port in self._HTTP_PORTS:
87
httpd = TestingHTTPServer(('localhost', port),
88
TestingHTTPRequestHandler,
90
except socket.error, e:
91
if e.args[0] == errno.EADDRINUSE:
93
print >>sys.stderr, "Cannot run webserver :-("
99
raise WebserverNotAvailable("Cannot run webserver :-( "
100
"no free ports in range %s..%s" %
101
(_HTTP_PORTS[0], _HTTP_PORTS[-1]))
103
self._http_base_url = 'http://localhost:%s/' % port
104
self._http_starting.release()
105
httpd.socket.settimeout(0.1)
107
while self._http_running:
109
httpd.handle_request()
110
except socket.timeout:
72
class InvalidStatusRequestHandler(TestingHTTPRequestHandler):
73
"""Whatever request comes in, returns am invalid status"""
75
def parse_request(self):
76
"""Fakes handling a single HTTP request, returns a bad status"""
77
ignored = TestingHTTPRequestHandler.parse_request(self)
78
self.wfile.write("Invalid status line\r\n")
82
class BadProtocolRequestHandler(TestingHTTPRequestHandler):
83
"""Whatever request comes in, returns a bad protocol version"""
85
def parse_request(self):
86
"""Fakes handling a single HTTP request, returns a bad status"""
87
ignored = TestingHTTPRequestHandler.parse_request(self)
88
# Returns an invalid protocol version, but curl just
89
# ignores it and those cannot be tested.
90
self.wfile.write("%s %d %s\r\n" % ('HTTP/0.0',
92
'Look at my protocol version'))
96
class ForbiddenRequestHandler(TestingHTTPRequestHandler):
97
"""Whatever request comes in, returns a 403 code"""
99
def parse_request(self):
100
"""Handle a single HTTP request, by replying we cannot handle it"""
101
ignored = TestingHTTPRequestHandler.parse_request(self)
106
class HTTPServerWithSmarts(HttpServer):
107
"""HTTPServerWithSmarts extends the HttpServer with POST methods that will
108
trigger a smart server to execute with a transport rooted at the rootdir of
113
HttpServer.__init__(self, SmartRequestHandler)
116
class SmartRequestHandler(TestingHTTPRequestHandler):
117
"""Extend TestingHTTPRequestHandler to support smart client POSTs."""
120
"""Hand the request off to a smart server instance."""
121
self.send_response(200)
122
self.send_header("Content-type", "application/octet-stream")
123
transport = get_transport(self.server.test_case._home_dir)
124
# TODO: We might like to support streaming responses. 1.0 allows no
125
# Content-length in this case, so for integrity we should perform our
126
# own chunking within the stream.
127
# 1.1 allows chunked responses, and in this case we could chunk using
128
# the HTTP chunking as this will allow HTTP persistence safely, even if
129
# we have to stop early due to error, but we would also have to use the
130
# HTTP trailer facility which may not be widely available.
131
out_buffer = StringIO()
132
smart_protocol_request = smart.SmartServerRequestProtocolOne(
133
transport, out_buffer.write)
134
# if this fails, we should return 400 bad request, but failure is
135
# failure for now - RBC 20060919
136
data_length = int(self.headers['Content-Length'])
137
# Perhaps there should be a SmartServerHTTPMedium that takes care of
138
# feeding the bytes in the http request to the smart_protocol_request,
139
# but for now it's simpler to just feed the bytes directly.
140
smart_protocol_request.accept_bytes(self.rfile.read(data_length))
141
assert smart_protocol_request.next_read_size() == 0, (
142
"not finished reading, but all data sent to protocol.")
143
self.send_header("Content-Length", str(len(out_buffer.getvalue())))
145
self.wfile.write(out_buffer.getvalue())
148
class SingleRangeRequestHandler(TestingHTTPRequestHandler):
149
"""Always reply to range request as if they were single.
151
Don't be explicit about it, just to annoy the clients.
154
def get_multiple_ranges(self, file, file_size, ranges):
155
"""Answer as if it was a single range request and ignores the rest"""
156
(start, end) = ranges[0]
157
return self.get_single_range(file, file_size, start, end)
160
class NoRangeRequestHandler(TestingHTTPRequestHandler):
161
"""Ignore range requests without notice"""
163
# Just bypass the range handling done by TestingHTTPRequestHandler
164
do_GET = SimpleHTTPRequestHandler.do_GET
167
class TestCaseWithWebserver(TestCaseWithTransport):
168
"""A support class that provides readonly urls that are http://.
170
This is done by forcing the readonly server to be an http
171
one. This will currently fail if the primary transport is not
172
backed by regular disk files.
175
super(TestCaseWithWebserver, self).setUp()
176
self.transport_readonly_server = HttpServer
179
class TestCaseWithTwoWebservers(TestCaseWithWebserver):
180
"""A support class providinf readonly urls (on two servers) that are http://.
182
We setup two webservers to allows various tests involving
183
proxies or redirections from one server to the other.
186
super(TestCaseWithTwoWebservers, self).setUp()
187
self.transport_secondary_server = HttpServer
188
self.__secondary_server = None
190
def create_transport_secondary_server(self):
191
"""Create a transport server from class defined at init.
193
This is mostly a hook for daughter classes.
195
return self.transport_secondary_server()
197
def get_secondary_server(self):
198
"""Get the server instance for the secondary transport."""
199
if self.__secondary_server is None:
200
self.__secondary_server = self.create_transport_secondary_server()
201
self.__secondary_server.setUp()
202
self.addCleanup(self.__secondary_server.tearDown)
203
return self.__secondary_server
206
class FakeProxyRequestHandler(TestingHTTPRequestHandler):
207
"""Append a '-proxied' suffix to file served"""
209
def translate_path(self, path):
210
# We need to act as a proxy and accept absolute urls,
211
# which SimpleHTTPRequestHandler (grand parent) is not
212
# ready for. So we just drop the protocol://host:port
213
# part in front of the request-url (because we know we
214
# would not forward the request to *another* proxy).
216
# So we do what SimpleHTTPRequestHandler.translate_path
217
# do beginning with python 2.4.3: abandon query
218
# parameters, scheme, host port, etc (which ensure we
219
# provide the right behaviour on all python versions).
220
path = urlparse.urlparse(path)[2]
221
# And now, we can apply *our* trick to proxy files
222
self.path += '-proxied'
223
# An finally we leave our mother class do whatever it
224
# wants with the path
225
return TestingHTTPRequestHandler.translate_path(self, path)
113
def get_remote_url(self, path):
116
path_parts = path.split(os.path.sep)
117
if os.path.isabs(path):
118
if path_parts[:len(self._local_path_parts)] != \
119
self._local_path_parts:
120
raise BadWebserverPath(path, self.test_dir)
121
remote_path = '/'.join(path_parts[len(self._local_path_parts):])
123
remote_path = '/'.join(path_parts)
125
self._http_starting.acquire()
126
self._http_starting.release()
127
return self._http_base_url + remote_path
130
TestCaseInTempDir.setUp(self)
132
self._local_path_parts = self.test_dir.split(os.path.sep)
133
self._http_starting = threading.Lock()
134
self._http_starting.acquire()
135
self._http_running = True
136
self._http_base_url = None
137
self._http_thread = threading.Thread(target=self._http_start)
138
self._http_thread.setDaemon(True)
139
self._http_thread.start()
140
self._http_proxy = os.environ.get("http_proxy")
141
if self._http_proxy is not None:
142
del os.environ["http_proxy"]
145
self._http_running = False
146
self._http_thread.join()
147
if self._http_proxy is not None:
149
os.environ["http_proxy"] = self._http_proxy
150
TestCaseInTempDir.tearDown(self)