diff --git a/README.md b/README.md index e172329ee..4410f4255 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)