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

Compute host header correctly #63

Merged
merged 1 commit into from Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion requests_aws4auth/aws4auth.py
Expand Up @@ -615,7 +615,15 @@ def get_canonical_headers(cls, req, include=None):
# in the signed headers, but Requests doesn't include it in a
# PreparedRequest
if 'host' not in headers:
headers['host'] = urlparse(str(req.url)).netloc.split(':')[0]
purl = urlparse(str(req.url))
netloc = purl.netloc
# Python's http client only includes the port if it is non-default,
# see http.client.HTTPConnection.putrequest. The request URL, on the
# other hand, might explicitly include it.
if ((purl.port == 80 and purl.scheme == 'http')
or (purl.port == 443 and purl.scheme == 'https')):
netloc = netloc.rsplit(":", 1)[0]
headers['host'] = netloc
# Aggregate for upper/lowercase header name collisions in header names,
# AMZ requires values of colliding headers be concatenated into a
# single header with lowercase name. Although this is not possible with
Expand Down
2 changes: 1 addition & 1 deletion requests_aws4auth/test/test_requests_aws4auth.py
Expand Up @@ -949,7 +949,7 @@ def test_netloc_port(self):
request.

"""
req = requests.Request('GET', 'http://amazonaws.com:8443')
req = requests.Request('GET', 'https://amazonaws.com:443')
preq = req.prepare()
self.assertNotIn('host', preq.headers)
result = AWS4Auth.get_canonical_headers(preq, include=['host'])
Expand Down