OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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/FTP/SYNC/TCP/UDP/ server used for testing Chrome. | 6 """This is a simple HTTP/FTP/SYNC/TCP/UDP/ 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 asyncore | 16 import asyncore |
17 import base64 | 17 import base64 |
18 import BaseHTTPServer | 18 import BaseHTTPServer |
19 import cgi | 19 import cgi |
20 import errno | 20 import errno |
21 import httplib | 21 import httplib |
| 22 import logging |
22 import minica | 23 import minica |
23 import optparse | 24 import optparse |
24 import os | 25 import os |
25 import random | 26 import random |
26 import re | 27 import re |
27 import select | 28 import select |
28 import socket | 29 import socket |
29 import SocketServer | 30 import SocketServer |
30 import struct | 31 import struct |
31 import sys | 32 import sys |
32 import threading | 33 import threading |
33 import time | 34 import time |
34 import urllib | 35 import urllib |
35 import urlparse | 36 import urlparse |
36 import warnings | 37 import warnings |
37 import zlib | 38 import zlib |
38 | 39 |
39 # Ignore deprecation warnings, they make our output more cluttered. | 40 # Ignore deprecation warnings, they make our output more cluttered. |
40 warnings.filterwarnings("ignore", category=DeprecationWarning) | 41 warnings.filterwarnings("ignore", category=DeprecationWarning) |
41 | 42 |
42 import echo_message | 43 import echo_message |
| 44 from mod_pywebsocket.standalone import WebSocketServer |
43 import pyftpdlib.ftpserver | 45 import pyftpdlib.ftpserver |
44 import tlslite | 46 import tlslite |
45 import tlslite.api | 47 import tlslite.api |
46 | 48 |
47 try: | 49 try: |
48 import hashlib | 50 import hashlib |
49 _new_md5 = hashlib.md5 | 51 _new_md5 = hashlib.md5 |
50 except ImportError: | 52 except ImportError: |
51 import md5 | 53 import md5 |
52 _new_md5 = md5.new | 54 _new_md5 = md5.new |
53 | 55 |
54 try: | 56 try: |
55 import json | 57 import json |
56 except ImportError: | 58 except ImportError: |
57 import simplejson as json | 59 import simplejson as json |
58 | 60 |
59 if sys.platform == 'win32': | 61 if sys.platform == 'win32': |
60 import msvcrt | 62 import msvcrt |
61 | 63 |
62 SERVER_HTTP = 0 | 64 SERVER_HTTP = 0 |
63 SERVER_FTP = 1 | 65 SERVER_FTP = 1 |
64 SERVER_SYNC = 2 | 66 SERVER_SYNC = 2 |
65 SERVER_TCP_ECHO = 3 | 67 SERVER_TCP_ECHO = 3 |
66 SERVER_UDP_ECHO = 4 | 68 SERVER_UDP_ECHO = 4 |
67 SERVER_BASIC_AUTH_PROXY = 5 | 69 SERVER_BASIC_AUTH_PROXY = 5 |
| 70 SERVER_WEBSOCKET = 6 |
| 71 |
| 72 # Default request queue size for WebSocketServer. |
| 73 _DEFAULT_REQUEST_QUEUE_SIZE = 128 |
68 | 74 |
69 # Using debug() seems to cause hangs on XP: see http://crbug.com/64515 . | 75 # Using debug() seems to cause hangs on XP: see http://crbug.com/64515 . |
70 debug_output = sys.stderr | 76 debug_output = sys.stderr |
71 def debug(str): | 77 def debug(str): |
72 debug_output.write(str + "\n") | 78 debug_output.write(str + "\n") |
73 debug_output.flush() | 79 debug_output.flush() |
74 | 80 |
| 81 class WebSocketOptions: |
| 82 """Holds options for WebSocketServer.""" |
| 83 |
| 84 def __init__(self, host, port, data_dir): |
| 85 self.request_queue_size = _DEFAULT_REQUEST_QUEUE_SIZE |
| 86 self.server_host = host |
| 87 self.port = port |
| 88 self.websock_handlers = data_dir |
| 89 self.scan_dir = None |
| 90 self.allow_handlers_outside_root_dir = False |
| 91 self.websock_handlers_map_file = None |
| 92 self.cgi_directories = [] |
| 93 self.is_executable_method = None |
| 94 self.allow_draft75 = False |
| 95 self.strict = True |
| 96 |
| 97 # TODO(toyoshim): Support SSL and authenticates (http://crbug.com/137639) |
| 98 self.use_tls = False |
| 99 self.private_key = None |
| 100 self.certificate = None |
| 101 self.tls_client_ca = None |
| 102 self.use_basic_auth = False |
| 103 |
75 class RecordingSSLSessionCache(object): | 104 class RecordingSSLSessionCache(object): |
76 """RecordingSSLSessionCache acts as a TLS session cache and maintains a log of | 105 """RecordingSSLSessionCache acts as a TLS session cache and maintains a log of |
77 lookups and inserts in order to test session cache behaviours.""" | 106 lookups and inserts in order to test session cache behaviours.""" |
78 | 107 |
79 def __init__(self): | 108 def __init__(self): |
80 self.log = [] | 109 self.log = [] |
81 | 110 |
82 def __getitem__(self, sessionID): | 111 def __getitem__(self, sessionID): |
83 self.log.append(('lookup', sessionID)) | 112 self.log.append(('lookup', sessionID)) |
84 raise KeyError() | 113 raise KeyError() |
(...skipping 2134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2219 server = HTTPServer((host, port), TestPageHandler) | 2248 server = HTTPServer((host, port), TestPageHandler) |
2220 print 'HTTP server started on %s:%d...' % (host, server.server_port) | 2249 print 'HTTP server started on %s:%d...' % (host, server.server_port) |
2221 | 2250 |
2222 server.data_dir = MakeDataDir() | 2251 server.data_dir = MakeDataDir() |
2223 server.file_root_url = options.file_root_url | 2252 server.file_root_url = options.file_root_url |
2224 server_data['port'] = server.server_port | 2253 server_data['port'] = server.server_port |
2225 server._device_management_handler = None | 2254 server._device_management_handler = None |
2226 server.policy_keys = options.policy_keys | 2255 server.policy_keys = options.policy_keys |
2227 server.policy_user = options.policy_user | 2256 server.policy_user = options.policy_user |
2228 server.gdata_auth_token = options.auth_token | 2257 server.gdata_auth_token = options.auth_token |
| 2258 elif options.server_type == SERVER_WEBSOCKET: |
| 2259 # Launch pywebsocket via WebSocketServer. |
| 2260 logger = logging.getLogger() |
| 2261 logger.addHandler(logging.StreamHandler()) |
| 2262 # TODO(toyoshim): Remove following os.chdir. Currently this operation |
| 2263 # is required to work correctly. It should be fixed from pywebsocket side. |
| 2264 os.chdir(MakeDataDir()) |
| 2265 server = WebSocketServer(WebSocketOptions(host, port, MakeDataDir())) |
| 2266 print 'WebSocket server started on %s:%d...' % (host, server.server_port) |
| 2267 server_data['port'] = server.server_port |
2229 elif options.server_type == SERVER_SYNC: | 2268 elif options.server_type == SERVER_SYNC: |
2230 xmpp_port = options.xmpp_port | 2269 xmpp_port = options.xmpp_port |
2231 server = SyncHTTPServer((host, port), xmpp_port, SyncPageHandler) | 2270 server = SyncHTTPServer((host, port), xmpp_port, SyncPageHandler) |
2232 print 'Sync HTTP server started on port %d...' % server.server_port | 2271 print 'Sync HTTP server started on port %d...' % server.server_port |
2233 print 'Sync XMPP server started on port %d...' % server.xmpp_port | 2272 print 'Sync XMPP server started on port %d...' % server.xmpp_port |
2234 server_data['port'] = server.server_port | 2273 server_data['port'] = server.server_port |
2235 server_data['xmpp_port'] = server.xmpp_port | 2274 server_data['xmpp_port'] = server.xmpp_port |
2236 elif options.server_type == SERVER_TCP_ECHO: | 2275 elif options.server_type == SERVER_TCP_ECHO: |
2237 # Used for generating the key (randomly) that encodes the "echo request" | 2276 # Used for generating the key (randomly) that encodes the "echo request" |
2238 # message. | 2277 # message. |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2323 help='start up a tcp echo server.') | 2362 help='start up a tcp echo server.') |
2324 option_parser.add_option('', '--udp-echo', action='store_const', | 2363 option_parser.add_option('', '--udp-echo', action='store_const', |
2325 const=SERVER_UDP_ECHO, default=SERVER_HTTP, | 2364 const=SERVER_UDP_ECHO, default=SERVER_HTTP, |
2326 dest='server_type', | 2365 dest='server_type', |
2327 help='start up a udp echo server.') | 2366 help='start up a udp echo server.') |
2328 option_parser.add_option('', '--basic-auth-proxy', action='store_const', | 2367 option_parser.add_option('', '--basic-auth-proxy', action='store_const', |
2329 const=SERVER_BASIC_AUTH_PROXY, default=SERVER_HTTP, | 2368 const=SERVER_BASIC_AUTH_PROXY, default=SERVER_HTTP, |
2330 dest='server_type', | 2369 dest='server_type', |
2331 help='start up a proxy server which requires basic ' | 2370 help='start up a proxy server which requires basic ' |
2332 'authentication.') | 2371 'authentication.') |
| 2372 option_parser.add_option('', '--websocket', action='store_const', |
| 2373 const=SERVER_WEBSOCKET, default=SERVER_HTTP, |
| 2374 dest='server_type', |
| 2375 help='start up a WebSocket server.') |
2333 option_parser.add_option('', '--log-to-console', action='store_const', | 2376 option_parser.add_option('', '--log-to-console', action='store_const', |
2334 const=True, default=False, | 2377 const=True, default=False, |
2335 dest='log_to_console', | 2378 dest='log_to_console', |
2336 help='Enables or disables sys.stdout logging to ' | 2379 help='Enables or disables sys.stdout logging to ' |
2337 'the console.') | 2380 'the console.') |
2338 option_parser.add_option('', '--port', default='0', type='int', | 2381 option_parser.add_option('', '--port', default='0', type='int', |
2339 help='Port used by the server. If unspecified, the ' | 2382 help='Port used by the server. If unspecified, the ' |
2340 'server will listen on an ephemeral port.') | 2383 'server will listen on an ephemeral port.') |
2341 option_parser.add_option('', '--xmpp-port', default='0', type='int', | 2384 option_parser.add_option('', '--xmpp-port', default='0', type='int', |
2342 help='Port used by the XMPP server. If unspecified, ' | 2385 help='Port used by the XMPP server. If unspecified, ' |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2405 dest='host', | 2448 dest='host', |
2406 help='Hostname or IP upon which the server will ' | 2449 help='Hostname or IP upon which the server will ' |
2407 'listen. Client connections will also only be ' | 2450 'listen. Client connections will also only be ' |
2408 'allowed from this address.') | 2451 'allowed from this address.') |
2409 option_parser.add_option('', '--auth-token', dest='auth_token', | 2452 option_parser.add_option('', '--auth-token', dest='auth_token', |
2410 help='Specify the auth token which should be used' | 2453 help='Specify the auth token which should be used' |
2411 'in the authorization header for GData.') | 2454 'in the authorization header for GData.') |
2412 options, args = option_parser.parse_args() | 2455 options, args = option_parser.parse_args() |
2413 | 2456 |
2414 sys.exit(main(options, args)) | 2457 sys.exit(main(options, args)) |
OLD | NEW |