[Twisted-web] possible bug in t.web.wsgi

Phil Christensen phil at bubblehouse.org
Fri Dec 12 16:02:25 EST 2008

Hi All,

I haven't opened a ticket for this because I'm not sure if it's a bug  
or not.

I found this in my WSGI code that I had copied from web2, and it looks  
like the current twisted.web.wsgi inherited the same usage.

Basically, the issue is with SERVER_PORT; in t.w.wsgi, around line  
171, the environ dictionary is created like this:

        self.environ = {
             'REQUEST_METHOD': request.method,
             'SCRIPT_NAME': '/' + '/'.join(request.prepath),
             'PATH_INFO': pathInfo,
             'QUERY_STRING': queryString,
             'CONTENT_TYPE': request.getHeader('content-type') or '',
             'CONTENT_LENGTH': request.getHeader('content-length') or  
             'SERVER_NAME': request.getRequestHostname(),
             'SERVER_PORT': str(request.getHost().port),
             'SERVER_PROTOCOL': request.clientproto}

This means SERVER_PORT is always set to the 'real' port that the  
transport is using, not the port that was provided in the request. The  
port used in the request ends up appended to the end of SERVER_NAME.  
Several sources I've referenced indicate something like the following:

	Typically, Web servers listen to HTTP requests on port 80. However, a  
Web server can
	listen to any port that's not in use by another application. A CGI  
program can find
	out the port the Web server is serving HTTP requests by looking at  
the value of the
	SERVER_PORT environment variable. When displaying self-referencing  
hypertext links
	at runtime by examining the contents of SERVER_NAME, be sure to  
append the port
	number of the Web server (typically port 80)  by concatenating it  
with the value of 	SERVER_PORT.

Which leads me to believe that SERVER_NAME should not have the port  
attached, and that SERVER_PORT should be the *requested* port, not the  
'real' port.

The end result of this is that WSGI apps break if run under a port- 
forwarding scenario, where the external port is different from the  
internal one, and where the code is using SERVER_PORT to construct URLs.

Let me know if this is the commonly accepted understanding of these  
variables, and I'll file a bug report.


More information about the Twisted-web mailing list