| 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 | |
| 23 import minica | 22 import minica |
| 24 import optparse | 23 import optparse |
| 25 import os | 24 import os |
| 26 import random | 25 import random |
| 27 import re | 26 import re |
| 28 import select | 27 import select |
| 29 import socket | 28 import socket |
| 30 import SocketServer | 29 import SocketServer |
| 31 import struct | 30 import struct |
| 32 import sys | 31 import sys |
| 33 import threading | 32 import threading |
| 34 import time | 33 import time |
| 35 import urllib | 34 import urllib |
| 36 import urlparse | 35 import urlparse |
| 37 import warnings | 36 import warnings |
| 38 import zlib | 37 import zlib |
| 39 | 38 |
| 40 # Ignore deprecation warnings, they make our output more cluttered. | 39 # Ignore deprecation warnings, they make our output more cluttered. |
| 41 warnings.filterwarnings("ignore", category=DeprecationWarning) | 40 warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 42 | 41 |
| 43 import echo_message | 42 import echo_message |
| 44 from mod_pywebsocket.standalone import WebSocketServer | |
| 45 import pyftpdlib.ftpserver | 43 import pyftpdlib.ftpserver |
| 46 import tlslite | 44 import tlslite |
| 47 import tlslite.api | 45 import tlslite.api |
| 48 | 46 |
| 49 try: | 47 try: |
| 50 import hashlib | 48 import hashlib |
| 51 _new_md5 = hashlib.md5 | 49 _new_md5 = hashlib.md5 |
| 52 except ImportError: | 50 except ImportError: |
| 53 import md5 | 51 import md5 |
| 54 _new_md5 = md5.new | 52 _new_md5 = md5.new |
| 55 | 53 |
| 56 try: | 54 try: |
| 57 import json | 55 import json |
| 58 except ImportError: | 56 except ImportError: |
| 59 import simplejson as json | 57 import simplejson as json |
| 60 | 58 |
| 61 if sys.platform == 'win32': | 59 if sys.platform == 'win32': |
| 62 import msvcrt | 60 import msvcrt |
| 63 | 61 |
| 64 SERVER_HTTP = 0 | 62 SERVER_HTTP = 0 |
| 65 SERVER_FTP = 1 | 63 SERVER_FTP = 1 |
| 66 SERVER_SYNC = 2 | 64 SERVER_SYNC = 2 |
| 67 SERVER_TCP_ECHO = 3 | 65 SERVER_TCP_ECHO = 3 |
| 68 SERVER_UDP_ECHO = 4 | 66 SERVER_UDP_ECHO = 4 |
| 69 SERVER_BASIC_AUTH_PROXY = 5 | 67 SERVER_BASIC_AUTH_PROXY = 5 |
| 70 SERVER_WEBSOCKET = 6 | |
| 71 | |
| 72 # Default request queue size for WebSocketServer. | |
| 73 _DEFAULT_REQUEST_QUEUE_SIZE = 128 | |
| 74 | 68 |
| 75 # Using debug() seems to cause hangs on XP: see http://crbug.com/64515 . | 69 # Using debug() seems to cause hangs on XP: see http://crbug.com/64515 . |
| 76 debug_output = sys.stderr | 70 debug_output = sys.stderr |
| 77 def debug(str): | 71 def debug(str): |
| 78 debug_output.write(str + "\n") | 72 debug_output.write(str + "\n") |
| 79 debug_output.flush() | 73 debug_output.flush() |
| 80 | 74 |
| 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 | |
| 104 class RecordingSSLSessionCache(object): | 75 class RecordingSSLSessionCache(object): |
| 105 """RecordingSSLSessionCache acts as a TLS session cache and maintains a log of | 76 """RecordingSSLSessionCache acts as a TLS session cache and maintains a log of |
| 106 lookups and inserts in order to test session cache behaviours.""" | 77 lookups and inserts in order to test session cache behaviours.""" |
| 107 | 78 |
| 108 def __init__(self): | 79 def __init__(self): |
| 109 self.log = [] | 80 self.log = [] |
| 110 | 81 |
| 111 def __getitem__(self, sessionID): | 82 def __getitem__(self, sessionID): |
| 112 self.log.append(('lookup', sessionID)) | 83 self.log.append(('lookup', sessionID)) |
| 113 raise KeyError() | 84 raise KeyError() |
| (...skipping 2134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2248 server = HTTPServer((host, port), TestPageHandler) | 2219 server = HTTPServer((host, port), TestPageHandler) |
| 2249 print 'HTTP server started on %s:%d...' % (host, server.server_port) | 2220 print 'HTTP server started on %s:%d...' % (host, server.server_port) |
| 2250 | 2221 |
| 2251 server.data_dir = MakeDataDir() | 2222 server.data_dir = MakeDataDir() |
| 2252 server.file_root_url = options.file_root_url | 2223 server.file_root_url = options.file_root_url |
| 2253 server_data['port'] = server.server_port | 2224 server_data['port'] = server.server_port |
| 2254 server._device_management_handler = None | 2225 server._device_management_handler = None |
| 2255 server.policy_keys = options.policy_keys | 2226 server.policy_keys = options.policy_keys |
| 2256 server.policy_user = options.policy_user | 2227 server.policy_user = options.policy_user |
| 2257 server.gdata_auth_token = options.auth_token | 2228 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 | |
| 2268 elif options.server_type == SERVER_SYNC: | 2229 elif options.server_type == SERVER_SYNC: |
| 2269 xmpp_port = options.xmpp_port | 2230 xmpp_port = options.xmpp_port |
| 2270 server = SyncHTTPServer((host, port), xmpp_port, SyncPageHandler) | 2231 server = SyncHTTPServer((host, port), xmpp_port, SyncPageHandler) |
| 2271 print 'Sync HTTP server started on port %d...' % server.server_port | 2232 print 'Sync HTTP server started on port %d...' % server.server_port |
| 2272 print 'Sync XMPP server started on port %d...' % server.xmpp_port | 2233 print 'Sync XMPP server started on port %d...' % server.xmpp_port |
| 2273 server_data['port'] = server.server_port | 2234 server_data['port'] = server.server_port |
| 2274 server_data['xmpp_port'] = server.xmpp_port | 2235 server_data['xmpp_port'] = server.xmpp_port |
| 2275 elif options.server_type == SERVER_TCP_ECHO: | 2236 elif options.server_type == SERVER_TCP_ECHO: |
| 2276 # Used for generating the key (randomly) that encodes the "echo request" | 2237 # Used for generating the key (randomly) that encodes the "echo request" |
| 2277 # message. | 2238 # message. |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2362 help='start up a tcp echo server.') | 2323 help='start up a tcp echo server.') |
| 2363 option_parser.add_option('', '--udp-echo', action='store_const', | 2324 option_parser.add_option('', '--udp-echo', action='store_const', |
| 2364 const=SERVER_UDP_ECHO, default=SERVER_HTTP, | 2325 const=SERVER_UDP_ECHO, default=SERVER_HTTP, |
| 2365 dest='server_type', | 2326 dest='server_type', |
| 2366 help='start up a udp echo server.') | 2327 help='start up a udp echo server.') |
| 2367 option_parser.add_option('', '--basic-auth-proxy', action='store_const', | 2328 option_parser.add_option('', '--basic-auth-proxy', action='store_const', |
| 2368 const=SERVER_BASIC_AUTH_PROXY, default=SERVER_HTTP, | 2329 const=SERVER_BASIC_AUTH_PROXY, default=SERVER_HTTP, |
| 2369 dest='server_type', | 2330 dest='server_type', |
| 2370 help='start up a proxy server which requires basic ' | 2331 help='start up a proxy server which requires basic ' |
| 2371 'authentication.') | 2332 '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.') | |
| 2376 option_parser.add_option('', '--log-to-console', action='store_const', | 2333 option_parser.add_option('', '--log-to-console', action='store_const', |
| 2377 const=True, default=False, | 2334 const=True, default=False, |
| 2378 dest='log_to_console', | 2335 dest='log_to_console', |
| 2379 help='Enables or disables sys.stdout logging to ' | 2336 help='Enables or disables sys.stdout logging to ' |
| 2380 'the console.') | 2337 'the console.') |
| 2381 option_parser.add_option('', '--port', default='0', type='int', | 2338 option_parser.add_option('', '--port', default='0', type='int', |
| 2382 help='Port used by the server. If unspecified, the ' | 2339 help='Port used by the server. If unspecified, the ' |
| 2383 'server will listen on an ephemeral port.') | 2340 'server will listen on an ephemeral port.') |
| 2384 option_parser.add_option('', '--xmpp-port', default='0', type='int', | 2341 option_parser.add_option('', '--xmpp-port', default='0', type='int', |
| 2385 help='Port used by the XMPP server. If unspecified, ' | 2342 help='Port used by the XMPP server. If unspecified, ' |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2448 dest='host', | 2405 dest='host', |
| 2449 help='Hostname or IP upon which the server will ' | 2406 help='Hostname or IP upon which the server will ' |
| 2450 'listen. Client connections will also only be ' | 2407 'listen. Client connections will also only be ' |
| 2451 'allowed from this address.') | 2408 'allowed from this address.') |
| 2452 option_parser.add_option('', '--auth-token', dest='auth_token', | 2409 option_parser.add_option('', '--auth-token', dest='auth_token', |
| 2453 help='Specify the auth token which should be used' | 2410 help='Specify the auth token which should be used' |
| 2454 'in the authorization header for GData.') | 2411 'in the authorization header for GData.') |
| 2455 options, args = option_parser.parse_args() | 2412 options, args = option_parser.parse_args() |
| 2456 | 2413 |
| 2457 sys.exit(main(options, args)) | 2414 sys.exit(main(options, args)) |
| OLD | NEW |