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:
Stephen Sadowski 2021-06-16 14:34:52 -05:00 committed by GitHub
parent aff6604636
commit 83c746ee57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

View File

@ -505,7 +505,7 @@ class Request:
:return: peer ip of the socket
:rtype: str
"""
return self.conn_info.client if self.conn_info else ""
return self.conn_info.client_ip if self.conn_info else ""
@property
def port(self) -> int:

View File

@ -65,6 +65,7 @@ class ConnInfo:
__slots__ = (
"client_port",
"client",
"client_ip",
"ctx",
"peername",
"server_port",
@ -78,6 +79,7 @@ class ConnInfo:
self.peername = None
self.server = self.client = ""
self.server_port = self.client_port = 0
self.client_ip = ""
self.sockname = addr = transport.get_extra_info("sockname")
self.ssl: bool = bool(transport.get_extra_info("sslcontext"))
@ -96,6 +98,7 @@ class ConnInfo:
if isinstance(addr, tuple):
self.client = addr[0] if len(addr) == 2 else f"[{addr[0]}]"
self.client_ip = addr[0]
self.client_port = addr[1]

View File

@ -122,3 +122,21 @@ def test_protocol_attribute(app):
_ = app.test_client.get("/", headers=headers)
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"