diff src/server/blog.py @ 889:2b091322c6cb

blog (atom feed): handle proxy headers like x-forwarded-host to adjust absolute URL in atom feed
author Goffi <goffi@goffi.org>
date Thu, 17 Mar 2016 18:42:15 +0100
parents dd2273617eb7
children dc27e29b7c22
line wrap: on
line diff
--- a/src/server/blog.py	Fri Mar 11 12:21:55 2016 +0100
+++ b/src/server/blog.py	Thu Mar 17 18:42:15 2016 +0100
@@ -404,9 +404,30 @@
             # and complete unknown parts with what we find in request's url
             url_path = request.URLPath()
             ext_data = self.host.base_url_ext_data
+            if request.requestHeaders.hasHeader('x-forwarded-host'):
+                # we are behing a proxy
+                # we fill proxy_scheme and proxy_netloc value
+                proxy_host = request.requestHeaders.getRawHeaders('x-forwarded-host')[0]
+                try:
+                    proxy_server = request.requestHeaders.getRawHeaders('x-forwarded-server')[0]
+                except TypeError:
+                    # no x-forwarded-server found, we use proxy_host
+                    proxy_netloc = proxy_host
+                else:
+                    # if the proxy host has a port, we use it with server name
+                    proxy_port = urlparse.urlsplit('//{}'.format(proxy_host)).port
+                    proxy_netloc = '{}:{}'.format(proxy_server, proxy_port) if proxy_port is not None else proxy_server
+                proxy_netloc = proxy_netloc.decode('utf-8')
+                try:
+                    proxy_scheme = request.requestHeaders.getRawHeaders('x-forwarded-proto')[0].decode('utf-8')
+                except TypeError:
+                    proxy_scheme = None
+            else:
+                proxy_scheme, proxy_netloc = None, None
+
             base_blog_url = urlparse.urlunsplit((
-                ext_data.scheme or url_path.scheme.decode('utf-8'),
-                ext_data.netloc or url_path.netloc.decode('utf-8'),
+                ext_data.scheme or proxy_scheme or url_path.scheme.decode('utf-8'),
+                ext_data.netloc or proxy_netloc or url_path.netloc.decode('utf-8'),
                 u'{}blog/{user}'.format(ext_data.path or u'/', user=profile),
                 '',
                 ''))