1. cherrypy
  2. CherryPy
Issue #49 resolved

BaseUrlFilter should also give the option of using X-Forwarded-Host

Anonymous created an issue

Well, I've been thinking about the mod_rewrite with CP2, and there's a simple solution for any redirects that the server does (using cpg.request.headerMap['Host'] as the host name). When a rewrite is made, Apache sets it to the host I rewrite to (for example, localhost:8000), but also sets "X-Forwarded-Host" header line for the original host.

So, the solution would be to use X-Forwarded-Host header, if exists, and if not, then fallback to the Host header line. So, replace the lines when {{{cpg.request.headerMap['Host']}}} appears with the lines with: {{{cpg.request.headerMap.get("X-Forwarded-Host", cpg.request.headerMap['Host'])}}}. That gives us the true hostname in both cases.

Comments (4)

  1. Anonymous

    Here's the patch for cherrypy/lib/filter/baseurlfilter.py

    @@ -25,6 +25,12 @@
             self.baseUrl = baseUrl
         def afterRequestHeader(self):
    +       if cpg.request.headerMap.has_key("X-Forwarded-Host"):
    +           browserUrl = cpg.request.browserUrl
    +           baseUrl = x[:x.find("://")] + "://" + cpg.request.headerMap["X-Forwarded-Host"]
    +       else:
    +           baseUrl = self.baseUrl
             cpg.request.browserUrl = cpg.request.browserUrl.replace(
    -            cpg.request.base, self.baseUrl)
    -        cpg.request.base = self.baseUrl
    +            cpg.request.base, baseUrl)
    +        cpg.request.base = baseUrl
  2. Log in to comment