1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import BaseHTTPServer
import logging
import multiprocessing
import optparse
import os
import SimpleHTTPServer # pylint: disable=W0611
import socket
import sys
import time
import urlparse
if sys.version_info < (2, 7, 0):
sys.stderr.write("python 2.7 or later is required run this script\n")
sys.exit(1)
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
NACL_SDK_ROOT = os.path.dirname(SCRIPT_DIR)
# We only run from the examples directory so that not too much is exposed
# via this HTTP server. Everything in the directory is served, so there should
# never be anything potentially sensitive in the serving directory, especially
# if the machine might be a multi-user machine and not all users are trusted.
# We only serve via the loopback interface.
def SanityCheckDirectory(dirname):
abs_serve_dir = os.path.abspath(dirname)
# Verify we don't serve anywhere above NACL_SDK_ROOT.
if abs_serve_dir[:len(NACL_SDK_ROOT)] == NACL_SDK_ROOT:
return
logging.error('For security, httpd.py should only be run from within the')
logging.error('example directory tree.')
logging.error('Attempting to serve from %s.' % abs_serve_dir)
logging.error('Run with --no-dir-check to bypass this check.')
sys.exit(1)
class HTTPServer(BaseHTTPServer.HTTPServer):
def __init__(self, *args, **kwargs):
BaseHTTPServer.HTTPServer.__init__(self, *args)
self.running = True
self.result = 0
def Shutdown(self, result=0):
self.running = False
self.result = result
class HTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def _SendNothingAndDie(self, result=0):
self.send_response(200, 'OK')
self.send_header('Content-type', 'text/html')
self.send_header('Content-length', '0')
self.end_headers()
self.server.Shutdown(result)
def do_GET(self):
# Browsing to ?quit=1 will kill the server cleanly.
_, _, _, query, _ = urlparse.urlsplit(self.path)
if query:
params = urlparse.parse_qs(query)
if '1' in params.get('quit', []):
self._SendNothingAndDie()
return
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
class LocalHTTPServer(object):
"""Class to start a local HTTP server as a child process."""
def __init__(self, dirname, port):
parent_conn, child_conn = multiprocessing.Pipe()
self.process = multiprocessing.Process(
target=_HTTPServerProcess,
args=(child_conn, dirname, port, {}))
self.process.start()
if parent_conn.poll(10): # wait 10 seconds
self.port = parent_conn.recv()
else:
raise Exception('Unable to launch HTTP server.')
self.conn = parent_conn
def ServeForever(self):
"""Serve until the child HTTP process tells us to stop.
Returns:
The result from the child (as an errorcode), or 0 if the server was
killed not by the child (by KeyboardInterrupt for example).
"""
child_result = 0
try:
# Block on this pipe, waiting for a response from the child process.
child_result = self.conn.recv()
except KeyboardInterrupt:
pass
finally:
self.Shutdown()
return child_result
def ServeUntilSubprocessDies(self, process):
"""Serve until the child HTTP process tells us to stop or |subprocess| dies.
Returns:
The result from the child (as an errorcode), or 0 if |subprocess| died,
or the server was killed some other way (by KeyboardInterrupt for
example).
"""
child_result = 0
try:
while True:
if process.poll() is not None:
child_result = 0
break
if self.conn.poll():
child_result = self.conn.recv()
break
time.sleep(0)
except KeyboardInterrupt:
pass
finally:
self.Shutdown()
return child_result
def Shutdown(self):
"""Send a message to the child HTTP server process and wait for it to
finish."""
self.conn.send(False)
self.process.join()
def GetURL(self, rel_url):
"""Get the full url for a file on the local HTTP server.
Args:
rel_url: A URL fragment to convert to a full URL. For example,
GetURL('foobar.baz') -> 'http://localhost:1234/foobar.baz'
"""
return 'http://localhost:%d/%s' % (self.port, rel_url)
def _HTTPServerProcess(conn, dirname, port, server_kwargs):
"""Run a local httpserver with the given port or an ephemeral port.
This function assumes it is run as a child process using multiprocessing.
Args:
conn: A connection to the parent process. The child process sends
the local port, and waits for a message from the parent to
stop serving. It also sends a "result" back to the parent -- this can
be used to allow a client-side test to notify the server of results.
dirname: The directory to serve. All files are accessible through
http://localhost:<port>/path/to/filename.
port: The port to serve on. If 0, an ephemeral port will be chosen.
server_kwargs: A dict that will be passed as kwargs to the server.
"""
try:
os.chdir(dirname)
httpd = HTTPServer(('', port), HTTPRequestHandler, **server_kwargs)
except socket.error as e:
sys.stderr.write('Error creating HTTPServer: %s\n' % e)
sys.exit(1)
try:
conn.send(httpd.server_address[1]) # the chosen port number
httpd.timeout = 0.5 # seconds
while httpd.running:
# Flush output for MSVS Add-In.
sys.stdout.flush()
sys.stderr.flush()
httpd.handle_request()
if conn.poll():
httpd.running = conn.recv()
except KeyboardInterrupt:
pass
finally:
conn.send(httpd.result)
conn.close()
def main(args):
parser = optparse.OptionParser()
parser.add_option('-C', '--serve-dir',
help='Serve files out of this directory.',
default=os.path.abspath('.'))
parser.add_option('-p', '--port',
help='Run server on this port.', default=5103)
parser.add_option('--no-dir-check', '--no_dir_check',
help='No check to ensure serving from safe directory.',
dest='do_safe_check', action='store_false', default=True)
# To enable bash completion for this command first install optcomplete
# and then add this line to your .bashrc:
# complete -F _optcomplete httpd.py
try:
import optcomplete
optcomplete.autocomplete(parser)
except ImportError:
pass
options, args = parser.parse_args(args)
if options.do_safe_check:
SanityCheckDirectory(options.serve_dir)
server = LocalHTTPServer(options.serve_dir, int(options.port))
# Serve until the client tells us to stop. When it does, it will give us an
# errorcode.
print 'Serving %s on %s...' % (options.serve_dir, server.GetURL(''))
return server.ServeForever()
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
|