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
import BaseHTTPServer, SimpleHTTPServer
17
import BaseHTTPServer, SimpleHTTPServer, socket, errno, time
18
18
from bzrlib.selftest import TestCaseInTempDir
28
28
class TestingHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
29
29
def log_message(self, format, *args):
30
self.server.test_case.log("webserver - %s - - [%s] %s\n" %
30
self.server.test_case.log("webserver - %s - - [%s] %s" %
31
31
(self.address_string(),
32
32
self.log_date_time_string(),
35
def handle_one_request(self):
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)
35
66
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
36
67
def __init__(self, server_address, RequestHandlerClass, test_case):
37
68
BaseHTTPServer.HTTPServer.__init__(self, server_address,
38
69
RequestHandlerClass)
39
70
self.test_case = test_case
41
73
class TestCaseWithWebserver(TestCaseInTempDir):
42
74
"""Derived class that starts a localhost-only webserver
43
75
(in addition to what TestCaseInTempDir does).
71
103
self._http_base_url = 'http://localhost:%s/' % port
72
104
self._http_starting.release()
73
httpd.socket.settimeout(1)
105
httpd.socket.settimeout(0.1)
75
107
while self._http_running:
95
127
return self._http_base_url + remote_path
98
super(TestCaseWithWebserver, self).setUp()
130
TestCaseInTempDir.setUp(self)
99
131
import threading, os
100
132
self._local_path_parts = self.test_dir.split(os.path.sep)
101
133
self._http_starting = threading.Lock()