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 | |
| parent | 1529692aaea6b46ef783ca48e12ace8093d272c2 (diff) | |
| download | httpy-d64582eee40b9e87fcb64299b88cb58ad8289311.tar.gz httpy-d64582eee40b9e87fcb64299b88cb58ad8289311.zip | |
Update path handling and API part
| -rw-r--r-- | main.py | 12 | ||||
| -rw-r--r-- | src/request.py | 22 |
2 files changed, 23 insertions, 11 deletions
diff --git a/main.py b/main.py index 0844fd7..9ff95e6 100644 --- a/main.py +++ b/main.py @@ -42,11 +42,8 @@ PATH_MAP = { } # API -API_PATH = { - "/APIv1/file/generated/1gib", - "/APIv1/file/generated/5gib", - "/APIv1/file/generated/10gib", - "/APIv1/file/generated/20gib", +API_VERSIONS = { + "APIv1" } # internal path map @@ -212,10 +209,7 @@ class HTTPServer: return # if it's an API request - elif request.path in API_PATH: - # get API version - api_version = request.path.split("/")[1] - + elif (api_version := request.path.split("/")[1]) in API_VERSIONS: data = b'' match api_version: case "APIv1": 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'): |