-
-
Notifications
You must be signed in to change notification settings - Fork 839
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
Improve error when redirecting with custom schemes #1002
Conversation
@@ -322,6 +322,10 @@ def redirect_url(self, request: Request, response: Response) -> URL: | |||
|
|||
url = URL(location, allow_relative=True) | |||
|
|||
# Check that we can handle the scheme | |||
if url.scheme and url.scheme not in ("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.
This pattern ("http", "https")
(or something similar) is repeated in a few places, like:
https://github.com/jcugat/httpx/blob/9160631d7fb957a319ce8985c97a858f8eab49a3/httpx/_client.py#L609
https://github.com/jcugat/httpx/blob/9160631d7fb957a319ce8985c97a858f8eab49a3/httpx/_models.py#L105
https://github.com/jcugat/httpx/blob/9160631d7fb957a319ce8985c97a858f8eab49a3/httpx/_models.py#L132
It would probably make sense to unify it in a single place.
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.
In these kinds of cases, if it's a single-liner I'd often prefer not to introduce indirection. It's as clear as it can possibly be, and it's not a complicated expression.
9160631
to
c57f992
Compare
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.
LGTM, thanks @jcugat!
Fixes #822
I used
Scheme "blah" not supported
as the error message instead ofURL scheme must be "http" or "https".
used in other parts of the code. It makes more sense to show the actual invalid scheme since it's coming from the server and not something that the user specified.I also tried the same test with
allow_redirects=False
but unfortunately when the response object is built, it needs a port which we can't derive from the custom scheme. Not sure how to solve this without changing the port definition ofUrl
to allow aNone
port, ideas?