refactor: Replace libevent use in urlDecode with our own code

This commit is contained in:
Fabian Jahr 2024-04-20 16:35:39 +02:00
parent 46bc6c2aaa
commit 650d43ec15
No known key found for this signature in database
GPG Key ID: F13D1E9D890798CD
5 changed files with 37 additions and 18 deletions

View File

@ -1706,7 +1706,6 @@ AM_CONDITIONAL([ENABLE_QT_TESTS], [test "$BUILD_TEST_QT" = "yes"])
AM_CONDITIONAL([ENABLE_BENCH], [test "$use_bench" = "yes"])
AM_CONDITIONAL([USE_QRCODE], [test "$use_qr" = "yes"])
AM_CONDITIONAL([USE_LCOV], [test "$use_lcov" = "yes"])
AM_CONDITIONAL([USE_LIBEVENT], [test "$use_libevent" = "yes"])
AM_CONDITIONAL([HARDEN], [test "$use_hardening" = "yes"])
AM_CONDITIONAL([ENABLE_SSE42], [test "$enable_sse42" = "yes"])
AM_CONDITIONAL([ENABLE_SSE41], [test "$enable_sse41" = "yes"])

View File

@ -679,6 +679,7 @@ libbitcoin_common_a_SOURCES = \
common/run_command.cpp \
common/settings.cpp \
common/system.cpp \
common/url.cpp \
compressor.cpp \
core_read.cpp \
core_write.cpp \
@ -711,11 +712,6 @@ libbitcoin_common_a_SOURCES = \
script/solver.cpp \
warnings.cpp \
$(BITCOIN_CORE_H)
if USE_LIBEVENT
libbitcoin_common_a_CPPFLAGS += $(EVENT_CFLAGS)
libbitcoin_common_a_SOURCES += common/url.cpp
endif
#
# util #

View File

@ -4,19 +4,41 @@
#include <common/url.h>
#include <event2/http.h>
#include <cstdlib>
#include <charconv>
#include <string>
#include <string_view>
#include <system_error>
std::string urlDecode(const std::string &urlEncoded) {
std::string urlDecode(std::string_view urlEncoded)
{
std::string res;
if (!urlEncoded.empty()) {
char *decoded = evhttp_uridecode(urlEncoded.c_str(), false, nullptr);
if (decoded) {
res = std::string(decoded);
free(decoded);
res.reserve(urlEncoded.size());
for (size_t i = 0; i < urlEncoded.size(); ++i) {
char c = urlEncoded[i];
// Special handling for percent which should be followed by two hex digits
// representing an octet values, see RFC 3986, Section 2.1 Percent-Encoding
if (c == '%' && i + 2 < urlEncoded.size()) {
unsigned int decoded_value{0};
auto [p, ec] = std::from_chars(urlEncoded.data() + i + 1, urlEncoded.data() + i + 3, decoded_value, 16);
// Only if there is no error and the pointer is set to the end of
// the string, we can be sure both characters were valid hex
if (ec == std::errc{} && p == urlEncoded.data() + i + 3) {
// A null character terminates the string
if (decoded_value == 0) {
return res;
}
res += static_cast<char>(decoded_value);
// Next two characters are part of the percent encoding
i += 2;
continue;
}
// In case of invalid percent encoding, add the '%' and continue
}
res += c;
}
return res;
}

View File

@ -6,8 +6,9 @@
#define BITCOIN_COMMON_URL_H
#include <string>
#include <string_view>
using UrlDecodeFn = std::string(const std::string& url_encoded);
using UrlDecodeFn = std::string(std::string_view url_encoded);
UrlDecodeFn urlDecode;
extern UrlDecodeFn* const URL_DECODE;

View File

@ -11,6 +11,7 @@
#include <wallet/context.h>
#include <wallet/wallet.h>
#include <string_view>
#include <univalue.h>
#include <boost/date_time/posix_time/posix_time.hpp>
@ -61,9 +62,9 @@ bool ParseIncludeWatchonly(const UniValue& include_watchonly, const CWallet& wal
bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name)
{
if (URL_DECODE && request.URI.substr(0, WALLET_ENDPOINT_BASE.size()) == WALLET_ENDPOINT_BASE) {
if (URL_DECODE && request.URI.starts_with(WALLET_ENDPOINT_BASE)) {
// wallet endpoint was used
wallet_name = URL_DECODE(request.URI.substr(WALLET_ENDPOINT_BASE.size()));
wallet_name = URL_DECODE(std::string_view{request.URI}.substr(WALLET_ENDPOINT_BASE.size()));
return true;
}
return false;