1#!/usr/bin/python 2# Copyright 2008 The Native Client Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6 7"""A tiny web server. 8 9This is intended to be used for testing, and 10only run from within the 11googleclient/native_client 12""" 13 14 15import BaseHTTPServer 16import logging 17import os 18import SimpleHTTPServer 19import SocketServer 20import sys 21 22logging.getLogger().setLevel(logging.INFO) 23 24# Using 'localhost' means that we only accept connections 25# via the loop back interface. 26SERVER_PORT = 5103 27SERVER_HOST = '' 28 29# We only run from the native_client directory, so that not too much 30# is exposed via this HTTP server. Everything in the directory is 31# served, so there should never be anything potentially sensitive in 32# the serving directory, especially if the machine might be a 33# multi-user machine and not all users are trusted. We only serve via 34# the loopback interface. 35 36SAFE_DIR_COMPONENTS = ['native_client'] 37SAFE_DIR_SUFFIX = apply(os.path.join, SAFE_DIR_COMPONENTS) 38 39 40def SanityCheckDirectory(): 41 if os.getcwd().endswith(SAFE_DIR_SUFFIX): 42 return 43 logging.error('httpd.py should only be run from the %s', SAFE_DIR_SUFFIX) 44 logging.error('directory for testing purposes.') 45 logging.error('We are currently in %s', os.getcwd()) 46 sys.exit(1) 47 48 49# the sole purpose of this class is to make the BaseHTTPServer threaded 50class ThreadedServer(SocketServer.ThreadingMixIn, 51 BaseHTTPServer.HTTPServer): 52 pass 53 54 55def Run(server_address, 56 server_class=ThreadedServer, 57 handler_class=SimpleHTTPServer.SimpleHTTPRequestHandler): 58 httpd = server_class(server_address, handler_class) 59 logging.info('started server on port %d', httpd.server_address[1]) 60 httpd.serve_forever() 61 62 63if __name__ == '__main__': 64 SanityCheckDirectory() 65 if len(sys.argv) > 1: 66 Run((SERVER_HOST, int(sys.argv[1]))) 67 else: 68 Run((SERVER_HOST, SERVER_PORT)) 69