-
Notifications
You must be signed in to change notification settings - Fork 0
/
microWebSrv.py
729 lines (606 loc) · 29.6 KB
/
microWebSrv.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
"""
The MIT License (MIT)
Copyright © 2018 Jean-Christophe Bos & HC² (www.hc2.fr)
"""
from json import loads, dumps
from os import stat
#from _thread import start_new_thread
import socket
import gc
import re
try :
from microWebTemplate import MicroWebTemplate
except :
pass
try :
from microWebSocket import MicroWebSocket
except :
pass
class MicroWebSrvRoute :
def __init__(self, route, method, func, routeArgNames, routeRegex) :
self.route = route
self.method = method
self.func = func
self.routeArgNames = routeArgNames
self.routeRegex = routeRegex
class MicroWebSrv :
# ============================================================================
# ===( Constants )============================================================
# ============================================================================
_indexPages = [
"index.html",
"index.htm",
"default.html",
"default.htm"
]
_mimeTypes = {
".txt" : "text/plain",
".htm" : "text/html",
".html" : "text/html",
".css" : "text/css",
".csv" : "text/csv",
".js" : "application/javascript",
".xml" : "application/xml",
".xhtml" : "application/xhtml+xml",
".json" : "application/json",
".zip" : "application/zip",
".jpg" : "image/jpeg",
".jpeg" : "image/jpeg",
".png" : "image/png",
".gif" : "image/gif",
".svg" : "image/svg+xml",
".ico" : "image/x-icon"
}
_html_escape_chars = {
"&" : "&",
'"' : """,
"'" : "'",
">" : ">",
"<" : "<"
}
# ============================================================================
# ===( Class globals )=======================================================
# ============================================================================
_docoratedRouteHandlers = []
# ============================================================================
# ===( Utils )===============================================================
# ============================================================================
@classmethod
def route(cls, url, method='GET'):
""" Adds a route handler function to the routing list """
def route_decorator(func):
item = (url, method, func)
cls._docoratedRouteHandlers.append(item)
return func
return route_decorator
# ----------------------------------------------------------------------------
@staticmethod
def HTMLEscape(s) :
return ''.join(MicroWebSrv._html_escape_chars.get(c, c) for c in s)
# ----------------------------------------------------------------------------
@staticmethod
def _startThread(func, args=()) :
return True
# ----------------------------------------------------------------------------
@staticmethod
def _unquote(s) :
r = str(s).split('%')
try :
b = r[0].encode()
for i in range(1, len(r)) :
try :
b += bytes([int(r[i][:2], 16)]) + r[i][2:].encode()
except :
b += b'%' + r[i].encode()
return b.decode('UTF-8')
except :
return str(s)
# ------------------------------------------------------------------------------
@staticmethod
def _unquote_plus(s) :
return MicroWebSrv._unquote(s.replace('+', ' '))
# ------------------------------------------------------------------------------
@staticmethod
def _fileExists(path) :
try :
stat(path)
return True
except :
return False
# ============================================================================
# ===( Constructor )==========================================================
# ============================================================================
def __init__( self,
routeHandlers = [],
port = 80,
bindIP = '0.0.0.0',
webPath = "/flash/www" ) :
self._srvAddr = (bindIP, port)
self._webPath = webPath
self._notFoundUrl = None
self._started = False
self.MaxWebSocketRecvLen = 1024
self.WebSocketThreaded = True
self.AcceptWebSocketCallback = None
self.LetCacheStaticContentLevel = 2
self._routeHandlers = []
routeHandlers += self._docoratedRouteHandlers
for route, method, func in routeHandlers :
routeParts = route.split('/')
# -> ['', 'users', '<uID>', 'addresses', '<addrID>', 'test', '<anotherID>']
routeArgNames = []
routeRegex = ''
for s in routeParts :
if s.startswith('<') and s.endswith('>') :
routeArgNames.append(s[1:-1])
routeRegex += '/(\\w*)'
elif s :
routeRegex += '/' + s
routeRegex += '$'
# -> '/users/(\w*)/addresses/(\w*)/test/(\w*)$'
routeRegex = re.compile(routeRegex)
self._routeHandlers.append(MicroWebSrvRoute(route, method, func, routeArgNames, routeRegex))
# ============================================================================
# ===( Server Process )=======================================================
# ============================================================================
def _serverProcess(self) :
self._started = True
while self._started :
try :
client, cliAddr = self._server.accept()
except Exception as ex :
if ex.args and ex.args[0] == 113 :
break
continue
self._client(self, client, cliAddr)
self._started = False
# ============================================================================
# ===( Functions )============================================================
# ============================================================================
def Start(self, threaded=False) :
if not self._started :
self._server = socket.socket()
self._server.setsockopt( socket.SOL_SOCKET,
socket.SO_REUSEADDR,
1 )
self._server.bind(self._srvAddr)
self._server.listen(16)
if threaded :
MicroWebSrv._startThread(self._serverProcess)
else :
self._serverProcess()
# ----------------------------------------------------------------------------
def Stop(self) :
if self._started :
self._server.close()
self._started = False
# ----------------------------------------------------------------------------
def IsStarted(self) :
return self._started
# ----------------------------------------------------------------------------
def SetNotFoundPageUrl(self, url=None) :
self._notFoundUrl = url
# ----------------------------------------------------------------------------
def GetMimeTypeFromFilename(self, filename) :
filename = filename.lower()
for ext in self._mimeTypes :
if filename.endswith(ext) :
return self._mimeTypes[ext]
return None
# ----------------------------------------------------------------------------
def GetRouteHandler(self, resUrl, method) :
if self._routeHandlers :
#resUrl = resUrl.upper()
if resUrl.endswith('/') :
resUrl = resUrl[:-1]
method = method.upper()
for rh in self._routeHandlers :
if rh.method == method :
m = rh.routeRegex.match(resUrl)
if m : # found matching route?
if rh.routeArgNames :
routeArgs = {}
for i, name in enumerate(rh.routeArgNames) :
value = m.group(i+1)
try :
value = int(value)
except :
pass
routeArgs[name] = value
return (rh.func, routeArgs)
else :
return (rh.func, None)
return (None, None)
# ----------------------------------------------------------------------------
def _physPathFromURLPath(self, urlPath) :
if urlPath == '/' :
for idxPage in self._indexPages :
physPath = self._webPath + '/' + idxPage
if MicroWebSrv._fileExists(physPath) :
return physPath
else :
physPath = self._webPath + urlPath.replace('../', '/')
if MicroWebSrv._fileExists(physPath) :
return physPath
return None
# ============================================================================
# ===( Class Client )========================================================
# ============================================================================
class _client :
# ------------------------------------------------------------------------
def __init__(self, microWebSrv, socket, addr) :
socket.settimeout(2)
self._microWebSrv = microWebSrv
self._socket = socket
self._addr = addr
self._method = None
self._path = None
self._httpVer = None
self._resPath = "/"
self._queryString = ""
self._queryParams = { }
self._headers = { }
self._contentType = None
self._contentLength = 0
if hasattr(socket, 'readline'): # MicroPython
self._socketfile = self._socket
else: # CPython
self._socketfile = self._socket.makefile('rwb')
self._processRequest()
# ------------------------------------------------------------------------
def _processRequest(self) :
try :
response = MicroWebSrv._response(self)
if self._parseFirstLine(response) :
if self._parseHeader(response) :
upg = self._getConnUpgrade()
if not upg :
routeHandler, routeArgs = self._microWebSrv.GetRouteHandler(self._resPath, self._method)
if routeHandler :
try :
if routeArgs is not None:
routeHandler(self, response, routeArgs)
else :
routeHandler(self, response)
except Exception as ex :
print('MicroWebSrv handler exception:\r\n - In route %s %s\r\n - %s' % (self._method, self._resPath, ex))
raise ex
elif self._method.upper() == "GET" :
filepath = self._microWebSrv._physPathFromURLPath(self._resPath)
if filepath :
contentType = self._microWebSrv.GetMimeTypeFromFilename(filepath)
if contentType :
if self._microWebSrv.LetCacheStaticContentLevel > 0 :
if self._microWebSrv.LetCacheStaticContentLevel > 1 and \
'if-modified-since' in self._headers :
response.WriteResponseNotModified()
else:
headers = { 'Last-Modified' : 'Fri, 1 Jan 2018 23:42:00 GMT', \
'Cache-Control' : 'max-age=315360000' }
response.WriteResponseFile(filepath, contentType, headers)
else :
response.WriteResponseFile(filepath, contentType)
else :
response.WriteResponseForbidden()
else :
response.WriteResponseNotFound()
else :
response.WriteResponseMethodNotAllowed()
elif upg == 'websocket' and 'MicroWebSocket' in globals() \
and self._microWebSrv.AcceptWebSocketCallback :
MicroWebSocket( socket = self._socket,
httpClient = self,
httpResponse = response,
maxRecvLen = self._microWebSrv.MaxWebSocketRecvLen,
threaded = self._microWebSrv.WebSocketThreaded,
acceptCallback = self._microWebSrv.AcceptWebSocketCallback )
return
else :
response.WriteResponseNotImplemented()
else :
response.WriteResponseBadRequest()
except :
response.WriteResponseInternalServerError()
try :
if self._socketfile is not self._socket:
self._socketfile.close()
self._socket.close()
except :
pass
# ------------------------------------------------------------------------
def _parseFirstLine(self, response) :
try :
elements = self._socketfile.readline().decode().strip().split()
if len(elements) == 3 :
self._method = elements[0].upper()
self._path = elements[1]
self._httpVer = elements[2].upper()
elements = self._path.split('?', 1)
if len(elements) > 0 :
self._resPath = MicroWebSrv._unquote_plus(elements[0])
if len(elements) > 1 :
self._queryString = elements[1]
elements = self._queryString.split('&')
for s in elements :
param = s.split('=', 1)
if len(param) > 0 :
value = MicroWebSrv._unquote(param[1]) if len(param) > 1 else ''
self._queryParams[MicroWebSrv._unquote(param[0])] = value
return True
except :
pass
return False
# ------------------------------------------------------------------------
def _parseHeader(self, response) :
while True :
elements = self._socketfile.readline().decode().strip().split(':', 1)
if len(elements) == 2 :
self._headers[elements[0].strip().lower()] = elements[1].strip()
elif len(elements) == 1 and len(elements[0]) == 0 :
if self._method == 'POST' or self._method == 'PUT' :
self._contentType = self._headers.get("content-type", None)
self._contentLength = int(self._headers.get("content-length", 0))
return True
else :
return False
# ------------------------------------------------------------------------
def _getConnUpgrade(self) :
if 'upgrade' in self._headers.get('connection', '').lower() :
return self._headers.get('upgrade', '').lower()
return None
# ------------------------------------------------------------------------
def ReadRequestContent(self, size=None) :
if size is None :
size = self._contentLength
if size > 0 :
try :
return self._socketfile.read(size)
except :
pass
return b''
# ------------------------------------------------------------------------
def ReadRequestPostedFormData(self) :
res = { }
data = self.ReadRequestContent()
if data :
elements = data.decode().split('&')
for s in elements :
param = s.split('=', 1)
if len(param) > 0 :
value = MicroWebSrv._unquote_plus(param[1]) if len(param) > 1 else ''
res[MicroWebSrv._unquote_plus(param[0])] = value
return res
# ============================================================================
# ===( Class Response )======================================================
# ============================================================================
class _response :
# ------------------------------------------------------------------------
def __init__(self, client) :
self._client = client
# ------------------------------------------------------------------------
def _write(self, data, strEncoding='ISO-8859-1') :
if data :
if type(data) == str :
data = data.encode(strEncoding)
data = memoryview(data)
while data :
n = self._client._socketfile.write(data)
if n is None :
return False
data = data[n:]
return True
return False
# ------------------------------------------------------------------------
def _writeFirstLine(self, code) :
reason = self._responseCodes.get(code, ('Unknown reason', ))[0]
return self._write("HTTP/1.1 %s %s\r\n" % (code, reason))
# ------------------------------------------------------------------------
def _writeHeader(self, name, value) :
return self._write("%s: %s\r\n" % (name, value))
# ------------------------------------------------------------------------
def _writeContentTypeHeader(self, contentType, charset=None) :
if contentType :
ct = contentType \
+ (("; charset=%s" % charset) if charset else "")
else :
ct = "application/octet-stream"
self._writeHeader("Content-Type", ct)
# ------------------------------------------------------------------------
def _writeServerHeader(self) :
self._writeHeader("Server", "MicroWebSrv by JC`zic")
# ------------------------------------------------------------------------
def _writeEndHeader(self) :
return self._write("\r\n")
# ------------------------------------------------------------------------
def _writeBeforeContent(self, code, headers, contentType, contentCharset, contentLength) :
self._writeFirstLine(code)
if isinstance(headers, dict) :
for header in headers :
self._writeHeader(header, headers[header])
if contentLength > 0 :
self._writeContentTypeHeader(contentType, contentCharset)
self._writeHeader("Content-Length", contentLength)
self._writeServerHeader()
self._writeHeader("Connection", "close")
self._writeEndHeader()
# ------------------------------------------------------------------------
def WriteResponse(self, code, headers, contentType, contentCharset, content) :
try :
if content :
if type(content) == str :
content = content.encode(contentCharset)
contentLength = len(content)
else :
contentLength = 0
self._writeBeforeContent(code, headers, contentType, contentCharset, contentLength)
if content :
return self._write(content)
return True
except :
return False
# ------------------------------------------------------------------------
def WriteResponseFile(self, filepath, contentType=None, headers=None) :
try :
size = stat(filepath)[6]
if size > 0 :
with open(filepath, 'rb') as file :
self._writeBeforeContent(200, headers, contentType, None, size)
try :
buf = bytearray(1024)
while size > 0 :
x = file.readinto(buf)
if x < len(buf) :
buf = memoryview(buf)[:x]
if not self._write(buf) :
return False
size -= x
return True
except Exception as ex:
self.WriteResponse( 500,
None,
"text/html",
"UTF-8",
self._execErrCtnTmpl % {
'module' : 'FileResponse',
'message' : str(ex)
} )
return False
except :
pass
self.WriteResponseNotFound()
return False
# ------------------------------------------------------------------------
def WriteResponseOk(self, headers=None, contentType=None, contentCharset=None, content=None) :
return self.WriteResponse(200, headers, contentType, contentCharset, content)
# ------------------------------------------------------------------------
def WriteResponseJSONOk(self, obj=None, headers=None) :
return self.WriteResponse(200, headers, "application/json", "UTF-8", dumps(obj))
# ------------------------------------------------------------------------
def WriteResponseRedirect(self, location) :
headers = { "Location" : location }
return self.WriteResponse(302, headers, None, None, None)
# ------------------------------------------------------------------------
def WriteResponseError(self, code) :
responseCode = self._responseCodes.get(code, ('Unknown reason', ''))
return self.WriteResponse( code,
None,
"text/html",
"UTF-8",
self._errCtnTmpl % {
'code' : code,
'reason' : responseCode[0],
'message' : responseCode[1]
} )
# ------------------------------------------------------------------------
def WriteResponseNotModified(self) :
return self.WriteResponseError(304)
# ------------------------------------------------------------------------
def WriteResponseBadRequest(self) :
return self.WriteResponseError(400)
# ------------------------------------------------------------------------
def WriteResponseForbidden(self) :
return self.WriteResponseError(403)
# ------------------------------------------------------------------------
def WriteResponseNotFound(self) :
if self._client._microWebSrv._notFoundUrl :
self.WriteResponseRedirect(self._client._microWebSrv._notFoundUrl)
else :
return self.WriteResponseError(404)
# ------------------------------------------------------------------------
def WriteResponseMethodNotAllowed(self) :
return self.WriteResponseError(405)
# ------------------------------------------------------------------------
def WriteResponseInternalServerError(self) :
return self.WriteResponseError(500)
# ------------------------------------------------------------------------
def WriteResponseNotImplemented(self) :
return self.WriteResponseError(501)
# ------------------------------------------------------------------------
_errCtnTmpl = """\
<html>
<head>
<title>Error</title>
</head>
<body>
<h1>%(code)d %(reason)s</h1>
%(message)s
</body>
</html>
"""
# ------------------------------------------------------------------------
_execErrCtnTmpl = """\
<html>
<head>
<title>Page execution error</title>
</head>
<body>
<h1>%(module)s page execution error</h1>
%(message)s
</body>
</html>
"""
# ------------------------------------------------------------------------
_responseCodes = {
100: ('Continue', 'Request received, please continue'),
101: ('Switching Protocols',
'Switching to new protocol; obey Upgrade header'),
200: ('OK', 'Request fulfilled, document follows'),
201: ('Created', 'Document created, URL follows'),
202: ('Accepted',
'Request accepted, processing continues off-line'),
203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
204: ('No Content', 'Request fulfilled, nothing follows'),
205: ('Reset Content', 'Clear input form for further input.'),
206: ('Partial Content', 'Partial content follows.'),
300: ('Multiple Choices',
'Object has several resources -- see URI list'),
301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
302: ('Found', 'Object moved temporarily -- see URI list'),
303: ('See Other', 'Object moved -- see Method and URL list'),
304: ('Not Modified',
'Document has not changed since given time'),
305: ('Use Proxy',
'You must use proxy specified in Location to access this '
'resource.'),
307: ('Temporary Redirect',
'Object moved temporarily -- see URI list'),
400: ('Bad Request',
'Bad request syntax or unsupported method'),
401: ('Unauthorized',
'No permission -- see authorization schemes'),
402: ('Payment Required',
'No payment -- see charging schemes'),
403: ('Forbidden',
'Request forbidden -- authorization will not help'),
404: ('Not Found', 'Nothing matches the given URI'),
405: ('Method Not Allowed',
'Specified method is invalid for this resource.'),
406: ('Not Acceptable', 'URI not available in preferred format.'),
407: ('Proxy Authentication Required', 'You must authenticate with '
'this proxy before proceeding.'),
408: ('Request Timeout', 'Request timed out; try again later.'),
409: ('Conflict', 'Request conflict.'),
410: ('Gone',
'URI no longer exists and has been permanently removed.'),
411: ('Length Required', 'Client must specify Content-Length.'),
412: ('Precondition Failed', 'Precondition in headers is false.'),
413: ('Request Entity Too Large', 'Entity is too large.'),
414: ('Request-URI Too Long', 'URI is too long.'),
415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
416: ('Requested Range Not Satisfiable',
'Cannot satisfy request range.'),
417: ('Expectation Failed',
'Expect condition could not be satisfied.'),
500: ('Internal Server Error', 'Server got itself in trouble'),
501: ('Not Implemented',
'Server does not support this operation'),
502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
503: ('Service Unavailable',
'The server cannot process the request due to a high load'),
504: ('Gateway Timeout',
'The gateway server did not receive a timely response'),
505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
}
# ============================================================================
# ============================================================================
# ============================================================================