Skip to content

Commit 326f8b0

Browse files
committed
feat: enhance the robustness of WebSocket error handling and remove the HTTP version restriction
1 parent 4346615 commit 326f8b0

File tree

6 files changed

+133
-145
lines changed

6 files changed

+133
-145
lines changed

src/fastapi_proxy_lib/core/_tool.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
"""The utils tools for both http proxy and websocket proxy."""
22

33
import ipaddress
4-
import logging
54
import warnings
65
from functools import lru_cache
76
from textwrap import dedent
87
from typing import (
98
Any,
10-
Iterable,
119
Mapping,
1210
Optional,
1311
Protocol,
@@ -17,7 +15,6 @@
1715
)
1816

1917
import httpx
20-
from starlette import status
2118
from starlette.background import BackgroundTask as BackgroundTask_t
2219
from starlette.datastructures import (
2320
Headers as StarletteHeaders,
@@ -26,13 +23,11 @@
2623
MutableHeaders as StarletteMutableHeaders,
2724
)
2825
from starlette.responses import JSONResponse
29-
from starlette.types import Scope
3026
from typing_extensions import deprecated, overload
3127

3228
__all__ = (
3329
"check_base_url",
3430
"return_err_msg_response",
35-
"check_http_version",
3631
"BaseURLError",
3732
"ErrMsg",
3833
"ErrRseponseJson",
@@ -129,10 +124,6 @@ class _RejectedProxyRequestError(RuntimeError):
129124
"""Should be raised when reject proxy request."""
130125

131126

132-
class _UnsupportedHttpVersionError(RuntimeError):
133-
"""Unsupported http version."""
134-
135-
136127
#################### Tools ####################
137128

138129

@@ -337,35 +328,6 @@ def return_err_msg_response(
337328
)
338329

339330

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-
369331
def default_proxy_filter(url: httpx.URL) -> Union[None, str]:
370332
"""Filter by host.
371333

src/fastapi_proxy_lib/core/http.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
_RejectedProxyRequestError, # pyright: ignore [reportPrivateUsage] # 允许使用本项目内部的私有成员
3232
change_necessary_client_header_for_httpx,
3333
check_base_url,
34-
check_http_version,
3534
return_err_msg_response,
3635
warn_for_none_filter,
3736
)
@@ -81,10 +80,6 @@ class _ReverseProxyServerError(RuntimeError):
8180
_NON_REQUEST_BODY_METHODS = ("GET", "HEAD", "OPTIONS", "TRACE")
8281
"""The http methods that should not contain request body."""
8382

84-
# https://asgi.readthedocs.io/en/latest/specs/www.html#http-connection-scope
85-
SUPPORTED_HTTP_VERSIONS = ("1.0", "1.1")
86-
"""The http versions that we supported now. It depends on `httpx`."""
87-
8883
# https://www.python-httpx.org/exceptions/
8984
_400_ERROR_NEED_TO_BE_CATCHED_IN_FORWARD_PROXY = (
9085
httpx.InvalidURL, # 解析url时出错
@@ -227,8 +222,6 @@ async def send_request_to_target( # pyright: ignore [reportIncompatibleMethodOv
227222
) -> StarletteResponse:
228223
"""Change request headers and send request to target url.
229224
230-
- The http version of request must be in [`SUPPORTED_HTTP_VERSIONS`][fastapi_proxy_lib.core.http.SUPPORTED_HTTP_VERSIONS].
231-
232225
Args:
233226
request: the original client request.
234227
target_url: target url that request will be sent to.
@@ -239,10 +232,6 @@ async def send_request_to_target( # pyright: ignore [reportIncompatibleMethodOv
239232
client = self.client
240233
follow_redirects = self.follow_redirects
241234

242-
check_result = check_http_version(request.scope, SUPPORTED_HTTP_VERSIONS)
243-
if check_result is not None:
244-
return check_result
245-
246235
# 将请求头中的host字段改为目标url的host
247236
# 同时强制移除"keep-alive"字段和添加"keep-alive"值到"connection"字段中保持连接
248237
require_close, proxy_header = _change_client_header(

0 commit comments

Comments
 (0)