Opened 5 years ago

Last modified 5 years ago

#5475 defect new

twisted.web.server.Request does not honor Host HTTP headers

Reported by: orestis Owned by:
Priority: normal Milestone:
Component: web Keywords:
Cc: jknight Branch:

Description (last modified by Jean-Paul Calderone)

When a twisted web server is placed behind a proxy, requests should honor the proxy-set Host: header in order to set the hostname and port at _prePathURL.

Current behavior picks up the correct hostname, but use the internal listening port, instead of the proxy-used listening port. In my particular instance, this breaks an athena-powered service when placed behind a proxy.

A patch is floating on the twisted-web mailing list:

Pasted here for reference, original author Richard Wall:

  • home/richard/lib/Twisted/trunk/twisted/web/

    342342        return self.session
    344344    def _prePathURL(self, prepath):
    345         port = self.getHost().port
    346345        if self.isSecure():
    347346            default = 443
    348347        else:
    349348            default = 80
     350        # There won't always be a hostheader, but if it's present, try and find
     351        # the port number from it.
     352        hostheader = self.getHeader('host')
     353        if hostheader:
     354            hostheader = hostheader.split(":", 1)
     355            if len(hostheader) == 2:
     356                port = int(hostheader[1])
     357            else:
     358                port = default
     359        else:
     360            port = self.getHost().port
    350362        if port == default:
    351363            hostport = ''
    352364        else:

The current twisted literature suggests using a VHostMonsterResource, but this feels clunky. If it covers any additional use-cases that using Host: doesn't, it should explain so in the documentation.

Change History (3)

comment:1 Changed 5 years ago by DefaultCC Plugin

Cc: jknight added

comment:2 Changed 5 years ago by Thijs Triemstra

Description: modified (diff)

comment:3 Changed 5 years ago by Jean-Paul Calderone

Description: modified (diff)
Note: See TracTickets for help on using tickets.