Skip to content

Commit

Permalink
Improve 9a4c0b1
Browse files Browse the repository at this point in the history
Actually escape utf8 characters in the user agent string using the `\xXX` notation and use such escaped string throughout the program rather than using the trimmed one only for usage with libgit2
  • Loading branch information
edo9300 committed Mar 31, 2023
1 parent 861a742 commit 857bc4b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gframe/repo_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ RepoManager::RepoManager() {
else
git_libgit2_opts(GIT_OPT_SET_SSL_CERT_LOCATIONS, "SYSTEM", "");
#endif
git_libgit2_opts(GIT_OPT_SET_USER_AGENT, ygo::Utils::GetASCIIUserAgent().data());
git_libgit2_opts(GIT_OPT_SET_USER_AGENT, ygo::Utils::GetUserAgent().data());
#if (LIBGIT2_VER_MAJOR>0 && LIBGIT2_VER_MINOR>=3) || LIBGIT2_VER_MAJOR>1
// disable option introduced with https://github.com/libgit2/libgit2/pull/6266
// due how this got backported in older libgitversion as well, and in case
Expand Down
30 changes: 19 additions & 11 deletions gframe/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,18 +478,26 @@ namespace ygo {
return nullptr;
}
const std::string& Utils::GetUserAgent() {
auto EscapeUTF8 = [](auto& to_escape) {
auto IsNonANSI = [](char c) {
return (static_cast<unsigned>(c) & ~0x7Fu) != 0;
};
const epro::stringview view{ to_escape.data(), to_escape.size() };
const auto total_unicode = std::count_if(view.begin(), view.end(), IsNonANSI);
std::string ret;
ret.reserve(view.size() + (total_unicode * 4));
for(auto c : view) {
if(IsNonANSI(c)) {
static constexpr std::array<char, 16> map{ {'0', '1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'} };
auto int_char = static_cast<unsigned>(c);
ret.append(R"(\x)").append(1, map[(int_char >> 4) & 0xf]).append(1, map[int_char & 0xf]);
} else
ret.append(1, c);
}
return ret;
};
static const std::string agent = epro::format("EDOPro-" OSSTRING "-" STR(EDOPRO_VERSION_MAJOR) "." STR(EDOPRO_VERSION_MINOR) "." STR(EDOPRO_VERSION_PATCH)" {}",
Utils::OSOperator->getOperatingSystemVersion());
return agent;
}
const std::string& Utils::GetASCIIUserAgent() {
static const std::string agent = [] {
auto full_agent = GetUserAgent();
full_agent.erase(std::remove_if(full_agent.begin(), full_agent.end(),
[](char c){ return (static_cast<unsigned>(c) & 0x80) != 0; }),
full_agent.end());
return full_agent;
}();
EscapeUTF8(Utils::OSOperator->getOperatingSystemVersion()));
return agent;
}
epro::path_string Utils::GetAbsolutePath(epro::path_stringview path) {
Expand Down
2 changes: 0 additions & 2 deletions gframe/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ namespace ygo {

static const std::string& GetUserAgent();

static const std::string& GetASCIIUserAgent();

static epro::path_string GetAbsolutePath(epro::path_stringview path);

template<typename T>
Expand Down

0 comments on commit 857bc4b

Please sign in to comment.