2018-08-15 19:08:03 -04:00
|
|
|
#!/usr/bin/env python
|
2020-01-02 15:13:47 -05:00
|
|
|
# Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2018-08-15 19:08:03 -04:00
|
|
|
# Many tests expect there to be an http server on port 4545 servering the deno
|
|
|
|
# root directory.
|
2019-05-30 16:40:40 -04:00
|
|
|
from collections import namedtuple
|
|
|
|
from contextlib import contextmanager
|
2018-08-15 19:08:03 -04:00
|
|
|
import os
|
|
|
|
import SimpleHTTPServer
|
|
|
|
import SocketServer
|
2019-11-19 12:56:37 -05:00
|
|
|
import socket
|
2019-05-30 16:40:40 -04:00
|
|
|
import sys
|
2018-08-15 19:08:03 -04:00
|
|
|
from time import sleep
|
2019-05-30 16:40:40 -04:00
|
|
|
from threading import Thread
|
|
|
|
from util import root_path
|
2018-08-15 19:08:03 -04:00
|
|
|
|
|
|
|
PORT = 4545
|
2018-10-09 20:31:06 -04:00
|
|
|
REDIRECT_PORT = 4546
|
2019-04-01 21:46:40 -04:00
|
|
|
ANOTHER_REDIRECT_PORT = 4547
|
|
|
|
DOUBLE_REDIRECTS_PORT = 4548
|
2019-06-24 09:34:09 -04:00
|
|
|
INF_REDIRECTS_PORT = 4549
|
2018-08-15 19:08:03 -04:00
|
|
|
|
2019-06-08 07:46:57 -04:00
|
|
|
QUIET = '-v' not in sys.argv and '--verbose' not in sys.argv
|
2018-08-15 19:08:03 -04:00
|
|
|
|
2019-06-08 07:46:57 -04:00
|
|
|
|
|
|
|
class QuietSimpleHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|
|
|
def log_request(self, code='-', size='-'):
|
|
|
|
if not QUIET:
|
|
|
|
SimpleHTTPServer.SimpleHTTPRequestHandler.log_request(
|
|
|
|
self, code, size)
|
|
|
|
|
|
|
|
|
|
|
|
class ContentTypeHandler(QuietSimpleHTTPRequestHandler):
|
2018-12-21 17:09:53 -05:00
|
|
|
def do_GET(self):
|
|
|
|
if "multipart_form_data.txt" in self.path:
|
|
|
|
self.protocol_version = 'HTTP/1.1'
|
|
|
|
self.send_response(200, 'OK')
|
|
|
|
self.send_header('Content-type',
|
|
|
|
'multipart/form-data;boundary=boundary')
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(
|
|
|
|
bytes('Preamble\r\n'
|
|
|
|
'--boundary\t \r\n'
|
|
|
|
'Content-Disposition: form-data; name="field_1"\r\n'
|
|
|
|
'\r\n'
|
|
|
|
'value_1 \r\n'
|
|
|
|
'\r\n--boundary\r\n'
|
|
|
|
'Content-Disposition: form-data; name="field_2"; '
|
|
|
|
'filename="file.js"\r\n'
|
|
|
|
'Content-Type: text/javascript\r\n'
|
|
|
|
'\r\n'
|
|
|
|
'console.log("Hi")'
|
|
|
|
'\r\n--boundary--\r\n'
|
|
|
|
'Epilogue'))
|
|
|
|
return
|
|
|
|
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
|
|
|
|
|
2019-01-03 06:41:20 -05:00
|
|
|
def do_POST(self):
|
|
|
|
# Simple echo server for request reflection
|
|
|
|
if "echo_server" in self.path:
|
|
|
|
self.protocol_version = 'HTTP/1.1'
|
|
|
|
self.send_response(200, 'OK')
|
|
|
|
if self.headers.has_key('content-type'):
|
|
|
|
self.send_header('content-type',
|
|
|
|
self.headers.getheader('content-type'))
|
2019-09-11 07:31:00 -04:00
|
|
|
if self.headers.has_key('user-agent'):
|
|
|
|
self.send_header('user-agent',
|
|
|
|
self.headers.getheader('user-agent'))
|
2019-01-03 06:41:20 -05:00
|
|
|
self.end_headers()
|
|
|
|
data_string = self.rfile.read(int(self.headers['Content-Length']))
|
|
|
|
self.wfile.write(bytes(data_string))
|
|
|
|
return
|
|
|
|
self.protocol_version = 'HTTP/1.1'
|
|
|
|
self.send_response(501)
|
|
|
|
self.send_header('content-type', 'text/plain')
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(bytes('Server does not support this operation'))
|
|
|
|
|
2018-10-21 22:14:27 -04:00
|
|
|
def guess_type(self, path):
|
|
|
|
if ".t1." in path:
|
|
|
|
return "text/typescript"
|
|
|
|
if ".t2." in path:
|
|
|
|
return "video/vnd.dlna.mpeg-tts"
|
|
|
|
if ".t3." in path:
|
|
|
|
return "video/mp2t"
|
2018-10-27 20:26:42 -04:00
|
|
|
if ".t4." in path:
|
|
|
|
return "application/x-typescript"
|
2018-10-21 22:14:27 -04:00
|
|
|
if ".j1." in path:
|
|
|
|
return "text/javascript"
|
|
|
|
if ".j2." in path:
|
|
|
|
return "application/ecmascript"
|
|
|
|
if ".j3." in path:
|
|
|
|
return "text/ecmascript"
|
|
|
|
if ".j4." in path:
|
|
|
|
return "application/x-javascript"
|
2018-12-21 17:09:53 -05:00
|
|
|
if "form_urlencoded" in path:
|
|
|
|
return "application/x-www-form-urlencoded"
|
2018-12-27 15:40:06 -05:00
|
|
|
if "no_ext" in path:
|
|
|
|
return "text/typescript"
|
|
|
|
if "unknown_ext" in path:
|
|
|
|
return "text/typescript"
|
|
|
|
if "mismatch_ext" in path:
|
|
|
|
return "text/javascript"
|
2018-10-21 22:14:27 -04:00
|
|
|
return SimpleHTTPServer.SimpleHTTPRequestHandler.guess_type(self, path)
|
|
|
|
|
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
RunningServer = namedtuple("RunningServer", ["server", "thread"])
|
|
|
|
|
|
|
|
|
2019-11-19 12:56:37 -05:00
|
|
|
def get_socket(port, handler):
|
|
|
|
SocketServer.TCPServer.allow_reuse_address = True
|
|
|
|
if os.name != "nt":
|
|
|
|
# We use AF_INET6 to avoid flaky test issue, particularly with
|
|
|
|
# the test 019_media_types. It's not well understood why this fixes the
|
|
|
|
# flaky tests, but it does appear to...
|
|
|
|
# See https://github.com/denoland/deno/issues/3332
|
|
|
|
SocketServer.TCPServer.address_family = socket.AF_INET6
|
|
|
|
return SocketServer.TCPServer(("", port), handler)
|
|
|
|
|
|
|
|
|
2018-09-25 21:39:04 -04:00
|
|
|
def server():
|
2018-08-15 19:08:03 -04:00
|
|
|
os.chdir(root_path) # Hopefully the main thread doesn't also chdir.
|
2018-10-21 22:14:27 -04:00
|
|
|
Handler = ContentTypeHandler
|
|
|
|
Handler.extensions_map.update({
|
|
|
|
".ts": "application/typescript",
|
|
|
|
".js": "application/javascript",
|
2019-10-16 13:35:04 -04:00
|
|
|
".tsx": "application/typescript",
|
|
|
|
".jsx": "application/javascript",
|
2018-10-21 22:14:27 -04:00
|
|
|
".json": "application/json",
|
|
|
|
})
|
2019-11-19 12:56:37 -05:00
|
|
|
s = get_socket(PORT, Handler)
|
2019-06-08 07:46:57 -04:00
|
|
|
if not QUIET:
|
|
|
|
print "Deno test server http://localhost:%d/" % PORT
|
2019-05-30 16:40:40 -04:00
|
|
|
return RunningServer(s, start(s))
|
2018-08-15 19:08:03 -04:00
|
|
|
|
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
def base_redirect_server(host_port, target_port, extra_path_segment=""):
|
2018-10-09 20:31:06 -04:00
|
|
|
os.chdir(root_path)
|
2019-04-01 21:46:40 -04:00
|
|
|
target_host = "http://localhost:%d" % target_port
|
2018-10-09 20:31:06 -04:00
|
|
|
|
2019-06-08 07:46:57 -04:00
|
|
|
class RedirectHandler(QuietSimpleHTTPRequestHandler):
|
2018-10-09 20:31:06 -04:00
|
|
|
def do_GET(self):
|
|
|
|
self.send_response(301)
|
2019-04-01 21:46:40 -04:00
|
|
|
self.send_header('Location',
|
|
|
|
target_host + extra_path_segment + self.path)
|
2018-10-09 20:31:06 -04:00
|
|
|
self.end_headers()
|
|
|
|
|
2019-11-19 12:56:37 -05:00
|
|
|
s = get_socket(host_port, RedirectHandler)
|
2019-06-08 07:46:57 -04:00
|
|
|
if not QUIET:
|
|
|
|
print "redirect server http://localhost:%d/ -> http://localhost:%d/" % (
|
|
|
|
host_port, target_port)
|
2019-05-30 16:40:40 -04:00
|
|
|
return RunningServer(s, start(s))
|
2018-10-09 20:31:06 -04:00
|
|
|
|
|
|
|
|
2019-04-01 21:46:40 -04:00
|
|
|
# redirect server
|
|
|
|
def redirect_server():
|
|
|
|
return base_redirect_server(REDIRECT_PORT, PORT)
|
|
|
|
|
|
|
|
|
|
|
|
# another redirect server pointing to the same port as the one above
|
|
|
|
# BUT with an extra subdir path
|
|
|
|
def another_redirect_server():
|
|
|
|
return base_redirect_server(
|
|
|
|
ANOTHER_REDIRECT_PORT, PORT, extra_path_segment="/tests/subdir")
|
|
|
|
|
|
|
|
|
|
|
|
# redirect server that points to another redirect server
|
|
|
|
def double_redirects_server():
|
|
|
|
return base_redirect_server(DOUBLE_REDIRECTS_PORT, REDIRECT_PORT)
|
|
|
|
|
|
|
|
|
2019-06-24 09:34:09 -04:00
|
|
|
# redirect server that points to itself
|
|
|
|
def inf_redirects_server():
|
|
|
|
return base_redirect_server(INF_REDIRECTS_PORT, INF_REDIRECTS_PORT)
|
|
|
|
|
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
def start(s):
|
|
|
|
thread = Thread(target=s.serve_forever, kwargs={"poll_interval": 0.05})
|
2018-08-15 19:08:03 -04:00
|
|
|
thread.daemon = True
|
|
|
|
thread.start()
|
2018-10-19 22:15:29 -04:00
|
|
|
return thread
|
2018-08-15 19:08:03 -04:00
|
|
|
|
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
@contextmanager
|
|
|
|
def spawn():
|
|
|
|
servers = (server(), redirect_server(), another_redirect_server(),
|
|
|
|
double_redirects_server())
|
2019-06-08 07:46:57 -04:00
|
|
|
while any(not s.thread.is_alive() for s in servers):
|
|
|
|
sleep(0.01)
|
2019-05-30 16:40:40 -04:00
|
|
|
try:
|
2019-10-29 16:06:14 -04:00
|
|
|
print "ready"
|
|
|
|
yield servers
|
2019-05-30 16:40:40 -04:00
|
|
|
finally:
|
|
|
|
for s in servers:
|
|
|
|
s.server.shutdown()
|
|
|
|
|
|
|
|
|
2018-11-30 03:27:41 -05:00
|
|
|
def main():
|
2019-10-29 16:06:14 -04:00
|
|
|
with spawn() as servers:
|
|
|
|
try:
|
|
|
|
while all(s.thread.is_alive() for s in servers):
|
|
|
|
sleep(1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2018-10-19 22:15:29 -04:00
|
|
|
sys.exit(1)
|
2018-11-30 03:27:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|