-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.py
422 lines (355 loc) · 12.3 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import asyncio
import json
import time
import pprint
import re
ROUTES = {
"get": {},
"post": {},
"put": {},
"delete": {}
}
CONTENT_TYPE = {
"html": "text/html",
"css": "text/css",
"js": "application/javascript",
"jpeg": "image/jpeg",
"jpg": "image/jpg",
"png": "image/png",
"gif": "image/gif",
"ico": "image/x-icon",
"text": "text/plain",
"json": "application/json",
"pdf": "application/pdf"
}
MIDDLEWARES = []
ALLOWED_ORIGINS = []
def add_route(method, path, func):
"""ADD ROUTES
Build ROUTES
"""
path_regex = build_route_regex(path)
ROUTES[method][path] = (func, path_regex)
def add_middleware(func):
"""ADD middlewares
"""
MIDDLEWARES.append(func)
def build_route_regex(route):
route_regex = re.sub(r'(<\w+>)', r'(?P\1.+)', route)
return re.compile("^{}$".format(route_regex))
def add_allowed_origin(origin):
"""ADD allowed sources
"""
ALLOWED_ORIGINS.append(origin)
# Server Functions
async def worker(data):
"""WORKER
Accept requests and invoke request handler
"""
request = {}
header_str = data["header"].split("\r\n\r\n")[0]
if not header_str:
return err_404_handler(request, {})
request = header_parser(request, header_str)
request["body"] = data["content"]
if request:
result = await request_handler(request)
return result
# Parsers
def get_content(request):
query_string = request["path"].split("?")
request["path"] = query_string[0]
content = {}
for val in query_string[1].split("&"):
temp = val.split("=")
content[temp[0]] = temp[1]
return [request["path"], content]
def get_header(header_list):
header = {}
for each_line in header_list:
key, value = each_line.split(": ", 1)
header[key] = value
if "Cookie" in header:
cookies = header["Cookie"].split(";")
client_cookies = {}
for cookie in cookies:
head, body = cookie.strip().split("=", 1)
client_cookies[head] = body
header["Cookie"] = client_cookies
else:
header["Cookie"] = ""
return header
def header_parser(request, header_str):
"""
HTTP Header Parser
"""
header_list = header_str.split("\r\n")
first = header_list.pop(0)
status_line = first.split()
request["method"], request["path"], request["protocol"] = status_line
if "?" in request["path"]:
request["path"], request["content"] = get_content(request)
request["header"] = get_header(header_list)
return request
def form_parser(request):
"""FORM Parser"""
content_type = request["header"]["Content-Type"]
boundary = content_type.split("; ")[1]
request["boundary"] = "--" + boundary.split("=")[1]
for content in request["body"].split(request["boundary"].encode()):
form_header_dict = {}
data = {}
form_data = content.split(b"\r\n\r\n", 1)
form_header = form_data[0].split(b"\r\n")
form_body = ""
if len(form_data) > 1:
form_body = form_data[1]
for each_line in form_header:
if not each_line or b": " not in each_line:
continue
key, value = each_line.split(b": ")
form_header_dict[key] = value
if not form_header_dict:
continue
form = {}
for each_item in form_header_dict[b"Content-Disposition"].split(b"; "):
if b"=" in each_item:
name, value = each_item.split(b"=", 1)
data[name] = value.strip(b'"')
data["body"] = form_body
form[data[b"name"]] = data
request["form"] = form
return request
def multipart_parser(request):
content_dict = {}
for key, value in request["form"].items():
if b"filename" not in value:
content_dict[key.decode()] = value["body"].decode()
else:
content_dict[key.decode()] = value["body"]
return content_dict
def parse_fields(body):
content_dict = {}
data_split = body.split("&")
for val in data_split:
key, value = val.split("=")
content_dict[key.decode()] = value.decode()
return content_dict
# Handler Functions
async def request_handler(request):
"""Request Handler"""
response = {}
if "Origin" in request["header"]:
response = cors_handler(request, response)
if MIDDLEWARES:
for middleware in MIDDLEWARES:
if middleware.PRE:
request, response = middleware(request, response)
return method_handler(request, response)
def cors_handler(request, response):
"""CORS Request handler
handles CORS requests, that
has a "Origin" header.
"""
origin = request["header"]["Origin"]
if origin in ALLOWED_ORIGINS:
response["Access-Control-Allow-Origin"] = origin
response["Access-Control-Allow-Credentials"] = "true"
return response
def method_handler(request, response):
"""METHOD Handler
call respective method handler
"""
METHOD = {
"GET": get_handler,
"POST": post_handler,
"HEAD": head_handler,
"DELETE": delete_handler,
"PUT": put_handler,
"OPTIONS": options_handler
}
handler = METHOD[request["method"]]
return handler(request, response)
def handle_regex_mismatch(request):
method_path_dict = {"GET": ROUTES["get"][request["path"]],
"POST": ROUTES["post"][request["path"]],
"PUT": ROUTES["put"][request["path"]],
"DELETE": ROUTES["delete"][request["path"]]}
return method_path_dict[request['method']]
def route_match(request, response, ROUTES):
for func, path_regex in ROUTES.values():
m = path_regex.match(request["path"])
if m:
return func(request, response, **m.groupdict())
return handle_regex_mismatch(request)(request, response)
def get_handler(request, response):
"""HTTP GET Handler"""
try:
return route_match(request, response, ROUTES["get"])
except KeyError:
return static_file_handler(request, response)
def post_handler(request, response):
"""HTTP POST Handler"""
try:
content_type = request["header"]["Content-Type"]
if "multipart" in content_type:
request = form_parser(request)
request["content"] = multipart_parser(request)
elif "json" in content_type:
request["content"] = json.loads(request["body"].decode())
else:
request["content"] = parse_fields(request["body"])
return route_match(request, response, ROUTES["post"])
except KeyboardInterrupt:
return err_404_handler(request, response)
def put_handler(request, response):
"""HTTP PUT Handler"""
try:
content_type = request["header"]["Content-Type"]
if "multipart" in content_type:
request = form_parser(request)
request["content"] = multipart_parser(request)
elif "json" in content_type:
request["content"] = json.loads(request["body"].decode())
else:
request["content"] = parse_fields(request["body"])
return route_match(request, response, ROUTES["put"])
except KeyboardInterrupt:
return err_404_handler(request, response)
def delete_handler(request, response):
"""HTTP DELETE Handler"""
try:
return route_match(request, response, ROUTES["delete"])
except KeyError:
return err_404_handler(request, response)
def options_handler(request, response):
"""HTTP OPTIONS Handler"""
path_methods = [i.upper() for i, j in ROUTES.items() if request[
"path"] in j.keys()]
response[
"Access-Control-Allow-Methods"] = ', '.join(path_methods)
response[
"Access-Control-Allow-Headers"] = request["header"]["Access-Control-Request-Headers"]
response["content"] = ""
return ok_200_handler(request, response)
def head_handler(request, response):
"""HTTP HEAD Handler"""
get_handler(request, response)
response["content"] = ""
return response_handler(request, response)
def static_file_handler(request, response):
"""HTTP Static File Handler"""
try:
with open("./public" + request["path"], "rb") as file_descriptor:
res = file_descriptor.read()
except IOError:
return err_404_handler(request, response)
except FileNotFoundError:
res = b""
response["content"] = res
content_type = request["path"].split(".")[-1].lower()
response["Content-type"] = CONTENT_TYPE[content_type]
return ok_200_handler(request, response)
def err_404_handler(request, response):
"""HTTP 404 Handler"""
response["status"] = "HTTP/1.1 404 Not Found"
response["content"] = "Content Not Found"
response["Content-type"] = "text/HTML"
return response_handler(request, response)
def ok_200_handler(request, response):
"""HTTP 200 Handler"""
response["status"] = "HTTP/1.1 200 OK"
if response["content"] and response["Content-type"]:
response["Content-Length"] = str(len(response["content"]))
res = response_handler(request, response)
return res
def redirect(request, response, tmp_uri):
"""HTTP 302 handler"""
response["status"] = "HTTP/1.1 302 Found"
response["location"] = tmp_uri
res = response_handler(request, response)
return res
def response_handler(request, response):
"""HTTP response Handler"""
response["Date"] = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
response["Connection"] = "close"
response["Server"] = "magicserver0.2"
if MIDDLEWARES:
for middleware in MIDDLEWARES:
if middleware.POST:
request, response = middleware(request, response)
response_string = make_response(response)
return response_string
def send_html_handler(request, response, content):
"""send_html handler
Add html content to response
"""
if content:
response["content"] = content
response["Content-type"] = "text/html"
res = ok_200_handler(request, response)
return res
else:
return err_404_handler(request, response)
def send_json_handler(request, response, content):
"""send_json handler
Add JSON content to response
"""
if content:
response["content"] = json.dumps(content)
response["Content-type"] = "application/json"
return ok_200_handler(request, response)
else:
return err_404_handler(request, response)
def make_response(response):
"""
Make a byte string of the response dictionary
"""
response_string = response["status"] + "\r\n"
keys = [key for key in response if key not in ["status", "content"]]
for key in keys:
response_string += key + ": " + response[key] + "\r\n"
response_string += "\r\n"
response_string = response_string.encode()
if "content" in response:
content = response["content"]
if isinstance(content, str):
content = content.encode()
new_line = b"\r\n\r\n"
response_string += content + new_line
return response_string
def check_content(headers):
if b"Content-Length" in headers:
con_pos = headers.find(b"Content-Length")
col_pos = headers.find(b":", con_pos)
srsn = headers.find(b"\r\n", col_pos)
con_len = int(headers[col_pos + 2:srsn])
return con_len
async def handle_connections(reader, writer):
addr = writer.get_extra_info("peername")
print("Connection from:{0}".format(addr))
header = await reader.readuntil(b"\r\n\r\n")
content_length = check_content(header)
data = {"content": None}
data["header"] = header.decode()
pprint.pprint(data["header"])
if content_length:
content = await reader.readexactly(content_length)
data["content"] = content
response = await worker(data)
writer.write(response)
await writer.drain()
writer.close()
def start_server(hostname, port):
add_allowed_origin('http://0.0.0.0:{}'.format(port))
loop = asyncio.get_event_loop()
coro = asyncio.start_server(handle_connections, hostname, port, loop=loop)
server = loop.run_until_complete(coro)
print("Serving on {0}".format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()