Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

fetcher: urlencode target names #996

Merged
merged 2 commits into from
Nov 9, 2018
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
5 changes: 3 additions & 2 deletions src/libaktualizr/uptane/fetcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ bool Fetcher::fetchVerifyTarget(const Target& target) {
bool retry = false;
do {
retry = false;
HttpResponse response = http->download(config.uptane.repo_server + "/targets/" + target.filename(),
DownloadHandler, &ds, ds.downloaded_length);
HttpResponse response =
http->download(config.uptane.repo_server + "/targets/" + Utils::urlEncode(target.filename()),
DownloadHandler, &ds, ds.downloaded_length);
if (!response.isOk() && !pause_) {
fhandle->wabort();
if (response.curl_code == CURLE_WRITE_ERROR) {
Expand Down
18 changes: 18 additions & 0 deletions src/libaktualizr/utilities/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,24 @@ void Utils::createDirectories(const boost::filesystem::path &path, mode_t mode)
std::cout << "created: " << path.native() << "\n";
}

std::string Utils::urlEncode(const std::string &input) {
std::string res;

for (char c : input) {
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '.' ||
c == '_' || c == '~') {
res.push_back(c);
} else {
res.push_back('%');
auto nib = static_cast<char>(((c >> 4) & 0x0F));
res.push_back(static_cast<char>((nib < 10) ? nib + '0' : nib - 10 + 'A'));
nib = static_cast<char>(c & 0x0F);
res.push_back(static_cast<char>((nib < 10) ? nib + '0' : nib - 10 + 'A'));
}
}
return res;
}

class SafeTempRoot {
public:
SafeTempRoot(const SafeTempRoot &) = delete;
Expand Down
1 change: 1 addition & 0 deletions src/libaktualizr/utilities/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct Utils {
static void setSocketPort(sockaddr_storage *addr, in_port_t port);
static std::vector<boost::filesystem::path> glob(const std::string &pat);
static void createDirectories(const boost::filesystem::path &path, mode_t mode);
static std::string urlEncode(const std::string &input);
};

/**
Expand Down
2 changes: 2 additions & 0 deletions src/libaktualizr/utilities/utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ TEST(Utils, shell) {
EXPECT_NE(statuscode, 0);
}

TEST(Utils, urlencode) { EXPECT_EQ(Utils::urlEncode("test! test@ test#"), "test%21%20test%40%20test%23"); }

TEST(Utils, BasedPath) {
BasedPath bp("a/test.xml");

Expand Down