-
Notifications
You must be signed in to change notification settings - Fork 0
/
micropAsyncWeb.py
254 lines (209 loc) · 8.6 KB
/
micropAsyncWeb.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
import uasyncio as asyncio
import uerrno
import json
import re
def dirname(string):
return "/".join(string.split("/")[:-1])
class HttpError(Exception):
pass
class Request:
url = ""
path = ""
method = ""
version = ""
headers = {}
route = ""
params = None
read = None
write = None
close = None
def __init__(self):
self.headers = {}
self.route = ""
self.params = []
async def setup(self, reader, writer):
self.read = reader.read
self.write = writer.awrite
self.close = writer.aclose
items = await reader.readline()
items = items.decode("ascii").split()
if len(items) != 3:
return
self.method, self.url, self.version = items
self.path = self.url.replace("?", "#").split("#")[0]
def getFilename(self):
return self.path.split("/")[-1]
def getFileExtension(self, filename=""):
if filename == "":
filename = self.getFilename()
return filename.split(".")[-1]
class Response:
fileTypes = {
"txt": {"binary": False, "blockSize": 64, "mimeType": "text/plain"},
"htm": {"binary": False, "blockSize": 64, "mimeType": "text/html"},
"html": {"binary": False, "blockSize": 64, "mimeType": "text/html"},
"css": {"binary": False, "blockSize": 64, "mimeType": "text/css"},
"js": {"binary": False, "blockSize": 64, "mimeType": "text/javascript"},
"json": {"binary": False, "blockSize": 64, "mimeType": "application/json"},
"ico": {
"binary": True,
"blockSize": 1024,
"mimeType": "image/vnd.microsoft.icon",
},
"jpg": {"binary": True, "blockSize": 1024, "mimeType": "image/jpeg"},
"png": {"binary": True, "blockSize": 1024, "mimeType": "image/png"},
"gif": {"binary": True, "blockSize": 1024, "mimeType": "image/gif"},
"webp": {"binary": True, "blockSize": 1024, "mimeType": "image/webp"},
"ttf": {"binary": True, "blockSize": 1024, "mimeType": "font/ttf"},
"otf": {"binary": True, "blockSize": 1024, "mimeType": "font/otf"},
"woff": {"binary": True, "blockSize": 1024, "mimeType": "font/woff"},
"woff2": {"binary": True, "blockSize": 1024, "mimeType": "font/woff2"},
}
@staticmethod
async def start(request, statusCode, statusText):
await request.write("HTTP/1.1 %s %s\r\n" % (statusCode, statusText))
@staticmethod
async def startBody(request):
await request.write("\r\n")
@staticmethod
async def sendJson(request, dict):
await Response.start(request, 200, "OK")
await request.write("Content-type: application/json;charset=utf-8\r\n")
await Response.startBody(request)
await request.write("{0}".format(json.dumps(dict)))
@staticmethod
async def sendFile(request, filename):
args = {
"blockSize": 64,
"binary": False,
"mimeType": "application/octet-stream",
}
try:
extension = request.getFileExtension(filename)
if extension in Response.fileTypes:
args = args | Response.fileTypes[extension]
with open(filename, "rb" if args["binary"] else "r") as fileHandle:
await Response.start(request, 200, "OK")
await request.write("Content-type: %s\r\n" % (args["mimeType"]))
await Response.startBody(request)
while True:
data = fileHandle.read(args["blockSize"])
if not data:
break
await request.write(data)
except OSError as e:
if e.args[0] != uerrno.ENOENT:
raise
raise HttpError(request, 404, "File Not Found")
@staticmethod
async def write(request, data):
await request.write(data.encode("utf-8") if type(data) == str else data)
@staticmethod
async def error(request, code, reason):
await request.write("HTTP/1.1 %s %s\r\n\r\n" % (code, reason))
await request.write("<h1>%s</h1>" % (reason))
class MicropAsyncWeb:
requestHeadersToKeep = ("Authorization", "Content-Length", "Content-Type")
headers = {}
def __init__(self, port=80, address="0.0.0.0", webroot=".", indexFile="index.html"):
self.port = port
self.address = address
self.webroot = webroot
self.indexFile = indexFile
self.routes = [
["/", self.fileRouteHandler, "GET"],
["/*", self.fileRouteHandler, "GET"],
]
def appendRoutes(self, routes):
for route in routes:
if len(route) < 3:
route.append("GET")
self.routes.append(route[:3])
def sortRoutes(self):
def routeSorter(routeConfig):
return len(routeConfig[0].replace("*", "/").split("/"))
self.routes = sorted(self.routes, key=routeSorter, reverse=False)
def compileRegexRoutes(self):
for routeConfig in self.routes:
route = routeConfig[0]
if "*" in route:
routeConfig.append(re.compile("^" + route.replace("*", "([^/]+)") + "$"))
async def fileRouteHandler(self, request):
filename = request.getFilename()
if filename == "":
filename = self.indexFile
await Response.sendFile(request, "%s/%s" % (self.webroot, filename))
def route(self, route, methods="GET"):
def decorator(func):
self.routes.append([route, func, methods])
return func
return decorator
async def generateOutput(self, request, handler):
"""Generate output from handler
`handler` can be :
* string, considered as a path to a file
* callable, the output of which is sent to the client
"""
while handler:
if isinstance(handler, str):
await Response.sendFile(request, handler)
break
handler = await handler(request)
async def handle(self, reader, writer):
request = Request()
await request.setup(reader, writer)
try:
try:
if request.version not in ("HTTP/1.0", "HTTP/1.1"):
raise HttpError(request, 505, "Version Not Supported")
while True:
items = await reader.readline()
items = items.decode("ascii").split(":", 1)
if len(items) == 2:
header, value = items
value = value.strip()
if header in self.requestHeadersToKeep:
request.headers[header] = value
elif len(items) == 1:
break
found = False
for routeConfig in self.routes:
route = routeConfig[0]
handler = routeConfig[1]
# print("trying {0}".format(routeConfig))
if len(routeConfig) > 2:
if request.method not in routeConfig[2].split(","):
continue
if request.path == route:
# print("1 route={0}".format(route))
found = True
await self.generateOutput(request, handler)
break
if len(routeConfig) > 3:
# has regex
match = routeConfig[3].match(request.path)
if match:
try:
for group in range(1, route.count("*") + 1):
request.params.append(match.group(group))
request.route = route
found = True
await self.generateOutput(request, handler)
break
except:
request.params = []
if not found:
raise HttpError(request, 404, "File Not Found")
except HttpError as e:
request, code, message = e.args
await Response.error(request, code, message)
except OSError as e:
# Skip ECONNRESET error (client abort request)
if e.args[0] != uerrno.ECONNRESET:
raise
finally:
await writer.aclose()
async def runAsync(self):
self.compileRegexRoutes()
self.sortRoutes()
return await asyncio.start_server(self.handle, self.address, self.port)