forked from encode/uvicorn
/
utils.py
78 lines (67 loc) 路 2.91 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import socket
import urllib
def get_remote_addr(transport):
socket_info = transport.get_extra_info("socket")
if socket_info is not None:
try:
info = socket_info.getpeername()
except OSError:
# This case appears to inconsistently occur with uvloop
# bound to a unix domain socket.
family = None
info = None
else:
family = socket_info.family
if family in (socket.AF_INET, socket.AF_INET6):
return (str(info[0]), int(info[1]))
elif hasattr(socket, "AF_UNIX") and family is socket.AF_UNIX:
if isinstance(info, tuple):
# fd case
# <uvloop.PseudoSocket fd=13, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 8000), raddr=('127.0.0.1', 38634)>
return (str(info[0]), int(info[1]))
else:
# unix socket case
# <uvloop.PseudoSocket fd=21, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0, laddr=/tmp/gunicorn.sock>
return None
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]))
elif hasattr(socket, "AF_UNIX") and family is socket.AF_UNIX:
if isinstance(info, tuple):
# fd case
# <uvloop.PseudoSocket fd=13, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 8000), raddr=('127.0.0.1', 38634)>
return (str(info[0]), int(info[1]))
else:
# unix socket case
# <uvloop.PseudoSocket fd=21, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0, laddr=/tmp/gunicorn.sock>
return None
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]))
return None
def is_ssl(transport):
return bool(transport.get_extra_info("sslcontext"))
def get_client_addr(scope):
client = scope.get("client")
if not client:
return ""
return "%s:%d" % client
def get_path_with_query_string(scope):
path_with_query_string = urllib.parse.quote(
scope.get("root_path", "") + scope["path"]
)
if scope["query_string"]:
path_with_query_string = "{}?{}".format(
path_with_query_string, scope["query_string"].decode("ascii")
)
return path_with_query_string