Skip to content

Commit

Permalink
set response code after receiving all headers
Browse files Browse the repository at this point in the history
  • Loading branch information
sbera87 committed Jan 10, 2025
1 parent a7fa73d commit 8bfb030
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
17 changes: 12 additions & 5 deletions 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 @@ -187,7 +190,6 @@ static size_t WriteData(char* ptr, size_t size, size_t nmemb, void* userdata)
{
if (ptr)
{
std::cout<<"WriteData called"<<std::endl;

CurlWriteCallbackContext* context = reinterpret_cast<CurlWriteCallbackContext*>(userdata);

Expand Down Expand Up @@ -264,7 +266,6 @@ static size_t WriteHeader(char* ptr, size_t size, size_t nmemb, void* userdata)
{
if (ptr)
{
std::cout<<"WriteHeader called"<<std::endl;
CurlWriteCallbackContext* context = reinterpret_cast<CurlWriteCallbackContext*>(userdata);
AWS_LOGSTREAM_TRACE(CURL_HTTP_CLIENT_TAG, ptr);
HttpResponse* response = context->m_response;
Expand All @@ -275,6 +276,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")
{
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 @@ -686,7 +695,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 Expand Up @@ -832,9 +841,7 @@ std::shared_ptr<HttpResponse> CurlHttpClient::MakeRequest(const std::shared_ptr<

OverrideOptionsOnConnectionHandle(connectionHandle);
Aws::Utils::DateTime startTransmissionTime = Aws::Utils::DateTime::Now();
std::cout<<"curl_easy_perform starting"<<std::endl;
CURLcode curlResponseCode = curl_easy_perform(connectionHandle);
std::cout<<"curl_easy_perform finished"<<std::endl;
bool shouldContinueRequest = ContinueRequest(*request);
if (curlResponseCode != CURLE_OK && shouldContinueRequest)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2581,49 +2581,38 @@ namespace
EXPECT_EQ(headOutcome.GetResult().GetContentEncoding(), "gzip");
}

TEST_F(BucketAndObjectOperationTest, TestResponse) {
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; // <== current implementation is always "response headers received: -1"
<< ::std::endl;
EXPECT_EQ(response->GetResponseCode(), Aws::Http::HttpResponseCode::NOT_FOUND );
}
);

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

if (!outcome.IsSuccess()) {
const Aws::S3::S3Error &err = outcome.GetError();
std::cerr << "Error: getObject: " <<
err.GetExceptionName() << ": " << err.GetMessage() << std::endl;
} else {
std::cout << "Successfully retrieved from '"
<< fullBucketName << "'." << std::endl;
}

AWS_EXPECT_SUCCESS(outcome);
}
}

0 comments on commit 8bfb030

Please sign in to comment.