Skip to content

Commit

Permalink
Fix wsgi.environment unix socket issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Skripnick committed Dec 3, 2015
1 parent dad000e commit cccb9cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
24 changes: 15 additions & 9 deletions aiohttp/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,22 @@ def create_wsgi_environ(self, message, payload):
# http://www.ietf.org/rfc/rfc3875

remote = self.transport.get_extra_info('peername')
environ['REMOTE_ADDR'] = remote[0]
environ['REMOTE_PORT'] = remote[1]

sockname = self.transport.get_extra_info('sockname')
environ['SERVER_PORT'] = str(sockname[1])
host = message.headers.get("HOST", None)
if host:
environ['SERVER_NAME'] = host.split(":")[0]
if remote:
environ['REMOTE_ADDR'] = remote[0]
environ['REMOTE_PORT'] = remote[1]
_host, port = self.transport.get_extra_info('sockname')
environ['SERVER_PORT'] = str(port)
host = message.headers.get("HOST", None)
# SERVER_NAME should be set to value of Host header, but this
# header is not required. In this case we shoud set it to local
# address of socket
environ['SERVER_NAME'] = host.split(":")[0] if host else _host
else:
environ['SERVER_NAME'] = sockname[0]
# Dealing with unix socket, so request was received from client by
# upstream server and this data may be found in the headers
for header in ('REMOTE_ADDR', 'REMOTE_PORT',
'SERVER_NAME', 'SERVER_PORT'):
environ[header] = message.headers.get(header, '')

path_info = uri_parts.path
if script_name:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,16 @@ def test_http_1_0_no_host(self):
environ = self._make_one()
self.assertEqual(environ['SERVER_NAME'], '2.3.4.5')
self.assertEqual(environ['SERVER_PORT'], '80')

def test_unix_socket(self):
self.transport.get_extra_info = unittest.mock.Mock(return_value=None)
headers = multidict.MultiDict({
'SERVER_NAME': '1.2.3.4', 'SERVER_PORT': '5678',
'REMOTE_ADDR': '4.3.2.1', 'REMOTE_PORT': '8765'})
self.message = protocol.RawRequestMessage(
'GET', '/', (1, 0), headers, True, 'deflate')
environ = self._make_one()
self.assertEqual(environ['SERVER_NAME'], '1.2.3.4')
self.assertEqual(environ['SERVER_PORT'], '5678')
self.assertEqual(environ['REMOTE_ADDR'], '4.3.2.1')
self.assertEqual(environ['REMOTE_PORT'], '8765')

0 comments on commit cccb9cb

Please sign in to comment.