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 | It can use https if you specify the flag --https=CERT where CERT is the path |
| 14 | to a pem file containing the certificate and private key that should be used. |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 15 | """ |
| 16 | |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 17 | from __future__ import print_function |
| 18 | |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 19 | import base64 |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 20 | import logging |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 21 | import os |
akalin@chromium.org | 4e4f3c9 | 2010-11-27 04:04:52 +0000 | [diff] [blame] | 22 | import select |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 23 | from six.moves import BaseHTTPServer, socketserver |
| 24 | import six.moves.urllib.parse as urlparse |
agl@chromium.org | b3ec346 | 2012-03-19 20:32:47 +0000 | [diff] [blame] | 25 | import socket |
yhirano@chromium.org | 51f90d9 | 2014-03-24 04:49:23 +0000 | [diff] [blame] | 26 | import ssl |
agl@chromium.org | 77a9ad9 | 2012-03-20 15:14:27 +0000 | [diff] [blame] | 27 | import sys |
phajdan.jr@chromium.org | bf74e2b | 2010-08-17 20:07:11 +0000 | [diff] [blame] | 28 | |
maruel@chromium.org | 5ddc64e | 2013-12-05 17:50:12 +0000 | [diff] [blame] | 29 | BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 30 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(BASE_DIR))) |
| 31 | |
davidben@chromium.org | 7d53b54 | 2014-04-10 17:56:44 +0000 | [diff] [blame] | 32 | # 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] | 33 | # unconditionally (since they contain modifications from anything that might be |
| 34 | # obtained from e.g. PyPi). |
Keita Suzuki | 83e26f9 | 2020-03-06 09:42:48 +0000 | [diff] [blame] | 35 | 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] | 36 | sys.path.insert(0, os.path.join(ROOT_DIR, 'third_party', 'tlslite')) |
| 37 | |
yhirano@chromium.org | 51f90d9 | 2014-03-24 04:49:23 +0000 | [diff] [blame] | 38 | import mod_pywebsocket.standalone |
pliard@chromium.org | 3f8873f | 2012-11-14 11:38:55 +0000 | [diff] [blame] | 39 | from mod_pywebsocket.standalone import WebSocketServer |
yhirano@chromium.org | 51f90d9 | 2014-03-24 04:49:23 +0000 | [diff] [blame] | 40 | # import manually |
| 41 | mod_pywebsocket.standalone.ssl = ssl |
davidben@chromium.org | 06fcf20 | 2010-09-22 18:15:23 +0000 | [diff] [blame] | 42 | |
davidben@chromium.org | 7d53b54 | 2014-04-10 17:56:44 +0000 | [diff] [blame] | 43 | import tlslite |
| 44 | import tlslite.api |
| 45 | |
davidben@chromium.org | 7d53b54 | 2014-04-10 17:56:44 +0000 | [diff] [blame] | 46 | import testserver_base |
| 47 | |
maruel@chromium.org | 756cf98 | 2009-03-05 12:46:38 +0000 | [diff] [blame] | 48 | SERVER_HTTP = 0 |
Matt Menke | 3a293bd | 2021-08-13 20:34:43 +0000 | [diff] [blame] | 49 | SERVER_BASIC_AUTH_PROXY = 1 |
| 50 | SERVER_WEBSOCKET = 2 |
| 51 | SERVER_PROXY = 3 |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 52 | |
| 53 | # Default request queue size for WebSocketServer. |
| 54 | _DEFAULT_REQUEST_QUEUE_SIZE = 128 |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 55 | |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 56 | class WebSocketOptions: |
| 57 | """Holds options for WebSocketServer.""" |
| 58 | |
| 59 | def __init__(self, host, port, data_dir): |
| 60 | self.request_queue_size = _DEFAULT_REQUEST_QUEUE_SIZE |
| 61 | self.server_host = host |
| 62 | self.port = port |
| 63 | self.websock_handlers = data_dir |
| 64 | self.scan_dir = None |
| 65 | self.allow_handlers_outside_root_dir = False |
| 66 | self.websock_handlers_map_file = None |
| 67 | self.cgi_directories = [] |
| 68 | self.is_executable_method = None |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 69 | |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 70 | self.use_tls = False |
| 71 | self.private_key = None |
| 72 | self.certificate = None |
toyoshim@chromium.org | d532cf3 | 2012-10-18 05:05:51 +0000 | [diff] [blame] | 73 | self.tls_client_auth = False |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 74 | self.tls_client_ca = None |
| 75 | self.use_basic_auth = False |
Keita Suzuki | 83e26f9 | 2020-03-06 09:42:48 +0000 | [diff] [blame] | 76 | self.basic_auth_credential = 'Basic ' + base64.b64encode( |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 77 | b'test:test').decode() |
toyoshim@chromium.org | aa1b6e7 | 2012-10-09 03:43:19 +0000 | [diff] [blame] | 78 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 79 | |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 80 | class HTTPServer(testserver_base.ClientRestrictingServerMixIn, |
| 81 | testserver_base.BrokenPipeHandlerMixIn, |
| 82 | testserver_base.StoppableHTTPServer): |
agl@chromium.org | 77a9ad9 | 2012-03-20 15:14:27 +0000 | [diff] [blame] | 83 | """This is a specialization of StoppableHTTPServer that adds client |
erikwright@chromium.org | 847ef28 | 2012-02-22 16:41:10 +0000 | [diff] [blame] | 84 | verification.""" |
| 85 | |
| 86 | pass |
| 87 | |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 88 | |
| 89 | class ThreadingHTTPServer(socketserver.ThreadingMixIn, HTTPServer): |
Adam Rice | 34b2e31 | 2018-04-06 16:48:30 +0000 | [diff] [blame] | 90 | """This variant of HTTPServer creates a new thread for every connection. It |
| 91 | should only be used with handlers that are known to be threadsafe.""" |
| 92 | |
| 93 | pass |
| 94 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 95 | |
erikwright@chromium.org | 847ef28 | 2012-02-22 16:41:10 +0000 | [diff] [blame] | 96 | class HTTPSServer(tlslite.api.TLSSocketServerMixIn, |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 97 | testserver_base.ClientRestrictingServerMixIn, |
| 98 | testserver_base.BrokenPipeHandlerMixIn, |
| 99 | testserver_base.StoppableHTTPServer): |
agl@chromium.org | 77a9ad9 | 2012-03-20 15:14:27 +0000 | [diff] [blame] | 100 | """This is a specialization of StoppableHTTPServer that add https support and |
erikwright@chromium.org | 847ef28 | 2012-02-22 16:41:10 +0000 | [diff] [blame] | 101 | client verification.""" |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 102 | |
agl@chromium.org | 77a9ad9 | 2012-03-20 15:14:27 +0000 | [diff] [blame] | 103 | def __init__(self, server_address, request_hander_class, pem_cert_and_key, |
David Benjamin | 3ff96ea | 2021-11-17 05:07:53 +0000 | [diff] [blame^] | 104 | ssl_client_auth, ssl_client_cas, simulate_tls13_downgrade, |
| 105 | simulate_tls12_downgrade, tls_max_version): |
davidben@chromium.org | 7d53b54 | 2014-04-10 17:56:44 +0000 | [diff] [blame] | 106 | self.cert_chain = tlslite.api.X509CertChain() |
| 107 | self.cert_chain.parsePemList(pem_cert_and_key) |
phajdan.jr@chromium.org | 9e6098d | 2013-06-24 19:00:38 +0000 | [diff] [blame] | 108 | # Force using only python implementation - otherwise behavior is different |
| 109 | # depending on whether m2crypto Python module is present (error is thrown |
| 110 | # when it is). m2crypto uses a C (based on OpenSSL) implementation under |
| 111 | # the hood. |
| 112 | self.private_key = tlslite.api.parsePEMKey(pem_cert_and_key, |
| 113 | private=True, |
| 114 | implementations=['python']) |
davidben@chromium.org | 31282a1 | 2010-08-07 01:10:02 +0000 | [diff] [blame] | 115 | self.ssl_client_auth = ssl_client_auth |
rsleevi@chromium.org | b2ecdab | 2010-08-21 04:02:44 +0000 | [diff] [blame] | 116 | self.ssl_client_cas = [] |
agl@chromium.org | 143daa4 | 2012-04-26 18:45:34 +0000 | [diff] [blame] | 117 | |
davidben@chromium.org | c52e2e6 | 2014-05-20 21:51:44 +0000 | [diff] [blame] | 118 | if ssl_client_auth: |
| 119 | for ca_file in ssl_client_cas: |
| 120 | s = open(ca_file).read() |
| 121 | x509 = tlslite.api.X509() |
| 122 | x509.parse(s) |
| 123 | self.ssl_client_cas.append(x509.subject) |
| 124 | |
rsleevi@chromium.org | 2124c81 | 2010-10-28 11:57:36 +0000 | [diff] [blame] | 125 | self.ssl_handshake_settings = tlslite.api.HandshakeSettings() |
davidben | c16cde3 | 2015-01-21 18:21:30 -0800 | [diff] [blame] | 126 | # Enable SSLv3 for testing purposes. |
| 127 | self.ssl_handshake_settings.minVersion = (3, 0) |
David Benjamin | f839f1c | 2018-10-16 06:01:29 +0000 | [diff] [blame] | 128 | if simulate_tls13_downgrade: |
| 129 | self.ssl_handshake_settings.simulateTLS13Downgrade = True |
| 130 | if simulate_tls12_downgrade: |
| 131 | self.ssl_handshake_settings.simulateTLS12Downgrade = True |
| 132 | if tls_max_version != 0: |
| 133 | self.ssl_handshake_settings.maxVersion = (3, tls_max_version) |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 134 | |
David Benjamin | 9aadbed | 2021-09-15 03:29:09 +0000 | [diff] [blame] | 135 | self.session_cache = tlslite.api.SessionCache() |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 136 | testserver_base.StoppableHTTPServer.__init__(self, |
| 137 | server_address, |
| 138 | request_hander_class) |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 139 | |
| 140 | def handshake(self, tlsConnection): |
| 141 | """Creates the SSL connection.""" |
toyoshim@chromium.org | 9d7219e | 2012-10-25 03:30:10 +0000 | [diff] [blame] | 142 | |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 143 | try: |
agl@chromium.org | 04700be | 2013-03-02 18:40:41 +0000 | [diff] [blame] | 144 | self.tlsConnection = tlsConnection |
David Benjamin | 1bb63e1 | 2021-09-20 19:07:44 +0000 | [diff] [blame] | 145 | tlsConnection.handshakeServer(certChain=self.cert_chain, |
| 146 | privateKey=self.private_key, |
| 147 | sessionCache=self.session_cache, |
| 148 | reqCert=self.ssl_client_auth, |
| 149 | settings=self.ssl_handshake_settings, |
David Benjamin | 3ff96ea | 2021-11-17 05:07:53 +0000 | [diff] [blame^] | 150 | reqCAs=self.ssl_client_cas) |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 151 | tlsConnection.ignoreAbruptClose = True |
| 152 | return True |
phajdan.jr@chromium.org | bf74e2b | 2010-08-17 20:07:11 +0000 | [diff] [blame] | 153 | except tlslite.api.TLSAbruptCloseError: |
| 154 | # Ignore abrupt close. |
| 155 | return True |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 156 | except tlslite.api.TLSError as error: |
| 157 | print("Handshake failure:", str(error)) |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 158 | return False |
| 159 | |
akalin@chromium.org | 154bb13 | 2010-11-12 02:20:27 +0000 | [diff] [blame] | 160 | |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 161 | class TestPageHandler(testserver_base.BasePageHandler): |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 162 | def __init__(self, request, client_address, socket_server): |
David Benjamin | 1f5bdcf | 2021-09-15 14:46:41 +0000 | [diff] [blame] | 163 | connect_handlers = [self.DefaultConnectResponseHandler] |
| 164 | get_handlers = [self.DefaultResponseHandler] |
| 165 | post_handlers = get_handlers |
| 166 | put_handlers = get_handlers |
| 167 | head_handlers = [self.DefaultResponseHandler] |
rsimha@chromium.org | 99a6f17 | 2013-01-20 01:10:24 +0000 | [diff] [blame] | 168 | testserver_base.BasePageHandler.__init__(self, request, client_address, |
| 169 | socket_server, connect_handlers, |
| 170 | get_handlers, head_handlers, |
| 171 | post_handlers, put_handlers) |
nsylvain@chromium.org | 8d5763b | 2008-12-30 23:44:27 +0000 | [diff] [blame] | 172 | |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 173 | def DefaultResponseHandler(self): |
| 174 | """This is the catch-all response handler for requests that aren't handled |
| 175 | by one of the special handlers above. |
| 176 | Note that we specify the content-length as without it the https connection |
| 177 | is not closed properly (and the browser keeps expecting data).""" |
| 178 | |
| 179 | contents = "Default response given for path: " + self.path |
| 180 | self.send_response(200) |
mmenke@chromium.org | bfff75b | 2011-11-01 02:32:05 +0000 | [diff] [blame] | 181 | self.send_header('Content-Type', 'text/html') |
| 182 | self.send_header('Content-Length', len(contents)) |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 183 | self.end_headers() |
mmenke@chromium.org | bfff75b | 2011-11-01 02:32:05 +0000 | [diff] [blame] | 184 | if (self.command != 'HEAD'): |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 185 | self.wfile.write(contents.encode('utf8')) |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 186 | return True |
| 187 | |
wtc@chromium.org | 743d77b | 2009-02-11 02:48:15 +0000 | [diff] [blame] | 188 | def DefaultConnectResponseHandler(self): |
| 189 | """This is the catch-all response handler for CONNECT requests that aren't |
| 190 | handled by one of the special handlers above. Real Web servers respond |
| 191 | with 400 to CONNECT requests.""" |
| 192 | |
| 193 | contents = "Your client has issued a malformed or illegal request." |
| 194 | self.send_response(400) # bad request |
mmenke@chromium.org | bfff75b | 2011-11-01 02:32:05 +0000 | [diff] [blame] | 195 | self.send_header('Content-Type', 'text/html') |
| 196 | self.send_header('Content-Length', len(contents)) |
wtc@chromium.org | 743d77b | 2009-02-11 02:48:15 +0000 | [diff] [blame] | 197 | self.end_headers() |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 198 | self.wfile.write(contents.encode('utf8')) |
wtc@chromium.org | 743d77b | 2009-02-11 02:48:15 +0000 | [diff] [blame] | 199 | return True |
| 200 | |
akalin@chromium.org | 154bb13 | 2010-11-12 02:20:27 +0000 | [diff] [blame] | 201 | |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 202 | class ProxyRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 203 | """A request handler that behaves as a proxy server. Only CONNECT, GET and |
| 204 | HEAD methods are supported. |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 205 | """ |
| 206 | |
Pavol Marko | 8ccaaed | 2018-01-04 18:40:04 +0100 | [diff] [blame] | 207 | redirect_connect_to_localhost = False; |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 208 | |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 209 | def _start_read_write(self, sock): |
| 210 | sock.setblocking(0) |
| 211 | self.request.setblocking(0) |
| 212 | rlist = [self.request, sock] |
| 213 | while True: |
toyoshim@chromium.org | 9d7219e | 2012-10-25 03:30:10 +0000 | [diff] [blame] | 214 | ready_sockets, _unused, errors = select.select(rlist, [], []) |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 215 | if errors: |
| 216 | self.send_response(500) |
| 217 | self.end_headers() |
| 218 | return |
| 219 | for s in ready_sockets: |
| 220 | received = s.recv(1024) |
| 221 | if len(received) == 0: |
| 222 | return |
| 223 | if s == self.request: |
| 224 | other = sock |
| 225 | else: |
| 226 | other = self.request |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 227 | # This will lose data if the kernel write buffer fills up. |
| 228 | # TODO(ricea): Correctly use the return value to track how much was |
| 229 | # written and buffer the rest. Use select to determine when the socket |
| 230 | # becomes writable again. |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 231 | other.send(received) |
| 232 | |
| 233 | def _do_common_method(self): |
| 234 | url = urlparse.urlparse(self.path) |
| 235 | port = url.port |
| 236 | if not port: |
| 237 | if url.scheme == 'http': |
| 238 | port = 80 |
| 239 | elif url.scheme == 'https': |
| 240 | port = 443 |
| 241 | if not url.hostname or not port: |
| 242 | self.send_response(400) |
| 243 | self.end_headers() |
| 244 | return |
| 245 | |
| 246 | if len(url.path) == 0: |
| 247 | path = '/' |
| 248 | else: |
| 249 | path = url.path |
| 250 | if len(url.query) > 0: |
| 251 | path = '%s?%s' % (url.path, url.query) |
| 252 | |
| 253 | sock = None |
| 254 | try: |
| 255 | sock = socket.create_connection((url.hostname, port)) |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 256 | sock.send(('%s %s %s\r\n' % |
| 257 | (self.command, path, self.protocol_version)).encode('utf-8')) |
| 258 | for name, value in self.headers.items(): |
| 259 | if (name.lower().startswith('connection') |
| 260 | or name.lower().startswith('proxy')): |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 261 | continue |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 262 | # HTTP headers are encoded in Latin-1. |
| 263 | sock.send(b'%s: %s\r\n' % |
| 264 | (name.encode('latin-1'), value.encode('latin-1'))) |
| 265 | sock.send(b'\r\n') |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 266 | # This is wrong: it will pass through connection-level headers and |
| 267 | # misbehave on connection reuse. The only reason it works at all is that |
| 268 | # our test servers have never supported connection reuse. |
| 269 | # TODO(ricea): Use a proper HTTP client library instead. |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 270 | self._start_read_write(sock) |
toyoshim@chromium.org | 9d7219e | 2012-10-25 03:30:10 +0000 | [diff] [blame] | 271 | except Exception: |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 272 | logging.exception('failure in common method: %s %s', self.command, path) |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 273 | self.send_response(500) |
| 274 | self.end_headers() |
| 275 | finally: |
| 276 | if sock is not None: |
| 277 | sock.close() |
| 278 | |
| 279 | def do_CONNECT(self): |
| 280 | try: |
| 281 | pos = self.path.rfind(':') |
| 282 | host = self.path[:pos] |
| 283 | port = int(self.path[pos+1:]) |
toyoshim@chromium.org | 9d7219e | 2012-10-25 03:30:10 +0000 | [diff] [blame] | 284 | except Exception: |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 285 | self.send_response(400) |
| 286 | self.end_headers() |
| 287 | |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 288 | if ProxyRequestHandler.redirect_connect_to_localhost: |
Pavol Marko | 8ccaaed | 2018-01-04 18:40:04 +0100 | [diff] [blame] | 289 | host = "127.0.0.1" |
| 290 | |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 291 | sock = None |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 292 | try: |
| 293 | sock = socket.create_connection((host, port)) |
| 294 | self.send_response(200, 'Connection established') |
| 295 | self.end_headers() |
| 296 | self._start_read_write(sock) |
toyoshim@chromium.org | 9d7219e | 2012-10-25 03:30:10 +0000 | [diff] [blame] | 297 | except Exception: |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 298 | logging.exception('failure in CONNECT: %s', path) |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 299 | self.send_response(500) |
| 300 | self.end_headers() |
| 301 | finally: |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 302 | if sock is not None: |
| 303 | sock.close() |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 304 | |
| 305 | def do_GET(self): |
| 306 | self._do_common_method() |
| 307 | |
| 308 | def do_HEAD(self): |
| 309 | self._do_common_method() |
| 310 | |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 311 | class BasicAuthProxyRequestHandler(ProxyRequestHandler): |
| 312 | """A request handler that behaves as a proxy server which requires |
| 313 | basic authentication. |
| 314 | """ |
| 315 | |
| 316 | _AUTH_CREDENTIAL = 'Basic Zm9vOmJhcg==' # foo:bar |
| 317 | |
| 318 | def parse_request(self): |
| 319 | """Overrides parse_request to check credential.""" |
| 320 | |
| 321 | if not ProxyRequestHandler.parse_request(self): |
| 322 | return False |
| 323 | |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 324 | auth = self.headers.get('Proxy-Authorization', None) |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 325 | if auth != self._AUTH_CREDENTIAL: |
| 326 | self.send_response(407) |
| 327 | self.send_header('Proxy-Authenticate', 'Basic realm="MyRealm1"') |
| 328 | self.end_headers() |
| 329 | return False |
| 330 | |
| 331 | return True |
| 332 | |
bashi@chromium.org | 3323353 | 2012-09-08 17:37:24 +0000 | [diff] [blame] | 333 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 334 | class ServerRunner(testserver_base.TestServerRunner): |
| 335 | """TestServerRunner for the net test servers.""" |
phajdan.jr@chromium.org | bf74e2b | 2010-08-17 20:07:11 +0000 | [diff] [blame] | 336 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 337 | def __init__(self): |
| 338 | super(ServerRunner, self).__init__() |
phajdan.jr@chromium.org | bf74e2b | 2010-08-17 20:07:11 +0000 | [diff] [blame] | 339 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 340 | def __make_data_dir(self): |
| 341 | if self.options.data_dir: |
| 342 | if not os.path.isdir(self.options.data_dir): |
| 343 | raise testserver_base.OptionError('specified data dir not found: ' + |
| 344 | self.options.data_dir + ' exiting...') |
| 345 | my_data_dir = self.options.data_dir |
| 346 | else: |
| 347 | # 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] | 348 | my_data_dir = os.path.join(BASE_DIR, "..", "..", "data") |
phajdan.jr@chromium.org | bf74e2b | 2010-08-17 20:07:11 +0000 | [diff] [blame] | 349 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 350 | return my_data_dir |
newt@chromium.org | 1fc3274 | 2012-10-20 00:28:35 +0000 | [diff] [blame] | 351 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 352 | def create_server(self, server_data): |
| 353 | port = self.options.port |
| 354 | host = self.options.host |
newt@chromium.org | 1fc3274 | 2012-10-20 00:28:35 +0000 | [diff] [blame] | 355 | |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 356 | logging.basicConfig() |
| 357 | |
estark | 21667d6 | 2015-04-08 21:00:16 -0700 | [diff] [blame] | 358 | # Work around a bug in Mac OS 10.6. Spawning a WebSockets server |
| 359 | # will result in a call to |getaddrinfo|, which fails with "nodename |
| 360 | # nor servname provided" for localhost:0 on 10.6. |
Adam Rice | 54443aa | 2018-06-06 00:11:54 +0000 | [diff] [blame] | 361 | # TODO(ricea): Remove this if no longer needed. |
estark | 21667d6 | 2015-04-08 21:00:16 -0700 | [diff] [blame] | 362 | if self.options.server_type == SERVER_WEBSOCKET and \ |
| 363 | host == "localhost" and \ |
| 364 | port == 0: |
| 365 | host = "127.0.0.1" |
| 366 | |
Ryan Sleevi | 3bfd15d | 2018-01-23 21:12:24 +0000 | [diff] [blame] | 367 | # Construct the subjectAltNames for any ad-hoc generated certificates. |
| 368 | # As host can be either a DNS name or IP address, attempt to determine |
| 369 | # which it is, so it can be placed in the appropriate SAN. |
| 370 | dns_sans = None |
| 371 | ip_sans = None |
| 372 | ip = None |
| 373 | try: |
| 374 | ip = socket.inet_aton(host) |
| 375 | ip_sans = [ip] |
| 376 | except socket.error: |
| 377 | pass |
| 378 | if ip is None: |
| 379 | dns_sans = [host] |
| 380 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 381 | if self.options.server_type == SERVER_HTTP: |
| 382 | if self.options.https: |
David Benjamin | 8ed48d1 | 2021-09-14 21:28:30 +0000 | [diff] [blame] | 383 | if not self.options.cert_and_key_file: |
| 384 | raise testserver_base.OptionError('server cert file not specified') |
| 385 | if not os.path.isfile(self.options.cert_and_key_file): |
| 386 | raise testserver_base.OptionError( |
| 387 | 'specified server cert file not found: ' + |
| 388 | self.options.cert_and_key_file + ' exiting...') |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 389 | pem_cert_and_key = open(self.options.cert_and_key_file, 'r').read() |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 390 | |
| 391 | for ca_cert in self.options.ssl_client_ca: |
| 392 | if not os.path.isfile(ca_cert): |
| 393 | raise testserver_base.OptionError( |
| 394 | 'specified trusted client CA file not found: ' + ca_cert + |
| 395 | ' exiting...') |
ekasper@google.com | 3bce2cf | 2013-12-17 00:25:51 +0000 | [diff] [blame] | 396 | |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 397 | server = HTTPSServer( |
| 398 | (host, port), TestPageHandler, pem_cert_and_key, |
| 399 | self.options.ssl_client_auth, self.options.ssl_client_ca, |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 400 | self.options.simulate_tls13_downgrade, |
| 401 | self.options.simulate_tls12_downgrade, self.options.tls_max_version) |
| 402 | print('HTTPS server started on https://%s:%d...' % |
| 403 | (host, server.server_port)) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 404 | else: |
| 405 | server = HTTPServer((host, port), TestPageHandler) |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 406 | print('HTTP server started on http://%s:%d...' % |
| 407 | (host, server.server_port)) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 408 | |
| 409 | server.data_dir = self.__make_data_dir() |
| 410 | server.file_root_url = self.options.file_root_url |
| 411 | server_data['port'] = server.server_port |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 412 | elif self.options.server_type == SERVER_WEBSOCKET: |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 413 | # TODO(toyoshim): Remove following os.chdir. Currently this operation |
| 414 | # is required to work correctly. It should be fixed from pywebsocket side. |
| 415 | os.chdir(self.__make_data_dir()) |
| 416 | websocket_options = WebSocketOptions(host, port, '.') |
davidben@chromium.org | 009843a | 2014-06-03 00:13:08 +0000 | [diff] [blame] | 417 | scheme = "ws" |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 418 | if self.options.cert_and_key_file: |
davidben@chromium.org | 009843a | 2014-06-03 00:13:08 +0000 | [diff] [blame] | 419 | scheme = "wss" |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 420 | websocket_options.use_tls = True |
Sergey Ulanov | dd8b8ea | 2017-09-08 22:53:25 +0000 | [diff] [blame] | 421 | key_path = os.path.join(ROOT_DIR, self.options.cert_and_key_file) |
| 422 | if not os.path.isfile(key_path): |
| 423 | raise testserver_base.OptionError( |
| 424 | 'specified server cert file not found: ' + |
| 425 | self.options.cert_and_key_file + ' exiting...') |
| 426 | websocket_options.private_key = key_path |
| 427 | websocket_options.certificate = key_path |
| 428 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 429 | if self.options.ssl_client_auth: |
pneubeck@chromium.org | f500711 | 2014-07-21 15:22:41 +0000 | [diff] [blame] | 430 | websocket_options.tls_client_cert_optional = False |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 431 | websocket_options.tls_client_auth = True |
| 432 | if len(self.options.ssl_client_ca) != 1: |
| 433 | raise testserver_base.OptionError( |
| 434 | 'one trusted client CA file should be specified') |
| 435 | if not os.path.isfile(self.options.ssl_client_ca[0]): |
| 436 | raise testserver_base.OptionError( |
| 437 | 'specified trusted client CA file not found: ' + |
| 438 | self.options.ssl_client_ca[0] + ' exiting...') |
| 439 | websocket_options.tls_client_ca = self.options.ssl_client_ca[0] |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 440 | print('Trying to start websocket server on %s://%s:%d...' % |
| 441 | (scheme, websocket_options.server_host, websocket_options.port)) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 442 | server = WebSocketServer(websocket_options) |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 443 | print('WebSocket server started on %s://%s:%d...' % |
| 444 | (scheme, host, server.server_port)) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 445 | server_data['port'] = server.server_port |
ricea@chromium.org | a52ebdc | 2014-07-29 07:42:29 +0000 | [diff] [blame] | 446 | websocket_options.use_basic_auth = self.options.ws_basic_auth |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 447 | elif self.options.server_type == SERVER_PROXY: |
| 448 | ProxyRequestHandler.redirect_connect_to_localhost = \ |
| 449 | self.options.redirect_connect_to_localhost |
| 450 | server = ThreadingHTTPServer((host, port), ProxyRequestHandler) |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 451 | print('Proxy server started on port %d...' % server.server_port) |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 452 | server_data['port'] = server.server_port |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 453 | elif self.options.server_type == SERVER_BASIC_AUTH_PROXY: |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 454 | ProxyRequestHandler.redirect_connect_to_localhost = \ |
Pavol Marko | 8ccaaed | 2018-01-04 18:40:04 +0100 | [diff] [blame] | 455 | self.options.redirect_connect_to_localhost |
Adam Rice | 34b2e31 | 2018-04-06 16:48:30 +0000 | [diff] [blame] | 456 | server = ThreadingHTTPServer((host, port), BasicAuthProxyRequestHandler) |
David Benjamin | f025abe | 2021-09-17 22:59:19 +0000 | [diff] [blame] | 457 | print('BasicAuthProxy server started on port %d...' % server.server_port) |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 458 | server_data['port'] = server.server_port |
erikkay@google.com | d5182ff | 2009-01-08 20:45:27 +0000 | [diff] [blame] | 459 | else: |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 460 | raise testserver_base.OptionError('unknown server type' + |
| 461 | self.options.server_type) |
erikkay@google.com | 70397b6 | 2008-12-30 21:49:21 +0000 | [diff] [blame] | 462 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 463 | return server |
erikkay@google.com | d5182ff | 2009-01-08 20:45:27 +0000 | [diff] [blame] | 464 | |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 465 | def add_options(self): |
| 466 | testserver_base.TestServerRunner.add_options(self) |
Adam Rice | 9476b8c | 2018-08-02 15:28:43 +0000 | [diff] [blame] | 467 | self.option_parser.add_option('--proxy', action='store_const', |
| 468 | const=SERVER_PROXY, |
| 469 | default=SERVER_HTTP, dest='server_type', |
| 470 | help='start up a proxy server.') |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 471 | self.option_parser.add_option('--basic-auth-proxy', action='store_const', |
| 472 | const=SERVER_BASIC_AUTH_PROXY, |
| 473 | default=SERVER_HTTP, dest='server_type', |
| 474 | help='start up a proxy server which requires ' |
| 475 | 'basic authentication.') |
| 476 | self.option_parser.add_option('--websocket', action='store_const', |
| 477 | const=SERVER_WEBSOCKET, default=SERVER_HTTP, |
| 478 | dest='server_type', |
| 479 | help='start up a WebSocket server.') |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 480 | self.option_parser.add_option('--https', action='store_true', |
| 481 | dest='https', help='Specify that https ' |
| 482 | 'should be used.') |
| 483 | self.option_parser.add_option('--cert-and-key-file', |
| 484 | dest='cert_and_key_file', help='specify the ' |
| 485 | 'path to the file containing the certificate ' |
| 486 | 'and private key for the server in PEM ' |
| 487 | 'format') |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 488 | self.option_parser.add_option('--ssl-client-auth', action='store_true', |
| 489 | help='Require SSL client auth on every ' |
| 490 | 'connection.') |
| 491 | self.option_parser.add_option('--ssl-client-ca', action='append', |
| 492 | default=[], help='Specify that the client ' |
| 493 | 'certificate request should include the CA ' |
| 494 | 'named in the subject of the DER-encoded ' |
| 495 | 'certificate contained in the specified ' |
| 496 | 'file. This option may appear multiple ' |
| 497 | 'times, indicating multiple CA names should ' |
| 498 | 'be sent in the request.') |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 499 | self.option_parser.add_option('--file-root-url', default='/files/', |
| 500 | help='Specify a root URL for files served.') |
ricea@chromium.org | a52ebdc | 2014-07-29 07:42:29 +0000 | [diff] [blame] | 501 | # TODO(ricea): Generalize this to support basic auth for HTTP too. |
| 502 | self.option_parser.add_option('--ws-basic-auth', action='store_true', |
| 503 | dest='ws_basic_auth', |
| 504 | help='Enable basic-auth for WebSocket') |
David Benjamin | f839f1c | 2018-10-16 06:01:29 +0000 | [diff] [blame] | 505 | self.option_parser.add_option('--simulate-tls13-downgrade', |
| 506 | action='store_true') |
| 507 | self.option_parser.add_option('--simulate-tls12-downgrade', |
| 508 | action='store_true') |
| 509 | self.option_parser.add_option('--tls-max-version', default='0', type='int', |
| 510 | help='If non-zero, the maximum TLS version ' |
| 511 | 'to support. 1 means TLS 1.0, 2 means ' |
| 512 | 'TLS 1.1, and 3 means TLS 1.2.') |
Pavol Marko | 8ccaaed | 2018-01-04 18:40:04 +0100 | [diff] [blame] | 513 | self.option_parser.add_option('--redirect-connect-to-localhost', |
| 514 | dest='redirect_connect_to_localhost', |
| 515 | default=False, action='store_true', |
| 516 | help='If set, the Proxy server will connect ' |
| 517 | 'to localhost instead of the requested URL ' |
| 518 | 'on CONNECT requests') |
erikkay@google.com | d5182ff | 2009-01-08 20:45:27 +0000 | [diff] [blame] | 519 | |
toyoshim@chromium.org | 16f5752 | 2012-10-24 06:14:38 +0000 | [diff] [blame] | 520 | |
initial.commit | 94958cf | 2008-07-26 22:42:52 +0000 | [diff] [blame] | 521 | if __name__ == '__main__': |
mattm@chromium.org | 830a371 | 2012-11-07 23:00:07 +0000 | [diff] [blame] | 522 | sys.exit(ServerRunner().main()) |