-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
SSLObject made available through exceptions #11553
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
base: master
Are you sure you want to change the base?
Changes from all commits
36df505
7d46b78
d5c428c
593499a
9dde0a6
260404a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Added ``ssl_object`` attribute to ``ClientResponseError`` and its subclasses | ||
to provide access to SSL certificate information even after connection closure | ||
-- by :user:`fed239`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,33 @@ | |
_CONTAINS_CONTROL_CHAR_RE = re.compile(r"[^-!#$%&'*+.^_`|~0-9a-zA-Z]") | ||
|
||
|
||
def _extract_ssl_object( | ||
connection: Optional[Union["Connection", object]], | ||
) -> Optional[object]: | ||
Comment on lines
+108
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optionals are considered confusing, use unions instead. Also, |
||
"""Extract SSL object from connection or transport if available.""" | ||
if connection is None: | ||
return None | ||
|
||
# Handle both Connection objects and Transport objects | ||
if hasattr(connection, "transport"): | ||
# This is a Connection object | ||
transport = connection.transport | ||
elif hasattr(connection, "get_extra_info"): | ||
# This is a Transport object | ||
transport = connection | ||
else: | ||
return None | ||
|
||
if transport is None: | ||
return None | ||
|
||
try: | ||
return transport.get_extra_info("ssl_object") | ||
except Exception: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that suppressing arbitrary exceptions is a good idea. There's a very small number of cases where this is acceptable in general. |
||
# If we can't get the SSL object for any reason, return None | ||
return None | ||
|
||
|
||
def _gen_default_accept_encoding() -> str: | ||
encodings = [ | ||
"gzip", | ||
|
@@ -476,6 +503,7 @@ async def start(self, connection: "Connection") -> "ClientResponse": | |
status=exc.code, | ||
message=exc.message, | ||
headers=exc.headers, | ||
ssl_object=_extract_ssl_object(connection), | ||
) from exc | ||
|
||
if message.code < 100 or message.code > 199 or message.code == 101: | ||
|
@@ -570,6 +598,7 @@ def raise_for_status(self) -> None: | |
status=self.status, | ||
message=self.reason, | ||
headers=self.headers, | ||
ssl_object=_extract_ssl_object(self._connection), | ||
) | ||
|
||
def _release_connection(self) -> None: | ||
|
@@ -691,6 +720,7 @@ async def json( | |
"unexpected mimetype: %s" % self.content_type | ||
), | ||
headers=self.headers, | ||
ssl_object=_extract_ssl_object(self._connection), | ||
) | ||
|
||
if encoding is None: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be an arbitrary object.