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
18
from bzrlib.tests import TestCaseInTempDir
17
import BaseHTTPServer, SimpleHTTPServer
18
from bzrlib.selftest import TestCaseInTempDir
21
21
class WebserverNotAvailable(Exception):
28
28
class TestingHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
29
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(),
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] in (errno.EAGAIN, errno.EWOULDBLOCK):
48
# omitted for now because some tests look at the log of
49
# the server and expect to see no errors. see recent
50
# email thread. -- mbp 20051021.
51
## self.log_message('EAGAIN (%d) while reading from raw_requestline' % i)
57
if not self.raw_requestline:
58
self.close_connection = 1
60
if not self.parse_request(): # An error code has been sent, just exit
62
mname = 'do_' + self.command
63
if not hasattr(self, mname):
64
self.send_error(501, "Unsupported method (%r)" % self.command)
66
method = getattr(self, mname)
30
self.server.test_case.log("webserver - %s - - [%s] %s\n" %
31
(self.address_string(),
32
self.log_date_time_string(),
69
35
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
70
36
def __init__(self, server_address, RequestHandlerClass, test_case):
106
71
self._http_base_url = 'http://localhost:%s/' % port
107
72
self._http_starting.release()
108
httpd.socket.settimeout(0.1)
73
httpd.socket.settimeout(1)
110
75
while self._http_running:
130
95
return self._http_base_url + remote_path
133
TestCaseInTempDir.setUp(self)
98
super(TestCaseWithWebserver, self).setUp()
134
99
import threading, os
135
100
self._local_path_parts = self.test_dir.split(os.path.sep)
136
101
self._http_starting = threading.Lock()
140
105
self._http_thread = threading.Thread(target=self._http_start)
141
106
self._http_thread.setDaemon(True)
142
107
self._http_thread.start()
143
self._http_proxy = os.environ.get("http_proxy")
144
if self._http_proxy is not None:
145
del os.environ["http_proxy"]
147
109
def tearDown(self):
148
110
self._http_running = False
149
111
self._http_thread.join()
150
if self._http_proxy is not None:
152
os.environ["http_proxy"] = self._http_proxy
153
TestCaseInTempDir.tearDown(self)
112
super(TestCaseWithWebserver, self).tearDown()