I would just have a middleware look for that header and rewrite
wsgi.url_scheme appropriately in the environment.  You could use repoze.vhm
to do that, but since you only need that one tweak, a few lines of
middleware should really be all you need.

Chris

class RewriteURLSchemeMiddleware(object):
    """Not tested."""

    key = 'HTTP_X_FORWARDED_PROTO'

    def __init__(self, app, global_config, **config):
        self.app = app

    def __call__(self, environ, start_response):
        if self.key in environ:
            environ = environ.copy()
            environ['wsgi.url_scheme'] = environ[key]
        return app(environ, start_response)


On Wed, Nov 10, 2010 at 11:40 AM, Hanno Schlichting <ha...@hannosch.eu>wrote:

> Hi.
>
> We have an application which supports both HTTP and HTTPS. If the user
> visits the site via https:// he should stay on that protocol and as
> such all links should be generated accordingly.
>
> We do the SSL handling in Nginx in front of the bfg application. So
> the application always gets a request.application_url with a http://
> link. We have an additional header "'HTTP_X_FORWARDED_PROTO" which
> gets sets to 'https'.
>
> In order to support this, we use our own route_url function that looks
> like this:
>
> from repoze.bfg.url import route_url as bfg_route_url
>
> def route_url(route_name, request, *elements, **kw):
>    https = request.environ.get('HTTP_X_FORWARDED_PROTO') == 'https'
>    value = bfg_route_url(route_name, request, *elements, **kw)
>    if https:
>        return value.replace('http://', 'https://')
>    return value
>
> Does this sound like a reasonable approach, or is there a better
> alternative? Having to use a different implementation of a core API
> feels wrong.
>
> Thanks,
> Hanno
> _______________________________________________
> Repoze-dev mailing list
> Repoze-dev@lists.repoze.org
> http://lists.repoze.org/listinfo/repoze-dev
>
_______________________________________________
Repoze-dev mailing list
Repoze-dev@lists.repoze.org
http://lists.repoze.org/listinfo/repoze-dev

Reply via email to