-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.py
More file actions
86 lines (65 loc) · 2.35 KB
/
Copy pathhttp.py
File metadata and controls
86 lines (65 loc) · 2.35 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
_hex_byte_cache = None
class HttpRequest:
def __init__(self, client_socket):
self.client_socket = client_socket
cl_file = client_socket.makefile('rwb', 0) # TODO: does the buffer really need to be writable?
self.raw_request_headers = self.get_raw_request_headers(cl_file)
self.params = {}
self.path = ''
self.parse_path()
@staticmethod
def get_raw_request_headers(cl_file):
request_headers = []
while True:
line = cl_file.readline()
request_headers.append(line.decode('utf-8'))
if not line or line == b'\r\n':
break
return request_headers
def parse_path(self):
url = self.raw_request_headers[0].split(' ')[1]
path_and_raw_params = url.split('?')
self.path = path_and_raw_params[0]
if len(path_and_raw_params) == 1:
return
param_pairs = path_and_raw_params[1].split('&')
for param_pair in param_pairs:
split = param_pair.split('=')
self.params.__setitem__(unquote(split[0]).decode('utf-8'), unquote(split[1]).decode('utf-8'))
class HttpResponse:
def __init__(self, client_socket):
self.client_socket = client_socket
def send(self, content):
self.client_socket.send(content)
self.client_socket.close()
def unquote(string):
"""unquote('abc%20def') -> b'abc def'."""
global _hex_byte_cache
# Note: strings are encoded as UTF-8. This is only an issue if it contains
# unescaped non-ASCII characters, which URIs should not.
if not string:
return b''
if isinstance(string, str):
string = string.replace('+', ' ')
string = string.encode('utf-8')
bits = string.split(b'%')
if len(bits) == 1:
return string
res = [bits[0]]
append = res.append
# Build cache for hex to char mapping on-the-fly only for codes
# that are actually used
if _hex_byte_cache is None:
_hex_byte_cache = {}
for item in bits[1:]:
try:
code = item[:2]
char = _hex_byte_cache.get(code)
if char is None:
char = _hex_byte_cache[code] = bytes([int(code, 16)])
append(char)
append(item[2:])
except KeyError:
append(b'%')
append(item)
return b''.join(res)