Warning
This document is for an old release of Galaxy. You can alternatively view this page in the latest release if it exists or view the top of the latest release's documentation.
Source code for galaxy.web.framework.middleware.xforwardedhost
[docs]class XForwardedHostMiddleware:
"""
A WSGI middleware that changes the HTTP host header in the WSGI environ
based on the X-Forwarded-Host header IF found
"""
def __call__(self, environ, start_response):
if x_forwarded_host := environ.get("HTTP_X_FORWARDED_HOST", None):
environ["ORIGINAL_HTTP_HOST"] = environ.get("HTTP_HOST")
environ["HTTP_HOST"] = x_forwarded_host.split(", ", 1)[0]
if x_forwarded_for := environ.get("HTTP_X_FORWARDED_FOR", None):
environ["ORIGINAL_REMOTE_ADDR"] = environ.get("REMOTE_ADDR")
environ["REMOTE_ADDR"] = x_forwarded_for.split(",", 1)[0].strip()
x_forwarded_proto = environ.get("HTTP_X_FORWARDED_PROTO", None)
if x_forwarded_proto:
x_forwarded_proto = x_forwarded_proto.split(",", 1)[0].strip()
if x_url_scheme := environ.get("HTTP_X_URL_SCHEME", x_forwarded_proto):
environ["original_wsgi.url_scheme"] = environ["wsgi.url_scheme"]
environ["wsgi.url_scheme"] = x_url_scheme
return self.app(environ, start_response)