Skip to content

Commit

Permalink
Ignore Content-Length header when setting 'chunked' for Transfer-Enco…
Browse files Browse the repository at this point in the history
…ding (#3360)
  • Loading branch information
jonathan343 authored Jan 31, 2025
1 parent 84ed226 commit a97cfe9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions botocore/httpchecksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ def _apply_request_trailer_checksum(request):
# services such as S3 may require the decoded content length
headers["X-Amz-Decoded-Content-Length"] = str(content_length)

if "Content-Length" in headers:
del headers["Content-Length"]
logger.debug(
"Removing the Content-Length header since 'chunked' is specified for Transfer-Encoding."
)

if isinstance(body, (bytes, bytearray)):
body = io.BytesIO(body)

Expand Down
26 changes: 25 additions & 1 deletion tests/functional/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,26 @@ def test_trailing_checksum_set(self):
body = self.http_stubber.requests[0].body.read()
self.assertIn(b"x-amz-checksum-crc32:eCQEmA==", body)

def test_trailing_checksum_set_with_content_length_removes_header(self):
with self.http_stubber:
self.client.put_object(
Bucket="foo", Key="bar", Body="baz", ContentLength=123
)
sent_headers = self.get_sent_headers()
self.assertEqual(sent_headers["Content-Encoding"], b"aws-chunked")
self.assertEqual(sent_headers["Transfer-Encoding"], b"chunked")
self.assertEqual(
sent_headers["X-Amz-Trailer"], b"x-amz-checksum-crc32"
)
self.assertEqual(sent_headers["X-Amz-Decoded-Content-Length"], b"3")
self.assertEqual(
sent_headers["x-amz-content-sha256"],
b"STREAMING-UNSIGNED-PAYLOAD-TRAILER",
)
body = self.http_stubber.requests[0].body.read()
self.assertIn(b"x-amz-checksum-crc32:eCQEmA==", body)
self.assertNotIn("Content-Length", sent_headers)

def test_trailing_checksum_set_empty_body(self):
with self.http_stubber:
self.client.put_object(Bucket="foo", Key="bar", Body="")
Expand Down Expand Up @@ -1686,7 +1706,11 @@ def test_content_sha256_set_s3_on_outpost(self):
class TestCanSendIntegerHeaders(BaseSessionTest):
def test_int_values_with_sigv4(self):
s3 = self.session.create_client(
"s3", config=Config(signature_version="s3v4")
"s3",
config=Config(
signature_version="s3v4",
request_checksum_calculation="when_required",
),
)
with ClientHTTPStubber(s3) as http_stubber:
http_stubber.add_response()
Expand Down

0 comments on commit a97cfe9

Please sign in to comment.