Skip to content

Commit

Permalink
Fix unexpected client_info error
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Oct 21, 2024
1 parent ff51692 commit e1efdf3
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions c2cwsgiutils/client_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import re
from typing import Any, Callable

_LOG = logging.getLogger(__name__)
SEP_RE = re.compile(r", *")


Expand Down Expand Up @@ -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]:
Expand Down

0 comments on commit e1efdf3

Please sign in to comment.