-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
executable file
·311 lines (252 loc) · 9.97 KB
/
server.py
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
#! /usr/bin/env python3
"""Simple Threaded HTTP 1.1 Server"""
# adapted from https://gist.github.com/nitaku/10d0662536f37a087e1b
import logging
import json
import signal
import sys
# import os
# import time
# import threading
# import traceback
import urllib
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
import re
import coloredlogs
HTTP_PORT = 9000
REDIS_PORT = 6379
ALLOW_FRONTEND_DOMAINS = [
re.compile(r"^(?:http:\/\/)localhost:" + str(HTTP_PORT) + '$'),
re.compile(r"^(?:http:\/\/)localhost:3000$"),
re.compile(r"^(?:https:\/\/)ttb-pot8o\.github\.io")
]
class Server(BaseHTTPRequestHandler):
'''
Base class for handling HTTP requests -- the core of the server.
'''
protocol_version = "HTTP/1.1"
def enable_dynamic_cors(self):
'''
Arguments: none
Returns: None
Throws: KeyError if the remote end never sent an Origin header.
Effects: Sends to the remote end a dynamically generated ACAO
header or none at all.
A trick needed to allow CORS from multiple, but not just any, other
domains.
The Access-Control-Allow-Origin header can only have one domain as
its value, so we test if the remote origin is allowed instead, and
send it back as the ACAO value.
If the remote origin isn't allowed, no ACAO header is sent, and
we assume the client implementation will enforce the same-origin
policy in that case (it's okay if this assumption falls through).
'''
http_origin = self.headers["origin"]
print(http_origin)
if any(map(lambda r: r.match(http_origin), ALLOW_FRONTEND_DOMAINS)):
self.send_header("Access-Control-Allow-Origin", http_origin)
def write_str(self, data):
'''
Arguments: data (a string or other string-like that can be cast to
bytes)
Returns: None
Throws: TypeError if data cannot be encoded to bytes() with
UTF8
Effects: Modifies self.wfile by writing bytes there.
Shorthand for writing a string back to the remote end.
'''
logger.debug("Response: " + str(data))
self.wfile.write(bytes(data, "utf-8"))
def write_json(self, obj):
'''
Arguments: obj (a dict)
Returns: None
Throws: json.dumps throws TypeError if the provided argument
is not serialisable to JSON, and anything thrown by
self.write_str
Effects: inherited
Take (probably) a Python dictionary and write it to the remote end
as JSON.
'''
self.write_str(json.dumps(obj, indent=2))
def write_json_error(self, err, expl=""):
'''
Arguments: err (an object) and expl (an object)
Returns: None
Throws: inherited
Effects: inherited
Take an error descriptor (a string, or other JSON-serialisable
object like a number or another dict) and an optional
explanation, and write them as a JSON object to the remote end.
'''
self.write_json( {"error": err, "explanation": expl} )
def set_headers(self, resp, headers=(), msg=None, close=True, csop=False):
'''
Arguments: resp (an int), headers (a tuple<tuple<string,
string>>), msg (a string), close (a bool), csop
(a bool)
Returns: None
Throws: inherited
its own exceptions)
Effects: Sends headers to the remote end, and calls
self.end_headers, meaning that no more headers can be
sent.
Sends the appropriate headers given an HTTP response code.
Also sends any headers specified in the headers argument.
If `headers` evaluates to False, a "Content-Type: application/json"
header is sent. Otherwise, the Content-Type is expected to be
present in `headers`.
An alternate message can be specified, so that instead of "200 OK",
"200 Hello" could be sent instead.
If close is True, its default value, the header "Connection: Close"
is sent. Otherwise, if close evaluates to False, "Connection:
keep-alive" is sent. This is not recommended.
If csop is True, Access-Control-Allow-Origin is set to *, allowing
requests from anywhere.
If called with 200 as the first argument, the following headers are
sent:
HTTP/1.1 200 OK
Server: BaseHTTP/0.6 Python/3.5.3
Date: Fri, 19 May 2017 12:14:12 GMT
Content-Type: application/json
Access-Control-Allow-Origin: <client origin or omitted>
Access-Control-Allow-Methods: HEAD,GET,POST,OPTIONS
Accept: application/json
Connection: Close
'''
self.send_response(resp, message=msg)
if not headers:
self.send_header("Content-Type", "application/json")
else:
for h in headers:
self.send_header(*h)
if csop:
self.send_header("Access-Control-Allow-Origin", "*")
else:
self.enable_dynamic_cors()
self.send_header("Connection", ["keep-alive", "Close"][close] )
self.send_header(
"Access-Control-Allow-Methods",
"HEAD,GET,POST,OPTIONS"
)
# self.send_header("Accept", "application/json")
# force HSTS
self.send_header("Strict-Transport-Security", "max-age=31536000")
self.end_headers()
def do_HEAD(self):
'''
Arguments: none
Returns: None
Throws: inherited
Effects: inherited
Reply to an HTTP HEAD request, sending the default headers.
'''
self.set_headers(200)
# handle GET, reply unsupported
def do_GET(self):
'''
Arguments: none
Returns: None
Throws: inherited
Effects: inherited
Reply to an HTTP GET request, probably with 404 or 405.
As yet undocumented: SOP Buster is a workaround for the Same Origin
Policy
'''
url = urllib.parse.urlparse(self.path)
# lose the leading `/` slash
endpoint = url.path[1:]
def _favicon():
self.set_headers(200, headers=(["Content-Type", "image/x-icon"],))
with open("favicon.ico", "rb") as icon:
self.wfile.write(icon.read())
def _all():
self.set_headers(200,
headers=(["Content-Type", "application/json"],))
with open("data/all.json", "r") as j:
self.write_str(j.read())
def _search():
qdict = urllib.parse.parse_qs(url.query)
search_query = qdict.get("query")
if search_query in (None, ""):
self.set_headers(406)
self.write_json_error(f"GET /search: requires ?query=<query>")
return
print(f"query was {search_query}")
self.set_headers(200,
headers=(["Content-Type", "application/json"],))
with open("data/all.json", "r") as j:
self.write_str(j.read())
def _unknown():
self.set_headers(404)
self.write_json_error(f"GET /{endpoint}: 404 Not Found")
{
"favicon.ico": _favicon,
"all": _all,
"search": _search,
}.get(endpoint, _unknown)()
def do_POST(self):
length = int(self.headers["content-length"])
msg_bytes = self.rfile.read(length)
msg_str = str(msg_bytes, "utf-8")
print(msg_str)
self.set_headers(400)
self.write_json_error('NotImplemented')
def do_OPTIONS(self):
'''
Arguments: none
Returns: None
Throws: inherited
Effects: inherited
Reply to an HTTP OPTIONS request.
Browsers use the OPTIONS method as a 'preflight check' on
XMLHttpRequest POST calls, to determine which headers are sent and
to tell whether making such a request would violate the same-origin
policy.
'''
self.set_headers(
200,
headers=(
(
"Access-Control-Allow-Headers",
"Content-Type, Access-Control-Allow-Headers, Origin, "
+ "Content-Length, Date, X-Unix-Epoch, Host, Connection"
),
)
)
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
def run(
server_class=ThreadedHTTPServer,
handler_class=Server,
http_port=HTTP_PORT,
redis_port=REDIS_PORT): # api_helper.LOCAL_PORT
http_address = ("", http_port)
httpd = server_class(http_address, handler_class)
logger.info("Starting HTTP on port {}...".format(http_port))
httpd.serve_forever()
def main():
from sys import argv
logger.info("=== STARTING ===")
if len(argv) == 2:
run(http_port=int(argv[1]))
else:
run()
def sigterm_handler(signo, stack_frame):
print()
logger.critical("it's all over")
# json_helper.kill_all_threads()
sys.exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGTERM, sigterm_handler)
signal.signal(signal.SIGINT, sigterm_handler)
coloredlogs.install(
level="NOTSET",
fmt="%(name)s[%(process)d] %(levelname)s %(message)s"
)
logger = logging.getLogger("server")
try:
main()
finally:
logger.critical("=== SHUTTING DOWN ===")