-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Implement granular URL error hierarchy in the HTTP client #6722
Implement granular URL error hierarchy in the HTTP client #6722
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #6722 +/- ##
==========================================
+ Coverage 97.51% 97.52% +0.01%
==========================================
Files 107 107
Lines 32733 32802 +69
Branches 3824 3840 +16
==========================================
+ Hits 31920 31991 +71
+ Misses 612 610 -2
Partials 201 201
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
@Dreamsorcerer can I get some clue if (and when if so) it's going to be merged? |
@setla looks like a few files diverged since your last update — could you fix the merge conflicts? @Dreamsorcerer has this slipped off of your review queue? |
Backport to 3.10: 💔 cherry-picking failed — conflicts found❌ Failed to cleanly apply fb465e1 on top of patchback/backports/3.10/fb465e155b872f01489173d11e35f02ccbf3a940/pr-6722 Backporting merged PR #6722 into master
🤖 @patchback |
Backport to 3.9: 💔 cherry-picking failed — conflicts found❌ Failed to cleanly apply fb465e1 on top of patchback/backports/3.9/fb465e155b872f01489173d11e35f02ccbf3a940/pr-6722 Backporting merged PR #6722 into master
🤖 @patchback |
@setla to get this released, backport PRs should be submitted per instructions above. |
Two — the branches are separate. The backports are to made in reversed order. Besides, I know that 3.10 is definitely necessary, but not sure about 3.9 — I'll wait to see if anybody objects since bits of this might be perceived as breaking/new behaviors and not just a bugfix. Though I'll be able to merge into 3.10 right away. |
I'm not sure it's important enough to bother going into 3.9. I think we have enough improvements in 3.10 that we should be planning a release in a couple of months anyway. |
I want to make a 3.9 release as soon as #8089 is in. So if there's anything that could piggyback with that for free, I'd rather include it for as long as it's eligible. OTOH, the new exceptions are a new API, so per SemVer it should go into a minor release, not patch. I lost sight of it, but I just checked that the change notes are of the feature type, which makes more sense. I suppose, a 3.9 backport isn't needed indeed. |
This patch introduces 5 granular user-facing exceptions that may occur when HTTP requests are made: * `InvalidUrlClientError` * `RedirectClientError` * `NonHttpUrlClientError` * `InvalidUrlRedirectClientError` * `NonHttpUrlRedirectClientError` Previously `ValueError` or `InvalidURL` was raised and screening out was complicated (a valid URL that redirects to invalid one raised the same error as an invalid URL). Ref: aio-libs#6722 (comment) PR aio-libs#6722 Resolves aio-libs#2507 Resolves aio-libs#2630 Resolves aio-libs#3315 Co-authored-by: Sviatoslav Sydorenko <sviat@redhat.com> (cherry picked from commit fb465e1)
…rchy in the HTTP client (#8158) **This is a backport of PR #6722 as merged into master (fb465e1).** This patch introduces 5 granular user-facing exceptions that may occur when HTTP requests are made: * `InvalidUrlClientError` * `RedirectClientError` * `NonHttpUrlClientError` * `InvalidUrlRedirectClientError` * `NonHttpUrlRedirectClientError` Previously `ValueError` or `InvalidURL` was raised and screening out was complicated (a valid URL that redirects to invalid one raised the same error as an invalid URL). Ref: #6722 (comment) PR #6722 Resolves #2507 Resolves #2630 Resolves #3315 Co-authored-by: Sviatoslav Sydorenko <sviat@redhat.com> (cherry picked from commit fb465e1)
@setla thank you! This improvement is what I've been wanting to get done for years and now it's mere months away.. Watch for new releases of the 3.10 series coming up this year! |
thanks :) |
It looks like this introduced a regression with websocket urls 2024-02-16 09:45:14.978 ERROR (MainThread) [pyunifiprotect.websocket] Websocket disconnect error: %s
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/pyunifiprotect/websocket.py", line 95, in _websocket_loop
self._ws_connection = await session.ws_connect(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 884, in _ws_connect
resp = await self.request(
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 472, in _request
raise NonHttpUrlClientError(url)
aiohttp.client_exceptions.NonHttpUrlClientError: wss://192.168.209.164/proxy/protect/ws/updates?lastUpdateId=80f54055-84de-4e15-95d7-80cac46aafbd |
@bdraco does this mean there's no test coverage for this case? Would you look into it? What's the expected behavior? What's supposed to handle this? |
@@ -167,6 +177,7 @@ class ClientTimeout: | |||
|
|||
# https://www.rfc-editor.org/rfc/rfc9110#section-9.2.2 | |||
IDEMPOTENT_METHODS = frozenset({"GET", "HEAD", "OPTIONS", "TRACE", "PUT", "DELETE"}) | |||
HTTP_SCHEMA_SET = frozenset({"http", "https", ""}) |
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.
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.
I will analyze how its being used in the case I posted above when I get back home next week and provide additional detail
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.
As a possible future improvement, I'd rename the constant to something like SUPPORTED_URI_SCHEMAS
. Especially, if it'll stop doing listing http only.
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.
@webknjaz @bdraco Mentioned exception is related because it's raised here https://github.com/aio-libs/aiohttp/pull/6722/files#diff-f334e752b4894ef951105572ab8b195aeb8db90eb6e48b1dfbd9a01da4c854f5R420 but it's also used in redirects check https://github.com/aio-libs/aiohttp/pull/6722/files#diff-f334e752b4894ef951105572ab8b195aeb8db90eb6e48b1dfbd9a01da4c854f5R647. If we don't want regression, probably first mentioned place should be deleted
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.
We could have two constants where one lists ws/wss and the check asserts against both in the first place but not the second one. Are websockets supposed to be disallowed in redirects?
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.
Since ws_connect
always calls into self.request
think the simplest solution to avoid a breaking change is:
diff --git a/aiohttp/client.py b/aiohttp/client.py
index 8d8d13f2..d608c009 100644
--- a/aiohttp/client.py
+++ b/aiohttp/client.py
@@ -178,7 +178,7 @@ DEFAULT_TIMEOUT: Final[ClientTimeout] = ClientTimeout(total=5 * 60)
# https://www.rfc-editor.org/rfc/rfc9110#section-9.2.2
IDEMPOTENT_METHODS = frozenset({"GET", "HEAD", "OPTIONS", "TRACE", "PUT", "DELETE"})
-HTTP_SCHEMA_SET = frozenset({"http", "https", ""})
+HTTP_SCHEMA_SET = frozenset({"http", "https", "ws", "wss", ""})
_RetType = TypeVar("_RetType")
_CharsetResolver = Callable[[ClientResponse, bytes], str]
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.
I think we probably want that in the _request() check, but probably still want to limit it to http/https in the redirects, and disable redirects in _ws_connect() (as mentioned above).
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.
That makes sense to me. So it seems we need to different constants
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 should include wss:// as well as this breaks discord.py entirely.
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.
I probably lost track of this, maybe nobody got round to making the change discussed above.
What do these changes do?
Added new exception type to separate cases with invalid redirect url during request. Previously ValueError or InvalidURL was raised and screening out was hard (valid url that redirects to invalid one raised the same error as invalid url).
Slightly improved test coverage.
Docs changes previews:
Are there changes in behavior for the user?
In case of invalid redirect url during request - new InvalidRedirectUrl will be raised instead of ValueError or InvalidURL, it derives from InvalidURL so it's backward compatibile
Related issue number
Resolves #2507
Resolves #2630
Resolves #3315
Checklist
CONTRIBUTORS.txt
CHANGES
folder<issue_id>.<type>
for example (588.bugfix)issue_id
change it to the pr id after creating the pr.feature
: Signifying a new feature..bugfix
: Signifying a bug fix..doc
: Signifying a documentation improvement..removal
: Signifying a deprecation or removal of public API..misc
: A ticket has been closed, but it is not of interest to users.