-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_handler.py
46 lines (36 loc) · 1.37 KB
/
request_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
import http.server
import router
from router import Response
class RequestHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
result = router.doGet(self)
return self.do_Request(result)
def do_POST(self):
result = router.doPost(self)
return self.do_Request(result)
def do_PUT(self):
result = router.doPut(self)
return self.do_Request(result)
def do_DELETE(self):
result = router.doDelete(self)
return self.do_Request(result)
def send(self, status_code = 200, message = None, headers = {}, content_type = "text/plain"):
self.protocol_version = "HTTP/1.1"
self.send_response(status_code)
self.send_header("Content-Length", len(message))
self.send_header("Content-Type", content_type)
for header in headers.keys():
self.send_header(header, headers[header])
self.end_headers()
self.wfile.write(bytes(message, "utf8"))
def do_Request(self, result):
if (result == None):
self.send(404, "Not found")
return
if (not isinstance(result, Response)):
self.send(404, "Not found")
return
self.send(status_code=result.statusCode,
message=result.responseBody,
headers=result.headers,
content_type=result.contentType)