Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ default_install_hook_types: [pre-commit, commit-msg]

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
rev: v6.0.0
hooks:
- id: no-commit-to-branch
- id: check-added-large-files
Expand All @@ -22,13 +22,13 @@ repos:
- id: trailing-whitespace
# ruff must before black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.11.2
rev: v0.14.13
hooks:
- id: ruff
alias: ruff # NOTE: don't change this alias, it's used in `ver_sync.py`, keep consistent with `pyproject.toml`
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 25.1.0
rev: 26.1.0
hooks:
- id: black
alias: black # NOTE: don't change this alias, it's used in `ver_sync.py`, keep consistent with `pyproject.toml`
Expand All @@ -40,7 +40,7 @@ repos:
additional_dependencies:
- tomli
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
rev: v9.22.0
rev: v9.24.0
hooks:
- id: commitlint
stages: [commit-msg]
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ dependencies = [
# NOTE: 👇
# The versions of `black`, `ruff`, `codespell`, must be consistent with the `.pre-commit-config.yaml`.
# Don't edit them manually, use `pre-commit run ver_sync` instead.
"black==25.1.0",
"ruff==0.11.2",
"black==26.1.0",
"ruff==0.14.13",
"codespell==2.4.1",
# Don't write comments on these lines, because they will be removed by `pre-commit run ver_sync`.
# NOTE: 👆
Expand Down
1 change: 0 additions & 1 deletion scripts/pre_commit_scripts/ver_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

"""Maintain lint tools version consistency between `.pre-commit-config.yaml` and `pyproject.toml`."""


# https://packaging.pypa.io/en/stable/requirements.html
# https://yaml.readthedocs.io/en/latest/example/
# https://tomlkit.readthedocs.io/en/latest/quickstart/
Expand Down
26 changes: 8 additions & 18 deletions src/fastapi_proxy_lib/core/_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,39 +204,31 @@ def check_base_url(base_url: Union[httpx.URL, str], /) -> httpx.URL:
)

if not base_url.scheme or not base_url.netloc:
raise BaseURLError(
dedent(
f"""\
raise BaseURLError(dedent(f"""\
`base_url` must contain scheme and netloc,
e.g. {example_url}
got: {base_url}\
"""
)
)
"""))

# NOTE: 尽量用 URL.copy_with() 来修改URL,而不是 URL.join(),因为后者性能较差

if base_url.query or base_url.fragment:
base_url = base_url.copy_with(query=None, fragment=None)
warnings.warn(
dedent(
f"""\
dedent(f"""\
`base_url` should not contain `query` or `fragment`, which will be ignored.
The `base_url` will be treated as: {base_url}\
"""
),
"""),
stacklevel=2,
)
# 我们在这里强制要求 base_url 以"/"结尾是有原因的:
# 因为 RouterHelper 生成的路由是以"/"结尾的,在反向代理时
# "/" 之后后路径参数将会被拼接到这个 base_url 后面
if not str(base_url).endswith("/"):
msg = dedent(
f"""\
msg = dedent(f"""\
`base_url` must ends with "/", may be you mean:
{base_url}/\
"""
)
""")
raise BaseURLError(msg)

return base_url
Expand Down Expand Up @@ -424,17 +416,15 @@ def warn_for_none_filter(
Else will just return the original argument `proxy_filter`.
"""
if proxy_filter is None:
msg = dedent(
"""\
msg = dedent("""\
The `proxy_filter` is None, which means no filter will be used.
It is not recommended, because it may cause security issues.

A default proxy filter will be used, which will reject the proxy request:
- if the host of url is ip address, and is not global ip address.

More info: https://wsh032.github.io/fastapi-proxy-lib/Usage/Security/
"""
)
""")
warnings.warn(msg, stacklevel=3)
return default_proxy_filter
else:
Expand Down
6 changes: 2 additions & 4 deletions src/fastapi_proxy_lib/fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
from textwrap import dedent

if find_spec("fastapi") is None: # pragma: no cover # 无法测试
msg: str = dedent(
"""\
msg: str = dedent("""\
`fastapi` is not installed.
`fastapi_proxy_lib.fastapi` module requires installing `fastapi` first:
pip install fastapi-proxy-lib[standard]
"""
)
""")
raise RuntimeError(msg)
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ruff: noqa: ANN201, ANN001
# ruff: noqa: ANN201

# pyright: reportMissingParameterType=false
# 返回值标注太麻烦,让pyright自己推断
Expand Down
Loading