Skip to content

Commit

Permalink
wsgi: UNIX socket address was trimmed in "wsgi starting" log; Thanks …
Browse files Browse the repository at this point in the history
…to Ihar Hrachyshka

For UNIX sockets, getsockname() returns a string, not a 2-tuple, that
represents the file path that is assigned to it. That's why we need a
special handling for the socket type.

#235
  • Loading branch information
temoto committed Jun 2, 2015
1 parent ed06553 commit 3ba7b02
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -120,3 +120,4 @@ Thanks To
* Tim Simmons, Use _socket_nodns and select in dnspython support
* Antonio Cuni, fix fd double close on PyPy
* Seyeong Kim
* Ihar Hrachyshka
33 changes: 20 additions & 13 deletions eventlet/wsgi.py
Expand Up @@ -711,6 +711,24 @@ def log_message(self, message):
ACCEPT_ERRNO = set((errno.EPIPE, errno.EBADF, errno.ECONNRESET))


def socket_repr(sock):
scheme = 'http'
if hasattr(sock, 'do_handshake'):
scheme = 'https'

name = sock.getsockname()
if sock.family == socket.AF_INET:
hier_part = '//{0}:{1}'.format(*name)
elif sock.family == socket.AF_INET6:
hier_part = '//[{0}]:{1}'.format(*name[:2])
elif sock.family == socket.AF_UNIX:
hier_part = name
else:
hier_part = repr(name)

return scheme + ':' + hier_part


def server(sock, site,
log=None,
environ=None,
Expand Down Expand Up @@ -805,19 +823,8 @@ def server(sock, site,
else:
pool = greenpool.GreenPool(max_size)
try:
host, port = sock.getsockname()[:2]
port = ':%s' % (port, )
if hasattr(sock, 'do_handshake'):
scheme = 'https'
if port == ':443':
port = ''
else:
scheme = 'http'
if port == ':80':
port = ''

serv.log.info("(%s) wsgi starting up on %s://%s%s/" % (
serv.pid, scheme, host, port))
serv.log.info("(%s) wsgi starting up on %s" % (
serv.pid, socket_repr(sock)))
while is_accepting:
try:
client_socket = sock.accept()
Expand Down
16 changes: 16 additions & 0 deletions tests/wsgi_test.py
Expand Up @@ -2,9 +2,11 @@
import collections
import errno
import os
import shutil
import signal
import socket
import sys
import tempfile
import traceback
import unittest

Expand Down Expand Up @@ -1488,6 +1490,20 @@ def wsgi_app(environ, start_response):
self.assertEqual(result.headers_lower[random_case_header[0].lower()], random_case_header[1])
self.assertEqual(result.headers_original[random_case_header[0]], random_case_header[1])

def test_log_unix_address(self):
tempdir = tempfile.mkdtemp('eventlet_test_log_unix_address')
path = ''
try:
sock = eventlet.listen(tempdir + '/socket', socket.AF_UNIX)
path = sock.getsockname()

log = six.StringIO()
self.spawn_server(sock=sock, log=log)
eventlet.sleep(0) # need to enter server loop
assert 'http:' + path in log.getvalue()
finally:
shutil.rmtree(tempdir)


def read_headers(sock):
fd = sock.makefile('rb')
Expand Down

0 comments on commit 3ba7b02

Please sign in to comment.