rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 1 | # Copyright 2013 The Chromium Authors. All rights reserved. |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Victor-Gabriel Savu | 1e3338b | 2020-05-20 07:59:50 +0000 | [diff] [blame] | 5 | from six.moves import BaseHTTPServer |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 6 | import errno |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 7 | import json |
| 8 | import optparse |
| 9 | import os |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 10 | import re |
| 11 | import socket |
Victor-Gabriel Savu | 1e3338b | 2020-05-20 07:59:50 +0000 | [diff] [blame] | 12 | from six.moves import socketserver as SocketServer |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 13 | import struct |
| 14 | import sys |
| 15 | import warnings |
| 16 | |
davidben@chromium.org | 7d53b54 | 2014-04-10 17:56:44 +0000 | [diff] [blame] | 17 | import tlslite.errors |
| 18 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 19 | # Ignore deprecation warnings, they make our output more cluttered. |
| 20 | warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 21 | |
| 22 | if sys.platform == 'win32': |
| 23 | import msvcrt |
| 24 | |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 25 | # Using debug() seems to cause hangs on XP: see http://crbug.com/64515. |
| 26 | debug_output = sys.stderr |
| 27 | def debug(string): |
| 28 | debug_output.write(string + "\n") |
| 29 | debug_output.flush() |
| 30 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 31 | |
| 32 | class Error(Exception): |
| 33 | """Error class for this module.""" |
| 34 | |
| 35 | |
| 36 | class OptionError(Error): |
| 37 | """Error for bad command line options.""" |
| 38 | |
| 39 | |
| 40 | class FileMultiplexer(object): |
| 41 | def __init__(self, fd1, fd2) : |
| 42 | self.__fd1 = fd1 |
| 43 | self.__fd2 = fd2 |
| 44 | |
| 45 | def __del__(self) : |
| 46 | if self.__fd1 != sys.stdout and self.__fd1 != sys.stderr: |
| 47 | self.__fd1.close() |
| 48 | if self.__fd2 != sys.stdout and self.__fd2 != sys.stderr: |
| 49 | self.__fd2.close() |
| 50 | |
| 51 | def write(self, text) : |
| 52 | self.__fd1.write(text) |
| 53 | self.__fd2.write(text) |
| 54 | |
| 55 | def flush(self) : |
| 56 | self.__fd1.flush() |
| 57 | self.__fd2.flush() |
| 58 | |
| 59 | |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 60 | class ClientRestrictingServerMixIn: |
| 61 | """Implements verify_request to limit connections to our configured IP |
| 62 | address.""" |
| 63 | |
| 64 | def verify_request(self, _request, client_address): |
| 65 | return client_address[0] == self.server_address[0] |
| 66 | |
| 67 | |
| 68 | class BrokenPipeHandlerMixIn: |
| 69 | """Allows the server to deal with "broken pipe" errors (which happen if the |
| 70 | browser quits with outstanding requests, like for the favicon). This mix-in |
| 71 | requires the class to derive from SocketServer.BaseServer and not override its |
| 72 | handle_error() method. """ |
| 73 | |
| 74 | def handle_error(self, request, client_address): |
| 75 | value = sys.exc_info()[1] |
davidben@chromium.org | 7d53b54 | 2014-04-10 17:56:44 +0000 | [diff] [blame] | 76 | if isinstance(value, tlslite.errors.TLSClosedConnectionError): |
Victor-Gabriel Savu | 1e3338b | 2020-05-20 07:59:50 +0000 | [diff] [blame] | 77 | print("testserver.py: Closed connection") |
davidben@chromium.org | 7d53b54 | 2014-04-10 17:56:44 +0000 | [diff] [blame] | 78 | return |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 79 | if isinstance(value, socket.error): |
| 80 | err = value.args[0] |
| 81 | if sys.platform in ('win32', 'cygwin'): |
| 82 | # "An established connection was aborted by the software in your host." |
| 83 | pipe_err = 10053 |
| 84 | else: |
| 85 | pipe_err = errno.EPIPE |
| 86 | if err == pipe_err: |
Victor-Gabriel Savu | 1e3338b | 2020-05-20 07:59:50 +0000 | [diff] [blame] | 87 | print("testserver.py: Broken pipe") |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 88 | return |
davidben@chromium.org | 7d53b54 | 2014-04-10 17:56:44 +0000 | [diff] [blame] | 89 | if err == errno.ECONNRESET: |
Victor-Gabriel Savu | 1e3338b | 2020-05-20 07:59:50 +0000 | [diff] [blame] | 90 | print("testserver.py: Connection reset by peer") |
davidben@chromium.org | 7d53b54 | 2014-04-10 17:56:44 +0000 | [diff] [blame] | 91 | return |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 92 | SocketServer.BaseServer.handle_error(self, request, client_address) |
| 93 | |
| 94 | |
| 95 | class StoppableHTTPServer(BaseHTTPServer.HTTPServer): |
| 96 | """This is a specialization of BaseHTTPServer to allow it |
| 97 | to be exited cleanly (by setting its "stop" member to True).""" |
| 98 | |
| 99 | def serve_forever(self): |
| 100 | self.stop = False |
| 101 | self.nonce_time = None |
| 102 | while not self.stop: |
| 103 | self.handle_request() |
| 104 | self.socket.close() |
| 105 | |
| 106 | |
mattm@chromium.org | 1695973 | 2012-11-28 07:14:19 +0000 | [diff] [blame] | 107 | def MultiplexerHack(std_fd, log_fd): |
| 108 | """Creates a FileMultiplexer that will write to both specified files. |
| 109 | |
| 110 | When running on Windows XP bots, stdout and stderr will be invalid file |
| 111 | handles, so log_fd will be returned directly. (This does not occur if you |
| 112 | run the test suite directly from a console, but only if the output of the |
| 113 | test executable is redirected.) |
| 114 | """ |
| 115 | if std_fd.fileno() <= 0: |
| 116 | return log_fd |
| 117 | return FileMultiplexer(std_fd, log_fd) |
| 118 | |
| 119 | |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 120 | class BasePageHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 121 | |
| 122 | def __init__(self, request, client_address, socket_server, |
| 123 | connect_handlers, get_handlers, head_handlers, post_handlers, |
| 124 | put_handlers): |
| 125 | self._connect_handlers = connect_handlers |
| 126 | self._get_handlers = get_handlers |
| 127 | self._head_handlers = head_handlers |
| 128 | self._post_handlers = post_handlers |
| 129 | self._put_handlers = put_handlers |
| 130 | BaseHTTPServer.BaseHTTPRequestHandler.__init__( |
| 131 | self, request, client_address, socket_server) |
| 132 | |
| 133 | def log_request(self, *args, **kwargs): |
| 134 | # Disable request logging to declutter test log output. |
| 135 | pass |
| 136 | |
| 137 | def _ShouldHandleRequest(self, handler_name): |
| 138 | """Determines if the path can be handled by the handler. |
| 139 | |
| 140 | We consider a handler valid if the path begins with the |
| 141 | handler name. It can optionally be followed by "?*", "/*". |
| 142 | """ |
| 143 | |
| 144 | pattern = re.compile('%s($|\?|/).*' % handler_name) |
| 145 | return pattern.match(self.path) |
| 146 | |
| 147 | def do_CONNECT(self): |
| 148 | for handler in self._connect_handlers: |
| 149 | if handler(): |
| 150 | return |
| 151 | |
| 152 | def do_GET(self): |
| 153 | for handler in self._get_handlers: |
| 154 | if handler(): |
| 155 | return |
| 156 | |
| 157 | def do_HEAD(self): |
| 158 | for handler in self._head_handlers: |
| 159 | if handler(): |
| 160 | return |
| 161 | |
| 162 | def do_POST(self): |
| 163 | for handler in self._post_handlers: |
| 164 | if handler(): |
| 165 | return |
| 166 | |
| 167 | def do_PUT(self): |
| 168 | for handler in self._put_handlers: |
| 169 | if handler(): |
| 170 | return |
| 171 | |
| 172 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 173 | class TestServerRunner(object): |
| 174 | """Runs a test server and communicates with the controlling C++ test code. |
| 175 | |
| 176 | Subclasses should override the create_server method to create their server |
| 177 | object, and the add_options method to add their own options. |
| 178 | """ |
| 179 | |
| 180 | def __init__(self): |
| 181 | self.option_parser = optparse.OptionParser() |
| 182 | self.add_options() |
| 183 | |
| 184 | def main(self): |
| 185 | self.options, self.args = self.option_parser.parse_args() |
| 186 | |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 187 | logfile = open(self.options.log_file, 'w') |
jaekyun | e5b139c | 2015-03-10 15:54:29 -0700 | [diff] [blame] | 188 | |
| 189 | # http://crbug.com/248796 : Error logs streamed to normal sys.stderr will be |
| 190 | # written to HTTP response payload when remote test server is used. |
| 191 | # For this reason, some tests like ResourceFetcherTests.ResourceFetcher404 |
| 192 | # were failing on Android because remote test server is being used there. |
| 193 | # To fix them, we need to use sys.stdout as sys.stderr if remote test server |
| 194 | # is used. |
| 195 | if self.options.on_remote_server: |
| 196 | sys.stderr = sys.stdout |
| 197 | |
mattm@chromium.org | 1695973 | 2012-11-28 07:14:19 +0000 | [diff] [blame] | 198 | sys.stderr = MultiplexerHack(sys.stderr, logfile) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 199 | if self.options.log_to_console: |
mattm@chromium.org | 1695973 | 2012-11-28 07:14:19 +0000 | [diff] [blame] | 200 | sys.stdout = MultiplexerHack(sys.stdout, logfile) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 201 | else: |
| 202 | sys.stdout = logfile |
| 203 | |
| 204 | server_data = { |
| 205 | 'host': self.options.host, |
| 206 | } |
| 207 | self.server = self.create_server(server_data) |
| 208 | self._notify_startup_complete(server_data) |
| 209 | self.run_server() |
| 210 | |
| 211 | def create_server(self, server_data): |
| 212 | """Creates a server object and returns it. |
| 213 | |
| 214 | Must populate server_data['port'], and can set additional server_data |
| 215 | elements if desired.""" |
| 216 | raise NotImplementedError() |
| 217 | |
| 218 | def run_server(self): |
| 219 | try: |
| 220 | self.server.serve_forever() |
| 221 | except KeyboardInterrupt: |
Victor-Gabriel Savu | 1e3338b | 2020-05-20 07:59:50 +0000 | [diff] [blame] | 222 | print('shutting down server') |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 223 | self.server.stop = True |
| 224 | |
| 225 | def add_options(self): |
| 226 | self.option_parser.add_option('--startup-pipe', type='int', |
| 227 | dest='startup_pipe', |
| 228 | help='File handle of pipe to parent process') |
| 229 | self.option_parser.add_option('--log-to-console', action='store_const', |
| 230 | const=True, default=False, |
| 231 | dest='log_to_console', |
| 232 | help='Enables or disables sys.stdout logging ' |
| 233 | 'to the console.') |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 234 | self.option_parser.add_option('--log-file', default='testserver.log', |
| 235 | dest='log_file', |
| 236 | help='The name of the server log file.') |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 237 | self.option_parser.add_option('--port', default=0, type='int', |
| 238 | help='Port used by the server. If ' |
| 239 | 'unspecified, the server will listen on an ' |
| 240 | 'ephemeral port.') |
| 241 | self.option_parser.add_option('--host', default='127.0.0.1', |
| 242 | dest='host', |
| 243 | help='Hostname or IP upon which the server ' |
| 244 | 'will listen. Client connections will also ' |
| 245 | 'only be allowed from this address.') |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 246 | self.option_parser.add_option('--data-dir', dest='data_dir', |
| 247 | help='Directory from which to read the ' |
| 248 | 'files.') |
jaekyun | e5b139c | 2015-03-10 15:54:29 -0700 | [diff] [blame] | 249 | self.option_parser.add_option('--on-remote-server', action='store_const', |
| 250 | const=True, default=False, |
| 251 | dest='on_remote_server', |
| 252 | help='Whether remote server is being used or ' |
| 253 | 'not.') |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 254 | |
| 255 | def _notify_startup_complete(self, server_data): |
| 256 | # Notify the parent that we've started. (BaseServer subclasses |
| 257 | # bind their sockets on construction.) |
wjia@chromium.org | ff532f3 | 2013-03-18 19:23:44 +0000 | [diff] [blame] | 258 | if self.options.startup_pipe is not None: |
Victor-Gabriel Savu | 1e3338b | 2020-05-20 07:59:50 +0000 | [diff] [blame] | 259 | server_data_json = json.dumps(server_data).encode() |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 260 | server_data_len = len(server_data_json) |
Victor-Gabriel Savu | 1e3338b | 2020-05-20 07:59:50 +0000 | [diff] [blame] | 261 | print('sending server_data: %s (%d bytes)' % |
| 262 | (server_data_json, server_data_len)) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 263 | if sys.platform == 'win32': |
| 264 | fd = msvcrt.open_osfhandle(self.options.startup_pipe, 0) |
| 265 | else: |
| 266 | fd = self.options.startup_pipe |
Victor-Gabriel Savu | 1e3338b | 2020-05-20 07:59:50 +0000 | [diff] [blame] | 267 | startup_pipe = os.fdopen(fd, "wb") |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 268 | # First write the data length as an unsigned 4-byte value. This |
| 269 | # is _not_ using network byte ordering since the other end of the |
| 270 | # pipe is on the same machine. |
| 271 | startup_pipe.write(struct.pack('=L', server_data_len)) |
| 272 | startup_pipe.write(server_data_json) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 273 | startup_pipe.close() |