From 7279c07c329e5eb9d5f0d0e635cbbdd8ca335327 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 d56cf04c9..41e9d77a2 100644 --- a/README.md +++ b/README.md @@ -694,6 +694,13 @@ To make a release: - 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`. + ## Testing ### Screenshots diff --git a/c2cwsgiutils/client_info.py b/c2cwsgiutils/client_info.py index ae2bbcc73..26e6b7d18 100644 --- a/c2cwsgiutils/client_info.py +++ b/c2cwsgiutils/client_info.py @@ -1,3 +1,4 @@ +import os import re from typing import Any, Callable @@ -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)