From e1ff6fe18b42385834782fde5699bc46d8285ed8 Mon Sep 17 00:00:00 2001 From: Raul Metsma Date: Thu, 19 Dec 2024 10:28:58 +0200 Subject: [PATCH] Handle proxy responses with case insensitive and OK result Signed-off-by: Raul Metsma #652 --- src/crypto/Connect.cpp | 14 +++++--------- src/util/algorithm.h | 6 ++++++ src/util/memory.h | 26 ++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/crypto/Connect.cpp b/src/crypto/Connect.cpp index 077cde27a..e1f62a07a 100644 --- a/src/crypto/Connect.cpp +++ b/src/crypto/Connect.cpp @@ -22,6 +22,7 @@ #include "Conf.h" #include "Container.h" #include "crypto/OpenSSLHelpers.h" +#include "util/algorithm.h" #include #include @@ -29,7 +30,6 @@ #include -#include #include #include @@ -129,7 +129,7 @@ Connect::Connect(const string &_url, string _method, int _timeout, const vector< sendProxyAuth(); doProxyConnect = true; Result r = exec(); - if(!r.isOK() || r.result.find("established") == string::npos) + if(!r.isOK() || (r.result.find("established") == string::npos && r.result.find("ok") == string::npos)) THROW_NETWORKEXCEPTION("Failed to create proxy connection with host: '%s'", hostname.c_str()) doProxyConnect = false; } @@ -292,10 +292,6 @@ Connect::Result Connect::exec(initializer_list> he stringstream stream(r.content); string line; - auto to_lower = [](string str) { - transform(str.begin(), str.end(), str.begin(), ::tolower); - return str; - }; while(getline(stream, line)) { line.resize(max(line.size() - 1, 0)); @@ -303,12 +299,12 @@ Connect::Result Connect::exec(initializer_list> he break; if(r.result.empty()) { - r.result = line; + r.result = to_lower(line); continue; } size_t split = line.find(": "); if(split != string::npos) - r.headers[to_lower(line.substr(0, split))] = line.substr(split + 2); + r.headers[to_lower(line.erase(split))] = line.substr(split + 2); else r.headers[to_lower(line)] = string(); } @@ -352,7 +348,7 @@ void Connect::sendProxyAuth() return; BIO_printf(d, "Proxy-Authorization: Basic "); - SCOPE(BIO, b64, BIO_new(BIO_f_base64())); + auto b64 = make_unique_ptr(BIO_new(BIO_f_base64())); BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL); BIO_push(b64.get(), d); BIO_printf(b64.get(), "%s:%s", c->proxyUser().c_str(), c->proxyPass().c_str()); diff --git a/src/util/algorithm.h b/src/util/algorithm.h index 683cd4953..7746e9823 100644 --- a/src/util/algorithm.h +++ b/src/util/algorithm.h @@ -58,6 +58,12 @@ constexpr bool starts_with(T str, std::string_view needle) { return str.size() >= needle.size() && str.compare(0, needle.size(), needle) == 0; } +inline auto to_lower(std::string str) +{ + std::transform(str.begin(), str.end(), str.begin(), ::tolower); + return str; +} + [[nodiscard]] constexpr auto trim_prefix(std::string_view src) { diff --git a/src/util/memory.h b/src/util/memory.h index 39f1a0516..5b11ad34a 100644 --- a/src/util/memory.h +++ b/src/util/memory.h @@ -21,6 +21,19 @@ #include +namespace digidoc +{ + +template +struct free_deleter +{ + template + void operator()(T *p) const noexcept + { + if (p) D(p); + } +}; + template using unique_free_t = std::unique_ptr; @@ -33,9 +46,9 @@ constexpr unique_free_t make_unique_ptr(U *p, void (*d)(T*)) noexcept template [[nodiscard]] -constexpr auto make_unique_ptr(nullptr_t, void (*d)(T*)) noexcept +constexpr unique_free_t make_unique_ptr(nullptr_t, void (*d)(T*)) noexcept { - return make_unique_ptr(nullptr, d); + return {nullptr, d}; } template @@ -44,3 +57,12 @@ constexpr std::unique_ptr make_unique_ptr(T *p, D d) noexcept { return {p, std::forward(d)}; } + +template +[[nodiscard]] +constexpr auto make_unique_ptr(T *p) noexcept +{ + return std::unique_ptr>(p); +} + +}