refactor: Replace libevent use in urlDecode with our own code

This commit is contained in:
Fabian Jahr committed 2024-04-24 23:23:34 +02:00
1 parent 46bc6c2aaa
commit 650d43ec15
5 files changed
+37 -18

No files matched your search

-1
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([ENABLE_BENCH], [test "$use_bench" = "yes"])
AM_CONDITIONAL([USE_QRCODE], [test "$use_qr" = "yes"]) AM_CONDITIONAL([USE_QRCODE], [test "$use_qr" = "yes"])
AM_CONDITIONAL([USE_LCOV], [test "$use_lcov" = "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([HARDEN], [test "$use_hardening" = "yes"])
AM_CONDITIONAL([ENABLE_SSE42], [test "$enable_sse42" = "yes"]) AM_CONDITIONAL([ENABLE_SSE42], [test "$enable_sse42" = "yes"])
AM_CONDITIONAL([ENABLE_SSE41], [test "$enable_sse41" = "yes"]) AM_CONDITIONAL([ENABLE_SSE41], [test "$enable_sse41" = "yes"])
+1 -5
View File
@@ -679,6 +679,7 @@ libbitcoin_common_a_SOURCES = \
common/run_command.cpp \ common/run_command.cpp \
common/settings.cpp \ common/settings.cpp \
common/system.cpp \ common/system.cpp \
common/url.cpp \
compressor.cpp \ compressor.cpp \
core_read.cpp \ core_read.cpp \
core_write.cpp \ core_write.cpp \
@@ -711,11 +712,6 @@ libbitcoin_common_a_SOURCES = \
script/solver.cpp \ script/solver.cpp \
warnings.cpp \ warnings.cpp \
$(BITCOIN_CORE_H) $(BITCOIN_CORE_H)
if USE_LIBEVENT
libbitcoin_common_a_CPPFLAGS += $(EVENT_CFLAGS)
libbitcoin_common_a_SOURCES += common/url.cpp
endif
# #
# util # # util #
+31 -9
View File
@@ -4,19 +4,41 @@
#include <common/url.h> #include <common/url.h>
#include <event2/http.h> #include <charconv>
#include <cstdlib>
#include <string> #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; std::string res;
if (!urlEncoded.empty()) { res.reserve(urlEncoded.size());
char *decoded = evhttp_uridecode(urlEncoded.c_str(), false, nullptr);
if (decoded) { for (size_t i = 0; i < urlEncoded.size(); ++i) {
res = std::string(decoded); char c = urlEncoded[i];
free(decoded); // 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; return res;
} }
+2 -1
View File
@@ -6,8 +6,9 @@
#define BITCOIN_COMMON_URL_H #define BITCOIN_COMMON_URL_H
#include <string> #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; UrlDecodeFn urlDecode;
extern UrlDecodeFn* const URL_DECODE; extern UrlDecodeFn* const URL_DECODE;
+3 -2
View File
@@ -11,6 +11,7 @@
#include <wallet/context.h> #include <wallet/context.h>
#include <wallet/wallet.h> #include <wallet/wallet.h>
#include <string_view>
#include <univalue.h> #include <univalue.h>
#include <boost/date_time/posix_time/posix_time.hpp> #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) 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 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 true;
} }
return false; return false;