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, socket, errno, time
17
import BaseHTTPServer, SimpleHTTPServer
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" %
30
self.server.test_case.log("webserver - %s - - [%s] %s\n" %
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)
66
35
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
67
36
def __init__(self, server_address, RequestHandlerClass, test_case):
68
37
BaseHTTPServer.HTTPServer.__init__(self, server_address,
69
38
RequestHandlerClass)
70
39
self.test_case = test_case
73
41
class TestCaseWithWebserver(TestCaseInTempDir):
74
42
"""Derived class that starts a localhost-only webserver
75
43
(in addition to what TestCaseInTempDir does).
103
71
self._http_base_url = 'http://localhost:%s/' % port
104
72
self._http_starting.release()
105
httpd.socket.settimeout(0.1)
73
httpd.socket.settimeout(1)
107
75
while self._http_running:
127
95
return self._http_base_url + remote_path
130
TestCaseInTempDir.setUp(self)
98
super(TestCaseWithWebserver, self).setUp()
131
99
import threading, os
132
100
self._local_path_parts = self.test_dir.split(os.path.sep)
133
101
self._http_starting = threading.Lock()
137
105
self._http_thread = threading.Thread(target=self._http_start)
138
106
self._http_thread.setDaemon(True)
139
107
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"]
144
109
def tearDown(self):
145
110
self._http_running = False
146
111
self._http_thread.join()
147
if self._http_proxy is not None:
149
os.environ["http_proxy"] = self._http_proxy
150
TestCaseInTempDir.tearDown(self)
112
super(TestCaseWithWebserver, self).tearDown()