-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
172 additions
and
61 deletions.
There are no files selected for viewing
File renamed without changes.
81 changes: 81 additions & 0 deletions
81
src/aws-cpp-sdk-core/include/aws/core/utils/stream/AwsChunkedStream.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
#pragma once | ||
#include <aws/core/utils/Outcome.h> | ||
#include <aws/core/utils/crypto/Hash.h> | ||
#include <aws/core/utils/HashingUtils.h> | ||
|
||
namespace Aws { | ||
namespace Utils { | ||
namespace Stream { | ||
|
||
static const size_t AWS_DATA_BUFFER_SIZE = 65536; | ||
|
||
template <size_t DataBufferSize = AWS_DATA_BUFFER_SIZE> | ||
class AwsChunkedStream { | ||
public: | ||
AwsChunkedStream(Http::HttpRequest *request, const std::shared_ptr<Aws::IOStream> &stream) | ||
: m_chunkingStream{Aws::MakeShared<StringStream>("AwsChunkedStream")}, m_request(request), m_stream(stream) {} | ||
|
||
size_t BufferedRead(char *dst, size_t amountToRead) { | ||
// the chunk has ended and cannot be read from | ||
if (m_chunkEnd) { | ||
return 0; | ||
} | ||
|
||
// If we've read all of the underlying stream write the checksum trailing header | ||
// the set that the chunked stream is over. | ||
if (m_stream->eof() && (m_chunkingStream->eof() || m_chunkingStream->peek() == EOF)) { | ||
Aws::StringStream chunkedTrailer; | ||
chunkedTrailer << "0\r\n"; | ||
if (m_request->GetRequestHash().second != nullptr) { | ||
chunkedTrailer << "x-amz-checksum-" << m_request->GetRequestHash().first << ":" | ||
<< HashingUtils::Base64Encode(m_request->GetRequestHash().second->GetHash().GetResult()) << "\r\n"; | ||
} | ||
chunkedTrailer << "\r\n"; | ||
auto trailerSize = chunkedTrailer.str().size(); | ||
memcpy(dst, chunkedTrailer.str().c_str(), trailerSize); | ||
m_chunkEnd = true; | ||
return trailerSize; | ||
} | ||
|
||
// Try to read in a 64K chunk, if we cant we know the stream is over | ||
size_t bytesRead = 0; | ||
while (m_stream->good() && bytesRead < DataBufferSize) { | ||
m_stream->read(&m_data[bytesRead], DataBufferSize - bytesRead); | ||
bytesRead += m_stream->gcount(); | ||
} | ||
|
||
// update the trailing checksum to be sent only if we read data and buffered. | ||
if (bytesRead > 0 && m_request->GetRequestHash().second != nullptr) { | ||
m_request->GetRequestHash().second->Update(reinterpret_cast<unsigned char *>(m_data.data()), bytesRead); | ||
} | ||
|
||
// Buffer chunked encoding from data if there was data read to the buffer, otherwise leave it alone/ | ||
if (bytesRead > 0 && m_chunkingStream != nullptr) { | ||
*m_chunkingStream << Aws::Utils::StringUtils::ToHexString(bytesRead) << "\r\n"; | ||
std::copy(m_data.begin(), m_data.begin() + bytesRead, std::ostream_iterator<char>(*m_chunkingStream)); | ||
*m_chunkingStream << "\r\n"; | ||
auto curr = m_chunkingStream->tellg(); | ||
const auto rn = m_chunkingStream->rdbuf(); | ||
AWS_UNREFERENCED_PARAM(rn); | ||
m_chunkingStream->seekg(curr); | ||
} | ||
|
||
// Read to destination buffer, return how much was read | ||
m_chunkingStream->read(dst, amountToRead); | ||
return m_chunkingStream->gcount(); | ||
} | ||
|
||
private: | ||
std::array<char, DataBufferSize> m_data; | ||
std::shared_ptr<Aws::IOStream> m_chunkingStream; | ||
bool m_chunkEnd{false}; | ||
Http::HttpRequest *m_request; | ||
std::shared_ptr<Aws::IOStream> m_stream; | ||
}; | ||
} // namespace Stream | ||
} // namespace Utils | ||
} // namespace Aws |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
tests/aws-cpp-sdk-core-tests/utils/stream/AwsChunkedStreamTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
#include <aws/core/http/standard/StandardHttpRequest.h> | ||
#include <aws/core/utils/crypto/CRC32.h> | ||
#include <aws/core/utils/stream/AwsChunkedStream.h> | ||
#include <aws/testing/AwsCppSdkGTestSuite.h> | ||
|
||
using namespace Aws; | ||
using namespace Aws::Http::Standard; | ||
using namespace Aws::Utils::Stream; | ||
using namespace Aws::Utils::Crypto; | ||
|
||
class AwsChunkedStreamTest : public Aws::Testing::AwsCppSdkGTestSuite {}; | ||
|
||
const char* TEST_LOG_TAG = "AWS_CHUNKED_STREAM_TEST"; | ||
|
||
TEST_F(AwsChunkedStreamTest, ChunkedStreamShouldWork) { | ||
StandardHttpRequest request{"www.elda.com/will", 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, "1234567890123456789012345"); | ||
AwsChunkedStream<10> chunkedStream{&request, inputStream}; | ||
std::array<char, 100> outputBuffer{}; | ||
Aws::StringStream output; | ||
size_t read = 0; | ||
do { | ||
read = chunkedStream.BufferedRead(outputBuffer.data(), 10); | ||
std::copy(outputBuffer.begin(), outputBuffer.begin() + read, std::ostream_iterator<char>(output)); | ||
} while (read > 0); | ||
const auto encodedStr = output.str(); | ||
auto expectedStreamWithChecksum = "A\r\n1234567890\r\nA\r\n1234567890\r\n5\r\n12345\r\n0\r\nx-amz-checksum-crc32:78DeVw==\r\n\r\n"; | ||
EXPECT_EQ(expectedStreamWithChecksum, encodedStr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters