|
1 | 1 | """The utils tools for both http proxy and websocket proxy.""" |
2 | 2 |
|
3 | 3 | import ipaddress |
4 | | -import logging |
5 | 4 | import warnings |
6 | 5 | from functools import lru_cache |
7 | 6 | from textwrap import dedent |
8 | 7 | from typing import ( |
9 | 8 | Any, |
10 | | - Iterable, |
11 | 9 | Mapping, |
12 | 10 | Optional, |
13 | 11 | Protocol, |
|
17 | 15 | ) |
18 | 16 |
|
19 | 17 | import httpx |
20 | | -from starlette import status |
21 | 18 | from starlette.background import BackgroundTask as BackgroundTask_t |
22 | 19 | from starlette.datastructures import ( |
23 | 20 | Headers as StarletteHeaders, |
|
26 | 23 | MutableHeaders as StarletteMutableHeaders, |
27 | 24 | ) |
28 | 25 | from starlette.responses import JSONResponse |
29 | | -from starlette.types import Scope |
30 | 26 | from typing_extensions import deprecated, overload |
31 | 27 |
|
32 | 28 | __all__ = ( |
33 | 29 | "check_base_url", |
34 | 30 | "return_err_msg_response", |
35 | | - "check_http_version", |
36 | 31 | "BaseURLError", |
37 | 32 | "ErrMsg", |
38 | 33 | "ErrRseponseJson", |
@@ -129,10 +124,6 @@ class _RejectedProxyRequestError(RuntimeError): |
129 | 124 | """Should be raised when reject proxy request.""" |
130 | 125 |
|
131 | 126 |
|
132 | | -class _UnsupportedHttpVersionError(RuntimeError): |
133 | | - """Unsupported http version.""" |
134 | | - |
135 | | - |
136 | 127 | #################### Tools #################### |
137 | 128 |
|
138 | 129 |
|
@@ -337,35 +328,6 @@ def return_err_msg_response( |
337 | 328 | ) |
338 | 329 |
|
339 | 330 |
|
340 | | -def check_http_version( |
341 | | - scope: Scope, supported_versions: Iterable[str] |
342 | | -) -> Union[JSONResponse, None]: |
343 | | - """Check whether the http version of scope is in supported_versions. |
344 | | -
|
345 | | - Args: |
346 | | - scope: asgi scope dict. |
347 | | - supported_versions: The supported http versions. |
348 | | -
|
349 | | - Returns: |
350 | | - If the http version of scope is not in supported_versions, return a JSONResponse with status_code=505, |
351 | | - else return None. |
352 | | - """ |
353 | | - # https://asgi.readthedocs.io/en/latest/specs/www.html#http-connection-scope |
354 | | - # https://asgi.readthedocs.io/en/latest/specs/www.html#websocket-connection-scope |
355 | | - http_version: str = scope.get("http_version", "") |
356 | | - # 如果明确指定了http版本(即不是""),但不在支持的版本内,则返回505 |
357 | | - if http_version not in supported_versions and http_version != "": |
358 | | - error = _UnsupportedHttpVersionError( |
359 | | - f"The request http version is {http_version}, but we only support {supported_versions}." |
360 | | - ) |
361 | | - # TODO: 或许可以logging记录下 scope.get("client") 的值 |
362 | | - return return_err_msg_response( |
363 | | - error, |
364 | | - status_code=status.HTTP_505_HTTP_VERSION_NOT_SUPPORTED, |
365 | | - logger=logging.info, |
366 | | - ) |
367 | | - |
368 | | - |
369 | 331 | def default_proxy_filter(url: httpx.URL) -> Union[None, str]: |
370 | 332 | """Filter by host. |
371 | 333 |
|
|
0 commit comments