226
226
self._thread = threading.Thread(target=self._accept_read_and_reply)
227
227
self._thread.setDaemon(True)
228
228
self._thread.start()
229
if 'threads' in tests.selftest_debug_flags:
230
print 'Thread started: %s' % (self._thread.ident,)
231
233
def _accept_read_and_reply(self):
232
234
self._sock.listen(1)
235
self._sock.settimeout(5)
233
236
self._ready.set()
234
self._sock.settimeout(5)
236
238
conn, address = self._sock.accept()
237
239
# On win32, the accepted connection will be non-blocking to start
238
240
# with because we're using settimeout.
239
241
conn.setblocking(True)
240
while not self.received_bytes.endswith(self._expect_body_tail):
241
self.received_bytes += conn.recv(4096)
242
conn.sendall('HTTP/1.1 200 OK\r\n')
242
if self._expect_body_tail is not None:
243
while not self.received_bytes.endswith(self._expect_body_tail):
244
self.received_bytes += conn.recv(4096)
245
conn.sendall('HTTP/1.1 200 OK\r\n')
243
246
except socket.timeout:
244
247
# Make sure the client isn't stuck waiting for us to e.g. accept.
245
250
self._sock.close()
246
251
except socket.error:
247
252
# The client may have already closed the socket.
255
def connect_socket(self):
256
err = socket.error('getaddrinfo returns an empty list')
257
for res in socket.getaddrinfo(self.host, self.port):
258
af, socktype, proto, canonname, sa = res
261
sock = socket.socket(af, socktype, proto)
265
except socket.error, err:
266
# err is now the most recent error
250
271
def stop_server(self):
273
# Issue a fake connection to wake up the server and allow it to
275
fake_conn = self.connect_socket()
253
277
except socket.error:
254
278
# We might have already closed it. We don't care.
283
if 'threads' in tests.selftest_debug_flags:
284
print 'Thread joined: %s' % (self._thread.ident,)
260
287
class TestAuthHeader(tests.TestCase):
323
350
server = http_server.HttpServer()
324
351
server.start_server()
326
self.assertTrue(server._http_running)
353
self.assertTrue(server._httpd is not None)
354
self.assertTrue(server._httpd.serving is not None)
355
self.assertTrue(server._httpd.serving.isSet())
328
357
server.stop_server()
329
self.assertFalse(server._http_running)
331
359
def test_create_http_server_one_zero(self):
332
360
class RequestHandlerOneZero(http_server.TestingHTTPRequestHandler):
600
628
class WallRequestHandler(http_server.TestingHTTPRequestHandler):
601
629
"""Whatever request comes in, close the connection"""
603
def handle_one_request(self):
631
def _handle_one_request(self):
604
632
"""Handle a single HTTP request, by abruptly closing the connection"""
605
633
self.close_connection = 1
1899
def handle_one_request(self):
1927
def _handle_one_request(self):
1900
1928
tcs = self.server.test_case_server
1901
1929
requestline = self.rfile.readline()
1902
1930
headers = self.MessageClass(self.rfile, 0)
2087
2115
t = self.get_transport()
2088
2116
# We must send a single line of body bytes, see
2089
# PredefinedRequestHandler.handle_one_request
2117
# PredefinedRequestHandler._handle_one_request
2090
2118
code, f = t._post('abc def end-of-body\n')
2091
2119
self.assertEqual('lalala whatever as long as itsssss\n', f.read())
2092
2120
self.assertActivitiesMatch()