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 By default, it listens on an ephemeral port and sends the port number back to | 9 By default, it listens on an ephemeral port and sends the port number back to |
10 the originating process over a pipe. The originating process can specify an | 10 the originating process over a pipe. The originating process can specify an |
11 explicit port if necessary. | 11 explicit port if necessary. |
12 It can use https if you specify the flag --https=CERT where CERT is the path | 12 It can use https if you specify the flag --https=CERT where CERT is the path |
13 to a pem file containing the certificate and private key that should be used. | 13 to a pem file containing the certificate and private key that should be used. |
14 """ | 14 """ |
15 | 15 |
16 import base64 | 16 import base64 |
17 import BaseHTTPServer | 17 import BaseHTTPServer |
18 import cgi | 18 import cgi |
19 import optparse | 19 import optparse |
20 import os | 20 import os |
21 import re | 21 import re |
22 import shutil | 22 import shutil |
23 import SocketServer | 23 import SocketServer |
24 import sys | 24 import sys |
25 import struct | 25 import struct |
26 import time | 26 import time |
27 import urlparse | 27 import urlparse |
28 import warnings | 28 import warnings |
29 | 29 |
| 30 # If we use simplejson always, we get some warnings when we run under |
| 31 # 2.6. |
| 32 if sys.version_info < (2, 6): |
| 33 import simplejson as json |
| 34 else: |
| 35 import json |
| 36 |
30 # Ignore deprecation warnings, they make our output more cluttered. | 37 # Ignore deprecation warnings, they make our output more cluttered. |
31 warnings.filterwarnings("ignore", category=DeprecationWarning) | 38 warnings.filterwarnings("ignore", category=DeprecationWarning) |
32 | 39 |
33 import pyftpdlib.ftpserver | 40 import pyftpdlib.ftpserver |
34 import tlslite | 41 import tlslite |
35 import tlslite.api | 42 import tlslite.api |
36 | 43 |
37 try: | 44 try: |
38 import hashlib | 45 import hashlib |
39 _new_md5 = hashlib.md5 | 46 _new_md5 = hashlib.md5 |
(...skipping 1274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1314 | 1321 |
1315 # Instantiate FTP server class and listen to 127.0.0.1:port | 1322 # Instantiate FTP server class and listen to 127.0.0.1:port |
1316 address = ('127.0.0.1', port) | 1323 address = ('127.0.0.1', port) |
1317 server = pyftpdlib.ftpserver.FTPServer(address, ftp_handler) | 1324 server = pyftpdlib.ftpserver.FTPServer(address, ftp_handler) |
1318 listen_port = server.socket.getsockname()[1] | 1325 listen_port = server.socket.getsockname()[1] |
1319 print 'FTP server started on port %d...' % listen_port | 1326 print 'FTP server started on port %d...' % listen_port |
1320 | 1327 |
1321 # Notify the parent that we've started. (BaseServer subclasses | 1328 # Notify the parent that we've started. (BaseServer subclasses |
1322 # bind their sockets on construction.) | 1329 # bind their sockets on construction.) |
1323 if options.startup_pipe is not None: | 1330 if options.startup_pipe is not None: |
| 1331 server_data = { |
| 1332 'port': listen_port |
| 1333 } |
| 1334 server_data_json = json.dumps(server_data) |
| 1335 debug('sending server_data: %s' % server_data_json) |
| 1336 server_data_len = len(server_data_json) |
1324 if sys.platform == 'win32': | 1337 if sys.platform == 'win32': |
1325 fd = msvcrt.open_osfhandle(options.startup_pipe, 0) | 1338 fd = msvcrt.open_osfhandle(options.startup_pipe, 0) |
1326 else: | 1339 else: |
1327 fd = options.startup_pipe | 1340 fd = options.startup_pipe |
1328 startup_pipe = os.fdopen(fd, "w") | 1341 startup_pipe = os.fdopen(fd, "w") |
1329 # Write the listening port as a 2 byte value. This is _not_ using | 1342 # First write the data length as an unsigned 4-byte value. This |
1330 # network byte ordering since the other end of the pipe is on the same | 1343 # is _not_ using network byte ordering since the other end of the |
1331 # machine. | 1344 # pipe is on the same machine. |
1332 startup_pipe.write(struct.pack('@H', listen_port)) | 1345 startup_pipe.write(struct.pack('=L', server_data_len)) |
| 1346 startup_pipe.write(server_data_json) |
1333 startup_pipe.close() | 1347 startup_pipe.close() |
1334 | 1348 |
1335 try: | 1349 try: |
1336 server.serve_forever() | 1350 server.serve_forever() |
1337 except KeyboardInterrupt: | 1351 except KeyboardInterrupt: |
1338 print 'shutting down server' | 1352 print 'shutting down server' |
1339 server.stop = True | 1353 server.stop = True |
1340 | 1354 |
1341 if __name__ == '__main__': | 1355 if __name__ == '__main__': |
1342 option_parser = optparse.OptionParser() | 1356 option_parser = optparse.OptionParser() |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1374 'option may appear multiple times, indicating ' | 1388 'option may appear multiple times, indicating ' |
1375 'multiple algorithms should be enabled.'); | 1389 'multiple algorithms should be enabled.'); |
1376 option_parser.add_option('', '--file-root-url', default='/files/', | 1390 option_parser.add_option('', '--file-root-url', default='/files/', |
1377 help='Specify a root URL for files served.') | 1391 help='Specify a root URL for files served.') |
1378 option_parser.add_option('', '--startup-pipe', type='int', | 1392 option_parser.add_option('', '--startup-pipe', type='int', |
1379 dest='startup_pipe', | 1393 dest='startup_pipe', |
1380 help='File handle of pipe to parent process') | 1394 help='File handle of pipe to parent process') |
1381 options, args = option_parser.parse_args() | 1395 options, args = option_parser.parse_args() |
1382 | 1396 |
1383 sys.exit(main(options, args)) | 1397 sys.exit(main(options, args)) |
OLD | NEW |