44import BaseHTTPServer
55import httplib
66import os
7+ import socket
78
89
910class StoppableHttpRequestHandler (SimpleHTTPServer .SimpleHTTPRequestHandler ):
1011 """http request handler with QUIT stopping the server"""
1112
13+ def setup (self ):
14+ SimpleHTTPServer .SimpleHTTPRequestHandler .setup (self )
15+ self .rfile .close ()
16+ self .wfile .close ()
17+ self .rfile = HttpEchoer (self .connection ._sock , 'rb' , self .rbufsize )
18+ self .wfile = HttpEchoer (self .connection ._sock , 'wb' , self .wbufsize )
19+
1220 def do_QUIT (self ):
1321 """send 200 OK response, and set server.stop to True"""
1422 self .send_response (200 )
1523 self .end_headers ()
1624 self .server .stop = True
17-
25+
1826 def do_POST (self ):
1927 # We could also process paremeters here using something like below.
2028 # length = self.headers['Content-Length']
@@ -63,7 +71,11 @@ def send_head(self):
6371 self .send_response (200 )
6472 self .send_header ("Content-type" , ctype )
6573 fs = os .fstat (f .fileno ())
66- self .send_header ("Content-Length" , str (fs [6 ]))
74+ # chrome hangs if this server runs on windows
75+ # it probably waits for more data -> os.fstat() does not return correct
76+ # length? We are responding with HTTP 1.0 = we do not need
77+ # content-length header:
78+ # self.send_header("Content-Length", str(fs[6]))
6779 self .send_header ("Last-Modified" , self .date_time_string (fs .st_mtime ))
6880 self .end_headers ()
6981 return f
@@ -78,27 +90,53 @@ def serve_forever(self):
7890 while not self .stop :
7991 self .handle_request ()
8092
93+
94+ class HttpEchoer (socket ._fileobject ):
95+ # echoes all HTTP traffic to a file if
96+ # the server output is set to a file in run_tests.py
97+ def read (self , size = - 1 ):
98+ data = super (HttpEchoer , self ).read (size )
99+ sys .stdout .write ("-->: %s" % data )
100+ sys .stdout .flush ()
101+ return data
102+
103+ def readline (self , size = - 1 ):
104+ data = super (HttpEchoer , self ).readline (size )
105+ sys .stdout .write ("-->: %s" % data )
106+ sys .stdout .flush ()
107+ return data
108+
109+ def readlines (self , sizehint = 0 ):
110+ data = super (HttpEchoer , self ).readlines (sizehint )
111+ sys .stdout .write ("-->: %s" % data )
112+ sys .stdout .flush ()
113+ return data
114+
115+ def write (self , data ):
116+ sys .stdout .write ("<--: %s" % data )
117+ sys .stdout .flush ()
118+ super (HttpEchoer , self ).write (data )
119+
120+
81121def stop_server (port = 7000 ):
82122 """send QUIT request to http server running on localhost:<port>"""
83123 conn = httplib .HTTPConnection ("localhost:%d" % port )
84124 conn .request ("QUIT" , "/" )
85125 conn .getresponse ()
86126
127+
87128def start_server (port = 7000 ):
88129 import os
89130 os .chdir (os .path .join (os .path .dirname (os .path .abspath (sys .argv [0 ])), '..' ))
90131 server = StoppableHttpServer (('' , port ), StoppableHttpRequestHandler )
91132 server .serve_forever ()
92-
93-
133+
94134if __name__ == '__main__' :
95135 import sys
96- if len (sys .argv ) != 2 or sys .argv [1 ] not in [ 'start' , 'stop' ]:
136+ if len (sys .argv ) != 2 or sys .argv [1 ] not in ['start' , 'stop' ]:
97137 print ('usage: %s start|stop' % sys .argv [0 ])
98138 sys .exit (1 )
99139 if sys .argv [1 ] == 'start' :
100140 start_server ()
101141 else :
102142 stop_server ()
103-
104-
0 commit comments