/
http.py
979 lines (926 loc) · 43.9 KB
/
http.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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
'''
Created on 2015/11/10
:author: hubo
'''
from vlcp.protocol.http import HttpProtocolException, HttpRequestEvent, escape, escape_b, _createstatus
import traceback
from vlcp.event.stream import Stream, MemoryStream
from vlcp.event import EventHandler
from vlcp.event.runnable import RoutineContainer
import base64
import uuid
import json
from vlcp.event.core import QuitException
from contextlib import closing
try:
from Cookie import SimpleCookie, Morsel
from urlparse import parse_qs, urlunsplit, urljoin, urlsplit
from urllib import unquote, quote, unquote_plus
unquote_to_bytes = unquote
quote_from_bytes = quote
unquote_plus_to_bytes = unquote_plus
except Exception:
from http.cookies import SimpleCookie, Morsel
from urllib.parse import parse_qs, urlunsplit, urljoin, urlsplit, unquote_to_bytes, quote_from_bytes
# There is not an unquote_plus_to_bytes in urllib.parse, but it is simple
def unquote_plus_to_bytes(s):
if isinstance(s, str):
return unquote_to_bytes(s.replace('+', ' '))
else:
return unquote_to_bytes(s.replace(b'+', b' '))
import re
from email.message import Message
from vlcp.server.module import call_api
import functools
import logging
import os.path
import sys
if sys.version_info[0] >= 3:
from email.parser import BytesFeedParser
else:
from email.parser import FeedParser
BytesFeedParser = FeedParser
class HttpInputException(Exception):
pass
class HttpExitException(Exception):
pass
class HttpRewriteLoopException(Exception):
pass
pathrep = re.compile(r'[\/]+')
_replace_characters = ['>', '<', '|', '?', '*', ':', '"', '\'', '!', '$', '{', '}']
def _safename(name):
name = os.path.normpath(os.path.basename(name))
if '\x00' in name or '/' in name or '\\' in name:
raise HttpInputException('Bad file name')
for c in _replace_characters:
name.replace(c, '_')
return name
class Environment(object):
"""
Environment object used in HTTP handlers
"""
def __init__(self, event, container = None, defaultencoding = 'utf-8'):
try:
self.event = event
self.host = event.host
self.fullpath = event.path
self.path_match = getattr(event, 'path_match', None)
self.path = getattr(event, 'realpath', self.fullpath)
self.originalpath = getattr(event, 'originalpath', self.fullpath)
self.rewritefrom = getattr(event, 'rewritefrom', None)
self.querystring = getattr(event, 'querystring', None)
self.method = event.method
self.connection = event.connection
self.container = container
if self.container is None:
self.container = self.connection
if hasattr(self.connection.socket, 'ssl_version'):
self.https = True
else:
self.https = False
self.connmark = event.connmark
self.xid = event.xid
self.protocol = event.createby
if not self.host:
self.host = getattr(self.protocol, 'defaulthost', None)
if not self.host:
self.host = str(self.connection.localaddr[0]) + ':' + str(self.connection.localaddr[1])
self.host = self.host.encode('ascii')
self.headers = event.headers
self.headerdict = event.headerdict
self.setcookies = event.setcookies
self.inputstream = event.stream
self.encoding = defaultencoding
self._sendHeaders = False
self.outputstream = None
self.showerrorinfo = self.protocol.showerrorinfo
# Parse cookies
self.rawcookie = self.headerdict.get(b'cookie')
if self.rawcookie is not None and not isinstance(self.rawcookie, str):
self.rawcookie = self.rawcookie.decode(defaultencoding)
if hasattr(event, 'status'):
self.status = event.status
self.sent_headers = getattr(event, 'sent_headers', [])
self.sent_cookies = getattr(event, 'sent_cookies', [])
if self.rawcookie:
self.cookies = dict((k, v.value.encode(defaultencoding)) for k,v in SimpleCookie(self.rawcookie).items())
else:
self.cookies = {}
# Parse query string
if self.querystring:
result = parse_qs(self.querystring, True)
def convert(k,v):
try:
k = str(k.decode('ascii'))
except Exception:
raise HttpInputException('Form-data key must be ASCII')
if not k.endswith('[]'):
v = v[-1]
else:
k = k[:-2]
return (k,v)
self.args = dict(convert(k,v) for k,v in result.items())
else:
self.args = {}
self.exception = None
except Exception as exc:
# Delay the exception
self.exception = exc
def __repr__(self, *args, **kwargs):
r = self.method.upper() + b' ' + self.path + (b', ' + self.connection.remoteaddr[0].encode(self.encoding)
if self.connection.remoteaddr
else b'')
if not isinstance(r, str):
return r.decode(self.encoding)
else:
return r
def start_response(self, status = 200, headers = [], clearheaders = True, disabletransferencoding = False):
"Start to send response"
if self._sendHeaders:
raise HttpProtocolException('Cannot modify response, headers already sent')
self.status = status
self.disabledeflate = disabletransferencoding
if clearheaders:
self.sent_headers = headers[:]
else:
self.sent_headers.extend(headers)
startResponse = start_response
def header(self, key, value, replace = True):
"Send a new header"
if hasattr(key, 'encode'):
key = key.encode('ascii')
if hasattr(value, 'encode'):
value = value.encode(self.encoding)
if replace:
self.sent_headers = [(k,v) for k,v in self.sent_headers if k.lower() != key.lower()]
self.sent_headers.append((key, value))
def rawheader(self, kv, replace = True):
"""
Add a header with "<Header>: Value" string
"""
if hasattr(kv, 'encode'):
kv = kv.encode(self.encoding)
k,v = kv.split(b':', 1)
self.header(k, v.strip(), replace)
def setcookie(self, key, value, max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False):
"""
Add a new cookie
"""
newcookie = Morsel()
newcookie.key = key
newcookie.value = value
newcookie.coded_value = value
if max_age is not None:
newcookie['max-age'] = max_age
if expires is not None:
newcookie['expires'] = expires
if path is not None:
newcookie['path'] = path
if domain is not None:
newcookie['domain'] = domain
if secure:
newcookie['secure'] = secure
if httponly:
newcookie['httponly'] = httponly
self.sent_cookies = [c for c in self.sent_cookies if c.key != key]
self.sent_cookies.append(newcookie)
def bufferoutput(self):
"""
Buffer the whole output until write EOF or flushed.
"""
new_stream = Stream(writebufferlimit=None)
if self._sendHeaders:
# An extra copy
self.container.subroutine(new_stream.copy_to(self.outputstream, self.container, buffering=False))
self.outputstream = Stream(writebufferlimit=None)
def _startResponse(self):
if not hasattr(self, 'status'):
self.start_response(200, clearheaders=False)
if all(k.lower() != b'content-type' for k,_ in self.sent_headers):
self.header('Content-Type', 'text/html; charset=' + self.encoding, False)
# Process cookies
for c in self.sent_cookies:
self.rawheader(c.output(), False)
self.protocol.start_response(self.connection, self.xid, self.status, self.sent_headers, self.outputstream, self.disabledeflate)
self._sendHeaders = True
async def rewrite(self, path, method = None, keepresponse = True):
"Rewrite this request to another processor. Must be called before header sent"
if self._sendHeaders:
raise HttpProtocolException('Cannot modify response, headers already sent')
if getattr(self.event, 'rewritedepth', 0) >= getattr(self.protocol, 'rewritedepthlimit', 32):
raise HttpRewriteLoopException
newpath = urljoin(quote_from_bytes(self.path).encode('ascii'), path)
if newpath == self.fullpath or newpath == self.originalpath:
raise HttpRewriteLoopException
extraparams = {}
if keepresponse:
if hasattr(self, 'status'):
extraparams['status'] = self.status
extraparams['sent_headers'] = self.sent_headers
extraparams['sent_cookies'] = self.sent_cookies
r = HttpRequestEvent(self.host,
newpath,
self.method if method is None else method,
self.connection,
self.connmark,
self.xid,
self.protocol,
headers = self.headers,
headerdict = self.headerdict,
setcookies = self.setcookies,
stream = self.inputstream,
rewritefrom = self.fullpath,
originalpath = self.originalpath,
rewritedepth = getattr(self.event, 'rewritedepth', 0) + 1,
**extraparams
)
await self.connection.wait_for_send(r)
self._sendHeaders = True
self.outputstream = None
async def redirect(self, path, status = 302):
"""
Redirect this request with 3xx status
"""
location = urljoin(urlunsplit((b'https' if self.https else b'http',
self.host,
quote_from_bytes(self.path).encode('ascii'),
'',
''
)), path)
self.start_response(status, [(b'Location', location)])
await self.write(b'<a href="' + self.escape(location, True) + b'">' + self.escape(location) + b'</a>')
await self.flush(True)
def nl2br(self, text):
"""
Replace \'\n\' with \'<br/>\\n\'
"""
if isinstance(text, bytes):
return text.replace(b'\n', b'<br/>\n')
else:
return text.replace('\n', '<br/>\n')
def escape(self, text, quote = True):
"""
Escape special characters in HTML
"""
if isinstance(text, bytes):
return escape_b(text, quote)
else:
return escape(text, quote)
async def error(self, status=500, allowredirect = True, close = True, showerror = None, headers = []):
"""
Show default error response
"""
if showerror is None:
showerror = self.showerrorinfo
if self._sendHeaders:
if showerror:
typ, exc, tb = sys.exc_info()
if exc:
await self.write('<span style="white-space:pre-wrap">\n', buffering = False)
await self.writelines((self.nl2br(self.escape(v)) for v in traceback.format_exception(typ, exc, tb)), buffering = False)
await self.write('</span>\n', close, False)
elif allowredirect and status in self.protocol.errorrewrite:
await self.rewrite(self.protocol.errorrewrite[status], b'GET')
elif allowredirect and status in self.protocol.errorredirect:
await self.redirect(self.protocol.errorredirect[status])
else:
self.start_response(status, headers)
typ, exc, tb = sys.exc_info()
if showerror and exc:
await self.write('<span style="white-space:pre-wrap">\n', buffering = False)
await self.writelines((self.nl2br(self.escape(v)) for v in traceback.format_exception(typ, exc, tb)), buffering = False)
await self.write('</span>\n', close, False)
else:
await self.write(b'<h1>' + _createstatus(status) + b'</h1>', close, False)
async def write(self, data, eof = False, buffering = True):
"""
Write output to current output stream
"""
if not self.outputstream:
self.outputstream = Stream()
self._startResponse()
elif (not buffering or eof) and not self._sendHeaders:
self._startResponse()
if not isinstance(data, bytes):
data = data.encode(self.encoding)
await self.outputstream.write(data, self.connection, eof, False, buffering)
async def writelines(self, lines, eof = False, buffering = True):
"""
Write lines to current output stream
"""
for l in lines:
await self.write(l, False, buffering)
if eof:
await self.write(b'', eof, buffering)
async def flush(self, eof = False):
"""
Flush the current output stream buffer
"""
await self.write(b'', eof, False)
def output(self, stream, disabletransferencoding = None):
"""
Set output stream and send response immediately
"""
if self._sendHeaders:
raise HttpProtocolException('Cannot modify response, headers already sent')
self.outputstream = stream
try:
content_length = len(stream)
except Exception:
pass
else:
self.header(b'Content-Length', str(content_length).encode('ascii'))
if disabletransferencoding is not None:
self.disabledeflate = disabletransferencoding
self._startResponse()
def outputdata(self, data):
"""
Send output with fixed length data
"""
if not isinstance(data, bytes):
data = str(data).encode(self.encoding)
self.output(MemoryStream(data))
async def close(self):
"""
Close this request, send all data. You can still run other operations in the handler.
"""
if not self._sendHeaders:
self._startResponse()
if self.inputstream is not None:
self.inputstream.close(self.connection.scheduler)
if self.outputstream is not None:
await self.flush(True)
if hasattr(self, 'session') and self.session:
self.session.unlock()
def exit(self, output=b''):
"Exit current HTTP processing"
raise HttpExitException(output)
async def parseform(self, limit = 67108864, tostr = True, safename = True):
'''
Parse form-data with multipart/form-data or application/x-www-form-urlencoded
In Python3, the keys of form and files are unicode, but values are bytes
If the key ends with '[]', it is considered to be a list:
a=1&b=2&b=3 => {'a':1,'b':3}
a[]=1&b[]=2&b[]=3 => {'a':[1],'b':[2,3]}
:param limit: limit total input size, default to 64MB. None = no limit. Note that all the form
data is stored in memory (including upload files), so it is dangerous to accept a very large input.
:param tostr: convert values to str in Python3. Only apply to form, files data are always bytes
:param safename: if True, extra security checks are performed on filenames to reduce known security risks.
'''
if tostr:
def _str(s):
try:
if not isinstance(s, str):
return s.decode(self.encoding)
else:
return s
except Exception:
raise HttpInputException('Invalid encoding in post data: ' + repr(s))
else:
def _str(s):
return s
try:
form = {}
files = {}
# If there is not a content-type header, maybe there is not a content.
if b'content-type' in self.headerdict and self.inputstream is not None:
contenttype = self.headerdict[b'content-type']
m = Message()
# Email library expects string, which is unicode in Python 3
try:
m.add_header('Content-Type', str(contenttype.decode('ascii')))
except UnicodeDecodeError:
raise HttpInputException('Content-Type has non-ascii characters')
if m.get_content_type() == 'multipart/form-data':
fp = BytesFeedParser()
fp.feed(b'Content-Type: ' + contenttype + b'\r\n\r\n')
total_length = 0
while True:
try:
await self.inputstream.prepareRead(self.container)
data = self.inputstream.readonce()
total_length += len(data)
if limit is not None and total_length > limit:
raise HttpInputException('Data is too large')
fp.feed(data)
except EOFError:
break
msg = fp.close()
if not msg.is_multipart() or msg.defects:
# Reject the data
raise HttpInputException('Not valid multipart/form-data format')
for part in msg.get_payload():
if part.is_multipart() or part.defects:
raise HttpInputException('Not valid multipart/form-data format')
disposition = part.get_params(header='content-disposition')
if not disposition:
raise HttpInputException('Not valid multipart/form-data format')
disposition = dict(disposition)
if 'form-data' not in disposition or 'name' not in disposition:
raise HttpInputException('Not valid multipart/form-data format')
if 'filename' in disposition:
name = disposition['name']
filename = disposition['filename']
if safename:
filename = _safename(filename)
if name.endswith('[]'):
files.setdefault(name[:-2], []).append({'filename': filename, 'content': part.get_payload(decode=True)})
else:
files[name] = {'filename': filename, 'content': part.get_payload(decode=True)}
else:
name = disposition['name']
if name.endswith('[]'):
form.setdefault(name[:-2], []).append(_str(part.get_payload(decode=True)))
else:
form[name] = _str(part.get_payload(decode=True))
elif m.get_content_type() == 'application/x-www-form-urlencoded' or \
m.get_content_type() == 'application/x-url-encoded':
if limit is not None:
data = await self.inputstream.read(self.container, limit + 1)
if len(data) > limit:
raise HttpInputException('Data is too large')
else:
data = await self.inputstream.read(self.container)
result = parse_qs(data, True)
def convert(k,v):
try:
k = str(k.decode('ascii'))
except Exception:
raise HttpInputException('Form-data key must be ASCII')
if not k.endswith('[]'):
v = _str(v[-1])
else:
k = k[:-2]
v = [_str(i) for i in v]
return (k,v)
form = dict(convert(k,v) for k,v in result.items())
else:
# Other formats, treat like no data
pass
self.form = form
self.files = files
except Exception as exc:
raise HttpInputException('Failed to parse form-data: ' + str(exc))
async def sessionstart(self):
"Start session. Must start service.utils.session.Session to use this method"
if not hasattr(self, 'session') or not self.session:
self.session, setcookies = await call_api(self.container, 'session', 'start', {'cookies':self.rawcookie})
for nc in setcookies:
self.sent_cookies = [c for c in self.sent_cookies if c.key != nc.key]
self.sent_cookies.append(nc)
async def sessiondestroy(self):
"""
Destroy current session. The session object is discarded and can no longer be used in other requests.
"""
if hasattr(self, 'session') and self.session:
setcookies = await call_api(self.container, 'session', 'destroy', {'sessionid':self.session.id})
self.session.unlock()
del self.session
for nc in setcookies:
self.sent_cookies = [c for c in self.sent_cookies if c.key != nc.key]
self.sent_cookies.append(nc)
def basicauth(self, realm = b'all', nofail = False):
"Try to get the basic authorize info, return (username, password) if succeeded, return 401 otherwise"
if b'authorization' in self.headerdict:
auth = self.headerdict[b'authorization']
auth_pair = auth.split(b' ', 1)
if len(auth_pair) < 2:
raise HttpInputException('Authorization header is malformed')
if auth_pair[0].lower() == b'basic':
try:
userpass = base64.b64decode(auth_pair[1])
except Exception:
raise HttpInputException('Invalid base-64 string')
userpass_pair = userpass.split(b':', 1)
if len(userpass_pair) != 2:
raise HttpInputException('Authorization header is malformed')
return userpass_pair
if nofail:
return (None, None)
else:
self.basicauthfail(realm)
def basicauthfail(self, realm = b'all'):
"""
Return 401 for authentication failure. This will end the handler.
"""
if not isinstance(realm, bytes):
realm = realm.encode('ascii')
self.start_response(401, [(b'WWW-Authenticate', b'Basic realm="' + realm + b'"')])
self.exit(b'<h1>' + _createstatus(401) + b'</h1>')
def getrealpath(self, root, path):
'''
Return the real path on disk from the query path, from a root path.
The input path from URL might be absolute '/abc', or point to parent '../test',
or even with UNC or drive '\\test\abc', 'c:\test.abc',
which creates security issues when accessing file contents with the path.
With getrealpath, these paths cannot point to files beyond the root path.
:param root: root path of disk files, any query is limited in root directory.
:param path: query path from URL.
'''
if not isinstance(path, str):
path = path.decode(self.encoding)
# In windows, if the path starts with multiple / or \, the os.path library may consider it an UNC path
# remove them; also replace \ to /
path = pathrep.subn('/', path)[0]
# The relative root is considered ROOT, eliminate any relative path like ../abc, which create security issues
# We can use os.path.relpath(..., '/') but python2.6 os.path.relpath is buggy
path = os.path.normpath(os.path.join('/', path))
# The normalized path can be an UNC path, or event a path with drive letter
# Send bad request for these types
if os.path.splitdrive(path)[0]:
raise HttpInputException('Bad path')
return os.path.join(root, path[1:])
def _tostr(self, arg):
if isinstance(arg, list):
return [self._tostr(v) for v in arg]
elif not isinstance(arg, str):
return arg.decode(self.encoding)
else:
return arg
def argstostr(self):
"Query string arguments are bytes in Python3. This function Convert bytes to string with env.encoding(default to utf-8)."
self.args = dict((k, self._tostr(v)) for k,v in self.args.items())
return self.args
def cookietostr(self):
"Cookie values are bytes in Python3. This function Convert bytes to string with env.encoding(default to utf-8)."
self.cookies = dict((k, (v.decode(self.encoding) if not isinstance(v, str) else v)) for k,v in self.cookies.items())
return self.cookies
async def createcsrf(self, csrfarg = '_csrf'):
"""
Create a anti-CSRF token in the session
"""
await self.sessionstart()
if not csrfarg in self.session.vars:
self.session.vars[csrfarg] = uuid.uuid4().hex
def outputjson(self, obj):
"""
Serialize `obj` with JSON and output to the client
"""
self.header('Content-Type', 'application/json')
self.outputdata(json.dumps(obj).encode('ascii'))
async def _handler(container, event, func):
try:
env = Environment(event, container)
try:
if env.exception:
raise HttpInputException('Bad request')
r = func(env)
if r:
timeout, _ = await env.container.execute_with_timeout(getattr(env.protocol, 'processtimeout', None), r)
if timeout:
if container and hasattr(container, 'logger'):
container.logger.warning('Timeout in HTTP processing, env=%r:', env)
await env.error(500, showerror=False)
except HttpExitException as exc:
if exc.args[0]:
await env.write(exc.args[0])
except HttpInputException:
# HTTP 400 Bad Request
await env.error(400)
except QuitException:
raise
except Exception:
if container and hasattr(container, 'logger'):
container.logger.exception('Unhandled exception in HTTP processing, env=%r:', env)
await env.error(500)
await env.close()
except QuitException:
raise
except Exception:
# Must ignore all exceptions, or the whole handler is unregistered
pass
def http(container = None):
"wrap a WSGI-style class method to a HTTPRequest event handler"
def decorator(func):
@functools.wraps(func)
def handler(self, event):
return _handler(self if container is None else container, event, lambda env: func(self, env))
return handler
return decorator
def statichttp(container = None):
"wrap a WSGI-style function to a HTTPRequest event handler"
def decorator(func):
@functools.wraps(func)
def handler(event):
return _handler(container, event, func)
if hasattr(func, '__self__'):
handler.__self__ = func.__self__
return handler
return decorator
class Dispatcher(EventHandler):
def __init__(self, scheduler=None, daemon=False, vhost = ''):
EventHandler.__init__(self, scheduler=scheduler, daemon=daemon)
self.vhost = vhost
def routeevent(self, path, routinemethod, container = None, host = None, vhost = None, method = [b'GET', b'HEAD']):
'''
Route specified path to a routine factory
:param path: path to match, can be a regular expression
:param routinemethod: factory function routinemethod(event), event is the HttpRequestEvent
:param container: routine container. If None, default to self for bound method, or event.connection if not
:param host: if specified, only response to request to specified host
:param vhost: if specified, only response to request to specified vhost.
If not specified, response to dispatcher default vhost.
:param method: if specified, response to specified methods
'''
regm = re.compile(path + b'$')
if vhost is None:
vhost = self.vhost
if container is None:
container = getattr(routinemethod, '__self__', None)
def ismatch(event):
# Check vhost
if vhost is not None and getattr(event.createby, 'vhost', '') != vhost:
return False
# First parse the path
# RFC said we should accept absolute path
psplit = urlsplit(event.path)
if psplit.path[:1] != b'/':
# For security reason, ignore unrecognized path
return False
if psplit.netloc and host is not None and host != psplit.netloc:
# Maybe a proxy request, ignore it
return False
if getattr(event.createby, 'unquoteplus', True):
realpath = unquote_plus_to_bytes(psplit.path)
else:
realpath = unquote_to_bytes(psplit.path)
m = regm.match(realpath)
if m is None:
return False
event.realpath = realpath
event.querystring = psplit.query
event.path_match = m
return True
def func(event, scheduler):
try:
if event.canignore:
# Already processed
return
event.canignore = True
c = event.connection if container is None else container
c.subroutine(routinemethod(event), False)
except Exception:
pass
for m in method:
self.registerHandler(HttpRequestEvent.createMatcher(host, None, m, _ismatch = ismatch), func)
def route(self, path, routinemethod, container = None, host = None, vhost = None, method = [b'GET', b'HEAD']):
'''
Route specified path to a WSGI-styled routine factory
:param path: path to match, can be a regular expression
:param routinemethod: factory function routinemethod(env), env is an Environment object
see also utils.http.Environment
:param container: routine container
:param host: if specified, only response to request to specified host
:param vhost: if specified, only response to request to specified vhost.
If not specified, response to dispatcher default vhost.
:param method: if specified, response to specified methods
'''
self.routeevent(path, statichttp(container)(routinemethod), container, host, vhost, method)
def routeargs(self, path, routinemethod, container = None, host = None, vhost = None, method = [b'POST'],
tostr = True, matchargs = (), fileargs=(), queryargs=(), cookieargs=(), sessionargs=(),
csrfcheck = False, csrfarg = '_csrf', formlimit = 67108864):
'''
Convenient way to route a processor with arguments. Automatically parse arguments and pass them to
the corresponding handler arguments. If required arguments are missing, HttpInputException is thrown which
creates a 400 Bad Request response. If optional arguments are missing, they are replaced with default values
just as normal Python call does. If handler accepts keyword arguments, extra arguments are sent
with kwargs. If not, they are safely ignored.
:param path: path to match, can be a regular expression
:param routinemethod: factory function routinemethod(env, arga, argb, argc...). env is an Environment
object. form or querystring arguments 'arga', 'argb', 'argc' are passed to arga, argb, argc.
:param container: routine container
:param host: if specified, only response to request to specified host
:param vhost: if specified, only response to request to specified vhost.
If not specified, response to dispatcher default vhost.
:param method: methods allowed. With POST method, arguments are extracted from form by default;
With GET or HEAD method, arguments are extracted from querystring(args).
:param tostr: In Python3, convert bytes to str before sending arguments to handler.
:param matchargs: Instead of using form or args, extract arguments from path match.
matchargs is a sequence of matcher group names. If specified a group name
by number, the argument is used as positional arguments; if specified a group
name by name(str), the argument is used as a keyword argument.
:param fileargs: Instead of using form or args, extract specified arguments from files.
:param queryargs: Instead of using form, extract specified arguments from args. Notice that when
GET is allowed, the arguments are always extracted from args by default.
:param cookieargs: Instead of using form or args, extract specified arguments from cookies.
:param sessionargs: Instead of using form or args, extract specified arguments from session.
Notice that if sessionargs is not empty, env.sessionstart() is called,
so vlcp.service.utils.session.Session module must be loaded.
:param csrfcheck: If True, check <csrfarg> in input arguments against <csrfarg> in session.
Notice that csrfcheck=True cause env.sessionstart() to be called, so
vlcp.service.utils.session.Session module must be loaded.
:param csrfarg: argument name to check, default to "_csrf"
:param formlimit: limit on parseform, default to 64MB. None to no limit.
For example, if using::
async def handler(env, target, arga, argb, argc):
...
dispatcher.routeargs(b'/do/(.*)', handler, matchargs=(1,), queryargs=('argc'))
And there is a HTTP POST::
POST /do/mytarget?argc=1 HTTP/1.1
Host: ...
...
arga=test&argb=test2
then handler accepts arguments: target="mytarget", arga="test", argb="test2", argc="1"
'''
code = routinemethod.__code__
if code.co_flags & 0x08:
haskwargs = True
else:
haskwargs = False
# Remove argument env
arguments = code.co_varnames[1:code.co_argcount]
if hasattr(routinemethod, '__self__') and routinemethod.__self__:
# First argument is self, remove an extra argument
arguments=arguments[1:]
# Optional arguments
if hasattr(routinemethod, '__defaults__') and routinemethod.__defaults__:
requires = arguments[:-len(routinemethod.__defaults__)]
else:
requires = arguments[:]
async def handler(env):
if tostr:
def _str(s):
if not isinstance(s, str):
return s.decode(env.encoding)
else:
return s
else:
def _str(s):
return s
if tostr:
env.argstostr()
env.cookietostr()
if env.method == b'POST':
await env.parseform(formlimit, tostr)
argfrom = env.form
else:
# Ignore input
env.form = {}
env.files = {}
argfrom = env.args
args = []
kwargs = dict(argfrom)
def discard(k):
if k in kwargs:
del kwargs[k]
def extract(k, source):
if k in source:
kwargs[k] = source[k]
else:
discard(k)
try:
ps = 0
for ma in matchargs:
v = _str(env.path_match.group(ma))
if v is not None:
if isinstance(ma, str):
kwargs[ma] = v
else:
args.append(v)
ps += 1
else:
if isinstance(ma, str):
discard(ma)
else:
if ps < len(arguments):
discard(arguments[ps])
ps += 1
for fa in fileargs:
extract(fa, env.files)
if env.method == b'POST':
for qa in queryargs:
extract(qa, env.args)
for ca in cookieargs:
extract(ca, env.cookies)
# CSRF check is done before session arguments to prevent check against session self
if csrfcheck:
if csrfarg not in kwargs:
raise HttpInputException('CSRF check failed')
await env.sessionstart()
if env.session.vars[csrfarg] != kwargs[csrfarg]:
raise HttpInputException('CSRF check failed')
if sessionargs:
await env.sessionstart()
for sa in sessionargs:
extract(sa, env.session.vars)
# Check required arguments
for k in requires[ps:]:
if k not in kwargs:
raise HttpInputException('Argument "' + k + '" is required')
# Remove positional arguments
for k in requires[:ps]:
if k in kwargs:
del kwargs[k]
if not haskwargs:
# Remove extra parameters
validargs = arguments[ps:]
kwargs = dict((k,v) for (k,v) in kwargs.items() if k in validargs)
r = routinemethod(env, *args, **kwargs)
except KeyError as exc:
raise HttpInputException('Missing argument: ' + str(exc))
except Exception as exc:
raise HttpInputException(str(exc))
if r:
return await r
self.route(path, handler, container, host, vhost, method)
class _EncodedMatch(object):
"Hacker for match.expand"
def __init__(self, innerobj):
self.__innerobj = innerobj
def __getattr__(self, key):
return getattr(self.__innerobj, key)
def group(self, index = 0):
return quote_from_bytes(self.__innerobj.group(index)).encode('ascii')
@classmethod
def expand(cls, match, expand):
"""
If use expand directly, the url-decoded context will be decoded again, which create a security
issue. Hack expand to quote the text before expanding
"""
return re._expand(match.re, cls._EncodedMatch(match), expand)
def rewrite(self, path, expand, newmethod = None, host = None, vhost = None, method = [b'GET', b'HEAD'], keepquery = True):
"Automatically rewrite a request to another location"
async def func(env):
newpath = self.expand(env.path_match, expand)
if keepquery and getattr(env, 'querystring', None):
if b'?' in newpath:
newpath += b'&' + env.querystring
else:
newpath += b'?' + env.querystring
await env.rewrite(newpath, newmethod)
self.route(path, func)
def redirect(self, path, expand, status = 302, host = None, vhost = None, method = [b'GET', b'HEAD'], keepquery = True):
"Automatically redirect a request to another location"
async def func(env):
newpath = self.expand(env.path_match, expand)
if keepquery and getattr(env, 'querystring', None):
if b'?' in newpath:
newpath += b'&' + env.querystring
else:
newpath += b'?' + env.querystring
await env.redirect(newpath, status)
self.route(path, func)
class HttpHandler(RoutineContainer):
logger = logging.getLogger(__name__ + '.HttpHandler')
def __init__(self, scheduler=None, daemon=False, vhost = ''):
RoutineContainer.__init__(self, scheduler=scheduler, daemon=daemon)
self.dispatcher = Dispatcher(scheduler, daemon, vhost)
@staticmethod
def route(path, host = None, vhost = None, method = [b'GET', b'HEAD']):
def decorator(func):
func.routemode = 'route'
func.route_path = path
func.route_host = host
func.route_vhost = vhost
func.route_method = method
func.route_kwargs = {}
return func
return decorator
@staticmethod
def routeevent(path, host = None, vhost = None, method = [b'GET', b'HEAD']):
def decorator(func):
func.routemode = 'routeevent'
func.route_path = path
func.route_host = host
func.route_vhost = vhost
func.route_method = method
func.route_kwargs = {}
return func
return decorator
@staticmethod
def routeargs(path, host = None, vhost = None, method = [b'POST'], **kwargs):
"For extra arguments, see Dispatcher.routeargs. They must be specified by keyword arguments"
def decorator(func):
func.routemode = 'routeargs'
func.route_path = path
func.route_host = host
func.route_vhost = vhost
func.route_method = method
func.route_kwargs = kwargs
return func
return decorator
def start(self, asyncStart=False):
for key in dir(self):
val = getattr(self, key)
if hasattr(val, '__func__') and hasattr(val.__func__, 'routemode'):
getattr(self.dispatcher, val.__func__.routemode)(val.__func__.route_path,
val,
self,
val.__func__.route_host,
val.__func__.route_vhost,
val.__func__.route_method,
**val.__func__.route_kwargs)
if hasattr(self, 'rewrites'):
for rw in self.rewrites:
if len(rw) > 2:
self.dispatcher.rewrite(rw[0], rw[1], **rw[2])
else:
self.dispatcher.rewrite(rw[0], rw[1])
if hasattr(self, 'redirects'):
for rw in self.redirects:
if len(rw) > 2:
self.dispatcher.redirect(rw[0], rw[1], **rw[2])
else:
self.dispatcher.redirect(rw[0], rw[1])
def close(self):
self.dispatcher.close()