Skip to content

Commit 7622598

Browse files
committed
Create another function to manipulate the header in the signer
then don’t need to change awsclient added a check for existing UA
1 parent 65ecdfd commit 7622598

File tree

1 file changed

+62
-38
lines changed

1 file changed

+62
-38
lines changed

src/aws-cpp-sdk-core/source/auth/signer/AWSAuthV4Signer.cpp

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <iomanip>
2929
#include <cstring>
30+
#include <map>
3031

3132
using namespace Aws;
3233
using namespace Aws::Client;
@@ -614,46 +615,69 @@ Aws::Auth::AWSCredentials AWSAuthV4Signer::GetCredentials(Aws::AmazonWebServiceR
614615
}
615616

616617
void AWSAuthV4Signer::UpdateUserAgentWithCredentialFeatures(Aws::Http::HttpRequest& request, const Aws::AmazonWebServiceRequest& awsRequest) const {
617-
if (request.HasHeader(USER_AGENT)) {
618-
auto existingUA = request.GetHeaderValue(USER_AGENT);
619-
auto features = awsRequest.GetUserAgentFeatures();
620-
621-
if (!features.empty()) {
622-
// Build business metrics string from features
623-
Aws::String businessMetrics = "m/";
624-
bool first = true;
625-
for (const auto& feature : features) {
626-
if (!first) businessMetrics += ",";
627-
first = false;
628-
629-
// Map features to their character codes
630-
switch (feature) {
631-
case Aws::Client::UserAgentFeature::CREDENTIALS_ENV_VARS:
632-
businessMetrics += "g";
633-
break;
634-
// Add other feature mappings as needed
635-
default:
636-
// Skip unknown features
637-
break;
638-
}
639-
}
640-
641-
// Parse existing User-Agent and replace business metrics section
642-
auto parts = Aws::Utils::StringUtils::Split(existingUA, ' ');
643-
for (auto& part : parts) {
644-
if (part.find("m/") == 0) {
645-
part = businessMetrics;
646-
break;
647-
}
618+
if (!request.HasHeader(USER_AGENT)) {
619+
return;
620+
}
621+
622+
const auto& features = awsRequest.GetUserAgentFeatures();
623+
if (features.empty()) {
624+
return;
625+
}
626+
627+
// Get existing User-Agent
628+
Aws::String existingUA = request.GetHeaderValue(USER_AGENT);
629+
630+
// Build credential metrics string
631+
Aws::StringStream credentialMetrics;
632+
bool firstFeature = true;
633+
634+
static const std::map<Aws::Client::UserAgentFeature, char> featureMap = {
635+
{Aws::Client::UserAgentFeature::CREDENTIALS_ENV_VARS, 'g'},
636+
// Add other credential mappings as needed
637+
};
638+
639+
for (const auto& feature : features) {
640+
auto it = featureMap.find(feature);
641+
if (it != featureMap.end()) {
642+
if (!firstFeature) {
643+
credentialMetrics << ",";
648644
}
649-
650-
// Rejoin and update
651-
Aws::String updatedUA;
652-
for (size_t i = 0; i < parts.size(); ++i) {
653-
if (i > 0) updatedUA += " ";
654-
updatedUA += parts[i];
645+
credentialMetrics << it->second;
646+
firstFeature = false;
647+
}
648+
}
649+
650+
if (credentialMetrics.str().empty()) {
651+
return;
652+
}
653+
654+
// Parse and update existing User-Agent
655+
bool metricsUpdated = false;
656+
Aws::StringStream updatedUA;
657+
const auto parts = Aws::Utils::StringUtils::Split(existingUA, ' ');
658+
659+
for (size_t i = 0; i < parts.size(); ++i) {
660+
if (i > 0) {
661+
updatedUA << ' ';
662+
}
663+
664+
if (parts[i].find("m/") == 0) {
665+
// Add credentials to business metrics
666+
updatedUA << parts[i];
667+
if (!parts[i].empty() && parts[i].back() != '/') {
668+
updatedUA << ',';
655669
}
656-
request.SetUserAgent(updatedUA);
670+
updatedUA << credentialMetrics.str();
671+
metricsUpdated = true;
672+
} else {
673+
updatedUA << parts[i];
657674
}
658675
}
676+
677+
// If no metrics section found, add one
678+
if (!metricsUpdated) {
679+
updatedUA << " m/" << credentialMetrics.str();
680+
}
681+
682+
request.SetUserAgent(updatedUA.str());
659683
}

0 commit comments

Comments
 (0)