##// END OF EJS Templates
test-static-http: flush access log per request...
Yuya Nishihara -
r34945:bf2db35a stable
parent child Browse files
Show More
@@ -1,68 +1,72 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2
2
3 from __future__ import absolute_import
3 from __future__ import absolute_import
4
4
5 """
5 """
6 Small and dumb HTTP server for use in tests.
6 Small and dumb HTTP server for use in tests.
7 """
7 """
8
8
9 import optparse
9 import optparse
10 import os
10 import os
11 import signal
11 import signal
12 import socket
12 import socket
13 import sys
13 import sys
14
14
15 from mercurial import (
15 from mercurial import (
16 server,
16 server,
17 util,
17 util,
18 )
18 )
19
19
20 httpserver = util.httpserver
20 httpserver = util.httpserver
21 OptionParser = optparse.OptionParser
21 OptionParser = optparse.OptionParser
22
22
23 if os.environ.get('HGIPV6', '0') == '1':
23 if os.environ.get('HGIPV6', '0') == '1':
24 class simplehttpserver(httpserver.httpserver):
24 class simplehttpserver(httpserver.httpserver):
25 address_family = socket.AF_INET6
25 address_family = socket.AF_INET6
26 else:
26 else:
27 simplehttpserver = httpserver.httpserver
27 simplehttpserver = httpserver.httpserver
28
28
29 class _httprequesthandler(httpserver.simplehttprequesthandler):
30 def log_message(self, format, *args):
31 httpserver.simplehttprequesthandler.log_message(self, format, *args)
32 sys.stderr.flush()
33
29 class simplehttpservice(object):
34 class simplehttpservice(object):
30 def __init__(self, host, port):
35 def __init__(self, host, port):
31 self.address = (host, port)
36 self.address = (host, port)
32 def init(self):
37 def init(self):
33 self.httpd = simplehttpserver(
38 self.httpd = simplehttpserver(self.address, _httprequesthandler)
34 self.address, httpserver.simplehttprequesthandler)
35 def run(self):
39 def run(self):
36 self.httpd.serve_forever()
40 self.httpd.serve_forever()
37
41
38 if __name__ == '__main__':
42 if __name__ == '__main__':
39 parser = OptionParser()
43 parser = OptionParser()
40 parser.add_option('-p', '--port', dest='port', type='int', default=8000,
44 parser.add_option('-p', '--port', dest='port', type='int', default=8000,
41 help='TCP port to listen on', metavar='PORT')
45 help='TCP port to listen on', metavar='PORT')
42 parser.add_option('-H', '--host', dest='host', default='localhost',
46 parser.add_option('-H', '--host', dest='host', default='localhost',
43 help='hostname or IP to listen on', metavar='HOST')
47 help='hostname or IP to listen on', metavar='HOST')
44 parser.add_option('--logfile', help='file name of access/error log')
48 parser.add_option('--logfile', help='file name of access/error log')
45 parser.add_option('--pid', dest='pid',
49 parser.add_option('--pid', dest='pid',
46 help='file name where the PID of the server is stored')
50 help='file name where the PID of the server is stored')
47 parser.add_option('-f', '--foreground', dest='foreground',
51 parser.add_option('-f', '--foreground', dest='foreground',
48 action='store_true',
52 action='store_true',
49 help='do not start the HTTP server in the background')
53 help='do not start the HTTP server in the background')
50 parser.add_option('--daemon-postexec', action='append')
54 parser.add_option('--daemon-postexec', action='append')
51
55
52 (options, args) = parser.parse_args()
56 (options, args) = parser.parse_args()
53
57
54 signal.signal(signal.SIGTERM, lambda x, y: sys.exit(0))
58 signal.signal(signal.SIGTERM, lambda x, y: sys.exit(0))
55
59
56 if options.foreground and options.logfile:
60 if options.foreground and options.logfile:
57 parser.error("options --logfile and --foreground are mutually "
61 parser.error("options --logfile and --foreground are mutually "
58 "exclusive")
62 "exclusive")
59 if options.foreground and options.pid:
63 if options.foreground and options.pid:
60 parser.error("options --pid and --foreground are mutually exclusive")
64 parser.error("options --pid and --foreground are mutually exclusive")
61
65
62 opts = {'pid_file': options.pid,
66 opts = {'pid_file': options.pid,
63 'daemon': not options.foreground,
67 'daemon': not options.foreground,
64 'daemon_postexec': options.daemon_postexec}
68 'daemon_postexec': options.daemon_postexec}
65 service = simplehttpservice(options.host, options.port)
69 service = simplehttpservice(options.host, options.port)
66 server.runservice(opts, initfn=service.init, runfn=service.run,
70 server.runservice(opts, initfn=service.init, runfn=service.run,
67 logfile=options.logfile,
71 logfile=options.logfile,
68 runargs=[sys.executable, __file__] + sys.argv[1:])
72 runargs=[sys.executable, __file__] + sys.argv[1:])
General Comments 0
You need to be logged in to leave comments. Login now