summaryrefslogtreecommitdiff
path: root/tools/http_server.py
blob: 7cb2e28f9c4afcef91bdbe403e4c0b49d9d7022b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python
# Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
# Many tests expect there to be an http server on port 4545 servering the deno
# root directory.
from collections import namedtuple
from contextlib import contextmanager
import os
import SimpleHTTPServer
import SocketServer
import socket
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
ANOTHER_REDIRECT_PORT = 4547
DOUBLE_REDIRECTS_PORT = 4548
INF_REDIRECTS_PORT = 4549
HTTPS_PORT = 5545


def create_http_arg_parser():
    parser = argparse.ArgumentParser()
    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 = os.path.join(root_path, "std/http/testdata/tls/localhost.crt")
KEY_FILE = os.path.join(root_path, "std/http/testdata/tls/localhost.key")
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):
    def log_request(self, code='-', size='-'):
        if not QUIET:
            SimpleHTTPServer.SimpleHTTPRequestHandler.log_request(
                self, code, size)


class ContentTypeHandler(QuietSimpleHTTPRequestHandler):
    def do_GET(self):

        # Check if there is a custom header configuration ending
        # with ".header" before sending the file
        maybe_header_file_path = "./" + self.path + ".header"
        if os.path.exists(maybe_header_file_path):
            self.protocol_version = 'HTTP/1.1'
            self.send_response(200, 'OK')

            f = open(maybe_header_file_path)
            for line in f:
                kv = line.split(": ")
                self.send_header(kv[0].strip(), kv[1].strip())
            f.close()
            self.end_headers()

            body = open("./" + self.path)
            self.wfile.write(body.read())
            body.close()
            return

        if "etag_script.ts" in self.path:
            self.protocol_version = 'HTTP/1.1'
            if_not_match = self.headers.getheader('if-none-match')
            if if_not_match == "33a64df551425fcc55e":
                self.send_response(304, 'Not Modified')
                self.send_header('Content-type', 'application/typescript')
                self.send_header('ETag', '33a64df551425fcc55e')
                self.end_headers()
            else:
                self.send_response(200, 'OK')
                self.send_header('Content-type', 'application/typescript')
                self.send_header('ETag', '33a64df551425fcc55e')
                self.end_headers()
                self.wfile.write(bytes("console.log('etag')"))
            return

        if "xTypeScriptTypes.js" in self.path:
            self.protocol_version = "HTTP/1.1"
            self.send_response(200, 'OK')
            self.send_header('Content-type', 'application/javascript')
            self.send_header('X-TypeScript-Types', './xTypeScriptTypes.d.ts')
            self.end_headers()
            self.wfile.write(bytes("export const foo = 'foo';"))
            return

        if "xTypeScriptTypes.d.ts" in self.path:
            self.protocol_version = "HTTP/1.1"
            self.send_response(200, 'OK')
            self.send_header('Content-type', 'application/typescript')
            self.end_headers()
            self.wfile.write(bytes("export const foo: 'foo';"))
            return

        if "referenceTypes.js" in self.path:
            self.protocol_version = "HTTP/1.1"
            self.send_response(200, 'OK')
            self.send_header('Content-type', 'application/javascript')
            self.end_headers()
            self.wfile.write(
                bytes('/// <reference types="./xTypeScriptTypes.d.ts" />\r\n'
                      'export const foo = "foo";\r\n'))
            return

        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)

    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'))
            if self.headers.has_key('user-agent'):
                self.send_header('user-agent',
                                 self.headers.getheader('user-agent'))
            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'))

    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"
        if ".t4." in path:
            return "application/x-typescript"
        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"
        if "form_urlencoded" in path:
            return "application/x-www-form-urlencoded"
        if "no_ext" in path:
            return "text/typescript"
        if "unknown_ext" in path:
            return "text/typescript"
        if "mismatch_ext" in path:
            return "text/javascript"
        return SimpleHTTPServer.SimpleHTTPRequestHandler.guess_type(self, path)


RunningServer = namedtuple("RunningServer", ["server", "thread"])


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
        # 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

    if use_https:
        return SSLThreadingTCPServer(("", port), handler, CERT_FILE, KEY_FILE)
    return SocketServer.TCPServer(("", port), handler)


def 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(PORT, Handler, False)
    if not QUIET:
        print "Deno test server http://localhost:%d/" % PORT
    return RunningServer(s, start(s))


def base_redirect_server(host_port, target_port, extra_path_segment=""):
    os.chdir(root_path)
    target_host = "http://localhost:%d" % target_port

    class RedirectHandler(QuietSimpleHTTPRequestHandler):
        def do_GET(self):
            self.send_response(301)
            self.send_header('Location',
                             target_host + extra_path_segment + self.path)
            self.end_headers()

    s = get_socket(host_port, RedirectHandler, False)
    if not QUIET:
        print "redirect server http://localhost:%d/ -> http://localhost:%d/" % (
            host_port, target_port)
    return RunningServer(s, start(s))


# 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="/cli/tests/subdir")


# redirect server that points to another redirect server
def double_redirects_server():
    return base_redirect_server(DOUBLE_REDIRECTS_PORT, REDIRECT_PORT)


# redirect server that points to itself
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
    thread.start()
    return thread


@contextmanager
def spawn():
    servers = (server(), redirect_server(), another_redirect_server(),
               double_redirects_server(), https_server())
    # In order to wait for each of the servers to be ready, we try connecting to
    # them with a tcp socket.
    for running_server in servers:
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        port = running_server.server.server_address[1]
        client.connect(("127.0.0.1", port))
        print "connected", port
        client.close()
        assert running_server.thread.is_alive()
    # The following output "ready" is specificly looked for in cli/test_util.rs
    # to prevent race conditions.
    print "ready"
    try:
        yield servers
    finally:
        for s in servers:
            s.server.shutdown()


def main():
    with spawn() as servers:
        try:
            while all(s.thread.is_alive() for s in servers):
                sleep(1)
        except KeyboardInterrupt:
            pass
    sys.exit(1)


if __name__ == '__main__':
    main()