From e1efdf394090bc2e65c840b18b94dbe075f52d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Mon, 21 Oct 2024 11:00:24 +0200 Subject: [PATCH] Fix unexpected client_info error --- c2cwsgiutils/client_info.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/c2cwsgiutils/client_info.py b/c2cwsgiutils/client_info.py index f4714232f..9957b6c08 100644 --- a/c2cwsgiutils/client_info.py +++ b/c2cwsgiutils/client_info.py @@ -1,6 +1,8 @@ +import logging import re from typing import Any, Callable +_LOG = logging.getLogger(__name__) SEP_RE = re.compile(r", *") @@ -57,16 +59,19 @@ def _handle_forwarded(environ: dict[str, str]) -> None: if "HTTP_" + header in environ: environ["HTTP_ORIGINAL_" + header] = environ.pop("HTTP_" + header) forwarded = SEP_RE.split(environ.pop("HTTP_FORWARDED"))[0] - fields = dict(tuple(f.split("=", maxsplit=1)) for f in forwarded.split(";")) - if "by" in fields: - environ["SERVER_NAME"] = fields["by"] - if "for" in fields: - environ["REMOTE_ADDR"] = fields["for"] - if "host" in fields: - environ["HTTP_ORIGINAL_HOST"] = environ["HTTP_HOST"] - environ["HTTP_HOST"] = fields["host"] - if "proto" in fields: - environ["wsgi.url_scheme"] = fields["proto"] + try: + fields = dict(tuple(f.split("=", maxsplit=1)) for f in forwarded.split(";")) + if "by" in fields: + environ["SERVER_NAME"] = fields["by"] + if "for" in fields: + environ["REMOTE_ADDR"] = fields["for"] + if "host" in fields: + environ["HTTP_ORIGINAL_HOST"] = environ["HTTP_HOST"] + environ["HTTP_HOST"] = fields["host"] + if "proto" in fields: + environ["wsgi.url_scheme"] = fields["proto"] + except ValueError: + _LOG.exception("Error parsing Forwarded header '%s'", forwarded) def filter_factory(*args: Any, **kwargs: Any) -> Callable[..., Any]: