Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AF_INET6 address family #383

Merged
merged 5 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/protocols/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def get_extra_info(self, key):
"sockname": self.sockname,
"peername": self.peername,
"sslcontext": self.sslcontext,
}[key]
}.get(key)

def write(self, data):
assert not self.closed
Expand Down
46 changes: 45 additions & 1 deletion tests/protocols/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
import socket
from uvicorn.protocols.utils import get_local_addr, get_remote_addr


class MockSocket:
def __init__(self, family, peername=None, sockname=None):
self.peername = peername
self.sockname = sockname
self.family = family

def getpeername(self):
return self.peername

def getsockname(self):
return self.sockname


class MockTransport:
def __init__(self, info):
self.info = info

def get_extra_info(self, info_type):
return self.info[info_type]
return self.info.get(info_type)


def test_get_local_addr_with_socket():
transport = MockTransport({"socket": MockSocket(family=socket.AF_IPX)})
assert get_local_addr(transport) == None

transport = MockTransport(
{"socket": MockSocket(family=socket.AF_INET6, sockname=["::1", 123])}
)
assert get_local_addr(transport) == ("::1", 123)

transport = MockTransport(
{"socket": MockSocket(family=socket.AF_INET, sockname=["123.45.6.7", 123])}
)
assert get_local_addr(transport) == ("123.45.6.7", 123)


def test_get_remote_addr_with_socket():
transport = MockTransport({"socket": MockSocket(family=socket.AF_IPX)})
assert get_remote_addr(transport) == None

transport = MockTransport(
{"socket": MockSocket(family=socket.AF_INET6, peername=["::1", 123])}
)
assert get_remote_addr(transport) == ("::1", 123)

transport = MockTransport(
{"socket": MockSocket(family=socket.AF_INET, peername=["123.45.6.7", 123])}
)
assert get_remote_addr(transport) == ("123.45.6.7", 123)


def test_get_local_addr():
Expand Down
17 changes: 17 additions & 0 deletions uvicorn/protocols/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import socket


def get_remote_addr(transport):
socket_info = transport.get_extra_info("socket")
if socket_info is not None:
info = socket_info.getpeername()
family = socket_info.family
if family in (socket.AF_INET, socket.AF_INET6):
return (str(info[0]), int(info[1]))
return None
info = transport.get_extra_info("peername")
if info is not None and isinstance(info, (list, tuple)) and len(info) == 2:
return (str(info[0]), int(info[1]))
return None


def get_local_addr(transport):
socket_info = transport.get_extra_info("socket")
if socket_info is not None:
info = socket_info.getsockname()
family = socket_info.family
if family in (socket.AF_INET, socket.AF_INET6):
return (str(info[0]), int(info[1]))
return None
info = transport.get_extra_info("sockname")
if info is not None and isinstance(info, (list, tuple)) and len(info) == 2:
return (str(info[0]), int(info[1]))
Expand Down