From 8da644e7870f30ca5163166ac73229d1483cac56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Mon, 14 Oct 2024 13:35:51 +0200 Subject: [PATCH] Be able to force the client information To be able to generate correct URL when we debug with pserve behind a reverse proxy. --- README.md | 7 +++++++ c2cwsgiutils/client_info.py | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/README.md b/README.md index c59da6c69..36673168c 100644 --- a/README.md +++ b/README.md @@ -665,3 +665,10 @@ To make a release: - Tag the GIT commit. - Add the new branch name in the `.github/workflows/rebuild.yaml` and `.github/workflows/audit.yaml` files. + +## Pserve + +Pserve will not set the headers in the environment then if you are behind a reverse proxy, you will have +wrong values in client information, you can force them by using the environment variables: +`C2CWSGIUTILS_FORCE_PROTO`, `C2CWSGIUTILS_FORCE_HOST` `C2CWSGIUTILS_FORCE_SERVER_NAME` and +`C2CWSGIUTILS_FORCE_REMOTE_ADDR`. diff --git a/c2cwsgiutils/client_info.py b/c2cwsgiutils/client_info.py index 7da8719ba..e18414e14 100644 --- a/c2cwsgiutils/client_info.py +++ b/c2cwsgiutils/client_info.py @@ -1,3 +1,4 @@ +import os import re from typing import Any, Callable, Dict @@ -22,6 +23,15 @@ def __call__(self, environ: Dict[str, str], start_response: Any) -> Any: else: _handle_others(environ) + if "C2CWSGIUTILS_FORCE_PROTO" in os.environ: + environ["wsgi.url_scheme"] = os.environ["C2CWSGIUTILS_FORCE_PROTO"] + if "C2CWSGIUTILS_FORCE_HOST" in os.environ: + environ["HTTP_HOST"] = os.environ["C2CWSGIUTILS_FORCE_HOST"] + if "C2CWSGIUTILS_FORCE_SERVER_NAME" in os.environ: + environ["SERVER_NAME"] = os.environ["C2CWSGIUTILS_FORCE_SERVER_NAME"] + if "C2CWSGIUTILS_FORCE_REMOTE_ADDR" in os.environ: + environ["REMOTE_ADDR"] = os.environ["C2CWSGIUTILS_FORCE_REMOTE_ADDR"] + return self._application(environ, start_response)