Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle proxy responses with case insensitive and OK result #653

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
Loading