-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Avoid getting stuck in a loop #2244
Conversation
This prevents a case where we make a request to URL A, which 301s to B which would then 301 back to A. Alternatively, for less simple schemes, this will also prevent us from getting stuck in a loop, e.g., it will prevent the following from causing an endless loop: A -> B -> C -> D -> E -> F -- ^ \ | / ---<------------<----------<-
This no longer gets us stuck in a loop by causing us to actually hit the URLs that we're being forcibly redirected to, starting with the end of the redirect chain. Is this what we want? Or do we want to throw an exception, e.g. |
I don't think we should be adding a new exception for this. This will preserve the previous behavior of having hotting a Max Redirects exception thrown. It's no worse than the behavior we had before the redirect cache was introduced.Sent from my Android device with K-9 Mail. Please excuse my brevity. |
But yes, I suspect we could subclass @kennethreitz thoughts on saving people from hitting the network more than necessary when we can detect an endless redirect loop? |
I should also have mentioned that this saves us from loops like this too:
Or any other case that essentially results in an endless loop |
The In my case, the url works (and redirects properly) in the browser. So maybe there is another issue on how the cache works. |
@RuudBurger both @Lukasa and I have our emails available on our GitHub profiles. If you'd rather use PGP, you can find my PGP key (and associated email address) by searching https://pgp.mit.edu/ for my real name. |
I see absolutely no reason why permanent redirects in the redirect cache should not count against the max redirects limit. They are still redirects, we're just not hitting the wire to do them. That behaviour will also fix our infinite redirects problem. |
In my opinion an exception needs to be raised and I like @sigmavirus24 set() solution, maybe you want to pack it a bit more:
|
@fcosantos I don't quite understand what you mean. Could you clarify this for me? |
I think the key point is that there's no need for a |
@Lukasa I can think of a couple ways to make it influence the max_redirects count.
Option 2 seems most practical, but I just loathe adding more arguments (that could confuse a user) to a public API like this. Also, I think there's value in using a subclass of |
I wonder if my concern about the redirect cache leading to reproducible behaviour is just my specific problem. I just realised that the other thing this redirect cache changes is the behaviour of |
@Lukasa good point. I'm not sure we can actually reconstruct the history accurately unless we also cache responses in the redirect cache. In other words, we'd have a cache something like: {'http://example.com/': ('http://www.example.com', <Response [301]>)} And a big problem with that would be timestamps in headers and such (e.g., cookies). All of this which makes me wonder exactly how good an idea it is to keep the redirect cache around. |
I don't know that we should necessarily throw out the redirect cache, but we should at the very least document the hell out of how it is going to behave. |
Avoid getting stuck in a loop
+1 |
Uh... this wasn't exactly ready to merge. (Not that it breaks anything, but we were discussing alternative solutions.) |
This prevents a case where we make a request to URL A, which 301s to B which
would then 301 back to A. Alternatively, for less simple schemes, this will
also prevent us from getting stuck in a loop, e.g., it will prevent the
following from causing an endless loop:
Fixes #2231.
I tested this by cloning httpbin and hard coding a permanent redirect loop from
/relative-redirect/1
to/relative-redirect/2
so that we could trigger this. This pull request fixes it.