David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 2 | # Copyright 2013 The Chromium Authors. All rights reserved. |
license.bot | f3378c2 | 2008-08-24 00:55:55 +0000 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 5 | |
Matt Menke | 3a293bd | 2021-08-13 20:34:43 +0000 | [diff] [blame] | 6 | """This is a simple HTTP/TCP/PROXY/BASIC_AUTH_PROXY/WEBSOCKET server used for |
| 7 | testing Chrome. |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 8 | |
| 9 | It supports several test URLs, as specified by the handlers in TestPageHandler. |
cbentzel@chromium.org | 0787bc7 | 2010-11-11 20:31:31 +0000 | [diff] [blame] | 10 | By default, it listens on an ephemeral port and sends the port number back to |
| 11 | the originating process over a pipe. The originating process can specify an |
| 12 | explicit port if necessary. |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 13 | """ |
| 14 | |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 15 | from __future__ import print_function |
| 16 | |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 17 | import base64 |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 18 | import logging |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 19 | import os |
akalin@chromium.org | 4e4f3c9 | 2010-11-27 04:04:52 +0000 | [diff] [blame] | 20 | import select |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 21 | from six.moves import BaseHTTPServer, socketserver |
| 22 | import six.moves.urllib.parse as urlparse |
agl@chromium.org | b3ec346 | 2012-03-19 20:32:47 +0000 | [diff] [blame] | 23 | import socket |
yhirano@chromium.org | 51f90d9 | 2014-03-24 04:49:23 +0000 | [diff] [blame] | 24 | import ssl |
agl@chromium.org | 77a9ad9 | 2012-03-20 15:14:27 +0000 | [diff] [blame] | 25 | import sys |
phajdan.jr@chromium.org | bf74e2b | 2010-08-17 20:07:11 +0000 | [diff] [blame] | 26 | |
maruel@chromium.org | 5ddc64e | 2013-12-05 17:50:12 +0000 | [diff] [blame] | 27 | BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 28 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(BASE_DIR))) |
| 29 | |
davidben@chromium.org | 7d53b54 | 2014-04-10 17:56:44 +0000 | [diff] [blame] | 30 | # Insert at the beginning of the path, we want to use our copies of the library |
Robert Iannucci | 0e7ec95 | 2018-01-18 22:44:16 +0000 | [diff] [blame] | 31 | # unconditionally (since they contain modifications from anything that might be |
| 32 | # obtained from e.g. PyPi). |
Keita Suzuki | 83e26f9 | 2020-03-06 09:42:48 +0000 | [diff] [blame] | 33 | sys.path.insert(0, os.path.join(ROOT_DIR, 'third_party', 'pywebsocket3', 'src')) |
davidben@chromium.org | 7d53b54 | 2014-04-10 17:56:44 +0000 | [diff] [blame] | 34 | |
yhirano@chromium.org | 51f90d9 | 2014-03-24 04:49:23 +0000 | [diff] [blame] | 35 | import mod_pywebsocket.standalone |
pliard@chromium.org | 3f8873f | 2012-11-14 11:38:55 +0000 | [diff] [blame] | 36 | from mod_pywebsocket.standalone import WebSocketServer |
yhirano@chromium.org | 51f90d9 | 2014-03-24 04:49:23 +0000 | [diff] [blame] | 37 | # import manually |
| 38 | mod_pywebsocket.standalone.ssl = ssl |
davidben@chromium.org | 06fcf20 | 2010-09-22 18:15:23 +0000 | [diff] [blame] | 39 | |
davidben@chromium.org | 7d53b54 | 2014-04-10 17:56:44 +0000 | [diff] [blame] | 40 | import testserver_base |
| 41 | |
maruel@chromium.org | 756cf98 | 2009-03-05 12:46:38 +0000 | [diff] [blame] | 42 | SERVER_HTTP = 0 |
Matt Menke | 3a293bd | 2021-08-13 20:34:43 +0000 | [diff] [blame] | 43 | SERVER_BASIC_AUTH_PROXY = 1 |
| 44 | SERVER_WEBSOCKET = 2 |
| 45 | SERVER_PROXY = 3 |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 46 | |
| 47 | # Default request queue size for WebSocketServer. |
| 48 | _DEFAULT_REQUEST_QUEUE_SIZE = 128 |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 49 | |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 50 | class WebSocketOptions: |
| 51 | """Holds options for WebSocketServer.""" |
| 52 | |
| 53 | def __init__(self, host, port, data_dir): |
| 54 | self.request_queue_size = _DEFAULT_REQUEST_QUEUE_SIZE |
| 55 | self.server_host = host |
| 56 | self.port = port |
| 57 | self.websock_handlers = data_dir |
| 58 | self.scan_dir = None |
| 59 | self.allow_handlers_outside_root_dir = False |
| 60 | self.websock_handlers_map_file = None |
| 61 | self.cgi_directories = [] |
| 62 | self.is_executable_method = None |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 63 | |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 64 | self.use_tls = False |
| 65 | self.private_key = None |
| 66 | self.certificate = None |
toyoshim@chromium.org | d532cf3 | 2012-10-18 05:05:51 +0000 | [diff] [blame] | 67 | self.tls_client_auth = False |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 68 | self.tls_client_ca = None |
| 69 | self.use_basic_auth = False |
Keita Suzuki | 83e26f9 | 2020-03-06 09:42:48 +0000 | [diff] [blame] | 70 | self.basic_auth_credential = 'Basic ' + base64.b64encode( |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 71 | b'test:test').decode() |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 72 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 73 | |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 74 | class HTTPServer(testserver_base.ClientRestrictingServerMixIn, |
| 75 | testserver_base.BrokenPipeHandlerMixIn, |
| 76 | testserver_base.StoppableHTTPServer): |
agl@chromium.org | 77a9ad9 | 2012-03-20 15:14:27 +0000 | [diff] [blame] | 77 | """This is a specialization of StoppableHTTPServer that adds client |
erikwright@chromium.org | 847ef28 | 2012-02-22 16:41:10 +0000 | [diff] [blame] | 78 | verification.""" |
| 79 | |
| 80 | pass |
| 81 | |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 82 | |
| 83 | class ThreadingHTTPServer(socketserver.ThreadingMixIn, HTTPServer): |
Adam Rice | 34b2e31 | 2018-04-06 16:48:30 +0000 | [diff] [blame] | 84 | """This variant of HTTPServer creates a new thread for every connection. It |
| 85 | should only be used with handlers that are known to be threadsafe.""" |
| 86 | |
| 87 | pass |
| 88 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 89 | |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 90 | class TestPageHandler(testserver_base.BasePageHandler): |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 91 | def __init__(self, request, client_address, socket_server): |
David Benjamin | 1f5bdcf | 2021-09-15 14:46:41 +0000 | [diff] [blame] | 92 | connect_handlers = [self.DefaultConnectResponseHandler] |
| 93 | get_handlers = [self.DefaultResponseHandler] |
| 94 | post_handlers = get_handlers |
| 95 | put_handlers = get_handlers |
| 96 | head_handlers = [self.DefaultResponseHandler] |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 97 | testserver_base.BasePageHandler.__init__(self, request, client_address, |
| 98 | socket_server, connect_handlers, |
| 99 | get_handlers, head_handlers, |
| 100 | post_handlers, put_handlers) |
nsylvain@chromium.org | 8d5763b | 2008-12-30 23:44:27 +0000 | [diff] [blame] | 101 | |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 102 | def DefaultResponseHandler(self): |
| 103 | """This is the catch-all response handler for requests that aren't handled |
| 104 | by one of the special handlers above. |
| 105 | Note that we specify the content-length as without it the https connection |
| 106 | is not closed properly (and the browser keeps expecting data).""" |
| 107 | |
| 108 | contents = "Default response given for path: " + self.path |
| 109 | self.send_response(200) |
mmenke@chromium.org | bfff75b | 2011-11-01 02:32:05 +0000 | [diff] [blame] | 110 | self.send_header('Content-Type', 'text/html') |
| 111 | self.send_header('Content-Length', len(contents)) |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 112 | self.end_headers() |
mmenke@chromium.org | bfff75b | 2011-11-01 02:32:05 +0000 | [diff] [blame] | 113 | if (self.command != 'HEAD'): |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 114 | self.wfile.write(contents.encode('utf8')) |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 115 | return True |
| 116 | |
wtc@chromium.org | 743d77b | 2009-02-11 02:48:15 +0000 | [diff] [blame] | 117 | def DefaultConnectResponseHandler(self): |
| 118 | """This is the catch-all response handler for CONNECT requests that aren't |
| 119 | handled by one of the special handlers above. Real Web servers respond |
| 120 | with 400 to CONNECT requests.""" |
| 121 | |
| 122 | contents = "Your client has issued a malformed or illegal request." |
| 123 | self.send_response(400) # bad request |
mmenke@chromium.org | bfff75b | 2011-11-01 02:32:05 +0000 | [diff] [blame] | 124 | self.send_header('Content-Type', 'text/html') |
| 125 | self.send_header('Content-Length', len(contents)) |
wtc@chromium.org | 743d77b | 2009-02-11 02:48:15 +0000 | [diff] [blame] | 126 | self.end_headers() |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 127 | self.wfile.write(contents.encode('utf8')) |
wtc@chromium.org | 743d77b | 2009-02-11 02:48:15 +0000 | [diff] [blame] | 128 | return True |
| 129 | |
akalin@chromium.org | 154bb13 | 2010-11-12 02:20:27 +0000 | [diff] [blame] | 130 | |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 131 | class ProxyRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 132 | """A request handler that behaves as a proxy server. Only CONNECT, GET and |
| 133 | HEAD methods are supported. |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 134 | """ |
| 135 | |
Pavol Marko | 8ccaaed | 2018-01-04 18:40:04 +0100 | [diff] [blame] | 136 | redirect_connect_to_localhost = False; |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 137 | |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 138 | def _start_read_write(self, sock): |
| 139 | sock.setblocking(0) |
| 140 | self.request.setblocking(0) |
| 141 | rlist = [self.request, sock] |
| 142 | while True: |
toyoshim@chromium.org | 9d7219e | 2012-10-25 03:30:10 +0000 | [diff] [blame] | 143 | ready_sockets, _unused, errors = select.select(rlist, [], []) |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 144 | if errors: |
| 145 | self.send_response(500) |
| 146 | self.end_headers() |
| 147 | return |
| 148 | for s in ready_sockets: |
| 149 | received = s.recv(1024) |
| 150 | if len(received) == 0: |
| 151 | return |
| 152 | if s == self.request: |
| 153 | other = sock |
| 154 | else: |
| 155 | other = self.request |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 156 | # This will lose data if the kernel write buffer fills up. |
| 157 | # TODO(ricea): Correctly use the return value to track how much was |
| 158 | # written and buffer the rest. Use select to determine when the socket |
| 159 | # becomes writable again. |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 160 | other.send(received) |
| 161 | |
| 162 | def _do_common_method(self): |
| 163 | url = urlparse.urlparse(self.path) |
| 164 | port = url.port |
| 165 | if not port: |
| 166 | if url.scheme == 'http': |
| 167 | port = 80 |
| 168 | elif url.scheme == 'https': |
| 169 | port = 443 |
| 170 | if not url.hostname or not port: |
| 171 | self.send_response(400) |
| 172 | self.end_headers() |
| 173 | return |
| 174 | |
| 175 | if len(url.path) == 0: |
| 176 | path = '/' |
| 177 | else: |
| 178 | path = url.path |
| 179 | if len(url.query) > 0: |
| 180 | path = '%s?%s' % (url.path, url.query) |
| 181 | |
| 182 | sock = None |
| 183 | try: |
| 184 | sock = socket.create_connection((url.hostname, port)) |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 185 | sock.send(('%s %s %s\r\n' % |
| 186 | (self.command, path, self.protocol_version)).encode('utf-8')) |
| 187 | for name, value in self.headers.items(): |
| 188 | if (name.lower().startswith('connection') |
| 189 | or name.lower().startswith('proxy')): |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 190 | continue |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 191 | # HTTP headers are encoded in Latin-1. |
| 192 | sock.send(b'%s: %s\r\n' % |
| 193 | (name.encode('latin-1'), value.encode('latin-1'))) |
| 194 | sock.send(b'\r\n') |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 195 | # This is wrong: it will pass through connection-level headers and |
| 196 | # misbehave on connection reuse. The only reason it works at all is that |
| 197 | # our test servers have never supported connection reuse. |
| 198 | # TODO(ricea): Use a proper HTTP client library instead. |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 199 | self._start_read_write(sock) |
toyoshim@chromium.org | 9d7219e | 2012-10-25 03:30:10 +0000 | [diff] [blame] | 200 | except Exception: |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 201 | logging.exception('failure in common method: %s %s', self.command, path) |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 202 | self.send_response(500) |
| 203 | self.end_headers() |
| 204 | finally: |
| 205 | if sock is not None: |
| 206 | sock.close() |
| 207 | |
| 208 | def do_CONNECT(self): |
| 209 | try: |
| 210 | pos = self.path.rfind(':') |
| 211 | host = self.path[:pos] |
| 212 | port = int(self.path[pos+1:]) |
toyoshim@chromium.org | 9d7219e | 2012-10-25 03:30:10 +0000 | [diff] [blame] | 213 | except Exception: |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 214 | self.send_response(400) |
| 215 | self.end_headers() |
| 216 | |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 217 | if ProxyRequestHandler.redirect_connect_to_localhost: |
Pavol Marko | 8ccaaed | 2018-01-04 18:40:04 +0100 | [diff] [blame] | 218 | host = "127.0.0.1" |
| 219 | |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 220 | sock = None |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 221 | try: |
| 222 | sock = socket.create_connection((host, port)) |
| 223 | self.send_response(200, 'Connection established') |
| 224 | self.end_headers() |
| 225 | self._start_read_write(sock) |
toyoshim@chromium.org | 9d7219e | 2012-10-25 03:30:10 +0000 | [diff] [blame] | 226 | except Exception: |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 227 | logging.exception('failure in CONNECT: %s', path) |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 228 | self.send_response(500) |
| 229 | self.end_headers() |
| 230 | finally: |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 231 | if sock is not None: |
| 232 | sock.close() |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 233 | |
| 234 | def do_GET(self): |
| 235 | self._do_common_method() |
| 236 | |
| 237 | def do_HEAD(self): |
| 238 | self._do_common_method() |
| 239 | |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 240 | class BasicAuthProxyRequestHandler(ProxyRequestHandler): |
| 241 | """A request handler that behaves as a proxy server which requires |
| 242 | basic authentication. |
| 243 | """ |
| 244 | |
| 245 | _AUTH_CREDENTIAL = 'Basic Zm9vOmJhcg==' # foo:bar |
| 246 | |
| 247 | def parse_request(self): |
| 248 | """Overrides parse_request to check credential.""" |
| 249 | |
| 250 | if not ProxyRequestHandler.parse_request(self): |
| 251 | return False |
| 252 | |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 253 | auth = self.headers.get('Proxy-Authorization', None) |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 254 | if auth != self._AUTH_CREDENTIAL: |
| 255 | self.send_response(407) |
| 256 | self.send_header('Proxy-Authenticate', 'Basic realm="MyRealm1"') |
| 257 | self.end_headers() |
| 258 | return False |
| 259 | |
| 260 | return True |
| 261 | |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 262 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 263 | class ServerRunner(testserver_base.TestServerRunner): |
| 264 | """TestServerRunner for the net test servers.""" |
phajdan.jr@chromium.org | bf74e2b | 2010-08-17 20:07:11 +0000 | [diff] [blame] | 265 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 266 | def __init__(self): |
| 267 | super(ServerRunner, self).__init__() |
phajdan.jr@chromium.org | bf74e2b | 2010-08-17 20:07:11 +0000 | [diff] [blame] | 268 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 269 | def __make_data_dir(self): |
| 270 | if self.options.data_dir: |
| 271 | if not os.path.isdir(self.options.data_dir): |
| 272 | raise testserver_base.OptionError('specified data dir not found: ' + |
| 273 | self.options.data_dir + ' exiting...') |
| 274 | my_data_dir = self.options.data_dir |
| 275 | else: |
| 276 | # Create the default path to our data dir, relative to the exe dir. |
Asanka Herath | 0ec3715 | 2019-08-02 15:23:57 +0000 | [diff] [blame] | 277 | my_data_dir = os.path.join(BASE_DIR, "..", "..", "data") |
phajdan.jr@chromium.org | bf74e2b | 2010-08-17 20:07:11 +0000 | [diff] [blame] | 278 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 279 | return my_data_dir |
newt@chromium.org | 1fc3274 | 2012-10-20 00:28:35 +0000 | [diff] [blame] | 280 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 281 | def create_server(self, server_data): |
| 282 | port = self.options.port |
| 283 | host = self.options.host |
newt@chromium.org | 1fc3274 | 2012-10-20 00:28:35 +0000 | [diff] [blame] | 284 | |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 285 | logging.basicConfig() |
| 286 | |
estark | 21667d6 | 2015-04-08 21:00:16 -0700 | [diff] [blame] | 287 | # Work around a bug in Mac OS 10.6. Spawning a WebSockets server |
| 288 | # will result in a call to |getaddrinfo|, which fails with "nodename |
| 289 | # nor servname provided" for localhost:0 on 10.6. |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 290 | # TODO(ricea): Remove this if no longer needed. |
estark | 21667d6 | 2015-04-08 21:00:16 -0700 | [diff] [blame] | 291 | if self.options.server_type == SERVER_WEBSOCKET and \ |
| 292 | host == "localhost" and \ |
| 293 | port == 0: |
| 294 | host = "127.0.0.1" |
| 295 | |
Ryan Sleevi | 3bfd15d | 2018-01-23 21:12:24 +0000 | [diff] [blame] | 296 | # Construct the subjectAltNames for any ad-hoc generated certificates. |
| 297 | # As host can be either a DNS name or IP address, attempt to determine |
| 298 | # which it is, so it can be placed in the appropriate SAN. |
| 299 | dns_sans = None |
| 300 | ip_sans = None |
| 301 | ip = None |
| 302 | try: |
| 303 | ip = socket.inet_aton(host) |
| 304 | ip_sans = [ip] |
| 305 | except socket.error: |
| 306 | pass |
| 307 | if ip is None: |
| 308 | dns_sans = [host] |
| 309 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 310 | if self.options.server_type == SERVER_HTTP: |
David Benjamin | 8599944 | 2021-11-22 21:01:55 +0000 | [diff] [blame^] | 311 | server = HTTPServer((host, port), TestPageHandler) |
| 312 | print('HTTP server started on http://%s:%d...' % |
| 313 | (host, server.server_port)) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 314 | |
| 315 | server.data_dir = self.__make_data_dir() |
| 316 | server.file_root_url = self.options.file_root_url |
| 317 | server_data['port'] = server.server_port |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 318 | elif self.options.server_type == SERVER_WEBSOCKET: |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 319 | # TODO(toyoshim): Remove following os.chdir. Currently this operation |
| 320 | # is required to work correctly. It should be fixed from pywebsocket side. |
| 321 | os.chdir(self.__make_data_dir()) |
| 322 | websocket_options = WebSocketOptions(host, port, '.') |
davidben@chromium.org | 009843a | 2014-06-03 00:13:08 +0000 | [diff] [blame] | 323 | scheme = "ws" |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 324 | if self.options.cert_and_key_file: |
davidben@chromium.org | 009843a | 2014-06-03 00:13:08 +0000 | [diff] [blame] | 325 | scheme = "wss" |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 326 | websocket_options.use_tls = True |
Sergey Ulanov | dd8b8ea | 2017-09-08 22:53:25 +0000 | [diff] [blame] | 327 | key_path = os.path.join(ROOT_DIR, self.options.cert_and_key_file) |
| 328 | if not os.path.isfile(key_path): |
| 329 | raise testserver_base.OptionError( |
| 330 | 'specified server cert file not found: ' + |
| 331 | self.options.cert_and_key_file + ' exiting...') |
| 332 | websocket_options.private_key = key_path |
| 333 | websocket_options.certificate = key_path |
| 334 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 335 | if self.options.ssl_client_auth: |
pneubeck@chromium.org | f500711 | 2014-07-21 15:22:41 +0000 | [diff] [blame] | 336 | websocket_options.tls_client_cert_optional = False |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 337 | websocket_options.tls_client_auth = True |
| 338 | if len(self.options.ssl_client_ca) != 1: |
| 339 | raise testserver_base.OptionError( |
| 340 | 'one trusted client CA file should be specified') |
| 341 | if not os.path.isfile(self.options.ssl_client_ca[0]): |
| 342 | raise testserver_base.OptionError( |
| 343 | 'specified trusted client CA file not found: ' + |
| 344 | self.options.ssl_client_ca[0] + ' exiting...') |
| 345 | websocket_options.tls_client_ca = self.options.ssl_client_ca[0] |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 346 | print('Trying to start websocket server on %s://%s:%d...' % |
| 347 | (scheme, websocket_options.server_host, websocket_options.port)) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 348 | server = WebSocketServer(websocket_options) |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 349 | print('WebSocket server started on %s://%s:%d...' % |
| 350 | (scheme, host, server.server_port)) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 351 | server_data['port'] = server.server_port |
ricea@chromium.org | a52ebdc | 2014-07-29 07:42:29 +0000 | [diff] [blame] | 352 | websocket_options.use_basic_auth = self.options.ws_basic_auth |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 353 | elif self.options.server_type == SERVER_PROXY: |
| 354 | ProxyRequestHandler.redirect_connect_to_localhost = \ |
| 355 | self.options.redirect_connect_to_localhost |
| 356 | server = ThreadingHTTPServer((host, port), ProxyRequestHandler) |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 357 | print('Proxy server started on port %d...' % server.server_port) |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 358 | server_data['port'] = server.server_port |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 359 | elif self.options.server_type == SERVER_BASIC_AUTH_PROXY: |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 360 | ProxyRequestHandler.redirect_connect_to_localhost = \ |
Pavol Marko | 8ccaaed | 2018-01-04 18:40:04 +0100 | [diff] [blame] | 361 | self.options.redirect_connect_to_localhost |
Adam Rice | 34b2e31 | 2018-04-06 16:48:30 +0000 | [diff] [blame] | 362 | server = ThreadingHTTPServer((host, port), BasicAuthProxyRequestHandler) |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 363 | print('BasicAuthProxy server started on port %d...' % server.server_port) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 364 | server_data['port'] = server.server_port |
erikkay@google.com | d5182ff | 2009-01-08 20:45:27 +0000 | [diff] [blame] | 365 | else: |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 366 | raise testserver_base.OptionError('unknown server type' + |
| 367 | self.options.server_type) |
erikkay@google.com | 70397b6 | 2008-12-30 21:49:21 +0000 | [diff] [blame] | 368 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 369 | return server |
erikkay@google.com | d5182ff | 2009-01-08 20:45:27 +0000 | [diff] [blame] | 370 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 371 | def add_options(self): |
| 372 | testserver_base.TestServerRunner.add_options(self) |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 373 | self.option_parser.add_option('--proxy', action='store_const', |
| 374 | const=SERVER_PROXY, |
| 375 | default=SERVER_HTTP, dest='server_type', |
| 376 | help='start up a proxy server.') |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 377 | self.option_parser.add_option('--basic-auth-proxy', action='store_const', |
| 378 | const=SERVER_BASIC_AUTH_PROXY, |
| 379 | default=SERVER_HTTP, dest='server_type', |
| 380 | help='start up a proxy server which requires ' |
| 381 | 'basic authentication.') |
| 382 | self.option_parser.add_option('--websocket', action='store_const', |
| 383 | const=SERVER_WEBSOCKET, default=SERVER_HTTP, |
| 384 | dest='server_type', |
| 385 | help='start up a WebSocket server.') |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 386 | self.option_parser.add_option('--cert-and-key-file', |
| 387 | dest='cert_and_key_file', help='specify the ' |
| 388 | 'path to the file containing the certificate ' |
| 389 | 'and private key for the server in PEM ' |
| 390 | 'format') |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 391 | self.option_parser.add_option('--ssl-client-auth', action='store_true', |
| 392 | help='Require SSL client auth on every ' |
| 393 | 'connection.') |
| 394 | self.option_parser.add_option('--ssl-client-ca', action='append', |
| 395 | default=[], help='Specify that the client ' |
| 396 | 'certificate request should include the CA ' |
| 397 | 'named in the subject of the DER-encoded ' |
| 398 | 'certificate contained in the specified ' |
| 399 | 'file. This option may appear multiple ' |
| 400 | 'times, indicating multiple CA names should ' |
| 401 | 'be sent in the request.') |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 402 | self.option_parser.add_option('--file-root-url', default='/files/', |
| 403 | help='Specify a root URL for files served.') |
ricea@chromium.org | a52ebdc | 2014-07-29 07:42:29 +0000 | [diff] [blame] | 404 | # TODO(ricea): Generalize this to support basic auth for HTTP too. |
| 405 | self.option_parser.add_option('--ws-basic-auth', action='store_true', |
| 406 | dest='ws_basic_auth', |
| 407 | help='Enable basic-auth for WebSocket') |
Pavol Marko | 8ccaaed | 2018-01-04 18:40:04 +0100 | [diff] [blame] | 408 | self.option_parser.add_option('--redirect-connect-to-localhost', |
| 409 | dest='redirect_connect_to_localhost', |
| 410 | default=False, action='store_true', |
| 411 | help='If set, the Proxy server will connect ' |
| 412 | 'to localhost instead of the requested URL ' |
| 413 | 'on CONNECT requests') |
erikkay@google.com | d5182ff | 2009-01-08 20:45:27 +0000 | [diff] [blame] | 414 | |
toyoshim@chromium.org | 16f5752 | 2012-10-24 06:14:38 +0000 | [diff] [blame] | 415 | |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 416 | if __name__ == '__main__': |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 417 | sys.exit(ServerRunner().main()) |