Skip to content

Commit

Permalink
Handle proxy responses with case insensitive and OK result
Browse files Browse the repository at this point in the history
Signed-off-by: Raul Metsma <raul@metsma.ee>

#652
  • Loading branch information
metsma committed Dec 19, 2024
1 parent e7bbc30 commit e1ff6fe
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
14 changes: 5 additions & 9 deletions src/crypto/Connect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
#include "Conf.h"
#include "Container.h"
#include "crypto/OpenSSLHelpers.h"
#include "util/algorithm.h"

#include <openssl/bio.h>
#include <openssl/ocsp.h>
#include <openssl/ssl.h>

#include <zlib.h>

#include <algorithm>
#include <sstream>
#include <thread>

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -292,23 +292,19 @@ Connect::Result Connect::exec(initializer_list<pair<string_view,string_view>> 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<size_t>(line.size() - 1, 0));
if(line.empty())
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();
}
Expand Down Expand Up @@ -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_free>(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());
Expand Down
6 changes: 6 additions & 0 deletions src/util/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
26 changes: 24 additions & 2 deletions src/util/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@

#include <memory>

namespace digidoc
{

template<auto D>
struct free_deleter
{
template<class T>
void operator()(T *p) const noexcept
{
if (p) D(p);
}
};

template <class T>
using unique_free_t = std::unique_ptr<T, void(*)(T*)>;

Expand All @@ -33,9 +46,9 @@ constexpr unique_free_t<T> make_unique_ptr(U *p, void (*d)(T*)) noexcept

template<class T>
[[nodiscard]]
constexpr auto make_unique_ptr(nullptr_t, void (*d)(T*)) noexcept
constexpr unique_free_t<T> make_unique_ptr(nullptr_t, void (*d)(T*)) noexcept
{
return make_unique_ptr<T, T>(nullptr, d);
return {nullptr, d};
}

template<class T, typename D>
Expand All @@ -44,3 +57,12 @@ constexpr std::unique_ptr<T, D> make_unique_ptr(T *p, D d) noexcept
{
return {p, std::forward<D>(d)};
}

template<auto D, class T>
[[nodiscard]]
constexpr auto make_unique_ptr(T *p) noexcept
{
return std::unique_ptr<T, free_deleter<D>>(p);
}

}

0 comments on commit e1ff6fe

Please sign in to comment.