-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
56 lines (43 loc) · 1.48 KB
/
handler.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
from mylib.http import HTTPResponse, HTTPRequest, MIME_TYPES
import os
from mylib.read_file import read_file
def handler(conn, document_root, ip):
resp = HTTPResponse().set_default()
while True:
data = conn.recv(1024)
if not data:
break
req = HTTPRequest(data.decode("utf-8"))
# print('{} -- [{}] \"{} {}\"'.format(str(ip), resp.headers['Date'], req.method, req.path)) # access.log
if req.error == 'Unknown method':
resp.set_status(400, 'Bad Request')
elif req.error == 'Root directory escape':
resp.set_status(403, 'Forbidden')
elif req.method not in ['GET', 'HEAD']:
resp.set_status(405, 'Method Not Allowed')
else:
full_path = os.path.join(document_root, req.path)
if not os.path.exists(full_path):
resp.set_status(404, 'Not Found')
conn.send(resp.to_bytes_string())
break
if os.path.isdir(full_path):
full_path_index = os.path.join(full_path, 'index.html')
if os.path.exists(full_path_index):
full_path = full_path_index
req.file_type = 'html'
else:
resp.set_status(403, 'Forbidden')
conn.send(resp.to_bytes_string())
break
result = read_file(full_path)
if req.method == 'GET':
resp.add_body(result)
if req.file_type in MIME_TYPES.keys():
resp.add_header('Content-Type', '{}'.format(MIME_TYPES[req.file_type]))
else:
resp.add_header('Content-Type', '{}'.format('text/plain'))
resp.add_header('Content-Length', len(result))
conn.send(resp.to_bytes_string())
break
conn.close()