-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
175 lines (138 loc) · 5.08 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
from http.server import HTTPServer, BaseHTTPRequestHandler, SimpleHTTPRequestHandler
from http import HTTPStatus
import urllib.parse
import posixpath
import mimetypes
import shutil
import hashlib
import struct
import ssl
import os
versioned_files = [
"./index.html",
"./global.css",
"./favicon.png",
"./app.webmanifest",
"./icon.png",
"./build/bundle.css",
"./build/bundle.js",
"./helpers.wasm",
"./load-icon.svg",
"./abc-icon.svg",
"./STK-icon.svg",
"./expand-icon.svg",
"./collapse-icon.svg"
]
def packString(buf, string):
string = string.encode("utf-8")
buf.extend(struct.pack("<H", len(string)))
buf.extend(string)
def packFileVersionRecord(buf, filename):
packString(buf, filename)
def generateFileVersionRecords(files):
buf = bytearray()
filehashes = []
size = 0
for filename in files:
with open(filename, "rb") as f:
data = f.read()
hasher = hashlib.new("sha256")
hasher.update(data)
filehash = hasher.hexdigest()
filehashes.append((filename, hasher.digest()))
size += len(data)
hasher = hashlib.new("sha256")
# sort so that the order of files doesn't matter
for (filename, filehash) in sorted(filehashes):
hasher.update(filename.encode("utf-8"))
hasher.update(bytes(1)) # 0-byte separator
hasher.update(filehash)
hasher.update(bytes(1))
apphash = hasher.hexdigest()
print("\n\nCurrent version: " + apphash + "\n")
packString(buf, apphash)
buf.extend(struct.pack("<I", size))
buf.extend(struct.pack("<H", len(files)))
for filename in files:
packFileVersionRecord(buf, filename)
return buf
class Handler(BaseHTTPRequestHandler):
server_version = "calm/0.1"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def do_GET(self):
path = urllib.parse.urlsplit(self.path).path
path = urllib.parse.unquote(path)
path = posixpath.normpath(path)
components = path.split("/")[1:]
path = "."
for component in components:
if component == os.pardir:
self.send_response(HTTPStatus.FORBIDDEN)
self.end_headers()
return
path += "/" + component
print()
print("Request for path: " + path)
print("Headers:")
for (header, value) in self.headers.items():
print(" " + header + ": " + value)
print()
if path == "./":
path = "./index.html"
if path == "./serviceworker.js":
path = "./serviceworker-release.js"
#pass
if path == "./version":
print("getting version...")
data = generateFileVersionRecords(versioned_files)
content_type = "application/octet-stream"
self.send_response(HTTPStatus.OK)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", len(data))
self.end_headers()
self.wfile.write(data)
print()
print(data)
print()
return
if path.split("/")[1] == "versioned":
version, *path = path.split("/")[2:]
path = "./" + "/".join(path)
else:
version = None
if os.path.isfile(path):
content_type = mimetypes.guess_type(path)[0]
if content_type is None:
if path.endswith(".wasm"):
content_type = "application/wasm"
else:
content_type = "text/plain"
with open(path, "rb") as f:
#if version:
# # hash file to check version
# hasher = hashlib.new("sha256")
# data = f.read()
# hasher.update(data)
# print("requested version: {}".format(version))
# print("current version: {}".format(hasher.hexdigest()))
# if version != hasher.hexdigest():
# print ("VERSION MISMATCH!!")
# self.send_response(HTTPStatus.NOT_FOUND)
# self.end_headers()
# return
# print ("all good, versions match.")
content_length = f.seek(0, 2);
f.seek(0, 0) # go back to start
self.send_response(HTTPStatus.OK)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", content_length)
self.end_headers()
print("headers sent")
shutil.copyfileobj(f, self.wfile)
else:
self.send_response(HTTPStatus.NOT_FOUND)
self.end_headers()
server = HTTPServer(("0.0.0.0", 5001), Handler)
server.socket = ssl.wrap_socket(server.socket, keyfile="/home/antonius/minica/portland-3/key.pem", certfile="/home/antonius/minica/portland-3/cert.pem", server_side=True)
server.serve_forever()