Skip to content
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

[fix] s3 user agent configuration is incorrect #2903

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/common/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
* 2018/08/30 Wenyu Zhou Initial version
*/

#include "src/common/configuration.h"

#include <glog/logging.h>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>

#include "src/common/configuration.h"
#include "src/common/string_util.h"

namespace curve {
namespace common {
Expand All @@ -38,16 +39,17 @@ bool Configuration::LoadConfig() {
if (cFile.is_open()) {
std::string line;
while (getline(cFile, line)) {
// FIXME: may not remove middle spaces
line.erase(std::remove_if(line.begin(), line.end(), isspace),
line.end());
Trim(line);
if (line.empty() || line[0] == '#') continue;

int delimiterPos = line.find("=");
std::string key = line.substr(0, delimiterPos);
Trim(key);

int commentPos = line.find("#");
std::string value = line.substr(delimiterPos + 1,
commentPos - delimiterPos - 1);
Trim(value);
SetValue(key, value);
}
} else {
Expand Down
22 changes: 22 additions & 0 deletions src/common/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <glog/logging.h>
#include <string>
#include <vector>
#include <algorithm>
#include <stdexcept>

namespace curve {
Expand Down Expand Up @@ -165,6 +166,27 @@ inline std::string ToHexString(void* p) {
return oss.str();
}

// trim from start (in place)
inline void LTrim(std::string &s) { // NOLINT
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](const char &ch) {
return !std::isspace(ch);
}));
}

// trim from end (in place)
inline void RTrim(std::string &s) { // NOLINT
s.erase(std::find_if(s.rbegin(), s.rend(),
[](const char &ch) { return !std::isspace(ch); })
.base(),
s.end());
}

// trim from both ends (in place)
inline void Trim(std::string &s) { // NOLINT
LTrim(s);
RTrim(s);
}

} // namespace common
} // namespace curve

Expand Down
51 changes: 50 additions & 1 deletion test/common/string_util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
namespace curve {
namespace common {

TEST(Common, SpliteString) {
TEST(Common, SplitString) {
const struct {std::string path; std::string sep;
const int size; std::vector<std::string> items;}
testCases[] = {
Expand Down Expand Up @@ -59,5 +59,54 @@ TEST(Common, StringToUll) {
str = "ffff";
ASSERT_FALSE(StringToUll(str, &out));
}

TEST(StringUtilTest, LTrim) {
std::array<std::array<std::string, 2>, 4> cases = {
std::array<std::string, 2>{"hello", "hello"},
std::array<std::string, 2>{"", ""},
std::array<std::string, 2>{" ", ""},
std::array<std::string, 2>{" hello", "hello"},
};

for (auto &c : cases) {
curve::common::LTrim(c[0]);
EXPECT_EQ(c[0], c[1]);
}
}

TEST(StringUtilTest, RTrim) {
std::array<std::array<std::string, 2>, 4> cases = {
std::array<std::string, 2>{"hello", "hello"},
std::array<std::string, 2>{"", ""},
std::array<std::string, 2>{" ", ""},
std::array<std::string, 2>{"hello ", "hello"},
};

for (auto &c : cases) {
curve::common::RTrim(c[0]);
EXPECT_EQ(c[0], c[1]);
}
}

TEST(StringUtilTest, Trim) {
std::array<std::array<std::string, 2>, 10> cases = {
std::array<std::string, 2>{"hello", "hello"},
std::array<std::string, 2>{" hello", "hello"},
std::array<std::string, 2>{"hello ", "hello"},
std::array<std::string, 2>{" hello ", "hello"},
std::array<std::string, 2>{"S3 Browser", "S3 Browser"},
std::array<std::string, 2>{"S3 Browser ", "S3 Browser"},
std::array<std::string, 2>{" S3 Browser", "S3 Browser"},
std::array<std::string, 2>{" S3 Browser ", "S3 Browser"},
std::array<std::string, 2>{" ", ""},
std::array<std::string, 2>{"", ""},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a case for middle space, such as " S3 Browser ", "S3 Browser", the expect trimed string is "S3 Browser".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

};

for (auto &c : cases) {
curve::common::Trim(c[0]);
EXPECT_EQ(c[0], c[1]);
}
}

} // namespace common
} // namespace curve