OLD | NEW |
1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
2 # Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2010 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 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 self.stop = False | 51 self.stop = False |
52 self.nonce_time = None | 52 self.nonce_time = None |
53 while not self.stop: | 53 while not self.stop: |
54 self.handle_request() | 54 self.handle_request() |
55 self.socket.close() | 55 self.socket.close() |
56 | 56 |
57 class HTTPSServer(tlslite.api.TLSSocketServerMixIn, StoppableHTTPServer): | 57 class HTTPSServer(tlslite.api.TLSSocketServerMixIn, StoppableHTTPServer): |
58 """This is a specialization of StoppableHTTPerver that add https support.""" | 58 """This is a specialization of StoppableHTTPerver that add https support.""" |
59 | 59 |
60 def __init__(self, server_address, request_hander_class, cert_path, | 60 def __init__(self, server_address, request_hander_class, cert_path, |
61 ssl_client_auth): | 61 ssl_client_auth, ssl_client_cas): |
62 s = open(cert_path).read() | 62 s = open(cert_path).read() |
63 x509 = tlslite.api.X509() | 63 x509 = tlslite.api.X509() |
64 x509.parse(s) | 64 x509.parse(s) |
65 self.cert_chain = tlslite.api.X509CertChain([x509]) | 65 self.cert_chain = tlslite.api.X509CertChain([x509]) |
66 s = open(cert_path).read() | 66 s = open(cert_path).read() |
67 self.private_key = tlslite.api.parsePEMKey(s, private=True) | 67 self.private_key = tlslite.api.parsePEMKey(s, private=True) |
68 self.ssl_client_auth = ssl_client_auth | 68 self.ssl_client_auth = ssl_client_auth |
| 69 self.ssl_client_cas = [] |
| 70 for ca_file in ssl_client_cas: |
| 71 s = open(ca_file).read() |
| 72 x509 = tlslite.api.X509() |
| 73 x509.parse(s) |
| 74 self.ssl_client_cas.append(x509.subject) |
69 | 75 |
70 self.session_cache = tlslite.api.SessionCache() | 76 self.session_cache = tlslite.api.SessionCache() |
71 StoppableHTTPServer.__init__(self, server_address, request_hander_class) | 77 StoppableHTTPServer.__init__(self, server_address, request_hander_class) |
72 | 78 |
73 def handshake(self, tlsConnection): | 79 def handshake(self, tlsConnection): |
74 """Creates the SSL connection.""" | 80 """Creates the SSL connection.""" |
75 try: | 81 try: |
76 tlsConnection.handshakeServer(certChain=self.cert_chain, | 82 tlsConnection.handshakeServer(certChain=self.cert_chain, |
77 privateKey=self.private_key, | 83 privateKey=self.private_key, |
78 sessionCache=self.session_cache, | 84 sessionCache=self.session_cache, |
79 reqCert=self.ssl_client_auth) | 85 reqCert=self.ssl_client_auth, |
| 86 reqCAs=self.ssl_client_cas) |
80 tlsConnection.ignoreAbruptClose = True | 87 tlsConnection.ignoreAbruptClose = True |
81 return True | 88 return True |
82 except tlslite.api.TLSError, error: | 89 except tlslite.api.TLSError, error: |
83 print "Handshake failure:", str(error) | 90 print "Handshake failure:", str(error) |
84 return False | 91 return False |
85 | 92 |
86 class TestPageHandler(BaseHTTPServer.BaseHTTPRequestHandler): | 93 class TestPageHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
87 | 94 |
88 def __init__(self, request, client_address, socket_server): | 95 def __init__(self, request, client_address, socket_server): |
89 self._connect_handlers = [ | 96 self._connect_handlers = [ |
(...skipping 1118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1208 | 1215 |
1209 port = options.port | 1216 port = options.port |
1210 | 1217 |
1211 # Try to free up the port if there's an orphaned old instance. | 1218 # Try to free up the port if there's an orphaned old instance. |
1212 TryKillingOldServer(port) | 1219 TryKillingOldServer(port) |
1213 | 1220 |
1214 if options.server_type == SERVER_HTTP: | 1221 if options.server_type == SERVER_HTTP: |
1215 if options.cert: | 1222 if options.cert: |
1216 # let's make sure the cert file exists. | 1223 # let's make sure the cert file exists. |
1217 if not os.path.isfile(options.cert): | 1224 if not os.path.isfile(options.cert): |
1218 print 'specified cert file not found: ' + options.cert + ' exiting...' | 1225 print 'specified server cert file not found: ' + options.cert + \ |
| 1226 ' exiting...' |
1219 return | 1227 return |
| 1228 for ca_cert in options.ssl_client_ca: |
| 1229 if not os.path.isfile(ca_cert): |
| 1230 print 'specified trusted client CA file not found: ' + ca_cert + \ |
| 1231 ' exiting...' |
| 1232 return |
1220 server = HTTPSServer(('127.0.0.1', port), TestPageHandler, options.cert, | 1233 server = HTTPSServer(('127.0.0.1', port), TestPageHandler, options.cert, |
1221 options.ssl_client_auth) | 1234 options.ssl_client_auth, options.ssl_client_ca) |
1222 print 'HTTPS server started on port %d...' % port | 1235 print 'HTTPS server started on port %d...' % port |
1223 else: | 1236 else: |
1224 server = StoppableHTTPServer(('127.0.0.1', port), TestPageHandler) | 1237 server = StoppableHTTPServer(('127.0.0.1', port), TestPageHandler) |
1225 print 'HTTP server started on port %d...' % port | 1238 print 'HTTP server started on port %d...' % port |
1226 | 1239 |
1227 server.data_dir = MakeDataDir() | 1240 server.data_dir = MakeDataDir() |
1228 server.file_root_url = options.file_root_url | 1241 server.file_root_url = options.file_root_url |
1229 server._sync_handler = None | 1242 server._sync_handler = None |
1230 | 1243 |
1231 MakeDumpDir(server.data_dir) | 1244 MakeDumpDir(server.data_dir) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1278 option_parser.add_option('', '--port', default='8888', type='int', | 1291 option_parser.add_option('', '--port', default='8888', type='int', |
1279 help='Port used by the server.') | 1292 help='Port used by the server.') |
1280 option_parser.add_option('', '--data-dir', dest='data_dir', | 1293 option_parser.add_option('', '--data-dir', dest='data_dir', |
1281 help='Directory from which to read the files.') | 1294 help='Directory from which to read the files.') |
1282 option_parser.add_option('', '--https', dest='cert', | 1295 option_parser.add_option('', '--https', dest='cert', |
1283 help='Specify that https should be used, specify ' | 1296 help='Specify that https should be used, specify ' |
1284 'the path to the cert containing the private key ' | 1297 'the path to the cert containing the private key ' |
1285 'the server should use.') | 1298 'the server should use.') |
1286 option_parser.add_option('', '--ssl-client-auth', action='store_true', | 1299 option_parser.add_option('', '--ssl-client-auth', action='store_true', |
1287 help='Require SSL client auth on every connection.') | 1300 help='Require SSL client auth on every connection.') |
| 1301 option_parser.add_option('', '--ssl-client-ca', action='append', default=[], |
| 1302 help='Specify that the client certificate request ' |
| 1303 'should indicate that it supports the CA contained ' |
| 1304 'in the specified certificate file') |
1288 option_parser.add_option('', '--file-root-url', default='/files/', | 1305 option_parser.add_option('', '--file-root-url', default='/files/', |
1289 help='Specify a root URL for files served.') | 1306 help='Specify a root URL for files served.') |
1290 option_parser.add_option('', '--never-die', default=False, | 1307 option_parser.add_option('', '--never-die', default=False, |
1291 action="store_true", | 1308 action="store_true", |
1292 help='Prevent the server from dying when visiting ' | 1309 help='Prevent the server from dying when visiting ' |
1293 'a /kill URL. Useful for manually running some ' | 1310 'a /kill URL. Useful for manually running some ' |
1294 'tests.') | 1311 'tests.') |
1295 options, args = option_parser.parse_args() | 1312 options, args = option_parser.parse_args() |
1296 | 1313 |
1297 sys.exit(main(options, args)) | 1314 sys.exit(main(options, args)) |
OLD | NEW |