# HG changeset patch # User Goffi # Date 1458236535 -3600 # Node ID 2b091322c6cb97aadad273a86392e6ef174ada2c # Parent 0d719b6a4115ef6a1c59fd2b4c7eb211180e54f4 blog (atom feed): handle proxy headers like x-forwarded-host to adjust absolute URL in atom feed diff -r 0d719b6a4115 -r 2b091322c6cb src/server/blog.py --- 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), '', ''))