Skip to content

raise InvalidHeader on multiple Location values#3417

Merged
sigmavirus24 merged 3 commits intopsf:proposed/3.0.0from
nateprewitt:2939_multiple_location_headers
Aug 22, 2016
Merged

raise InvalidHeader on multiple Location values#3417
sigmavirus24 merged 3 commits intopsf:proposed/3.0.0from
nateprewitt:2939_multiple_location_headers

Conversation

@nateprewitt
Copy link
Copy Markdown
Member

Addresses issue raised in #2939 with a fix for multiple Location headers in response. This currently breaks on test_requests_are_updated_each_time because the RedirectSession populates it's raw attribute with StringIO rather than a urllib3 HTTPResponse. I can convert the _build_raw method to fix the test, if this looks like a workable solution.

@nateprewitt nateprewitt force-pushed the 2939_multiple_location_headers branch from 6bf0345 to 25da055 Compare July 15, 2016 05:44
@Lukasa
Copy link
Copy Markdown
Member

Lukasa commented Jul 15, 2016

@nateprewitt Thanks for starting this!

Unfortunately, quite a lot of people use non-urllib3 file-like objects to back requests, and I'd like to avoid breaking that if we can. Can you rearrange this to support urllib3 if possible but otherwise fallback to some other behaviour, per my comment.

@nateprewitt nateprewitt force-pushed the 2939_multiple_location_headers branch 2 times, most recently from 43ad63d to 25da055 Compare July 16, 2016 03:19
request = response.request

while response.is_redirect:
if len(response.raw.headers.getlist('location')) > 1:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to include a getattr check. Frankly, it's a bit implementation detaily, so we may want to hide this in a utility function.

@nateprewitt nateprewitt force-pushed the 2939_multiple_location_headers branch from fc19fbb to 2b307de Compare July 16, 2016 15:06
@sigmavirus24
Copy link
Copy Markdown
Contributor

This looks good to me.

@sigmavirus24
Copy link
Copy Markdown
Contributor

Sadly we have no CI for this though, so one or both of us should pull this down and test it.

@nateprewitt
Copy link
Copy Markdown
Member Author

Sorry, @sigmavirus24, I just pushed a new copy with partially finished changes. I have a couple of questions on how we're expecting the code to function that I was going to annotate for discussion.

elif hasattr(response.raw, 'read'):
response.raw.seek(0) # Move to beginning of file
data = response.raw.read()
# parse_headers(data) # try to parse headers out of raw response
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going through a few libraries that implement different adapters, the raw field seems considerably varied in it's use.

In the event we end up with something file-like, such as StringIO, we can parse the headers out, but only if it's the complete response. In the limited testing I did though, this was typically just the response body. I'm not sure if we can do anything useful in that case. Also if it is the response body and happens to contain contents mimicking an HTTP response, then we could in theory flag this incorrectly.

I'm just seeing a lot of unknowns here that I'm not sure have a clean fix. Perhaps just returning True when we can't make a reasonable discernment would be best.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is more than we need. If we can't get headers in the above case, don't worry about it.

Mostly what I'm concerned about here is not exploding in the non-urllib3 case: we don't need to be too clever, or even really "handle" it in any meaningful way.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, great, that helps narrow the scope considerably. I'll shorten this down to just catching the urllib3 case then. Thanks!

@nateprewitt nateprewitt force-pushed the 2939_multiple_location_headers branch from 2b307de to c7eb191 Compare July 16, 2016 15:51
@nateprewitt
Copy link
Copy Markdown
Member Author

Ok, I think this is probably ready for another glance. I'm getting an AssertionError from test_requests_are_updated_each_time on line 1641, but that appears to be pre-existing in the proposed/3.0.0 branch. I was able to reproduce it in a fresh virtualenv with proposed/3.0.0 checked out with python 2.7.

@nateprewitt nateprewitt force-pushed the 2939_multiple_location_headers branch from c7eb191 to 7e42d65 Compare July 16, 2016 16:03
returned from the last response.
"""
if hasattr(response.raw, 'headers'):
if hasattr(response.raw.headers, 'getlist'):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we shouldn't use hasattr, we should use getattr. This is because of this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, I suggest the getattr pattern in this case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per usual, I prefer the Python 2 behavior here hahaha

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm onboard with this. I updated the PR with a somewhat verbose implementation of getattr.

@nateprewitt nateprewitt force-pushed the 2939_multiple_location_headers branch from 7e42d65 to c418c4c Compare July 16, 2016 18:38
@Lukasa
Copy link
Copy Markdown
Member

Lukasa commented Jul 16, 2016

Ok, I'm happy with this. @sigmavirus24?

@nateprewitt
Copy link
Copy Markdown
Member Author

On a quick side note, I PR'd this against proposed/3.0.0 because of comments in the issue, but is there a reason this shouldn't go straight against master?

@Lukasa
Copy link
Copy Markdown
Member

Lukasa commented Jul 19, 2016

@nateprewitt Yeah, this is a breaking change. =)

@nateprewitt
Copy link
Copy Markdown
Member Author

Hey @Lukasa @sigmavirus24, just wanted to check in on this. Let me know if you're waiting on anything from me.

@Lukasa
Copy link
Copy Markdown
Member

Lukasa commented Aug 22, 2016

No problem from me. @sigmavirus24?

@sigmavirus24
Copy link
Copy Markdown
Contributor

Looks fine as a start. Thanks @nateprewitt

@Lukasa since we're sans CI, have you pulled this and run the tests?

@Lukasa
Copy link
Copy Markdown
Member

Lukasa commented Aug 22, 2016

@sigmavirus24 I haven't, would you like me to?

@sigmavirus24
Copy link
Copy Markdown
Contributor

I'll do it. Just wanted to make sure someone was doing it.

@nateprewitt
Copy link
Copy Markdown
Member Author

It's still failing on #3442 because I haven't rebased on to master since the merge. Let me do that really quickly.

@sigmavirus24
Copy link
Copy Markdown
Contributor

@nateprewitt I merged the original branch into the head of proposed/3.0.0 and it passed just fine. No need to rebase

@sigmavirus24
Copy link
Copy Markdown
Contributor

Oh I see you already did, effectively invalidating the tests. Let me run them again

@nateprewitt
Copy link
Copy Markdown
Member Author

Sorry, I'd already pushed it before the message.

@sigmavirus24 sigmavirus24 merged commit 2b2a07e into psf:proposed/3.0.0 Aug 22, 2016
@sigmavirus24
Copy link
Copy Markdown
Contributor

Tests passed both times.

@nateprewitt nateprewitt deleted the 2939_multiple_location_headers branch November 3, 2016 22:47
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants