-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_header.py
executable file
·57 lines (47 loc) · 1.66 KB
/
parse_header.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
keys = ('method', 'path', 'range', 'cookie')
class HTTPHeader(object):
def __init__(self):
self.headers = {key: None for key in keys}
def parse_header(self, line):
fields = line.split(' ')
if fields[0] == 'GET' or fields[0] == 'POST' or fields[0] == 'HEAD':
self.headers['method'] = fields[0]
self.headers['path'] = fields[1]
if fields[0] == 'Range:':
self.headers['range'] = HTTPRange(fields[1][:-2]).get_range()
if fields[0] == 'Cookie:':
self.headers['cookie'] = HTTPCookie(fields[1][:-2])
def get(self, key):
return self.headers.get(key)
class HTTPCookie(object):
def __init__(self, string):
self.__cookie = {}
items = string.split(';')
for i in items:
entry = i.split('=')
self.__cookie[entry[0]] = entry[1]
def get(self, entry):
if entry in self.__cookie:
return self.__cookie[entry]
else:
return None
class HTTPRange(object):
def __init__(self, string):
self.__start = None
self.__end = None
parts = string.split('=')
if parts[0] == 'bytes':
range_int = parts[1].split('-')
if range_int[0] != '':
self.__start = int(range_int[0])
else:
self.__start = 0
if range_int[1] != '':
self.__end = int(range_int[1])
else:
self.__end = -1
def get_range(self) -> (int, int):
if self.__start is not None and self.__end is not None:
return self.__start, self.__end
else:
return None