-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
65 lines (54 loc) · 1.28 KB
/
Copy pathparser.py
File metadata and controls
65 lines (54 loc) · 1.28 KB
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
import re
import io
import sys
#data=open(sys.argv[1],"rb").read()
def request_parser(data):
BAD_R=False
# if data==b"":
# BAD_R=True
f=io.BytesIO(data)
payload=False
client_payload=b''
req_line=f.readline().decode("utf8").rstrip()
req=[req_line]
req_line=req[0]
req_line = re.match("^([A-Z]+)\s+(\S+)\s+([\w\/\.]+)$", req_line.replace("\r", ""))
if req_line:
req[0]=(req_line[1],req_line[2],req_line[3])
else:
BAD_R=True
print("invalid status line")
for line in f:
line=line.replace(b'\r',b'')
line=line.decode("utf8").rstrip()
try:
reqheader,value= line.split(': ',1)
reqheader=reqheader.lower()
req.append((reqheader,value))
if reqheader=="content-length":
payload=True
payload_size=int(value)
except:
if line=="":
#print("End of headers")
break
else:
BAD_R=True
break
if payload:
client_payload=f.read(payload_size)
is_residue=False
residue=f.read()
if residue not in {b'',b'\n'}:
is_residue=True
parsed_dic=dict(req=req, bad_req=BAD_R, is_payload=payload, client_payload=client_payload, is_residue=is_residue, residue=residue)
return parsed_dic
#print(parsed_dic)
# if residue: #put some /
# print(f'residue is present')
# print(residue)
# print(req)
# print("content")
# print(client_payload)
# print(BAD_R)
#