-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Refactor AWS Chunked logic from HTTP clients #3635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kai-ion
wants to merge
10
commits into
main
Choose a base branch
from
chunky
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6f6b084
refactor: centralize aws-chunked encoding in ChunkingInterceptor
kai-ion 3834efd
added testing for httpclient override
kai-ion 5bb0545
changing the streambuff type to a vector
kai-ion 5c16cc1
changing the streambuff type to a vector
kai-ion 631c894
adding a way to remove unbound growth
kai-ion a59d0a8
reversing logic to check for chunked mode
kai-ion e8d12d2
reversing logic to check for chunked mode
kai-ion aba0f89
changing it to just passing in an enum
kai-ion 0c72d39
changing chunking interceptor to use array instead of vector
kai-ion e7cb1f6
changing chunking interceptor to use array instead of vector
kai-ion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
232 changes: 232 additions & 0 deletions
232
src/aws-cpp-sdk-core/include/smithy/client/features/ChunkingInterceptor.h
This file contains hidden or 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,232 @@ | ||
| /** | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0. | ||
| */ | ||
| #pragma once | ||
| #include <aws/core/http/HttpRequest.h> | ||
| #include <aws/core/utils/Array.h> | ||
| #include <aws/core/utils/StringUtils.h> | ||
| #include <aws/core/utils/HashingUtils.h> | ||
| #include <aws/core/utils/logging/LogMacros.h> | ||
| #include <aws/core/utils/memory/stl/AWSStringStream.h> | ||
| #include <aws/core/utils/memory/stl/AWSVector.h> | ||
| #include <smithy/interceptor/Interceptor.h> | ||
| #include <aws/core/client/ClientConfiguration.h> | ||
| #include <aws/core/utils/Outcome.h> | ||
| #include <aws/core/client/AWSError.h> | ||
| #include <memory> | ||
|
|
||
| namespace smithy { | ||
| namespace client { | ||
| namespace features { | ||
|
|
||
| static const size_t AWS_DATA_BUFFER_SIZE = 65536; | ||
| static const char* ALLOCATION_TAG = "ChunkingInterceptor"; | ||
| static const char* CHECKSUM_HEADER_PREFIX = "x-amz-checksum-"; | ||
|
|
||
| template <size_t DataBufferSize = AWS_DATA_BUFFER_SIZE> | ||
| class AwsChunkedStreamBuf : public std::streambuf { | ||
| public: | ||
| AwsChunkedStreamBuf(Aws::Http::HttpRequest* request, | ||
| const std::shared_ptr<Aws::IOStream>& stream, | ||
| size_t bufferSize = DataBufferSize) | ||
| : m_request(request), | ||
| m_stream(stream), | ||
| m_data(bufferSize) | ||
| { | ||
| assert(m_stream != nullptr); | ||
| if (m_stream == nullptr) { | ||
| AWS_LOGSTREAM_ERROR("AwsChunkedStream", "stream is null"); | ||
| } | ||
| assert(m_request != nullptr); | ||
| if (m_request == nullptr) { | ||
| AWS_LOGSTREAM_ERROR("AwsChunkedStream", "request is null"); | ||
| } | ||
| } | ||
|
|
||
| protected: | ||
| int_type underflow() override { | ||
| if (gptr() && gptr() < egptr()) { | ||
| return traits_type::to_int_type(*gptr()); | ||
| } | ||
|
|
||
| // Compact buffer when consumed data exceeds half buffer size | ||
| if (m_chunkingBufferPos > m_chunkingBuffer.GetLength() / 2) { | ||
| size_t remaining = m_chunkingBufferSize - m_chunkingBufferPos; | ||
| if (remaining > 0) { | ||
| std::memmove(m_chunkingBuffer.GetUnderlyingData(), | ||
| m_chunkingBuffer.GetUnderlyingData() + m_chunkingBufferPos, | ||
| remaining); | ||
| } | ||
| m_chunkingBufferSize = remaining; | ||
| m_chunkingBufferPos = 0; | ||
| } | ||
|
|
||
| // only read and write to chunked stream if the underlying stream | ||
| // is still in a valid state and we have buffer space | ||
| if (m_stream->good()) { | ||
| // Check if we have enough space for worst-case chunk (data + header + footer) | ||
| size_t maxChunkSize = m_data.GetLength() + 20; // data + hex header + CRLF | ||
| if (m_chunkingBufferSize + maxChunkSize <= m_chunkingBuffer.GetLength()) { | ||
| // Try to read in a 64K chunk, if we cant we know the stream is over | ||
| m_stream->read(m_data.GetUnderlyingData(), m_data.GetLength()); | ||
| size_t bytesRead = static_cast<size_t>(m_stream->gcount()); | ||
| writeChunk(bytesRead); | ||
|
|
||
| // if we've read everything from the stream, we want to add the trailer | ||
| // to the underlying stream | ||
| if ((m_stream->peek() == EOF || m_stream->eof()) && !m_stream->bad()) { | ||
| writeTrailerToUnderlyingStream(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // if the chunking buffer is empty there is nothing to read | ||
| if (m_chunkingBufferPos >= m_chunkingBufferSize) { | ||
| return traits_type::eof(); | ||
| } | ||
|
|
||
| // Set up buffer pointers to read from chunking buffer | ||
| size_t remainingBytes = m_chunkingBufferSize - m_chunkingBufferPos; | ||
| size_t bytesToRead = std::min(remainingBytes, DataBufferSize); | ||
|
|
||
| setg(m_chunkingBuffer.GetUnderlyingData() + m_chunkingBufferPos, | ||
| m_chunkingBuffer.GetUnderlyingData() + m_chunkingBufferPos, | ||
| m_chunkingBuffer.GetUnderlyingData() + m_chunkingBufferPos + bytesToRead); | ||
|
|
||
| m_chunkingBufferPos += bytesToRead; | ||
|
|
||
| return traits_type::to_int_type(*gptr()); | ||
| } | ||
|
|
||
| private: | ||
| void writeTrailerToUnderlyingStream() { | ||
| Aws::String trailer = "0\r\n"; | ||
| if (m_request->GetRequestHash().second != nullptr) { | ||
| trailer += "x-amz-checksum-" + m_request->GetRequestHash().first + ":" | ||
| + Aws::Utils::HashingUtils::Base64Encode(m_request->GetRequestHash().second->GetHash().GetResult()) + "\r\n"; | ||
| } | ||
| trailer += "\r\n"; | ||
| if (m_chunkingBufferSize + trailer.length() <= m_chunkingBuffer.GetLength()) { | ||
| std::memcpy(m_chunkingBuffer.GetUnderlyingData() + m_chunkingBufferSize, trailer.c_str(), trailer.length()); | ||
| m_chunkingBufferSize += trailer.length(); | ||
| } | ||
| } | ||
|
|
||
| void writeChunk(size_t bytesRead) { | ||
| if (m_request->GetRequestHash().second != nullptr) { | ||
| m_request->GetRequestHash().second->Update(reinterpret_cast<unsigned char*>(m_data.GetUnderlyingData()), bytesRead); | ||
| } | ||
|
|
||
| if (bytesRead > 0) { | ||
| Aws::String chunkHeader = Aws::Utils::StringUtils::ToHexString(bytesRead) + "\r\n"; | ||
| size_t totalSize = chunkHeader.length() + bytesRead + 2; | ||
| if (m_chunkingBufferSize + totalSize <= m_chunkingBuffer.GetLength()) { | ||
| std::memcpy(m_chunkingBuffer.GetUnderlyingData() + m_chunkingBufferSize, chunkHeader.c_str(), chunkHeader.length()); | ||
| m_chunkingBufferSize += chunkHeader.length(); | ||
| std::memcpy(m_chunkingBuffer.GetUnderlyingData() + m_chunkingBufferSize, m_data.GetUnderlyingData(), bytesRead); | ||
| m_chunkingBufferSize += bytesRead; | ||
| std::memcpy(m_chunkingBuffer.GetUnderlyingData() + m_chunkingBufferSize, "\r\n", 2); | ||
| m_chunkingBufferSize += 2; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Aws::Utils::Array<char> m_chunkingBuffer{DataBufferSize * 4}; | ||
| size_t m_chunkingBufferSize{0}; | ||
| size_t m_chunkingBufferPos{0}; | ||
| Aws::Http::HttpRequest* m_request{nullptr}; | ||
| std::shared_ptr<Aws::IOStream> m_stream; | ||
| Aws::Utils::Array<char> m_data; | ||
| }; | ||
|
|
||
| class AwsChunkedIOStream : public Aws::IOStream { | ||
| public: | ||
| AwsChunkedIOStream(Aws::Http::HttpRequest* request, | ||
| const std::shared_ptr<Aws::IOStream>& originalBody, | ||
| size_t bufferSize = AWS_DATA_BUFFER_SIZE) | ||
| : Aws::IOStream(&m_buf), | ||
| m_buf(request, originalBody, bufferSize) {} | ||
|
|
||
| private: | ||
| AwsChunkedStreamBuf<> m_buf; | ||
| }; | ||
|
|
||
| /** | ||
| * Interceptor that handles chunked encoding for streaming requests with checksums. | ||
| * Wraps request body with chunked stream and sets appropriate headers. | ||
| */ | ||
| class ChunkingInterceptor : public smithy::interceptor::Interceptor { | ||
| public: | ||
| explicit ChunkingInterceptor(Aws::Client::HttpClientChunkedMode httpClientChunkedMode) | ||
| : m_httpClientChunkedMode(httpClientChunkedMode) {} | ||
| ~ChunkingInterceptor() override = default; | ||
|
|
||
| ModifyRequestOutcome ModifyBeforeSigning(smithy::interceptor::InterceptorContext& context) override { | ||
| auto request = context.GetTransmitRequest(); | ||
|
|
||
| if (!ShouldApplyChunking(request)) { | ||
| return request; | ||
| } | ||
|
|
||
| auto originalBody = request->GetContentBody(); | ||
| if (!originalBody) { | ||
| return request; | ||
| } | ||
|
|
||
| // Set up chunked encoding headers for checksum calculation | ||
| const auto& hashPair = request->GetRequestHash(); | ||
| if (hashPair.second != nullptr) { | ||
| Aws::String checksumHeaderValue = Aws::String(CHECKSUM_HEADER_PREFIX) + hashPair.first; | ||
| request->DeleteHeader(checksumHeaderValue.c_str()); | ||
| request->SetHeaderValue(Aws::Http::AWS_TRAILER_HEADER, checksumHeaderValue); | ||
| request->SetTransferEncoding(Aws::Http::CHUNKED_VALUE); | ||
|
|
||
| if (!request->HasContentEncoding()) { | ||
| request->SetContentEncoding(Aws::Http::AWS_CHUNKED_VALUE); | ||
| } else { | ||
| Aws::String currentEncoding = request->GetContentEncoding(); | ||
| if (currentEncoding.find(Aws::Http::AWS_CHUNKED_VALUE) == Aws::String::npos) { | ||
| request->SetContentEncoding(Aws::String{Aws::Http::AWS_CHUNKED_VALUE} + "," + currentEncoding); | ||
| } | ||
| } | ||
|
|
||
| if (request->HasHeader(Aws::Http::CONTENT_LENGTH_HEADER)) { | ||
| request->SetHeaderValue(Aws::Http::DECODED_CONTENT_LENGTH_HEADER, request->GetHeaderValue(Aws::Http::CONTENT_LENGTH_HEADER)); | ||
| request->DeleteHeader(Aws::Http::CONTENT_LENGTH_HEADER); | ||
| } | ||
| } | ||
|
|
||
| auto chunkedBody = Aws::MakeShared<AwsChunkedIOStream>( | ||
| ALLOCATION_TAG, request.get(), originalBody); | ||
|
|
||
| request->AddContentBody(chunkedBody); | ||
| return request; | ||
| } | ||
|
|
||
| ModifyResponseOutcome ModifyBeforeDeserialization(smithy::interceptor::InterceptorContext& context) override { | ||
| return context.GetTransmitResponse(); | ||
| } | ||
|
|
||
| private: | ||
| bool ShouldApplyChunking(const std::shared_ptr<Aws::Http::HttpRequest>& request) const { | ||
| // Use configuration setting to determine chunking behavior | ||
| if (m_httpClientChunkedMode != Aws::Client::HttpClientChunkedMode::DEFAULT) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!request || !request->GetContentBody()) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if request has checksum requirements | ||
| const auto& hashPair = request->GetRequestHash(); | ||
| return hashPair.second != nullptr; | ||
| } | ||
|
|
||
| Aws::Client::HttpClientChunkedMode m_httpClientChunkedMode; | ||
| }; | ||
|
|
||
| } // namespace features | ||
| } // namespace client | ||
| } // namespace smithy | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.