1
# Copyright (C) 2006 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
20
from SimpleHTTPServer import SimpleHTTPRequestHandler
28
from bzrlib.transport import Server
31
class WebserverNotAvailable(Exception):
35
class BadWebserverPath(ValueError):
37
return 'path %s is not in %s' % self.args
40
class TestingHTTPRequestHandler(SimpleHTTPRequestHandler):
42
def log_message(self, format, *args):
43
self.server.test_case.log('webserver - %s - - [%s] %s "%s" "%s"',
44
self.address_string(),
45
self.log_date_time_string(),
47
self.headers.get('referer', '-'),
48
self.headers.get('user-agent', '-'))
50
def handle_one_request(self):
51
"""Handle a single HTTP request.
53
You normally don't need to override this method; see the class
54
__doc__ string for information on how to handle specific HTTP
55
commands such as GET and POST.
58
for i in xrange(1,11): # Don't try more than 10 times
60
self.raw_requestline = self.rfile.readline()
61
except socket.error, e:
62
if e.args[0] in (errno.EAGAIN, errno.EWOULDBLOCK):
63
# omitted for now because some tests look at the log of
64
# the server and expect to see no errors. see recent
65
# email thread. -- mbp 20051021.
66
## self.log_message('EAGAIN (%d) while reading from raw_requestline' % i)
72
if not self.raw_requestline:
73
self.close_connection = 1
75
if not self.parse_request(): # An error code has been sent, just exit
77
mname = 'do_' + self.command
78
if getattr(self, mname, None) is None:
79
self.send_error(501, "Unsupported method (%r)" % self.command)
81
method = getattr(self, mname)
84
_range_regexp = re.compile(r'^(?P<start>\d+)-(?P<end>\d+)$')
85
_tail_regexp = re.compile(r'^-(?P<tail>\d+)$')
87
def parse_ranges(self, ranges_header):
88
"""Parse the range header value and returns ranges and tail"""
91
assert ranges_header.startswith('bytes=')
92
ranges_header = ranges_header[len('bytes='):]
93
for range_str in ranges_header.split(','):
94
range_match = self._range_regexp.match(range_str)
95
if range_match is not None:
96
ranges.append((int(range_match.group('start')),
97
int(range_match.group('end'))))
99
tail_match = self._tail_regexp.match(range_str)
100
if tail_match is not None:
101
tail = int(tail_match.group('tail'))
104
def send_range_content(self, file, start, length):
106
self.wfile.write(file.read(length))
108
def get_single_range(self, file, file_size, start, end):
109
self.send_response(206)
110
length = end - start + 1
111
self.send_header('Accept-Ranges', 'bytes')
112
self.send_header("Content-Length", "%d" % length)
114
self.send_header("Content-Type", 'application/octet-stream')
115
self.send_header("Content-Range", "bytes %d-%d/%d" % (start,
119
self.send_range_content(file, start, length)
121
def get_multiple_ranges(self, file, file_size, ranges):
122
self.send_response(206)
123
self.send_header('Accept-Ranges', 'bytes')
124
boundary = "%d" % random.randint(0,0x7FFFFFFF)
125
self.send_header("Content-Type",
126
"multipart/byteranges; boundary=%s" % boundary)
128
for (start, end) in ranges:
129
self.wfile.write("--%s\r\n" % boundary)
130
self.send_header("Content-type", 'application/octet-stream')
131
self.send_header("Content-Range", "bytes %d-%d/%d" % (start,
135
self.send_range_content(file, start, end - start + 1)
136
self.wfile.write("--%s\r\n" % boundary)
140
"""Serve a GET request.
142
Handles the Range header.
145
path = self.translate_path(self.path)
146
ranges_header_value = self.headers.get('Range')
147
if ranges_header_value is None or os.path.isdir(path):
148
# Let the mother class handle most cases
149
return SimpleHTTPRequestHandler.do_GET(self)
152
# Always read in binary mode. Opening files in text
153
# mode may cause newline translations, making the
154
# actual size of the content transmitted *less* than
155
# the content-length!
156
file = open(path, 'rb')
158
self.send_error(404, "File not found")
161
file_size = os.fstat(file.fileno())[6]
162
tail, ranges = self.parse_ranges(ranges_header_value)
163
# Normalize tail into ranges
165
ranges.append((file_size - tail, file_size))
171
for (start, end) in ranges:
172
if start >= file_size or end >= file_size:
176
# RFC2616 14-16 says that invalid Range headers
177
# should be ignored and in that case, the whole file
178
# should be returned as if no Range header was
180
file.close() # Will be reopened by the following call
181
return SimpleHTTPRequestHandler.do_GET(self)
184
(start, end) = ranges[0]
185
self.get_single_range(file, file_size, start, end)
187
self.get_multiple_ranges(file, file_size, ranges)
190
if sys.platform == 'win32':
191
# On win32 you cannot access non-ascii filenames without
192
# decoding them into unicode first.
193
# However, under Linux, you can access bytestream paths
194
# without any problems. If this function was always active
195
# it would probably break tests when LANG=C was set
196
def translate_path(self, path):
197
"""Translate a /-separated PATH to the local filename syntax.
199
For bzr, all url paths are considered to be utf8 paths.
200
On Linux, you can access these paths directly over the bytestream
201
request, but on win32, you must decode them, and access them
204
# abandon query parameters
205
path = urlparse.urlparse(path)[2]
206
path = posixpath.normpath(urllib.unquote(path))
207
path = path.decode('utf-8')
208
words = path.split('/')
209
words = filter(None, words)
212
drive, word = os.path.splitdrive(word)
213
head, word = os.path.split(word)
214
if word in (os.curdir, os.pardir): continue
215
path = os.path.join(path, word)
219
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
220
def __init__(self, server_address, RequestHandlerClass, test_case):
221
BaseHTTPServer.HTTPServer.__init__(self, server_address,
223
self.test_case = test_case
226
class HttpServer(Server):
227
"""A test server for http transports.
229
Subclasses can provide a specific request handler.
232
# used to form the url that connects to this server
233
_url_protocol = 'http'
235
# Subclasses can provide a specific request handler
236
def __init__(self, request_handler=TestingHTTPRequestHandler):
237
Server.__init__(self)
238
self.request_handler = request_handler
240
def _get_httpd(self):
241
return TestingHTTPServer(('localhost', 0),
242
self.request_handler,
245
def _http_start(self):
247
httpd = self._get_httpd()
248
host, port = httpd.socket.getsockname()
249
self._http_base_url = '%s://localhost:%s/' % (self._url_protocol, port)
250
self._http_starting.release()
251
httpd.socket.settimeout(0.1)
253
while self._http_running:
255
httpd.handle_request()
256
except socket.timeout:
259
def _get_remote_url(self, path):
260
path_parts = path.split(os.path.sep)
261
if os.path.isabs(path):
262
if path_parts[:len(self._local_path_parts)] != \
263
self._local_path_parts:
264
raise BadWebserverPath(path, self.test_dir)
265
remote_path = '/'.join(path_parts[len(self._local_path_parts):])
267
remote_path = '/'.join(path_parts)
269
self._http_starting.acquire()
270
self._http_starting.release()
271
return self._http_base_url + remote_path
273
def log(self, format, *args):
274
"""Capture Server log output."""
275
self.logs.append(format % args)
278
"""See bzrlib.transport.Server.setUp."""
279
self._home_dir = os.getcwdu()
280
self._local_path_parts = self._home_dir.split(os.path.sep)
281
self._http_starting = threading.Lock()
282
self._http_starting.acquire()
283
self._http_running = True
284
self._http_base_url = None
285
self._http_thread = threading.Thread(target=self._http_start)
286
self._http_thread.setDaemon(True)
287
self._http_thread.start()
288
self._http_proxy = os.environ.get("http_proxy")
289
if self._http_proxy is not None:
290
del os.environ["http_proxy"]
294
"""See bzrlib.transport.Server.tearDown."""
295
self._http_running = False
296
self._http_thread.join()
297
if self._http_proxy is not None:
299
os.environ["http_proxy"] = self._http_proxy
302
"""See bzrlib.transport.Server.get_url."""
303
return self._get_remote_url(self._home_dir)
305
def get_bogus_url(self):
306
"""See bzrlib.transport.Server.get_bogus_url."""
307
# this is chosen to try to prevent trouble with proxies, weird dns,
309
return 'http://127.0.0.1:1/'
312
class HttpServer_urllib(HttpServer):
313
"""Subclass of HttpServer that gives http+urllib urls.
315
This is for use in testing: connections to this server will always go
316
through urllib where possible.
319
# urls returned by this server should require the urllib client impl
320
_url_protocol = 'http+urllib'
323
class HttpServer_PyCurl(HttpServer):
324
"""Subclass of HttpServer that gives http+pycurl urls.
326
This is for use in testing: connections to this server will always go
327
through pycurl where possible.
330
# We don't care about checking the pycurl availability as
331
# this server will be required only when pycurl is present
333
# urls returned by this server should require the pycurl client impl
334
_url_protocol = 'http+pycurl'