Added new client_ip accessor (#2114)
* Added new client_ip accessor for ConnInfo class, updated request to use client_ip instead of client to be more representative of what will be returned (actual ipv6 ip instead of bracket wrapped ip) * Fix ConnInfo init * add ipv6 test - maybe will work? * fixed silly indentation error * Bump testing client * Extend testing * Fix text Co-authored-by: Adam Hopkins <adam@amhopkins.com> Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
This commit is contained in:
parent
aff6604636
commit
83c746ee57
@ -505,7 +505,7 @@ class Request:
|
|||||||
:return: peer ip of the socket
|
:return: peer ip of the socket
|
||||||
:rtype: str
|
:rtype: str
|
||||||
"""
|
"""
|
||||||
return self.conn_info.client if self.conn_info else ""
|
return self.conn_info.client_ip if self.conn_info else ""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def port(self) -> int:
|
def port(self) -> int:
|
||||||
|
@ -65,6 +65,7 @@ class ConnInfo:
|
|||||||
__slots__ = (
|
__slots__ = (
|
||||||
"client_port",
|
"client_port",
|
||||||
"client",
|
"client",
|
||||||
|
"client_ip",
|
||||||
"ctx",
|
"ctx",
|
||||||
"peername",
|
"peername",
|
||||||
"server_port",
|
"server_port",
|
||||||
@ -78,6 +79,7 @@ class ConnInfo:
|
|||||||
self.peername = None
|
self.peername = None
|
||||||
self.server = self.client = ""
|
self.server = self.client = ""
|
||||||
self.server_port = self.client_port = 0
|
self.server_port = self.client_port = 0
|
||||||
|
self.client_ip = ""
|
||||||
self.sockname = addr = transport.get_extra_info("sockname")
|
self.sockname = addr = transport.get_extra_info("sockname")
|
||||||
self.ssl: bool = bool(transport.get_extra_info("sslcontext"))
|
self.ssl: bool = bool(transport.get_extra_info("sslcontext"))
|
||||||
|
|
||||||
@ -96,6 +98,7 @@ class ConnInfo:
|
|||||||
|
|
||||||
if isinstance(addr, tuple):
|
if isinstance(addr, tuple):
|
||||||
self.client = addr[0] if len(addr) == 2 else f"[{addr[0]}]"
|
self.client = addr[0] if len(addr) == 2 else f"[{addr[0]}]"
|
||||||
|
self.client_ip = addr[0]
|
||||||
self.client_port = addr[1]
|
self.client_port = addr[1]
|
||||||
|
|
||||||
|
|
||||||
|
@ -122,3 +122,21 @@ def test_protocol_attribute(app):
|
|||||||
_ = app.test_client.get("/", headers=headers)
|
_ = app.test_client.get("/", headers=headers)
|
||||||
|
|
||||||
assert isinstance(retrieved, HttpProtocol)
|
assert isinstance(retrieved, HttpProtocol)
|
||||||
|
|
||||||
|
|
||||||
|
def test_ipv6_address_is_not_wrapped(app):
|
||||||
|
@app.get("/")
|
||||||
|
async def get(request):
|
||||||
|
return response.json(
|
||||||
|
{
|
||||||
|
"client_ip": request.conn_info.client_ip,
|
||||||
|
"client": request.conn_info.client,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
request, resp = app.test_client.get("/", host="::1")
|
||||||
|
|
||||||
|
assert request.route is list(app.router.routes)[0]
|
||||||
|
assert resp.json["client"] == "[::1]"
|
||||||
|
assert resp.json["client_ip"] == "::1"
|
||||||
|
assert request.ip == "::1"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user