Block remapping or unmapping code mappings
[nativeclient.git] / tools / httpd.py
blobc11e099ec2c79f0ce4ec7e4474c6c6bbbfceeaee
1 #!/usr/bin/python
2 # Copyright 2008, Google Inc.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
14 # distribution.
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 """A tiny web server.
34 This is intended to be used for testing, and
35 only run from within the
36 googleclient/native_client
37 """
40 import BaseHTTPServer
41 import logging
42 import os
43 import SimpleHTTPServer
44 import SocketServer
45 import sys
47 logging.getLogger().setLevel(logging.INFO)
49 # Using 'localhost' means that we only accept connections
50 # via the loop back interface.
51 SERVER_PORT = 5103
52 SERVER_HOST = ''
54 # We only run from the native_client directory, so that not too much
55 # is exposed via this HTTP server. Everything in the directory is
56 # served, so there should never be anything potentially sensitive in
57 # the serving directory, especially if the machine might be a
58 # multi-user machine and not all users are trusted. We only serve via
59 # the loopback interface.
61 SAFE_DIR_COMPONENTS = ['googleclient', 'native_client']
62 SAFE_DIR_SUFFIX = apply(os.path.join, SAFE_DIR_COMPONENTS)
65 def SanityCheckDirectory():
66 if os.getcwd().endswith(SAFE_DIR_SUFFIX):
67 return
68 # endif
69 logging.error('httpd.py should only be run from the %s', SAFE_DIR_SUFFIX)
70 logging.error('directory for testing purposes.')
71 logging.error('We are currently in %s', os.getcwd())
72 sys.exit(1)
73 # enddef
75 # the sole purpose of this class is to make the BaseHTTPServer threaded
76 class ThreadedServer(SocketServer.ThreadingMixIn,
77 BaseHTTPServer.HTTPServer):
78 pass
81 def Run(server_address,
82 server_class=ThreadedServer,
83 handler_class=SimpleHTTPServer.SimpleHTTPRequestHandler):
84 httpd = server_class(server_address, handler_class)
85 logging.info('started server on port %d', httpd.server_address[1])
86 httpd.serve_forever()
87 # enddef
90 if __name__ == '__main__':
91 SanityCheckDirectory()
92 if len(sys.argv) > 1:
93 Run((SERVER_HOST, int(sys.argv[1])))
94 else:
95 Run((SERVER_HOST, SERVER_PORT))
96 # endif
97 # endif