Skip to content

Commit 04545bb

Browse files
committed
Reading Env and Config for UseRequestCompression setting
1 parent 01c3db4 commit 04545bb

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

src/aws-cpp-sdk-core/include/aws/core/client/ClientConfiguration.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ namespace Aws
4545
NEVER
4646
};
4747

48+
/**
49+
* This setting is an enumeration, not a boolean, to allow for future expansion.
50+
*/
51+
enum class UseRequestCompression
52+
{
53+
FALSE,
54+
TRUE,
55+
};
56+
4857
/**
4958
* This mutable structure is used to configure any of the AWS clients.
5059
* Default values can only be overwritten prior to passing to the client constructors.
@@ -265,7 +274,14 @@ namespace Aws
265274
Aws::String profileName;
266275

267276
/**
268-
* A helper function to read config value from env variable of aws profile config
277+
* Use request compression when available.
278+
* To use this feature, the service needs to provide the support, and the compression
279+
* algorithms needs to be available at SDK build time.
280+
*/
281+
Aws::Client::UseRequestCompression useRequestCompression;
282+
283+
/**
284+
* A helper function to read config value from env variable or aws profile config
269285
*/
270286
static Aws::String LoadConfigFromEnvOrProfile(const Aws::String& envKey,
271287
const Aws::String& profile,

src/aws-cpp-sdk-core/source/client/ClientConfiguration.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace Client
2828
{
2929

3030
static const char* CLIENT_CONFIG_TAG = "ClientConfiguration";
31+
static const char* USE_REQUEST_COMPRESSION_ENV_VAR = "USE_REQUEST_COMPRESSION";
32+
static const char* USE_REQUEST_COMPRESSION_CONFIG_VAR = "use_request_compression";
3133

3234
Aws::String ComputeUserAgentString()
3335
{
@@ -70,6 +72,21 @@ void setLegacyClientConfigurationParameters(ClientConfiguration& clientConfig)
7072
clientConfig.enableHostPrefixInjection = true;
7173
clientConfig.profileName = Aws::Auth::GetConfigProfileName();
7274

75+
Aws::String useCompressionConfig = clientConfig.LoadConfigFromEnvOrProfile(
76+
USE_REQUEST_COMPRESSION_ENV_VAR,
77+
Aws::Auth::GetConfigProfileName(),
78+
USE_REQUEST_COMPRESSION_CONFIG_VAR,
79+
{"TRUE", "FALSE", "true", "false"},
80+
"TRUE"
81+
);
82+
83+
if (Aws::Utils::StringUtils::ToLower(useCompressionConfig.c_str()) == "false") {
84+
clientConfig.useRequestCompression = Aws::Client::UseRequestCompression::FALSE;
85+
} else {
86+
//Using default to true for forward compatibility in case new config is added but SDK is not updated.
87+
clientConfig.useRequestCompression = Aws::Client::UseRequestCompression::TRUE;
88+
}
89+
7390
AWS_LOGSTREAM_DEBUG(CLIENT_CONFIG_TAG, "ClientConfiguration will use SDK Auto Resolved profile: [" << clientConfig.profileName << "] if not specified by users.");
7491

7592
// Automatically determine the AWS region from environment variables, configuration file and EC2 metadata.

tests/aws-cpp-sdk-core-tests/aws/client/AwsConfigTest.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class AWSConfigTestSuite : public ::testing::Test
2727
SaveEnvironmentVariable("AWS_DEFAULT_REGION");
2828
SaveEnvironmentVariable("AWS_REGION");
2929
SaveEnvironmentVariable("AWS_EC2_METADATA_SERVICE_ENDPOINT");
30+
SaveEnvironmentVariable("USE_REQUEST_COMPRESSION");
3031

3132
Aws::StringStream ss;
3233
ss << Aws::Auth::GetConfigProfileFilename() + "_blah" << std::this_thread::get_id();
@@ -37,6 +38,7 @@ class AWSConfigTestSuite : public ::testing::Test
3738
Aws::Environment::UnSetEnv("AWS_DEFAULT_REGION");
3839
Aws::Environment::UnSetEnv("AWS_REGION");
3940
Aws::Environment::UnSetEnv("AWS_EC2_METADATA_SERVICE_ENDPOINT");
41+
Aws::Environment::UnSetEnv("USE_REQUEST_COMPRESSION");
4042

4143
auto profileDirectory = Aws::Auth::ProfileConfigFileAWSCredentialsProvider::GetProfileDirectory();
4244
Aws::FileSystem::CreateDirectoryIfNotExists(profileDirectory.c_str());
@@ -115,6 +117,78 @@ TEST_F(AWSConfigTestSuite, TestClientConfigurationSetsRegionToProfile)
115117
EXPECT_EQ(Aws::Region::US_WEST_2, config.region);
116118
EXPECT_STREQ("Dijkstra", config.profileName.c_str());
117119

120+
// cleanup
121+
Aws::FileSystem::RemoveFileIfExists(m_configFileName.c_str());
122+
}
123+
124+
TEST_F(AWSConfigTestSuite, TestNoEnvNoConfigSetsUseRequestCompressionToTrue){
125+
// create an empty config file
126+
Aws::OFStream configFileNew(m_configFileName.c_str(), Aws::OFStream::out | Aws::OFStream::trunc);
127+
128+
configFileNew.flush();
129+
configFileNew.close();
130+
Aws::Config::ReloadCachedConfigFile();
131+
132+
Aws::Client::ClientConfiguration config;
133+
134+
EXPECT_EQ(Aws::Client::UseRequestCompression::TRUE, config.useRequestCompression);
135+
136+
// cleanup
137+
Aws::FileSystem::RemoveFileIfExists(m_configFileName.c_str());
138+
}
139+
140+
TEST_F(AWSConfigTestSuite, TestEnvToFalseAndNoConfigSetsUseRequestCompressionToFalse){
141+
//Set Env variable
142+
Aws::Environment::SetEnv("USE_REQUEST_COMPRESSION", "FALSE", 1/*overwrite*/);
143+
// create an empty config file
144+
Aws::OFStream configFileNew(m_configFileName.c_str(), Aws::OFStream::out | Aws::OFStream::trunc);
145+
146+
configFileNew.flush();
147+
configFileNew.close();
148+
Aws::Config::ReloadCachedConfigFile();
149+
150+
Aws::Client::ClientConfiguration config;
151+
152+
EXPECT_EQ(Aws::Client::UseRequestCompression::FALSE, config.useRequestCompression);
153+
154+
// cleanup
155+
Aws::FileSystem::RemoveFileIfExists(m_configFileName.c_str());
156+
}
157+
158+
TEST_F(AWSConfigTestSuite, TestEnvToTrueAndConfigSetToFalseSetsUseRequestCompressionToTrue){
159+
//Set Env variable
160+
Aws::Environment::SetEnv("USE_REQUEST_COMPRESSION", "TRUE", 1/*overwrite*/);
161+
// create an empty config file
162+
Aws::OFStream configFileNew(m_configFileName.c_str(), Aws::OFStream::out | Aws::OFStream::trunc);
163+
configFileNew << "[profile Dijkstra]" << std::endl; // profile keyword is mandatory per specification
164+
configFileNew << "use_request_compression = false" << std::endl;
165+
166+
configFileNew.flush();
167+
configFileNew.close();
168+
Aws::Config::ReloadCachedConfigFile();
169+
170+
Aws::Client::ClientConfiguration config("Dijkstra");
171+
172+
EXPECT_EQ(Aws::Client::UseRequestCompression::TRUE, config.useRequestCompression);
173+
174+
// cleanup
175+
Aws::FileSystem::RemoveFileIfExists(m_configFileName.c_str());
176+
}
177+
178+
TEST_F(AWSConfigTestSuite, TestNoEnvAndConfigSetToFalseSetsUseRequestCompressionToFalse){
179+
// create an empty config file
180+
Aws::OFStream configFileNew(m_configFileName.c_str(), Aws::OFStream::out | Aws::OFStream::trunc);
181+
configFileNew << "[profile default]" << std::endl; // profile keyword is mandatory per specification
182+
configFileNew << "use_request_compression = false" << std::endl;
183+
184+
configFileNew.flush();
185+
configFileNew.close();
186+
Aws::Config::ReloadCachedConfigFile();
187+
188+
Aws::Client::ClientConfiguration config;
189+
190+
EXPECT_EQ(Aws::Client::UseRequestCompression::FALSE, config.useRequestCompression);
191+
118192
// cleanup
119193
Aws::FileSystem::RemoveFileIfExists(m_configFileName.c_str());
120194
}

0 commit comments

Comments
 (0)