diff libervia/server/server.py @ 1454:fc91b78b71db

server: handle "proxy" scheme in configuration
author Goffi <goffi@goffi.org>
date Tue, 28 Sep 2021 18:18:37 +0200
parents 396d5606477f
children 595e7fef41f3
line wrap: on
line diff
--- 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(