OLD | NEW |
1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """This is a simple HTTP server used for testing Chrome. | 6 """This is a simple HTTP server used for testing Chrome. |
7 | 7 |
8 It supports several test URLs, as specified by the handlers in TestPageHandler. | 8 It supports several test URLs, as specified by the handlers in TestPageHandler. |
9 It defaults to living on localhost:8888. | 9 It defaults to living on localhost:8888. |
10 It can use https if you specify the flag --https=CERT where CERT is the path | 10 It can use https if you specify the flag --https=CERT where CERT is the path |
11 to a pem file containing the certificate and private key that should be used. | 11 to a pem file containing the certificate and private key that should be used. |
12 To shut it down properly, visit localhost:8888/kill. | 12 To shut it down properly, visit localhost:8888/kill. |
13 """ | 13 """ |
14 | 14 |
15 import base64 | 15 import base64 |
16 import BaseHTTPServer | 16 import BaseHTTPServer |
17 import cgi | 17 import cgi |
18 import md5 | 18 import md5 |
19 import optparse | 19 import optparse |
20 import os | 20 import os |
21 import re | 21 import re |
22 import SocketServer | 22 import SocketServer |
23 import sys | 23 import sys |
24 import time | 24 import time |
25 import tlslite | 25 import tlslite |
26 import tlslite.api | 26 import tlslite.api |
| 27 import pyftpdlib.ftpserver |
| 28 |
| 29 SERVER_HTTP = 0 |
| 30 SERVER_FTP = 1 |
27 | 31 |
28 debug_output = sys.stderr | 32 debug_output = sys.stderr |
29 def debug(str): | 33 def debug(str): |
30 debug_output.write(str + "\n") | 34 debug_output.write(str + "\n") |
31 debug_output.flush() | 35 debug_output.flush() |
32 | 36 |
33 class StoppableHTTPServer(BaseHTTPServer.HTTPServer): | 37 class StoppableHTTPServer(BaseHTTPServer.HTTPServer): |
34 """This is a specialization of of BaseHTTPServer to allow it | 38 """This is a specialization of of BaseHTTPServer to allow it |
35 to be exited cleanly (by setting its "stop" member to True).""" | 39 to be exited cleanly (by setting its "stop" member to True).""" |
36 | 40 |
(...skipping 810 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
847 contents = "Default response given for path: " + self.path | 851 contents = "Default response given for path: " + self.path |
848 self.send_response(200) | 852 self.send_response(200) |
849 self.send_header('Content-type', 'text/html') | 853 self.send_header('Content-type', 'text/html') |
850 self.send_header("Content-Length", len(contents)) | 854 self.send_header("Content-Length", len(contents)) |
851 self.end_headers() | 855 self.end_headers() |
852 self.wfile.write(contents) | 856 self.wfile.write(contents) |
853 return True | 857 return True |
854 | 858 |
855 def do_GET(self): | 859 def do_GET(self): |
856 for handler in self._get_handlers: | 860 for handler in self._get_handlers: |
857 if (handler()): | 861 if handler(): |
858 return | 862 return |
859 | 863 |
860 def do_POST(self): | 864 def do_POST(self): |
861 for handler in self._post_handlers: | 865 for handler in self._post_handlers: |
862 if(handler()): | 866 if handler(): |
863 return | 867 return |
864 | 868 |
865 # called by the redirect handling function when there is no parameter | 869 # called by the redirect handling function when there is no parameter |
866 def sendRedirectHelp(self, redirect_name): | 870 def sendRedirectHelp(self, redirect_name): |
867 self.send_response(200) | 871 self.send_response(200) |
868 self.send_header('Content-type', 'text/html') | 872 self.send_header('Content-type', 'text/html') |
869 self.end_headers() | 873 self.end_headers() |
870 self.wfile.write('<html><body><h1>Error: no redirect destination</h1>') | 874 self.wfile.write('<html><body><h1>Error: no redirect destination</h1>') |
871 self.wfile.write('Use <pre>%s?http://dest...</pre>' % redirect_name) | 875 self.wfile.write('Use <pre>%s?http://dest...</pre>' % redirect_name) |
872 self.wfile.write('</body></html>') | 876 self.wfile.write('</body></html>') |
873 | 877 |
| 878 def MakeDataDir(): |
| 879 if options.data_dir: |
| 880 if not os.path.isdir(options.data_dir): |
| 881 print 'specified data dir not found: ' + options.data_dir + ' exiting...' |
| 882 return None |
| 883 my_data_dir = options.data_dir |
| 884 else: |
| 885 # Create the default path to our data dir, relative to the exe dir. |
| 886 my_data_dir = os.path.dirname(sys.argv[0]) |
| 887 my_data_dir = os.path.join(my_data_dir, "..", "..", "..", "..", |
| 888 "test", "data") |
| 889 |
| 890 #TODO(ibrar): Must use Find* funtion defined in google\tools |
| 891 #i.e my_data_dir = FindUpward(my_data_dir, "test", "data") |
| 892 |
| 893 return my_data_dir |
| 894 |
874 def main(options, args): | 895 def main(options, args): |
875 # redirect output to a log file so it doesn't spam the unit test output | 896 # redirect output to a log file so it doesn't spam the unit test output |
876 logfile = open('testserver.log', 'w') | 897 logfile = open('testserver.log', 'w') |
877 sys.stderr = sys.stdout = logfile | 898 sys.stderr = sys.stdout = logfile |
878 | 899 |
879 port = options.port | 900 port = options.port |
880 | 901 |
881 if options.cert: | 902 if options.server_type == SERVER_HTTP: |
882 # let's make sure the cert file exists. | 903 if options.cert: |
883 if not os.path.isfile(options.cert): | 904 # let's make sure the cert file exists. |
884 print 'specified cert file not found: ' + options.cert + ' exiting...' | 905 if not os.path.isfile(options.cert): |
885 return | 906 print 'specified cert file not found: ' + options.cert + ' exiting...' |
886 server = HTTPSServer(('127.0.0.1', port), TestPageHandler, options.cert) | 907 return |
887 print 'HTTPS server started on port %d...' % port | 908 server = HTTPSServer(('127.0.0.1', port), TestPageHandler, options.cert) |
| 909 print 'HTTPS server started on port %d...' % port |
| 910 else: |
| 911 server = StoppableHTTPServer(('127.0.0.1', port), TestPageHandler) |
| 912 print 'HTTP server started on port %d...' % port |
| 913 |
| 914 server.data_dir = MakeDataDir() |
| 915 |
| 916 # means FTP Server |
888 else: | 917 else: |
889 server = StoppableHTTPServer(('127.0.0.1', port), TestPageHandler) | 918 my_data_dir = MakeDataDir() |
890 print 'HTTP server started on port %d...' % port | |
891 | 919 |
892 if options.data_dir: | 920 def line_logger(msg): |
893 if not os.path.isdir(options.data_dir): | 921 if (msg.find("kill") >= 0): |
894 print 'specified data dir not found: ' + options.data_dir + ' exiting...' | 922 server.stop = True |
895 return | 923 print 'shutting down server' |
896 server.data_dir = options.data_dir | 924 sys.exit(0) |
897 else: | 925 |
898 # Create the default path to our data dir, relative to the exe dir. | 926 # Instantiate a dummy authorizer for managing 'virtual' users |
899 server.data_dir = os.path.dirname(sys.argv[0]) | 927 authorizer = pyftpdlib.ftpserver.DummyAuthorizer() |
900 server.data_dir = os.path.join(server.data_dir, "..", "..", "..", "..", | 928 |
901 "test", "data") | 929 # Define a new user having full r/w permissions and a read-only |
| 930 # anonymous user |
| 931 authorizer.add_user('chrome', 'chrome', my_data_dir, perm='elradfmw') |
| 932 |
| 933 authorizer.add_anonymous(my_data_dir) |
| 934 |
| 935 # Instantiate FTP handler class |
| 936 ftp_handler = pyftpdlib.ftpserver.FTPHandler |
| 937 ftp_handler.authorizer = authorizer |
| 938 pyftpdlib.ftpserver.logline = line_logger |
| 939 |
| 940 # Define a customized banner (string returned when client connects) |
| 941 ftp_handler.banner = "pyftpdlib %s based ftpd ready." % pyftpdlib.ftpserver.
__ver__ |
| 942 |
| 943 # Instantiate FTP server class and listen to 127.0.0.1:port |
| 944 address = ('127.0.0.1', port) |
| 945 server = pyftpdlib.ftpserver.FTPServer(address, ftp_handler) |
| 946 print 'FTP server started on port %d...' % port |
902 | 947 |
903 try: | 948 try: |
904 server.serve_forever() | 949 server.serve_forever() |
905 except KeyboardInterrupt: | 950 except KeyboardInterrupt: |
906 print 'shutting down server' | 951 print 'shutting down server' |
907 server.stop = True | 952 server.stop = True |
908 | 953 |
909 if __name__ == '__main__': | 954 if __name__ == '__main__': |
910 option_parser = optparse.OptionParser() | 955 option_parser = optparse.OptionParser() |
| 956 option_parser.add_option("-f", '--ftp', action='store_const', |
| 957 const=SERVER_FTP, default=SERVER_HTTP, |
| 958 dest='server_type', |
| 959 help='FTP or HTTP server default HTTP') |
911 option_parser.add_option('', '--port', default='8888', type='int', | 960 option_parser.add_option('', '--port', default='8888', type='int', |
912 help='Port used by the server') | 961 help='Port used by the server') |
913 option_parser.add_option('', '--data-dir', dest='data_dir', | 962 option_parser.add_option('', '--data-dir', dest='data_dir', |
914 help='Directory from which to read the files') | 963 help='Directory from which to read the files') |
915 option_parser.add_option('', '--https', dest='cert', | 964 option_parser.add_option('', '--https', dest='cert', |
916 help='Specify that https should be used, specify ' | 965 help='Specify that https should be used, specify ' |
917 'the path to the cert containing the private key ' | 966 'the path to the cert containing the private key ' |
918 'the server should use') | 967 'the server should use') |
919 options, args = option_parser.parse_args() | 968 options, args = option_parser.parse_args() |
920 | 969 |
921 sys.exit(main(options, args)) | 970 sys.exit(main(options, args)) |
922 | 971 |
OLD | NEW |