diff options
| author | UltraQbik <no1skill@yandex.ru> | 2024-08-22 18:08:46 +0300 |
|---|---|---|
| committer | UltraQbik <no1skill@yandex.ru> | 2024-08-22 18:08:46 +0300 |
| commit | d64582eee40b9e87fcb64299b88cb58ad8289311 (patch) | |
| tree | 463c1922f4f56135cb066e552311b437c3fa18aa /src | |
| parent | 1529692aaea6b46ef783ca48e12ace8093d272c2 (diff) | |
| download | httpy-d64582eee40b9e87fcb64299b88cb58ad8289311.tar.gz httpy-d64582eee40b9e87fcb64299b88cb58ad8289311.zip | |
Update path handling and API part
Diffstat (limited to 'src')
| -rw-r--r-- | src/request.py | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/request.py b/src/request.py index 3484e72..56f67be 100644 --- a/src/request.py +++ b/src/request.py @@ -6,6 +6,7 @@ class Request: def __init__(self): self.type: str = "" self.path: str = "" + self.path_args: dict[str, str | None] = dict() @staticmethod def create(raw_request: bytes): @@ -18,9 +19,26 @@ class Request: # new request request = Request() - # fix type and path + # change type and path request.type = raw_request[:raw_request.find(b' ')].decode("ascii") - request.path = raw_request[len(request.type)+1:raw_request.find(b' ', len(request.type)+1)].decode("ascii") + raw_path = raw_request[len(request.type)+1:raw_request.find(b' ', len(request.type)+1)].decode("ascii") + + # 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 + + # remove path args from path + request.path = raw_path.split("?")[0] # decode headers for raw_header in raw_request.split(b'\r\n'): |