Skip to content

Commit

Permalink
Merge pull request boto#2276 from vz10/develop
Browse files Browse the repository at this point in the history
Fall back to Transfer-Encoding 'chunked'
  • Loading branch information
vz10 authored Jan 29, 2021
2 parents 7ff55b1 + 62d4ea2 commit c4dd9bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
19 changes: 13 additions & 6 deletions botocore/awsrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import io
import sys
import logging
import functools
Expand Down Expand Up @@ -404,12 +405,18 @@ def _determine_content_length(self, body):

# Try getting the length from a seekable stream
if hasattr(body, 'seek') and hasattr(body, 'tell'):
orig_pos = body.tell()
body.seek(0, 2)
end_file_pos = body.tell()
body.seek(orig_pos)
return end_file_pos - orig_pos

try:
orig_pos = body.tell()
body.seek(0, 2)
end_file_pos = body.tell()
body.seek(orig_pos)
return end_file_pos - orig_pos
except io.UnsupportedOperation:
# in case when body is, for example, io.BufferedIOBase object
# it has "seek" method which throws "UnsupportedOperation"
# exception in such case we want to fall back to "chunked"
# encoding
pass
# Failed to determine the length
return None

Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import io

from tests import unittest
from tests import RawResponse
from dateutil.tz import tzutc, tzoffset
Expand Down Expand Up @@ -918,6 +920,19 @@ def test_is_unaffected_by_sigv4(self):
'https://bucket.s3.amazonaws.com/key.txt')


class TestSwitchToChunkedEncodingForNonSeekableObjects(unittest.TestCase):
def test_switch_to_chunked_encodeing_for_stream_like_object(self):
request = AWSRequest(
method='POST', headers={},
data=io.BufferedIOBase(b"some initial binary data"),
url='https://foo.amazonaws.com/bucket/key.txt'
)
prepared_request = request.prepare()
self.assertEqual(
prepared_request.headers, {'Transfer-Encoding': 'chunked'}
)


class TestInstanceCache(unittest.TestCase):
class DummyClass(object):
def __init__(self, cache):
Expand Down

0 comments on commit c4dd9bd

Please sign in to comment.