diff options
| author | UltraQbik <no1skill@yandex.ru> | 2024-08-27 01:26:35 +0300 |
|---|---|---|
| committer | UltraQbik <no1skill@yandex.ru> | 2024-08-27 01:26:35 +0300 |
| commit | 817b9b5131f665390e8340941817169b0615f441 (patch) | |
| tree | 554332d6700c5adb0f5d06dc1be483ae596b21c4 /main.py | |
| parent | fae7cca5091c69083f9f2943310ee5e9c5016520 (diff) | |
| download | httpy-817b9b5131f665390e8340941817169b0615f441.tar.gz httpy-817b9b5131f665390e8340941817169b0615f441.zip | |
Add compression
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/main.py b/main.py index ad1e2fe..88674af 100644 --- a/main.py +++ b/main.py @@ -156,7 +156,8 @@ class HTTPServer: if request.path in self.path_map: # assume browser filepath = self.path_map[request.path]["path"] filedata = self.fetch_file(filepath) - response = Response(b'', STATUS_CODE_OK, data_stream=filedata) + headers = self.fetch_file_headers(filepath) + response = Response(b'', STATUS_CODE_OK, data_stream=filedata, headers=headers) return response elif len(split_path) >= 2 and split_path[0] in API_VERSIONS: # assume script @@ -211,22 +212,32 @@ class HTTPServer: break return None - def fetch_file(self, path: str) -> tuple[Generator[bytes, None, None], dict[str, str]] | None: + def fetch_file_headers(self, path: str) -> dict[str, Any] | None: + """ + Fetcher file header data + :param path: filepath + :return: headers + """ + + if path in self.path_map: + return self.path_map[path]["headers"] + return None + + def fetch_file(self, path: str) -> Generator[bytes, None, None] | None: """ Fetches file :param path: filepath - :return: data stream, headers + :return: data stream """ if path in self.path_map: filepath = self.path_map[path]["path"] - headers = self.path_map[path]["headers"] with open(filepath, "rb") as file: - yield file.read(BUFFER_LENGTH), headers + yield file.read(BUFFER_LENGTH) + yield None def main(): - server = HTTPServer(port=13700, enable_ssl=False) server.start() |