Skip to content

Commit

Permalink
Add experimental flag for checking the request body before setting 10…
Browse files Browse the repository at this point in the history
…0 continue
  • Loading branch information
SamRemis committed Feb 16, 2024
1 parent d592cbf commit 30c6146
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions botocore/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ def add_expect_header(model, params, **kwargs):
if 'body' in params:
body = params['body']
if hasattr(body, 'read'):
check_body = utils.ensure_boolean(
os.environ.get(
'BOTO_EXPERIMENTAL__CHECK_BODY_BEFORE_CONTINUE',
'',
)
)
if check_body and utils.determine_content_length(body) == 0:
return
# Any file like object will use an expect 100-continue
# header regardless of size.
logger.debug("Adding expect 100 continue header to request.")
Expand Down
26 changes: 26 additions & 0 deletions tests/functional/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3619,3 +3619,29 @@ def test_escape_keys_in_xml_put_bucket_lifecycle_configuration(self):
self.assertNotIn(b"my\r\n\rprefix", request.body)
self.assertIn(b"my

prefix", request.body)
self.assert_correct_content_md5(request)


class TestExpectContinueBehavior(BaseSessionTest):
def test_sets_100_continute_with_body(self):
op_kwargs = {
"Bucket": "mybucket",
"Key": "mykey",
"Body": b"foo",
}
s3 = _create_s3_client()
with ClientHTTPStubber(s3) as http_stubber:
http_stubber.add_response()
s3.put_object(**op_kwargs)
expect_header = http_stubber.requests[-1].headers.get("Expect")
self.assertIsNotNone(expect_header)
self.assertEqual(expect_header, b"100-continue")

def test_does_not_set_100_continute_with_empty_body(self):
self.environ["BOTO_EXPERIMENTAL__CHECK_BODY_BEFORE_CONTINUE"] = "True"
op_kwargs = {"Bucket": "mybucket", "Key": "mykey", "Body": ""}
s3 = _create_s3_client()
with ClientHTTPStubber(s3) as http_stubber:
http_stubber.add_response()
s3.put_object(**op_kwargs)
expect_header = http_stubber.requests[-1].headers.get("Expect")
self.assertIsNone(expect_header)

0 comments on commit 30c6146

Please sign in to comment.