2018-08-15 19:08:03 -04:00
|
|
|
#!/usr/bin/env python
|
2018-10-19 15:25:29 -04:00
|
|
|
# Copyright 2018 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.
|
|
|
|
import os
|
2018-10-16 11:54:08 -04:00
|
|
|
import sys
|
2018-08-15 19:08:03 -04:00
|
|
|
from threading import Thread
|
|
|
|
import SimpleHTTPServer
|
|
|
|
import SocketServer
|
|
|
|
from util import root_path
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
PORT = 4545
|
2018-10-09 20:31:06 -04:00
|
|
|
REDIRECT_PORT = 4546
|
2018-08-15 19:08:03 -04:00
|
|
|
|
|
|
|
|
2018-10-21 22:14:27 -04:00
|
|
|
class ContentTypeHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
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)
|
|
|
|
|
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-10-21 22:14:27 -04:00
|
|
|
return SimpleHTTPServer.SimpleHTTPRequestHandler.guess_type(self, path)
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
".json": "application/json",
|
|
|
|
})
|
2018-08-23 20:03:45 -04:00
|
|
|
SocketServer.TCPServer.allow_reuse_address = True
|
2018-09-25 21:39:04 -04:00
|
|
|
s = SocketServer.TCPServer(("", PORT), Handler)
|
2018-08-15 19:08:03 -04:00
|
|
|
print "Deno test server http://localhost:%d/" % PORT
|
2018-09-25 21:39:04 -04:00
|
|
|
return s
|
2018-08-15 19:08:03 -04:00
|
|
|
|
|
|
|
|
2018-10-09 20:31:06 -04:00
|
|
|
def redirect_server():
|
|
|
|
os.chdir(root_path)
|
|
|
|
target_host = "http://localhost:%d" % PORT
|
|
|
|
|
|
|
|
class RedirectHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|
|
|
def do_GET(self):
|
|
|
|
self.send_response(301)
|
|
|
|
self.send_header('Location', target_host + self.path)
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
Handler = RedirectHandler
|
|
|
|
SocketServer.TCPServer.allow_reuse_address = True
|
|
|
|
s = SocketServer.TCPServer(("", REDIRECT_PORT), Handler)
|
2018-11-30 03:27:41 -05:00
|
|
|
print "redirect server http://localhost:%d/ -> http://localhost:%d/" % (
|
2018-10-09 20:31:06 -04:00
|
|
|
REDIRECT_PORT, PORT)
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
2018-08-15 19:08:03 -04:00
|
|
|
def spawn():
|
2018-10-09 20:31:06 -04:00
|
|
|
# Main http server
|
2018-09-25 21:39:04 -04:00
|
|
|
s = server()
|
|
|
|
thread = Thread(target=s.serve_forever)
|
2018-08-15 19:08:03 -04:00
|
|
|
thread.daemon = True
|
|
|
|
thread.start()
|
2018-10-09 20:31:06 -04:00
|
|
|
# Redirect server
|
|
|
|
rs = redirect_server()
|
|
|
|
r_thread = Thread(target=rs.serve_forever)
|
|
|
|
r_thread.daemon = True
|
|
|
|
r_thread.start()
|
2018-08-15 19:08:03 -04:00
|
|
|
sleep(1) # TODO I'm too lazy to figure out how to do this properly.
|
2018-10-19 22:15:29 -04:00
|
|
|
return thread
|
2018-08-15 19:08:03 -04:00
|
|
|
|
|
|
|
|
2018-11-30 03:27:41 -05:00
|
|
|
def main():
|
2018-10-16 11:54:08 -04:00
|
|
|
try:
|
2018-10-19 22:15:29 -04:00
|
|
|
thread = spawn()
|
|
|
|
while thread.is_alive():
|
|
|
|
sleep(10)
|
2018-10-16 11:54:08 -04:00
|
|
|
except KeyboardInterrupt:
|
2018-10-19 22:15:29 -04:00
|
|
|
pass
|
|
|
|
sys.exit(1)
|
2018-11-30 03:27:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|