@@ -88,16 +88,22 @@ def create_wsgi_environ(self, message, payload):
88
88
# http://www.ietf.org/rfc/rfc3875
89
89
90
90
remote = self .transport .get_extra_info ('peername' )
91
- environ ['REMOTE_ADDR' ] = remote [0 ]
92
- environ ['REMOTE_PORT' ] = remote [1 ]
93
-
94
- sockname = self .transport .get_extra_info ('sockname' )
95
- environ ['SERVER_PORT' ] = str (sockname [1 ])
96
- host = message .headers .get ("HOST" , None )
97
- if host :
98
- environ ['SERVER_NAME' ] = host .split (":" )[0 ]
91
+ if remote :
92
+ environ ['REMOTE_ADDR' ] = remote [0 ]
93
+ environ ['REMOTE_PORT' ] = remote [1 ]
94
+ _host , port = self .transport .get_extra_info ('sockname' )
95
+ environ ['SERVER_PORT' ] = str (port )
96
+ host = message .headers .get ("HOST" , None )
97
+ # SERVER_NAME should be set to value of Host header, but this
98
+ # header is not required. In this case we shoud set it to local
99
+ # address of socket
100
+ environ ['SERVER_NAME' ] = host .split (":" )[0 ] if host else _host
99
101
else :
100
- environ ['SERVER_NAME' ] = sockname [0 ]
102
+ # Dealing with unix socket, so request was received from client by
103
+ # upstream server and this data may be found in the headers
104
+ for header in ('REMOTE_ADDR' , 'REMOTE_PORT' ,
105
+ 'SERVER_NAME' , 'SERVER_PORT' ):
106
+ environ [header ] = message .headers .get (header , '' )
101
107
102
108
path_info = uri_parts .path
103
109
if script_name :
0 commit comments