-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhttp_request.py
78 lines (64 loc) · 2.38 KB
/
http_request.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
class HTTPRequest:
def __init__(self, file, replacements=[], secure=True):
protocol = 'http'
if secure:
protocol += 's'
self.raw_request = open(file, "r").read()
self.perform_replacements(replacements)
self.method = None
self.path = None
self.headers = {}
self.cookies = None
self.body = None
self.parse_request()
self.protocol = protocol
def parse_request(self):
lines = self.raw_request.split('\n')
line_number = 1
pre_body = 0
for line in lines:
line = line.strip()
if line_number == 1:
space = line.index(' ')
self.method = line[0:space]
self.path = line[space + 1: line.rindex(' ')]
elif line == '' and pre_body == 0:
pre_body += 1
elif pre_body == 0:
if 'Cookie:' in line and line.index('Cookie:') == 0:
self.cookies = line[7:].strip()
key, val = line.split(':', 1)
if key != 'Content-Length':
self.headers[key.strip()] = val.strip()
if (pre_body > 0 and line != '') or pre_body > 1: # in body
pre_body += 1
if self.body is None:
self.body = line
else:
self.body += '\n' + line
line_number += 1
def get_method(self):
return self.method
def get_headers(self, with_cookies=True):
if not with_cookies:
new_arr = self.headers.copy()
new_arr.pop('Cookie')
return new_arr
return self.headers
def get_cookies(self, split=False):
if split:
return self.cookies.split(';')
return self.cookies
def get_path(self):
return self.path
def get_protocol(self):
return self.protocol
def get_body(self):
return self.body
def get_full_url(self):
return '{}://{}{}'.format(self.get_protocol(), self.get_headers().get('Host'), self.get_path())
def perform_replacements(self, replacements):
if len(replacements) > 0 and not isinstance(replacements[0], list):
replacements = [replacements]
for replacement in replacements:
self.raw_request = self.raw_request.replace(replacement[0], replacement[1])