-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverRequestsHandler.py
More file actions
86 lines (64 loc) · 2.78 KB
/
serverRequestsHandler.py
File metadata and controls
86 lines (64 loc) · 2.78 KB
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
from http.server import BaseHTTPRequestHandler
import os
class HandleRequests(BaseHTTPRequestHandler):
def _set_headers(self, responseCode, message, contentType):
self.send_response(responseCode, message)
self.send_header('Content-type', contentType)
self.send_header("Access-Control-Allow-Origin","*")
self.send_header("Access-Control-Allow-Methods","*")
self.send_header("Access-Control-Allow-Headers","*")
self.end_headers()
def getServedStorageContentType(self, path: str):
extension = path.split(".")
if(len(extension) <= 1):
return 'text/plain'
else:
match extension[len(extension)-1].lower():
case "html":
return "text/html"
case "json":
return "application/json"
case "js":
return "text/javascript"
case "css":
return "text/css"
case _:
return "text/plain"
def serveStorageContent(self, baseHTTPRequestHandler, path: str ):
path = path.split('?')[0]
if path == "/":
path = "/index.html"
try:
file_to_serve = open('./storage'+path, 'rb')
self._set_headers(200, "OK", self.getServedStorageContentType(path))
baseHTTPRequestHandler.wfile.write(file_to_serve.read())
file_to_serve.close()
except Exception as e:
self._set_headers(501, "Generic Error", "text/plain")
def do_OPTIONS(self):
self._set_headers(200, "OK", "text/plain")
def do_HEAD(self):
self._set_headers(200, "OK", "text/plain")
def do_GET(self):
path = self.path
print(path)
self.serveStorageContent(self, path)
def do_POST(self):
print("received put request")
path = self.path
filename = 'test.txt'
if os.path.exists(filename):
self._set_headers(409, 'Conflict', "text/plain")
reply_body = '"%s" already exists\n' % filename
self.wfile.write(reply_body.encode('utf-8'))
return
file_length = int(self.headers['Content-Length'])
read = 0
with open(filename, 'wb+') as output_file:
while read < file_length:
new_read = self.rfile.read(min(66556, file_length - read))
read += len(new_read)
output_file.write(new_read)
self._set_headers(201, 'Created', "text/plain")
reply_body = 'Saved "%s"\n' % filename
self.wfile.write(reply_body.encode('utf-8'))