changeset 1454:fc91b78b71db

server: handle "proxy" scheme in configuration
author Goffi <goffi@goffi.org>
date Tue, 28 Sep 2021 18:18:37 +0200
parents d62df81440c6
children 595e7fef41f3
files libervia/server/proxy.py libervia/server/server.py
diffstat 2 files changed, 24 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/server/proxy.py	Wed Sep 08 12:08:00 2021 +0200
+++ b/libervia/server/proxy.py	Tue Sep 28 18:18:37 2021 +0200
@@ -52,7 +52,8 @@
         )
 
     def render(self, request):
-        # Forwarded and X-Forwarded-xxx headers can be set if we have behin an other proxy
+        # Forwarded and X-Forwarded-xxx headers can be set
+        # if we have behind an other proxy
         if ((not request.getHeader(C.H_FORWARDED)
              and not request.getHeader(C.H_X_FORWARDED_HOST))):
             forwarded_data = []
--- a/libervia/server/server.py	Wed Sep 08 12:08:00 2021 +0200
+++ b/libervia/server/server.py	Tue Sep 28 18:18:37 2021 +0200
@@ -499,6 +499,28 @@
 
                 # normal redirection system is not used here
                 continue
+            elif new_url.scheme == "proxy":
+                # a reverse proxy
+                host, port = new_url.hostname, new_url.port
+                if host is None or port is None:
+                    raise ValueError(
+                        "invalid host or port in proxy redirection, please check your "
+                        "configuration: {new_url.geturl()}"
+                    )
+                url_prefix = (new_url.path or old).rstrip('/')
+                res = proxy.SatReverseProxyResource(
+                    host,
+                    port,
+                    url_prefix.encode(),
+                )
+                self.addResourceToPath(old, res)
+                log.info(
+                    f"[{self.host_name}] Added redirection from /{old} to reverse proxy "
+                    f"{new_url.netloc} with URL prefix {url_prefix}/"
+                )
+
+                # normal redirection system is not used here
+                continue
             else:
                 raise NotImplementedError(
                     "{scheme}: scheme is not managed for url_redirections_dict".format(