forked from h3h3da/MicroWAF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
77 lines (69 loc) · 2.32 KB
/
parse.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class Request():
"""解析http请求"""
method = None
uri = None
version = None
body = ''
headers = dict()
__methods = dict.fromkeys((
'GET', 'PUT', 'ICY',
'COPY', 'HEAD', 'LOCK', 'MOVE', 'POLL', 'POST',
'BCOPY', 'BMOVE', 'MKCOL', 'TRACE', 'LABEL', 'MERGE',
'DELETE', 'SEARCH', 'UNLOCK', 'REPORT', 'UPDATE', 'NOTIFY',
'BDELETE', 'CONNECT', 'OPTIONS', 'CHECKIN',
'PROPFIND', 'CHECKOUT', 'CCM_POST',
'SUBSCRIBE', 'PROPPATCH', 'BPROPFIND',
'BPROPPATCH', 'UNCHECKOUT', 'MKACTIVITY',
'MKWORKSPACE', 'UNSUBSCRIBE', 'RPC_CONNECT',
'VERSION-CONTROL',
'BASELINE-CONTROL'
))
__proto = 'HTTP'
def __init__(self,buf):
self.parse(buf)
def parse(self,buf):
try:
# buf.decode("ascii", "ignore")
b = buf.decode("ascii", "ignore").strip().split("\r\n", 1)
line = b[0]
head = b[1]
except Exception as e:
print("err: ", e)
# 解析请求行
line=line.strip().split()
# print("line ", line)
# print("head ", head)
if len(line) < 2:
raise Exception("invalid request")
if line[0] not in self.__methods:
raise Exception("invalid request")
if len(line) == 2:
self.version = '0.9'
else:
if not line[2].startswith(self.__proto):
raise Exception("invalid request")
self.version = line[2][len(self.__proto)+1:]
self.method = line[0]
self.uri = line[1]
# 解析请求头及post内容
head = head.strip().split("\r\n")
if self.method.lower() == 'post':
self.body=head[-1:]
head=head[:-1]
for hrd in head:
if not hrd:
break
h = hrd.split(':',1)
if len(h[0].split()) != 1:
raise Exception("invalid request")
a = h[0].lower()
b = len(h) !=1 and h[1].lstrip() or ''
if a in self.headers:
#if not type(self.headers[a]) is list:
#self.headers[a] = [self.headers[a]]
#self.headers[a].append(b)
pass
else:
self.headers[a] = b