Skip to content

Commit

Permalink
fix aws chunked encoding for a empty stream
Browse files Browse the repository at this point in the history
  • Loading branch information
sbiscigl committed Jan 20, 2025
1 parent 18df00a commit 731b659
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class AwsChunkedStream {
m_request->GetRequestHash().second->Update(reinterpret_cast<unsigned char *>(m_data.GetUnderlyingData()), bytesRead);
}

if (m_chunkingStream != nullptr && !m_chunkingStream->bad()) {
if (bytesRead > 0 && m_chunkingStream != nullptr && !m_chunkingStream->bad()) {
if (m_chunkingStream->eof()) {
m_chunkingStream->clear();
}
Expand Down
16 changes: 16 additions & 0 deletions tests/aws-cpp-sdk-core-tests/utils/stream/AwsChunkedStreamTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,20 @@ TEST_F(AwsChunkedStreamTest, ShouldWorkOnSmallBuffer) {
// Any subsequent reads will return 0 because all streams are exhausted
amountRead = chunkedStream.BufferedRead(outputBuffer.GetUnderlyingData(), 100);
EXPECT_EQ(0ul, amountRead);
}

TEST_F(AwsChunkedStreamTest, ShouldWorkOnEmptyStream) {
StandardHttpRequest request{"www.nidia.com/juna", Http::HttpMethod::HTTP_GET};
auto requestHash = Aws::MakeShared<CRC32>(TEST_LOG_TAG);
request.SetRequestHash("crc32", requestHash);
std::shared_ptr<IOStream> inputStream = Aws::MakeShared<StringStream>(TEST_LOG_TAG, "");
AwsChunkedStream<5> chunkedStream{&request, inputStream};
Aws::Utils::Array<char> outputBuffer{100};
// Read first 5 bytes, we get back ten bytes chunk encoded since it is "5\r\n12345\r\n"
Aws::StringStream firstRead;
auto amountRead = chunkedStream.BufferedRead(outputBuffer.GetUnderlyingData(), 100);
std::copy(outputBuffer.GetUnderlyingData(), outputBuffer.GetUnderlyingData() + amountRead, std::ostream_iterator<char>(firstRead));
EXPECT_EQ(36ul, amountRead);
auto encodedStr = firstRead.str();
EXPECT_EQ("0\r\nx-amz-checksum-crc32:AAAAAA==\r\n\r\n", encodedStr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,28 @@ namespace
ASSERT_TRUE(isErrorEventReceived);
}

TEST_F(BucketAndObjectOperationTest, TestNullBody)
{
const Aws::String fullBucketName = CalculateBucketName(BASE_PUT_OBJECTS_BUCKET_NAME.c_str());
SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName);
CreateBucketRequest createBucketRequest;
createBucketRequest.SetBucket(fullBucketName);
createBucketRequest.SetACL(BucketCannedACL::private_);

CreateBucketOutcome createBucketOutcome = Client->CreateBucket(createBucketRequest);
AWS_ASSERT_SUCCESS(createBucketOutcome);
const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult();
ASSERT_TRUE(!createBucketResult.GetLocation().empty());
ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName));
TagTestBucket(fullBucketName, Client);

PutObjectRequest putObjectRequest;
putObjectRequest.SetBucket(fullBucketName);
putObjectRequest.SetKey("sbiscigl_was_here_null");
PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest);
AWS_ASSERT_SUCCESS(putObjectOutcome);
}

TEST_F(BucketAndObjectOperationTest, TestEmptyBody)
{
const Aws::String fullBucketName = CalculateBucketName(BASE_PUT_OBJECTS_BUCKET_NAME.c_str());
Expand All @@ -1328,7 +1350,8 @@ namespace

PutObjectRequest putObjectRequest;
putObjectRequest.SetBucket(fullBucketName);
putObjectRequest.SetKey("sbiscigl_was_here");
putObjectRequest.SetKey("sbiscigl_was_here_empty");
putObjectRequest.SetBody(Aws::MakeShared<StringStream>(ALLOCATION_TAG, ""));
PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest);
AWS_ASSERT_SUCCESS(putObjectOutcome);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2387,6 +2387,28 @@ namespace
}
}

TEST_F(BucketAndObjectOperationTest, TestNullBody) {
const Aws::String fullBucketName = CalculateBucketName(BASE_PUT_OBJECTS_BUCKET_NAME.c_str());
SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName);

CreateBucketRequest createBucketRequest;
createBucketRequest.SetBucket(fullBucketName);
createBucketRequest.SetACL(BucketCannedACL::private_);

CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest);
AWS_ASSERT_SUCCESS(createBucketOutcome);
const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult();
ASSERT_TRUE(!createBucketResult.GetLocation().empty());
ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client));
TagTestBucket(fullBucketName, Client);

PutObjectRequest putObjectRequest;
putObjectRequest.SetBucket(fullBucketName);
putObjectRequest.SetKey("sbiscigl_was_here_null");
PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest);
AWS_ASSERT_SUCCESS(putObjectOutcome);
}

TEST_F(BucketAndObjectOperationTest, TestEmptyBody) {
const Aws::String fullBucketName = CalculateBucketName(BASE_PUT_OBJECTS_BUCKET_NAME.c_str());
SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName);
Expand All @@ -2404,7 +2426,8 @@ namespace

PutObjectRequest putObjectRequest;
putObjectRequest.SetBucket(fullBucketName);
putObjectRequest.SetKey("sbiscigl_was_here");
putObjectRequest.SetKey("sbiscigl_was_here_empty");
putObjectRequest.SetBody(Aws::MakeShared<StringStream>(ALLOCATION_TAG, ""));
PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest);
AWS_ASSERT_SUCCESS(putObjectOutcome);
}
Expand Down

0 comments on commit 731b659

Please sign in to comment.