Skip to content
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

Add fallback to GET if HEAD doesn't work #9

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

samdbmg
Copy link

@samdbmg samdbmg commented Jul 28, 2021

This works around a problems where servers don't support HEAD correctly, such as AWS S3 presigned URLs which incorporate the method into the URL signature, so you can't produce one that supports both GET and HEAD. This PR adds a fallback behaviour in response to 405 Method Not Implemented or 403 Forbidden (S3's response) on HEAD to try a GET instead, but avoids reading the response body to prompt the server not to send any more data.

I've also made a tweak to the unit tests in the process to generate data with the slightly faster (but less random) random.getrandbits and factor out sample data generation across both tests.

cc @jamesba - would you mind reviewing this too?

Fixes test failures when mocking async code.
Adds a fallback behaviour when either `no_head_request` is set, or the
original HEAD request returns a 403 or 405 code.
Mitigates cases where HEAD isn't implemented (405), and also a specific
problem with AWS S3 presigned URLs which will can't be generated for
more than one method, so will return 403 on HEAD.
Adds the same fallback behaviour to the async implementation.
Adds a parameter to pass kwargs to the ClientSession constructor, for
example so that `trust_env` can be set when working behind proxies.
Copy link
Contributor

@jamesba jamesba left a comment

Choose a reason for hiding this comment

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

Looks good to me, I'd probably use an async exit stack these days, but that's a bigger change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants