Skip to content

Commit

Permalink
Improve bind_addr storing code @ server.HTTPServer
Browse files Browse the repository at this point in the history
Fixes #81
  • Loading branch information
webknjaz committed Apr 8, 2018
2 parents 06f2191 + d751d46 commit 3ac3382
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
9 changes: 9 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
v6.1.2
======

- :issue:`81`: Fix regression introduced by :pr:`80`.

* Restore :py:attr:`storing bound socket
<cheroot.server.HTTPServer.bind_addr>` in Windows broken by use of
:py:obj:`socket.AF_UNIX`

v6.1.1
======

Expand Down
8 changes: 6 additions & 2 deletions cheroot/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
'Gateway', 'get_ssl_adapter_class')


if 'win' in sys.platform and hasattr(socket, 'AF_INET6'):
if platform.system() == 'Windows' and hasattr(socket, 'AF_INET6'):
if not hasattr(socket, 'IPPROTO_IPV6'):
socket.IPPROTO_IPV6 = 41
if not hasattr(socket, 'IPV6_V6ONLY'):
Expand Down Expand Up @@ -1610,7 +1610,11 @@ def bind(self, family, type, proto=0):
# TODO: keep requested bind_addr separate real bound_addr (port is
# different in case of ephemeral port 0)
self.bind_addr = self.socket.getsockname()
if family != socket.AF_UNIX:
if family in (
# Windows doesn't have socket.AF_UNIX, so not using it in check
socket.AF_INET,
socket.AF_INET6,
):
"""UNIX domain sockets are strings or bytes.
In case of bytes with a leading null-byte it's an abstract socket.
Expand Down
11 changes: 10 additions & 1 deletion cheroot/test/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# vim: set fileencoding=utf-8 :

import os
import socket
import tempfile
import threading
import time
Expand Down Expand Up @@ -33,6 +34,12 @@ def make_http_server(bind_addr):
return httpserver


non_windows_sock_test = pytest.mark.skipif(
not hasattr(socket, 'AF_UNIX'),
reason='UNIX domain sockets are only available under UNIX-based OS',
)


@pytest.fixture
def http_server():
"""Provision a server creator as a fixture."""
Expand Down Expand Up @@ -80,14 +87,16 @@ def test_bind_addr_inet(http_server, ip_addr):
assert httpserver.bind_addr[1] != EPHEMERAL_PORT


@non_windows_sock_test
def test_bind_addr_unix(http_server, unix_sock_file):
"""Check that bound UNIX socket address is stored in server."""
httpserver = http_server.send(unix_sock_file)

assert httpserver.bind_addr == unix_sock_file


@pytest.mark.skip # FIXME: investigate binding to abstract sockets issue
@pytest.mark.skip(reason="Abstract sockets don't work currently")
@non_windows_sock_test
def test_bind_addr_unix_abstract(http_server):
"""Check that bound UNIX socket address is stored in server."""
unix_abstract_sock = b'\x00cheroot/test/socket/here.sock'
Expand Down
5 changes: 4 additions & 1 deletion cheroot/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ def _probe_ipv6_sock(interface):


def _get_conn_data(server):
host, port = server.bind_addr
if isinstance(server.bind_addr, tuple):
host, port = server.bind_addr
else:
host, port = server.bind_addr, 0

interface = webtest.interface(host)

Expand Down

0 comments on commit 3ac3382

Please sign in to comment.