(self, path: pathlib.Path, request_path: str)
| 125 | return self._serve_file(path, urlparse(self.uri.decode()).path) |
| 126 | |
| 127 | def _serve_file(self, path: pathlib.Path, request_path: str) -> None: |
| 128 | server = self.channel.factory.server_instance |
| 129 | file_content = None |
| 130 | try: |
| 131 | file_content = path.read_bytes() |
| 132 | content_type = mimetypes.guess_type(path)[0] |
| 133 | if content_type and content_type.startswith("text/"): |
| 134 | content_type += "; charset=utf-8" |
| 135 | self.setHeader(b"Content-Type", content_type) |
| 136 | self.setHeader(b"Cache-Control", "no-cache, no-store") |
| 137 | if request_path in server.gzip_routes: |
| 138 | self.setHeader("Content-Encoding", "gzip") |
| 139 | self.write(gzip.compress(file_content)) |
| 140 | else: |
| 141 | self.setHeader(b"Content-Length", str(len(file_content))) |
| 142 | self.write(file_content) |
| 143 | self.setResponseCode(HTTPStatus.OK) |
| 144 | except (FileNotFoundError, IsADirectoryError, PermissionError): |
| 145 | self.setHeader(b"Content-Type", "text/plain") |
| 146 | self.setResponseCode(HTTPStatus.NOT_FOUND) |
| 147 | if self.method != "HEAD": |
| 148 | self.write(f"File not found: {path}".encode()) |
| 149 | self.finish() |
| 150 | |
| 151 | |
| 152 | class TestServerHTTPChannel(http.HTTPChannel): |
no outgoing calls
no test coverage detected