2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/HTTPCommon.h>
|
2016-12-30 20:52:56 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
#include <Server/HTTP/HTTPServerResponse.h>
|
2023-07-03 21:11:32 +00:00
|
|
|
#include <Poco/Any.h>
|
2023-07-14 09:09:22 +00:00
|
|
|
#include <Common/Concepts.h>
|
2018-06-14 21:20:39 +00:00
|
|
|
#include <Common/DNSResolver.h>
|
|
|
|
#include <Common/Exception.h>
|
2023-06-20 18:27:56 +00:00
|
|
|
#include <Common/MemoryTrackerSwitcher.h>
|
2019-02-10 17:40:52 +00:00
|
|
|
#include <Common/PoolBase.h>
|
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
#include <Common/SipHash.h>
|
|
|
|
|
2022-09-28 08:45:15 +00:00
|
|
|
#include "config.h"
|
2020-04-16 12:31:57 +00:00
|
|
|
|
2020-05-08 14:11:19 +00:00
|
|
|
#if USE_SSL
|
2020-04-16 12:31:57 +00:00
|
|
|
# include <Poco/Net/AcceptCertificateHandler.h>
|
|
|
|
# include <Poco/Net/Context.h>
|
|
|
|
# include <Poco/Net/HTTPSClientSession.h>
|
|
|
|
# include <Poco/Net/InvalidCertificateHandler.h>
|
|
|
|
# include <Poco/Net/PrivateKeyPassphraseHandler.h>
|
|
|
|
# include <Poco/Net/RejectCertificateHandler.h>
|
|
|
|
# include <Poco/Net/SSLManager.h>
|
2020-10-22 02:02:26 +00:00
|
|
|
# include <Poco/Net/SecureStreamSocket.h>
|
2017-03-28 20:30:57 +00:00
|
|
|
#endif
|
2018-11-18 22:15:42 +00:00
|
|
|
|
2017-02-27 21:07:57 +00:00
|
|
|
#include <Poco/Util/Application.h>
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2023-07-14 09:09:22 +00:00
|
|
|
#include <sstream>
|
2019-02-10 17:40:52 +00:00
|
|
|
#include <tuple>
|
|
|
|
#include <unordered_map>
|
2023-11-04 17:47:52 +00:00
|
|
|
#include <Common/ProxyConfiguration.h>
|
2018-11-18 22:15:42 +00:00
|
|
|
|
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event CreatedHTTPConnections;
|
|
|
|
}
|
2016-12-30 20:52:56 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2018-06-14 21:20:39 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int RECEIVED_ERROR_FROM_REMOTE_IO_SERVER;
|
|
|
|
extern const int RECEIVED_ERROR_TOO_MANY_REQUESTS;
|
2018-06-16 05:54:06 +00:00
|
|
|
extern const int FEATURE_IS_NOT_ENABLED_AT_BUILD_TIME;
|
2018-11-20 17:21:32 +00:00
|
|
|
extern const int UNSUPPORTED_URI_SCHEME;
|
2023-07-03 21:11:32 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2018-06-14 21:20:39 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 05:54:06 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
namespace
|
|
|
|
{
|
2023-11-04 17:47:52 +00:00
|
|
|
Poco::Net::HTTPClientSession::ProxyConfig proxyConfigurationToPocoProxyConfig(const ProxyConfiguration & proxy_configuration)
|
|
|
|
{
|
|
|
|
Poco::Net::HTTPClientSession::ProxyConfig poco_proxy_config;
|
|
|
|
|
|
|
|
poco_proxy_config.host = proxy_configuration.host;
|
|
|
|
poco_proxy_config.port = proxy_configuration.port;
|
|
|
|
poco_proxy_config.protocol = ProxyConfiguration::protocolToString(proxy_configuration.protocol);
|
|
|
|
poco_proxy_config.tunnel = proxy_configuration.tunneling;
|
|
|
|
poco_proxy_config.originalRequestProtocol = ProxyConfiguration::protocolToString(proxy_configuration.original_request_protocol);
|
|
|
|
|
|
|
|
return poco_proxy_config;
|
|
|
|
}
|
|
|
|
|
2023-07-14 09:09:22 +00:00
|
|
|
template <typename Session>
|
|
|
|
requires std::derived_from<Session, Poco::Net::HTTPClientSession>
|
|
|
|
class HTTPSessionAdapter : public Session
|
|
|
|
{
|
|
|
|
static_assert(std::has_virtual_destructor_v<Session>, "The base class must have a virtual destructor");
|
|
|
|
|
|
|
|
public:
|
|
|
|
HTTPSessionAdapter(const std::string & host, UInt16 port) : Session(host, port), log{&Poco::Logger::get("HTTPSessionAdapter")} { }
|
|
|
|
~HTTPSessionAdapter() override = default;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void reconnect() override
|
|
|
|
{
|
|
|
|
// First of all will try to establish connection with last used addr.
|
|
|
|
if (!Session::getResolvedHost().empty())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Session::reconnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Session::close();
|
|
|
|
LOG_TRACE(
|
|
|
|
log,
|
2023-07-20 14:56:30 +00:00
|
|
|
"Last ip ({}) is unreachable for {}:{}. Will try another resolved address.",
|
2023-07-14 09:09:22 +00:00
|
|
|
Session::getResolvedHost(),
|
|
|
|
Session::getHost(),
|
|
|
|
Session::getPort());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto endpoinds = DNSResolver::instance().resolveHostAll(Session::getHost());
|
|
|
|
|
|
|
|
for (auto it = endpoinds.begin();;)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Session::setResolvedHost(it->toString());
|
|
|
|
Session::reconnect();
|
|
|
|
|
|
|
|
LOG_TRACE(
|
|
|
|
log,
|
|
|
|
"Created HTTP(S) session with {}:{} ({}:{})",
|
|
|
|
Session::getHost(),
|
|
|
|
Session::getPort(),
|
|
|
|
it->toString(),
|
|
|
|
Session::getPort());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Session::close();
|
|
|
|
if (++it == endpoinds.end())
|
|
|
|
{
|
|
|
|
Session::setResolvedHost("");
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
LOG_TRACE(
|
|
|
|
log,
|
|
|
|
"Failed to create connection with {}:{}, Will try another resolved address. {}",
|
|
|
|
Session::getResolvedHost(),
|
|
|
|
Session::getPort(),
|
|
|
|
getCurrentExceptionMessage(false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Poco::Logger * log;
|
|
|
|
};
|
|
|
|
|
2018-11-20 17:21:32 +00:00
|
|
|
bool isHTTPS(const Poco::URI & uri)
|
|
|
|
{
|
|
|
|
if (uri.getScheme() == "https")
|
|
|
|
return true;
|
|
|
|
else if (uri.getScheme() == "http")
|
|
|
|
return false;
|
|
|
|
else
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::UNSUPPORTED_URI_SCHEME, "Unsupported scheme in URI '{}'", uri.toString());
|
2018-11-20 17:21:32 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 13:07:26 +00:00
|
|
|
HTTPSessionPtr makeHTTPSessionImpl(
|
|
|
|
const std::string & host,
|
|
|
|
UInt16 port,
|
|
|
|
bool https,
|
|
|
|
bool keep_alive,
|
2023-11-04 17:47:52 +00:00
|
|
|
DB::ProxyConfiguration proxy_configuration = {})
|
2018-11-16 13:15:17 +00:00
|
|
|
{
|
|
|
|
HTTPSessionPtr session;
|
|
|
|
|
2023-11-04 17:47:52 +00:00
|
|
|
if (!proxy_configuration.host.empty())
|
|
|
|
{
|
|
|
|
bool is_proxy_http_and_is_tunneling_off = DB::ProxyConfiguration::Protocol::HTTP == proxy_configuration.protocol
|
|
|
|
&& !proxy_configuration.tunneling;
|
|
|
|
|
|
|
|
// If it is an HTTPS request, proxy server is HTTP and user opted for tunneling off, we must not create an HTTPS request.
|
|
|
|
// The desired flow is: HTTP request to the proxy server, then proxy server will initiate an HTTPS request to the target server.
|
|
|
|
// There is a weak link in the security, but that's what the user opted for.
|
|
|
|
if (https && is_proxy_http_and_is_tunneling_off)
|
|
|
|
{
|
|
|
|
https = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
if (https)
|
2020-10-22 02:02:26 +00:00
|
|
|
{
|
2020-05-08 14:11:19 +00:00
|
|
|
#if USE_SSL
|
2023-07-14 09:09:22 +00:00
|
|
|
session = std::make_shared<HTTPSessionAdapter<Poco::Net::HTTPSClientSession>>(host, port);
|
2018-11-16 13:15:17 +00:00
|
|
|
#else
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::FEATURE_IS_NOT_ENABLED_AT_BUILD_TIME, "ClickHouse was built without HTTPS support");
|
2018-11-16 13:15:17 +00:00
|
|
|
#endif
|
2020-10-22 02:02:26 +00:00
|
|
|
}
|
2018-11-16 13:15:17 +00:00
|
|
|
else
|
2020-10-22 02:02:26 +00:00
|
|
|
{
|
2023-07-14 09:09:22 +00:00
|
|
|
session = std::make_shared<HTTPSessionAdapter<Poco::Net::HTTPClientSession>>(host, port);
|
2020-10-22 02:02:26 +00:00
|
|
|
}
|
2018-11-16 13:15:17 +00:00
|
|
|
|
|
|
|
ProfileEvents::increment(ProfileEvents::CreatedHTTPConnections);
|
|
|
|
|
2018-11-23 09:24:59 +00:00
|
|
|
/// doesn't work properly without patch
|
2018-11-20 13:15:44 +00:00
|
|
|
session->setKeepAlive(keep_alive);
|
2023-08-24 13:07:26 +00:00
|
|
|
|
2023-11-04 17:47:52 +00:00
|
|
|
if (!proxy_configuration.host.empty())
|
|
|
|
{
|
|
|
|
session->setProxyConfig(proxyConfigurationToPocoProxyConfig(proxy_configuration));
|
|
|
|
}
|
2023-08-24 13:07:26 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SingleEndpointHTTPSessionPool : public PoolBase<Poco::Net::HTTPClientSession>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
const std::string host;
|
|
|
|
const UInt16 port;
|
2023-04-14 10:18:20 +00:00
|
|
|
const bool https;
|
2023-11-04 17:47:52 +00:00
|
|
|
ProxyConfiguration proxy_config;
|
2023-04-14 10:18:20 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
using Base = PoolBase<Poco::Net::HTTPClientSession>;
|
2023-04-14 10:18:20 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
ObjectPtr allocObject() override
|
|
|
|
{
|
2023-06-20 18:27:56 +00:00
|
|
|
/// Pool is global, we shouldn't attribute this memory to query/user.
|
|
|
|
MemoryTrackerSwitcher switcher{&total_memory_tracker};
|
|
|
|
|
2023-11-04 17:47:52 +00:00
|
|
|
auto session = makeHTTPSessionImpl(host, port, https, true, proxy_config);
|
2020-12-03 04:18:12 +00:00
|
|
|
return session;
|
2018-11-16 13:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2020-12-03 04:18:12 +00:00
|
|
|
SingleEndpointHTTPSessionPool(
|
2023-04-14 10:18:20 +00:00
|
|
|
const std::string & host_,
|
|
|
|
UInt16 port_,
|
|
|
|
bool https_,
|
2023-11-04 17:47:52 +00:00
|
|
|
ProxyConfiguration proxy_config_,
|
2023-04-14 10:18:20 +00:00
|
|
|
size_t max_pool_size_,
|
2023-06-12 14:19:05 +00:00
|
|
|
bool wait_on_pool_size_limit)
|
|
|
|
: Base(
|
|
|
|
static_cast<unsigned>(max_pool_size_),
|
|
|
|
&Poco::Logger::get("HTTPSessionPool"),
|
|
|
|
wait_on_pool_size_limit ? BehaviourOnLimit::Wait : BehaviourOnLimit::AllocateNewBypassingPool)
|
2020-12-03 04:18:12 +00:00
|
|
|
, host(host_)
|
|
|
|
, port(port_)
|
|
|
|
, https(https_)
|
2023-11-04 17:47:52 +00:00
|
|
|
, proxy_config(proxy_config_)
|
2018-11-16 13:15:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-22 03:24:05 +00:00
|
|
|
class HTTPSessionPool : private boost::noncopyable
|
2018-11-16 13:15:17 +00:00
|
|
|
{
|
2020-12-03 04:18:12 +00:00
|
|
|
public:
|
|
|
|
struct Key
|
|
|
|
{
|
|
|
|
String target_host;
|
|
|
|
UInt16 target_port;
|
|
|
|
bool is_target_https;
|
2023-11-04 17:47:52 +00:00
|
|
|
ProxyConfiguration proxy_config;
|
2023-06-12 14:19:05 +00:00
|
|
|
bool wait_on_pool_size_limit;
|
2020-12-03 04:18:12 +00:00
|
|
|
|
2020-12-21 07:48:26 +00:00
|
|
|
bool operator ==(const Key & rhs) const
|
|
|
|
{
|
2023-11-04 17:47:52 +00:00
|
|
|
return std::tie(
|
|
|
|
target_host,
|
|
|
|
target_port,
|
|
|
|
is_target_https,
|
|
|
|
proxy_config.host,
|
|
|
|
proxy_config.port,
|
|
|
|
proxy_config.protocol,
|
|
|
|
proxy_config.tunneling,
|
|
|
|
proxy_config.original_request_protocol,
|
|
|
|
wait_on_pool_size_limit)
|
|
|
|
== std::tie(
|
|
|
|
rhs.target_host,
|
|
|
|
rhs.target_port,
|
|
|
|
rhs.is_target_https,
|
|
|
|
rhs.proxy_config.host,
|
|
|
|
rhs.proxy_config.port,
|
|
|
|
rhs.proxy_config.protocol,
|
|
|
|
rhs.proxy_config.tunneling,
|
|
|
|
rhs.proxy_config.original_request_protocol,
|
|
|
|
rhs.wait_on_pool_size_limit);
|
2020-12-21 07:48:26 +00:00
|
|
|
}
|
2020-12-03 04:18:12 +00:00
|
|
|
};
|
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
private:
|
|
|
|
using PoolPtr = std::shared_ptr<SingleEndpointHTTPSessionPool>;
|
|
|
|
using Entry = SingleEndpointHTTPSessionPool::Entry;
|
|
|
|
|
|
|
|
struct Hasher
|
|
|
|
{
|
|
|
|
size_t operator()(const Key & k) const
|
|
|
|
{
|
|
|
|
SipHash s;
|
2020-12-03 04:18:12 +00:00
|
|
|
s.update(k.target_host);
|
|
|
|
s.update(k.target_port);
|
|
|
|
s.update(k.is_target_https);
|
2023-11-04 17:47:52 +00:00
|
|
|
s.update(k.proxy_config.host);
|
|
|
|
s.update(k.proxy_config.port);
|
|
|
|
s.update(k.proxy_config.protocol);
|
|
|
|
s.update(k.proxy_config.tunneling);
|
|
|
|
s.update(k.proxy_config.original_request_protocol);
|
2023-06-12 14:19:05 +00:00
|
|
|
s.update(k.wait_on_pool_size_limit);
|
2018-11-16 13:15:17 +00:00
|
|
|
return s.get64();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::mutex mutex;
|
|
|
|
std::unordered_map<Key, PoolPtr, Hasher> endpoints_pool;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
HTTPSessionPool() = default;
|
|
|
|
|
|
|
|
public:
|
2019-08-22 03:24:05 +00:00
|
|
|
static auto & instance()
|
|
|
|
{
|
|
|
|
static HTTPSessionPool instance;
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:10:03 +00:00
|
|
|
Entry getSession(
|
|
|
|
const Poco::URI & uri,
|
2023-11-04 17:47:52 +00:00
|
|
|
const ProxyConfiguration & proxy_config,
|
2019-03-29 18:10:03 +00:00
|
|
|
const ConnectionTimeouts & timeouts,
|
2020-12-03 04:18:12 +00:00
|
|
|
size_t max_connections_per_endpoint,
|
2023-06-12 14:19:05 +00:00
|
|
|
bool wait_on_pool_size_limit)
|
2018-11-16 13:15:17 +00:00
|
|
|
{
|
2023-06-12 14:19:05 +00:00
|
|
|
std::unique_lock lock(mutex);
|
2018-11-16 13:15:17 +00:00
|
|
|
const std::string & host = uri.getHost();
|
|
|
|
UInt16 port = uri.getPort();
|
2018-11-20 17:21:32 +00:00
|
|
|
bool https = isHTTPS(uri);
|
2020-12-03 04:18:12 +00:00
|
|
|
|
2023-11-04 17:47:52 +00:00
|
|
|
HTTPSessionPool::Key key{host, port, https, proxy_config, wait_on_pool_size_limit};
|
2018-11-16 13:15:17 +00:00
|
|
|
auto pool_ptr = endpoints_pool.find(key);
|
|
|
|
if (pool_ptr == endpoints_pool.end())
|
|
|
|
std::tie(pool_ptr, std::ignore) = endpoints_pool.emplace(
|
2023-06-12 14:19:05 +00:00
|
|
|
key,
|
|
|
|
std::make_shared<SingleEndpointHTTPSessionPool>(
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
https,
|
2023-11-04 17:47:52 +00:00
|
|
|
proxy_config,
|
2023-06-12 14:19:05 +00:00
|
|
|
max_connections_per_endpoint,
|
|
|
|
wait_on_pool_size_limit));
|
|
|
|
|
|
|
|
/// Some routines held session objects until the end of its lifetime. Also this routines may create another sessions in this time frame.
|
|
|
|
/// If some other session holds `lock` because it waits on another lock inside `pool_ptr->second->get` it isn't possible to create any
|
|
|
|
/// new session and thus finish routine, return session to the pool and unlock the thread waiting inside `pool_ptr->second->get`.
|
|
|
|
/// To avoid such a deadlock we unlock `lock` before entering `pool_ptr->second->get`.
|
|
|
|
lock.unlock();
|
2018-11-16 13:15:17 +00:00
|
|
|
|
2023-09-08 08:02:30 +00:00
|
|
|
auto retry_timeout = timeouts.connection_timeout.totalMilliseconds();
|
2018-11-20 13:15:44 +00:00
|
|
|
auto session = pool_ptr->second->get(retry_timeout);
|
|
|
|
|
2023-10-17 09:13:59 +00:00
|
|
|
const auto & session_data = session->sessionData();
|
|
|
|
if (session_data.empty() || !Poco::AnyCast<HTTPSessionReuseTag>(&session_data))
|
|
|
|
{
|
|
|
|
/// Reset session if it is not reusable. See comment for HTTPSessionReuseTag.
|
|
|
|
session->reset();
|
|
|
|
}
|
|
|
|
session->attachSessionData({});
|
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
setTimeouts(*session, timeouts);
|
2019-03-29 18:10:03 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
return session;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-20 13:53:22 +00:00
|
|
|
void setTimeouts(Poco::Net::HTTPClientSession & session, const ConnectionTimeouts & timeouts)
|
|
|
|
{
|
|
|
|
session.setTimeout(timeouts.connection_timeout, timeouts.send_timeout, timeouts.receive_timeout);
|
|
|
|
session.setKeepAliveTimeout(timeouts.http_keep_alive_timeout);
|
|
|
|
}
|
|
|
|
|
2022-10-07 10:46:45 +00:00
|
|
|
void setResponseDefaultHeaders(HTTPServerResponse & response, size_t keep_alive_timeout)
|
2016-12-31 02:05:37 +00:00
|
|
|
{
|
2016-12-30 20:52:56 +00:00
|
|
|
if (!response.getKeepAlive())
|
|
|
|
return;
|
|
|
|
|
2017-09-08 11:57:43 +00:00
|
|
|
Poco::Timespan timeout(keep_alive_timeout, 0);
|
|
|
|
if (timeout.totalSeconds())
|
|
|
|
response.set("Keep-Alive", "timeout=" + std::to_string(timeout.totalSeconds()));
|
2016-12-30 20:52:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 13:07:26 +00:00
|
|
|
HTTPSessionPtr makeHTTPSession(
|
|
|
|
const Poco::URI & uri,
|
|
|
|
const ConnectionTimeouts & timeouts,
|
2023-11-04 17:47:52 +00:00
|
|
|
ProxyConfiguration proxy_configuration
|
2023-08-24 13:07:26 +00:00
|
|
|
)
|
2018-06-14 21:20:39 +00:00
|
|
|
{
|
2018-11-16 13:15:17 +00:00
|
|
|
const std::string & host = uri.getHost();
|
|
|
|
UInt16 port = uri.getPort();
|
2018-11-20 17:21:32 +00:00
|
|
|
bool https = isHTTPS(uri);
|
2018-06-14 21:20:39 +00:00
|
|
|
|
2023-11-04 17:47:52 +00:00
|
|
|
auto session = makeHTTPSessionImpl(host, port, https, false, proxy_configuration);
|
2018-11-16 13:15:17 +00:00
|
|
|
setTimeouts(*session, timeouts);
|
|
|
|
return session;
|
|
|
|
}
|
2018-06-14 21:20:39 +00:00
|
|
|
|
2023-06-12 14:19:05 +00:00
|
|
|
PooledHTTPSessionPtr makePooledHTTPSession(
|
|
|
|
const Poco::URI & uri,
|
|
|
|
const ConnectionTimeouts & timeouts,
|
|
|
|
size_t per_endpoint_pool_size,
|
2023-11-04 17:47:52 +00:00
|
|
|
bool wait_on_pool_size_limit,
|
|
|
|
ProxyConfiguration proxy_config)
|
2018-11-16 13:15:17 +00:00
|
|
|
{
|
2023-11-04 17:47:52 +00:00
|
|
|
return HTTPSessionPool::instance().getSession(uri, proxy_config, timeouts, per_endpoint_pool_size, wait_on_pool_size_limit);
|
2018-06-14 21:20:39 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 17:28:41 +00:00
|
|
|
bool isRedirect(const Poco::Net::HTTPResponse::HTTPStatus status) { return status == Poco::Net::HTTPResponse::HTTP_MOVED_PERMANENTLY || status == Poco::Net::HTTPResponse::HTTP_FOUND || status == Poco::Net::HTTPResponse::HTTP_SEE_OTHER || status == Poco::Net::HTTPResponse::HTTP_TEMPORARY_REDIRECT; }
|
2018-06-14 21:20:39 +00:00
|
|
|
|
2018-06-16 05:54:06 +00:00
|
|
|
std::istream * receiveResponse(
|
2019-09-16 17:28:41 +00:00
|
|
|
Poco::Net::HTTPClientSession & session, const Poco::Net::HTTPRequest & request, Poco::Net::HTTPResponse & response, const bool allow_redirects)
|
2018-06-14 21:20:39 +00:00
|
|
|
{
|
2019-06-17 17:32:57 +00:00
|
|
|
auto & istr = session.receiveResponse(response);
|
2019-09-23 08:53:09 +00:00
|
|
|
assertResponseIsOk(request, response, istr, allow_redirects);
|
2019-06-17 17:32:57 +00:00
|
|
|
return &istr;
|
2019-05-31 10:58:43 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 08:53:09 +00:00
|
|
|
void assertResponseIsOk(const Poco::Net::HTTPRequest & request, Poco::Net::HTTPResponse & response, std::istream & istr, const bool allow_redirects)
|
2019-06-17 18:06:28 +00:00
|
|
|
{
|
2018-06-14 21:20:39 +00:00
|
|
|
auto status = response.getStatus();
|
|
|
|
|
2020-10-29 21:29:10 +00:00
|
|
|
if (!(status == Poco::Net::HTTPResponse::HTTP_OK
|
|
|
|
|| status == Poco::Net::HTTPResponse::HTTP_CREATED
|
|
|
|
|| status == Poco::Net::HTTPResponse::HTTP_ACCEPTED
|
2021-06-19 16:36:39 +00:00
|
|
|
|| status == Poco::Net::HTTPResponse::HTTP_PARTIAL_CONTENT /// Reading with Range header was successful.
|
2020-10-29 21:29:10 +00:00
|
|
|
|| (isRedirect(status) && allow_redirects)))
|
2019-09-11 20:18:22 +00:00
|
|
|
{
|
2022-11-10 21:58:15 +00:00
|
|
|
int code = status == Poco::Net::HTTPResponse::HTTP_TOO_MANY_REQUESTS
|
|
|
|
? ErrorCodes::RECEIVED_ERROR_TOO_MANY_REQUESTS
|
|
|
|
: ErrorCodes::RECEIVED_ERROR_FROM_REMOTE_IO_SERVER;
|
|
|
|
|
|
|
|
std::stringstream body; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
|
|
|
|
body.exceptions(std::ios::failbit);
|
|
|
|
body << istr.rdbuf();
|
|
|
|
|
|
|
|
throw HTTPException(code, request.getURI(), status, response.getReason(), body.str());
|
2018-06-14 21:20:39 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-16 05:54:06 +00:00
|
|
|
|
2023-01-17 16:39:07 +00:00
|
|
|
Exception HTTPException::makeExceptionMessage(
|
|
|
|
int code,
|
2022-11-10 21:58:15 +00:00
|
|
|
const std::string & uri,
|
|
|
|
Poco::Net::HTTPResponse::HTTPStatus http_status,
|
|
|
|
const std::string & reason,
|
|
|
|
const std::string & body)
|
|
|
|
{
|
2023-01-17 16:39:07 +00:00
|
|
|
return Exception(code,
|
2022-11-10 21:58:15 +00:00
|
|
|
"Received error from remote server {}. "
|
|
|
|
"HTTP status code: {} {}, "
|
|
|
|
"body: {}",
|
2022-12-04 01:23:43 +00:00
|
|
|
uri, static_cast<int>(http_status), reason, body);
|
2022-11-10 21:58:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-03 21:11:32 +00:00
|
|
|
void markSessionForReuse(Poco::Net::HTTPSession & session)
|
|
|
|
{
|
|
|
|
const auto & session_data = session.sessionData();
|
|
|
|
if (!session_data.empty() && !Poco::AnyCast<HTTPSessionReuseTag>(&session_data))
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::LOGICAL_ERROR, "Data of an unexpected type ({}) is attached to the session", session_data.type().name());
|
|
|
|
|
|
|
|
session.attachSessionData(HTTPSessionReuseTag{});
|
|
|
|
}
|
|
|
|
|
|
|
|
void markSessionForReuse(HTTPSessionPtr session)
|
|
|
|
{
|
|
|
|
markSessionForReuse(*session);
|
|
|
|
}
|
|
|
|
|
|
|
|
void markSessionForReuse(PooledHTTPSessionPtr session)
|
|
|
|
{
|
|
|
|
markSessionForReuse(static_cast<Poco::Net::HTTPSession &>(*session));
|
|
|
|
}
|
|
|
|
|
2016-12-30 20:52:56 +00:00
|
|
|
}
|