Skip to content

Commit

Permalink
Add ClientResponse.certificate and populate it, if appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
auxsvr committed Jan 28, 2023
1 parent 4635161 commit f2ef428
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/2816.feature
@@ -0,0 +1 @@
Add `ClientResponse.certificate` and populate it, if appropriate.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -260,6 +260,7 @@ Pavol Vargovčík
Pawel Kowalski
Pawel Miech
Pepe Osca
Petros Blanis
Philipp A.
Pieter van Beek
Qiao Han
Expand Down
5 changes: 5 additions & 0 deletions aiohttp/client_reqrep.py
Expand Up @@ -698,6 +698,7 @@ def __init__(

self.method = method
self.cookies: SimpleCookie[str] = SimpleCookie()
self.certificate: Optional[Dict[str, Union[Tuple[Any, ...], int, str]]] = None

self._real_url = url
self._url = url.with_fragment(None)
Expand Down Expand Up @@ -880,6 +881,10 @@ async def start(self, connection: "Connection") -> "ClientResponse":
self.cookies.load(hdr)
except CookieError as exc:
client_logger.warning("Can not load response cookies: %s", exc)

# certificate
if self._connection is not None and self._connection.transport is not None:
self.certificate = self._connection.transport.get_extra_info("peercert")
return self

def _response_eof(self) -> None:
Expand Down
4 changes: 4 additions & 0 deletions docs/client_reference.rst
Expand Up @@ -1360,6 +1360,10 @@ Response object
objects of preceding requests (earliest request first) if there were
redirects, an empty sequence otherwise.

.. attribute:: certificate

The server certificate, if available.

.. method:: close()

Close response and underlying connection.
Expand Down
6 changes: 6 additions & 0 deletions tests/test_client_request.py
Expand Up @@ -1230,3 +1230,9 @@ def test_loose_cookies_types(loop: Any) -> None:
def test_gen_default_accept_encoding(has_brotli: Any, expected: Any) -> None:
with mock.patch("aiohttp.client_reqrep.HAS_BROTLI", has_brotli):
assert _gen_default_accept_encoding() == expected


async def test_certificate(loop: Any, conn: Any) -> None:
req = ClientRequest("get", URL("https://www.python.org"), loop=loop)
resp = await req.send(conn)
assert resp.certificate is not None

0 comments on commit f2ef428

Please sign in to comment.