From 998dd167cb455aaf7b5aabe2099140a26c19170c Mon Sep 17 00:00:00 2001 From: ChenyangGao <2339083510@qq.com> Date: Thu, 16 Jan 2025 02:46:54 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20the=20issue=20where=20the=20host=20is=20n?= =?UTF-8?q?ot=20the=20proxy=20address=20when=20there=20is=20a=E2=80=A6=20(?= =?UTF-8?q?#516)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix the issue where the host is not the proxy address when there is a proxy. * fix: compression.py "accept-encoding" default value --- blacksheep/server/asgi.py | 12 ++++++++++-- blacksheep/server/compression.py | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/blacksheep/server/asgi.py b/blacksheep/server/asgi.py index 5b6bac4c..df161057 100644 --- a/blacksheep/server/asgi.py +++ b/blacksheep/server/asgi.py @@ -20,11 +20,19 @@ def get_request_url_from_scope( try: path = scope["path"] protocol = scope["scheme"] - host, port = scope["server"] + for key, val in scope["headers"]: + if key.lower() in (b"host", b"x-forwarded-host", b"x-original-host"): + host = val.decode("latin-1") + port = 0 + break + else: + host, port = scope["server"] except KeyError as key_error: raise ValueError(f"Invalid scope: {key_error}") - if protocol == "http" and port == 80: + if not port: + port_part = "" + elif protocol == "http" and port == 80: port_part = "" elif protocol == "https" and port == 443: port_part = "" diff --git a/blacksheep/server/compression.py b/blacksheep/server/compression.py index 24a5dbac..7959b3cf 100644 --- a/blacksheep/server/compression.py +++ b/blacksheep/server/compression.py @@ -75,7 +75,7 @@ def _is_handled_type(content_type) -> bool: return any(_type in content_type for _type in self.handled_types) def is_handled_encoding() -> bool: - return b"gzip" in (request.get_first_header(b"accept-encoding") or "") + return b"gzip" in (request.get_first_header(b"accept-encoding") or b"") def is_handled_response_content() -> bool: if response is None or response.content is None: