summaryrefslogtreecommitdiff
path: root/tools/http_server.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/http_server.py')
-rwxr-xr-xtools/http_server.py77
1 files changed, 72 insertions, 5 deletions
diff --git a/tools/http_server.py b/tools/http_server.py
index 871888a4e..2097c153d 100755
--- a/tools/http_server.py
+++ b/tools/http_server.py
@@ -12,6 +12,9 @@ import sys
from time import sleep
from threading import Thread
from util import root_path
+import ssl
+import getopt
+import argparse
PORT = 4545
REDIRECT_PORT = 4546
@@ -19,7 +22,52 @@ ANOTHER_REDIRECT_PORT = 4547
DOUBLE_REDIRECTS_PORT = 4548
INF_REDIRECTS_PORT = 4549
-QUIET = '-v' not in sys.argv and '--verbose' not in sys.argv
+HTTPS_PORT = 5545
+
+
+def create_http_arg_parser():
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--certfile')
+ parser.add_argument('--keyfile')
+ parser.add_argument('--verbose', '-v', action='store_true')
+ return parser
+
+
+HttpArgParser = create_http_arg_parser()
+
+args, unknown = HttpArgParser.parse_known_args(sys.argv[1:])
+CERT_FILE = args.certfile
+KEY_FILE = args.keyfile
+QUIET = not args.verbose
+
+
+class SSLTCPServer(SocketServer.TCPServer):
+ def __init__(self,
+ server_address,
+ request_handler,
+ certfile,
+ keyfile,
+ ssl_version=ssl.PROTOCOL_TLSv1_2,
+ bind_and_activate=True):
+ SocketServer.TCPServer.__init__(self, server_address, request_handler,
+ bind_and_activate)
+ self.certfile = certfile
+ self.keyfile = keyfile
+ self.ssl_version = ssl_version
+
+ def get_request(self):
+ newsocket, fromaddr = self.socket.accept()
+ connstream = ssl.wrap_socket(
+ newsocket,
+ server_side=True,
+ certfile=self.certfile,
+ keyfile=self.keyfile,
+ ssl_version=self.ssl_version)
+ return connstream, fromaddr
+
+
+class SSLThreadingTCPServer(SocketServer.ThreadingMixIn, SSLTCPServer):
+ pass
class QuietSimpleHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
@@ -169,7 +217,7 @@ class ContentTypeHandler(QuietSimpleHTTPRequestHandler):
RunningServer = namedtuple("RunningServer", ["server", "thread"])
-def get_socket(port, handler):
+def get_socket(port, handler, use_https):
SocketServer.TCPServer.allow_reuse_address = True
if os.name != "nt":
# We use AF_INET6 to avoid flaky test issue, particularly with
@@ -177,6 +225,9 @@ def get_socket(port, handler):
# flaky tests, but it does appear to...
# See https://github.com/denoland/deno/issues/3332
SocketServer.TCPServer.address_family = socket.AF_INET6
+
+ if use_https:
+ return SSLThreadingTCPServer(("", port), handler, CERT_FILE, KEY_FILE)
return SocketServer.TCPServer(("", port), handler)
@@ -190,7 +241,7 @@ def server():
".jsx": "application/javascript",
".json": "application/json",
})
- s = get_socket(PORT, Handler)
+ s = get_socket(PORT, Handler, False)
if not QUIET:
print "Deno test server http://localhost:%d/" % PORT
return RunningServer(s, start(s))
@@ -207,7 +258,7 @@ def base_redirect_server(host_port, target_port, extra_path_segment=""):
target_host + extra_path_segment + self.path)
self.end_headers()
- s = get_socket(host_port, RedirectHandler)
+ s = get_socket(host_port, RedirectHandler, False)
if not QUIET:
print "redirect server http://localhost:%d/ -> http://localhost:%d/" % (
host_port, target_port)
@@ -236,6 +287,22 @@ def inf_redirects_server():
return base_redirect_server(INF_REDIRECTS_PORT, INF_REDIRECTS_PORT)
+def https_server():
+ os.chdir(root_path) # Hopefully the main thread doesn't also chdir.
+ Handler = ContentTypeHandler
+ Handler.extensions_map.update({
+ ".ts": "application/typescript",
+ ".js": "application/javascript",
+ ".tsx": "application/typescript",
+ ".jsx": "application/javascript",
+ ".json": "application/json",
+ })
+ s = get_socket(HTTPS_PORT, Handler, True)
+ if not QUIET:
+ print "Deno https test server https://localhost:%d/" % HTTPS_PORT
+ return RunningServer(s, start(s))
+
+
def start(s):
thread = Thread(target=s.serve_forever, kwargs={"poll_interval": 0.05})
thread.daemon = True
@@ -246,7 +313,7 @@ def start(s):
@contextmanager
def spawn():
servers = (server(), redirect_server(), another_redirect_server(),
- double_redirects_server())
+ double_redirects_server(), https_server())
while any(not s.thread.is_alive() for s in servers):
sleep(0.01)
try: