Skip to content

Commit

Permalink
Set response code after receiving header (#3241)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbera87 authored Jan 14, 2025
1 parent 43cc553 commit 82f91fa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/aws-cpp-sdk-core/source/http/curl/CurlHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,20 @@ static char* strdup_callback(const char* str)
struct CurlWriteCallbackContext
{
CurlWriteCallbackContext(const CurlHttpClient* client,
CURL* curlHandle,
HttpRequest* request,
HttpResponse* response,
Aws::Utils::RateLimits::RateLimiterInterface* rateLimiter) :
m_client(client),
m_curlHandle(curlHandle),
m_request(request),
m_response(response),
m_rateLimiter(rateLimiter),
m_numBytesResponseReceived(0)
{}

const CurlHttpClient* m_client;
CURL* m_curlHandle{nullptr};
HttpRequest* m_request;
HttpResponse* m_response;
Aws::Utils::RateLimits::RateLimiterInterface* m_rateLimiter;
Expand Down Expand Up @@ -272,6 +275,14 @@ static size_t WriteHeader(char* ptr, size_t size, size_t nmemb, void* userdata)
{
response->AddHeader(StringUtils::Trim(keyValuePair[0].c_str()), StringUtils::Trim(keyValuePair[1].c_str()));
}
//checking for end of all the headers before setting response code
else if (headerLine == "\r\n" && context->m_curlHandle)
{
long responseCode{-1};
curl_easy_getinfo(context->m_curlHandle, CURLINFO_RESPONSE_CODE, &responseCode);
response->SetResponseCode(static_cast<HttpResponseCode>(responseCode));
AWS_LOGSTREAM_DEBUG(CURL_HTTP_CLIENT_TAG, "Returned http response code " << responseCode);
}

return size * nmemb;
}
Expand Down Expand Up @@ -683,7 +694,7 @@ std::shared_ptr<HttpResponse> CurlHttpClient::MakeRequest(const std::shared_ptr<
curl_easy_setopt(connectionHandle, CURLOPT_HTTPHEADER, headers);
}

CurlWriteCallbackContext writeContext(this, request.get(), response.get(), readLimiter);
CurlWriteCallbackContext writeContext(this, connectionHandle ,request.get(), response.get(), readLimiter);

const auto readContext = [this, &connectionHandle, &request, &writeLimiter]() -> CurlReadCallbackContext {
if (request->GetContentBody() != nullptr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2580,4 +2580,38 @@ namespace
AWS_EXPECT_SUCCESS(headOutcome);
EXPECT_EQ(headOutcome.GetResult().GetContentEncoding(), "gzip");
}

TEST_F(BucketAndObjectOperationTest, TestHeaderResponse) {
ClientConfiguration configuration;
configuration.region = "us-east-1";
configuration.enableHttpClientTrace = true;
std::shared_ptr<Aws::S3::S3Client> client = Aws::MakeShared<Aws::S3::S3Client>("test",configuration);

const String fullBucketName = CalculateBucketName(BASE_CONTENT_ENCODING_BUCKET_NAME.c_str());
CreateBucketRequest createBucketRequest;
createBucketRequest.SetBucket(fullBucketName);
createBucketRequest.SetACL(BucketCannedACL::private_);
CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest,client);
AWS_EXPECT_SUCCESS(createBucketOutcome);

Aws::S3::Model::GetObjectRequest request;
request.SetBucket(fullBucketName);
request.SetKey("one_object_that_does_not_exist_in_the_bucket"); // <== we should get 404 when calling GetResponseCode
request.SetHeadersReceivedEventHandler (
[] (
const Aws::Http::HttpRequest * ,
Aws::Http::HttpResponse * response
) {
::std::cout << "response headers received: "
<< response->GetResponseCode ( )
<< ::std::endl;
EXPECT_EQ(response->GetResponseCode(), Aws::Http::HttpResponseCode::NOT_FOUND );
}
);

Aws::S3::Model::GetObjectOutcome outcome =
client->GetObject(request);
EXPECT_FALSE(outcome.IsSuccess());

}
}

0 comments on commit 82f91fa

Please sign in to comment.