blob: 0437dfd88a15c809ae1fa2ba7aa7a0ed7a406c27 (
plain)
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
|
class Request:
"""
Just a request
"""
def __init__(self):
self.type: str = ""
self.path: str = ""
self.path_args: dict[str, str | None] = dict()
@staticmethod
def create(raw_request: bytes):
"""
Creates self class from raw request
:param raw_request: bytes
:return: self
"""
# new request
request = Request()
# change type and path
request.type = raw_request[:raw_request.find(b' ')].decode("ascii")
raw_path = raw_request[len(request.type)+1:raw_request.find(b' ', len(request.type)+1)].decode("ascii")
# remove path args from path
request.path = raw_path.split("?")[0]
# decode path args
raw_args = raw_path.split("/")[-1].split("?")
raw_args = raw_args[1] if len(raw_args) == 2 else ""
for raw_arg in raw_args.split("&"):
split = raw_arg.split("=")
# if there is a key value pair present
if len(split) == 2:
request.path_args[split[0]] = split[1]
# if there is only a key present (and it's a valid key)
elif len(split) == 1 and split[0] != "":
request.path_args[split[0]] = None
# decode headers
for raw_header in raw_request.split(b'\r\n'):
if len(pair := raw_header.decode("ascii").split(":")) == 2:
key, val = pair
val = val.strip()
# set attribute to key value pair
setattr(request, key, val)
# return request
return request
def __str__(self):
return '\n'.join([f"{key}: {val}" for key, val in self.__dict__.items()])
|