|
12 | 12 | import time |
13 | 13 | import socket |
14 | 14 | import logging |
15 | | -import mimetypes |
16 | 15 | from typing import Any, Dict, List, Tuple, Union, Pattern, Optional |
17 | 16 |
|
18 | 17 | from .plugin import HttpWebServerBasePlugin |
|
21 | 20 | from .protocols import httpProtocolTypes |
22 | 21 | from ..exception import HttpProtocolException |
23 | 22 | from ..protocols import httpProtocols |
24 | | -from ..responses import NOT_FOUND_RESPONSE_PKT, okResponse |
| 23 | +from ..responses import NOT_FOUND_RESPONSE_PKT |
25 | 24 | from ..websocket import WebsocketFrame, websocketOpcodes |
26 | 25 | from ...common.flag import flags |
27 | 26 | from ...common.types import Readables, Writables, Descriptors |
28 | | -from ...common.utils import text_, bytes_, build_websocket_handshake_response |
| 27 | +from ...common.utils import text_, build_websocket_handshake_response |
29 | 28 | from ...common.constants import ( |
30 | 29 | DEFAULT_ENABLE_WEB_SERVER, DEFAULT_STATIC_SERVER_DIR, |
31 | 30 | DEFAULT_ENABLE_REVERSE_PROXY, DEFAULT_ENABLE_STATIC_SERVER, |
32 | | - DEFAULT_MIN_COMPRESSION_LIMIT, DEFAULT_WEB_ACCESS_LOG_FORMAT, |
| 31 | + DEFAULT_WEB_ACCESS_LOG_FORMAT, DEFAULT_MIN_COMPRESSION_LENGTH, |
33 | 32 | ) |
34 | 33 |
|
35 | 34 |
|
|
65 | 64 | flags.add_argument( |
66 | 65 | '--min-compression-length', |
67 | 66 | type=int, |
68 | | - default=DEFAULT_MIN_COMPRESSION_LIMIT, |
69 | | - help='Default: ' + str(DEFAULT_MIN_COMPRESSION_LIMIT) + ' bytes. ' + |
| 67 | + default=DEFAULT_MIN_COMPRESSION_LENGTH, |
| 68 | + help='Default: ' + str(DEFAULT_MIN_COMPRESSION_LENGTH) + ' bytes. ' + |
70 | 69 | 'Sets the minimum length of a response that will be compressed (gzipped).', |
71 | 70 | ) |
72 | 71 |
|
@@ -124,27 +123,6 @@ def encryption_enabled(self) -> bool: |
124 | 123 | return self.flags.keyfile is not None and \ |
125 | 124 | self.flags.certfile is not None |
126 | 125 |
|
127 | | - @staticmethod |
128 | | - def read_and_build_static_file_response(path: str) -> memoryview: |
129 | | - try: |
130 | | - with open(path, 'rb') as f: |
131 | | - content = f.read() |
132 | | - content_type = mimetypes.guess_type(path)[0] |
133 | | - if content_type is None: |
134 | | - content_type = 'text/plain' |
135 | | - headers = { |
136 | | - b'Content-Type': bytes_(content_type), |
137 | | - b'Cache-Control': b'max-age=86400', |
138 | | - } |
139 | | - return okResponse( |
140 | | - content=content, |
141 | | - headers=headers, |
142 | | - # TODO: Should we really close or take advantage of keep-alive? |
143 | | - conn_close=True, |
144 | | - ) |
145 | | - except FileNotFoundError: |
146 | | - return NOT_FOUND_RESPONSE_PKT |
147 | | - |
148 | 126 | def switch_to_websocket(self) -> None: |
149 | 127 | self.client.queue( |
150 | 128 | memoryview( |
@@ -309,7 +287,8 @@ def _try_route(self, path: bytes) -> bool: |
309 | 287 | def _try_static_or_404(self, path: bytes) -> None: |
310 | 288 | path = text_(path).split('?', 1)[0] |
311 | 289 | self.client.queue( |
312 | | - self.read_and_build_static_file_response( |
| 290 | + HttpWebServerBasePlugin.serve_static_file( |
313 | 291 | self.flags.static_server_dir + path, |
| 292 | + self.flags.min_compression_length, |
314 | 293 | ), |
315 | 294 | ) |
0 commit comments