2016-11-19 00:07:58 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-02-07 15:38:57 +00:00
|
|
|
#include <functional>
|
2020-09-15 09:55:57 +00:00
|
|
|
#include <common/types.h>
|
2018-11-16 13:15:17 +00:00
|
|
|
#include <IO/ConnectionTimeouts.h>
|
|
|
|
#include <IO/HTTPCommon.h>
|
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/ReadBufferFromIStream.h>
|
2019-06-05 09:23:41 +00:00
|
|
|
#include <Poco/Any.h>
|
2018-07-26 15:10:57 +00:00
|
|
|
#include <Poco/Net/HTTPBasicCredentials.h>
|
2016-11-19 00:07:58 +00:00
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
2018-11-16 13:15:17 +00:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
2016-11-24 01:01:11 +00:00
|
|
|
#include <Poco/URI.h>
|
2018-11-16 13:15:17 +00:00
|
|
|
#include <Poco/Version.h>
|
|
|
|
#include <Common/DNSResolver.h>
|
2020-05-20 20:16:32 +00:00
|
|
|
#include <Common/RemoteHostFilter.h>
|
2018-11-16 13:15:17 +00:00
|
|
|
#include <common/logger_useful.h>
|
2019-09-11 16:39:30 +00:00
|
|
|
#include <Poco/URIStreamFactory.h>
|
2018-11-16 13:15:17 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
# include <Common/config.h>
|
|
|
|
#endif
|
|
|
|
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2017-04-06 18:32:00 +00:00
|
|
|
#define DEFAULT_HTTP_READ_BUFFER_TIMEOUT 1800
|
|
|
|
#define DEFAULT_HTTP_READ_BUFFER_CONNECTION_TIMEOUT 1
|
2017-02-07 06:18:16 +00:00
|
|
|
|
2016-11-19 00:07:58 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
/** Perform HTTP POST request and provide response to read.
|
|
|
|
*/
|
2018-11-16 13:15:17 +00:00
|
|
|
|
2019-09-16 17:28:41 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int TOO_MANY_REDIRECTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename SessionPtr>
|
|
|
|
class UpdatableSessionBase
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
SessionPtr session;
|
|
|
|
UInt64 redirects { 0 };
|
|
|
|
Poco::URI initial_uri;
|
|
|
|
const ConnectionTimeouts & timeouts;
|
2020-07-16 22:08:44 +00:00
|
|
|
UInt64 max_redirects;
|
2019-09-16 17:28:41 +00:00
|
|
|
|
|
|
|
public:
|
2019-09-18 08:49:46 +00:00
|
|
|
virtual void buildNewSession(const Poco::URI & uri) = 0;
|
2019-09-16 17:28:41 +00:00
|
|
|
|
|
|
|
explicit UpdatableSessionBase(const Poco::URI uri,
|
|
|
|
const ConnectionTimeouts & timeouts_,
|
2020-07-16 22:08:44 +00:00
|
|
|
UInt64 max_redirects_)
|
2019-09-16 17:28:41 +00:00
|
|
|
: initial_uri { uri }
|
|
|
|
, timeouts { timeouts_ }
|
|
|
|
, max_redirects { max_redirects_ }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SessionPtr getSession()
|
|
|
|
{
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateSession(const Poco::URI & uri)
|
|
|
|
{
|
2019-09-23 18:16:34 +00:00
|
|
|
++redirects;
|
|
|
|
if (redirects <= max_redirects)
|
2019-09-16 17:28:41 +00:00
|
|
|
{
|
|
|
|
buildNewSession(uri);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::stringstream error_message;
|
|
|
|
error_message << "Too many redirects while trying to access " << initial_uri.toString();
|
|
|
|
|
|
|
|
throw Exception(error_message.str(), ErrorCodes::TOO_MANY_REDIRECTS);
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 08:49:46 +00:00
|
|
|
|
|
|
|
virtual ~UpdatableSessionBase()
|
|
|
|
{
|
|
|
|
}
|
2019-09-16 17:28:41 +00:00
|
|
|
};
|
|
|
|
|
2019-09-23 18:16:34 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
namespace detail
|
2016-11-19 00:07:58 +00:00
|
|
|
{
|
2019-09-16 17:28:41 +00:00
|
|
|
template <typename UpdatableSessionPtr>
|
2018-11-16 13:15:17 +00:00
|
|
|
class ReadWriteBufferFromHTTPBase : public ReadBuffer
|
|
|
|
{
|
2019-09-25 08:08:46 +00:00
|
|
|
public:
|
2019-09-26 03:34:22 +00:00
|
|
|
using HTTPHeaderEntry = std::tuple<std::string, std::string>;
|
|
|
|
using HTTPHeaderEntries = std::vector<HTTPHeaderEntry>;
|
2019-09-25 08:08:46 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
protected:
|
|
|
|
Poco::URI uri;
|
|
|
|
std::string method;
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2019-09-16 17:28:41 +00:00
|
|
|
UpdatableSessionPtr session;
|
2018-11-16 13:15:17 +00:00
|
|
|
std::istream * istr; /// owned by session
|
|
|
|
std::unique_ptr<ReadBuffer> impl;
|
2019-09-18 10:38:00 +00:00
|
|
|
std::function<void(std::ostream &)> out_stream_callback;
|
|
|
|
const Poco::Net::HTTPBasicCredentials & credentials;
|
2019-09-06 12:18:56 +00:00
|
|
|
std::vector<Poco::Net::HTTPCookie> cookies;
|
2019-09-26 03:34:22 +00:00
|
|
|
HTTPHeaderEntries http_header_entries;
|
2019-10-23 11:58:35 +00:00
|
|
|
RemoteHostFilter remote_host_filter;
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2019-09-16 17:28:41 +00:00
|
|
|
std::istream * call(const Poco::URI uri_, Poco::Net::HTTPResponse & response)
|
2018-11-16 13:15:17 +00:00
|
|
|
{
|
|
|
|
// With empty path poco will send "POST HTTP/1.1" its bug.
|
|
|
|
if (uri.getPath().empty())
|
|
|
|
uri.setPath("/");
|
|
|
|
|
2019-09-16 17:28:41 +00:00
|
|
|
Poco::Net::HTTPRequest request(method, uri_.getPathAndQuery(), Poco::Net::HTTPRequest::HTTP_1_1);
|
|
|
|
request.setHost(uri_.getHost()); // use original, not resolved host name in header
|
2018-11-16 13:15:17 +00:00
|
|
|
|
|
|
|
if (out_stream_callback)
|
|
|
|
request.setChunkedTransferEncoding(true);
|
|
|
|
|
2019-09-25 08:08:46 +00:00
|
|
|
for (auto & http_header_entry: http_header_entries)
|
|
|
|
{
|
|
|
|
request.set(std::get<0>(http_header_entry), std::get<1>(http_header_entry));
|
|
|
|
}
|
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
if (!credentials.getUsername().empty())
|
|
|
|
credentials.authenticate(request);
|
|
|
|
|
2020-05-30 21:57:37 +00:00
|
|
|
LOG_TRACE((&Poco::Logger::get("ReadWriteBufferFromHTTP")), "Sending request to {}", uri.toString());
|
2018-11-16 13:15:17 +00:00
|
|
|
|
2019-09-18 10:38:00 +00:00
|
|
|
auto sess = session->getSession();
|
2019-09-16 17:28:41 +00:00
|
|
|
|
2019-06-05 09:23:41 +00:00
|
|
|
try
|
|
|
|
{
|
2019-09-20 12:36:09 +00:00
|
|
|
auto & stream_out = sess->sendRequest(request);
|
|
|
|
|
|
|
|
if (out_stream_callback)
|
|
|
|
out_stream_callback(stream_out);
|
|
|
|
|
2019-09-16 17:28:41 +00:00
|
|
|
istr = receiveResponse(*sess, request, response, true);
|
2019-09-18 13:38:17 +00:00
|
|
|
response.getCookies(cookies);
|
2018-11-16 13:15:17 +00:00
|
|
|
|
2019-09-16 17:28:41 +00:00
|
|
|
return istr;
|
2016-11-25 00:16:20 +00:00
|
|
|
|
2019-09-16 17:28:41 +00:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
|
|
|
/// We use session data storage as storage for exception text
|
|
|
|
/// Depend on it we can deduce to reconnect session or reresolve session host
|
|
|
|
sess->attachSessionData(e.message());
|
|
|
|
throw;
|
|
|
|
}
|
2019-09-18 10:38:00 +00:00
|
|
|
}
|
2018-11-16 13:15:17 +00:00
|
|
|
|
2019-09-16 17:28:41 +00:00
|
|
|
public:
|
|
|
|
using OutStreamCallback = std::function<void(std::ostream &)>;
|
|
|
|
|
2020-06-15 22:35:15 +00:00
|
|
|
explicit ReadWriteBufferFromHTTPBase(
|
|
|
|
UpdatableSessionPtr session_,
|
2019-09-16 17:28:41 +00:00
|
|
|
Poco::URI uri_,
|
|
|
|
const std::string & method_ = {},
|
|
|
|
OutStreamCallback out_stream_callback_ = {},
|
|
|
|
const Poco::Net::HTTPBasicCredentials & credentials_ = {},
|
2019-09-25 08:08:46 +00:00
|
|
|
size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE,
|
2019-10-23 11:58:35 +00:00
|
|
|
HTTPHeaderEntries http_header_entries_ = {},
|
|
|
|
const RemoteHostFilter & remote_host_filter_ = {})
|
2019-09-16 17:28:41 +00:00
|
|
|
: ReadBuffer(nullptr, 0)
|
|
|
|
, uri {uri_}
|
2019-09-18 14:53:45 +00:00
|
|
|
, method {!method_.empty() ? method_ : out_stream_callback_ ? Poco::Net::HTTPRequest::HTTP_POST : Poco::Net::HTTPRequest::HTTP_GET}
|
2019-09-16 17:28:41 +00:00
|
|
|
, session {session_}
|
2019-09-18 10:38:00 +00:00
|
|
|
, out_stream_callback {out_stream_callback_}
|
|
|
|
, credentials {credentials_}
|
2019-09-25 08:08:46 +00:00
|
|
|
, http_header_entries {http_header_entries_}
|
2019-10-23 11:58:35 +00:00
|
|
|
, remote_host_filter {remote_host_filter_}
|
2019-09-16 17:28:41 +00:00
|
|
|
{
|
|
|
|
Poco::Net::HTTPResponse response;
|
2019-09-18 10:38:00 +00:00
|
|
|
|
2019-09-16 17:28:41 +00:00
|
|
|
istr = call(uri, response);
|
|
|
|
|
|
|
|
while (isRedirect(response.getStatus()))
|
|
|
|
{
|
2019-09-18 10:38:00 +00:00
|
|
|
Poco::URI uri_redirect(response.get("Location"));
|
2019-10-23 11:58:35 +00:00
|
|
|
remote_host_filter.checkURL(uri_redirect);
|
2019-09-16 17:28:41 +00:00
|
|
|
|
|
|
|
session->updateSession(uri_redirect);
|
|
|
|
|
|
|
|
istr = call(uri_redirect,response);
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2019-06-05 09:23:41 +00:00
|
|
|
impl = std::make_unique<ReadBufferFromIStream>(*istr, buffer_size_);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
|
|
|
/// We use session data storage as storage for exception text
|
|
|
|
/// Depend on it we can deduce to reconnect session or reresolve session host
|
2019-09-18 10:38:00 +00:00
|
|
|
auto sess = session->getSession();
|
2019-09-16 17:28:41 +00:00
|
|
|
sess->attachSessionData(e.message());
|
2019-06-05 09:23:41 +00:00
|
|
|
throw;
|
|
|
|
}
|
2018-11-16 13:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool nextImpl() override
|
|
|
|
{
|
|
|
|
if (!impl->next())
|
|
|
|
return false;
|
|
|
|
internal_buffer = impl->buffer();
|
|
|
|
working_buffer = internal_buffer;
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-06 12:18:56 +00:00
|
|
|
|
|
|
|
std::string getResponseCookie(const std::string & name, const std::string & def) const
|
|
|
|
{
|
|
|
|
for (const auto & cookie : cookies)
|
|
|
|
if (cookie.getName() == name)
|
|
|
|
return cookie.getValue();
|
|
|
|
return def;
|
|
|
|
}
|
2018-11-16 13:15:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-16 17:28:41 +00:00
|
|
|
class UpdatableSession : public UpdatableSessionBase<HTTPSessionPtr>
|
2018-11-16 13:15:17 +00:00
|
|
|
{
|
2019-09-16 17:28:41 +00:00
|
|
|
using Parent = UpdatableSessionBase<HTTPSessionPtr>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit UpdatableSession(const Poco::URI uri,
|
|
|
|
const ConnectionTimeouts & timeouts_,
|
2020-07-16 22:08:44 +00:00
|
|
|
const UInt64 max_redirects_)
|
2019-09-18 10:38:00 +00:00
|
|
|
: Parent(uri, timeouts_, max_redirects_)
|
2019-09-16 17:28:41 +00:00
|
|
|
{
|
|
|
|
session = makeHTTPSession(initial_uri, timeouts);
|
|
|
|
}
|
|
|
|
|
2019-09-18 08:49:46 +00:00
|
|
|
void buildNewSession(const Poco::URI & uri) override
|
2019-09-16 17:28:41 +00:00
|
|
|
{
|
|
|
|
session = makeHTTPSession(uri, timeouts);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReadWriteBufferFromHTTP : public detail::ReadWriteBufferFromHTTPBase<std::shared_ptr<UpdatableSession>>
|
|
|
|
{
|
|
|
|
using Parent = detail::ReadWriteBufferFromHTTPBase<std::shared_ptr<UpdatableSession>>;
|
2018-11-16 13:15:17 +00:00
|
|
|
|
|
|
|
public:
|
2019-01-07 10:40:58 +00:00
|
|
|
explicit ReadWriteBufferFromHTTP(Poco::URI uri_,
|
2020-06-15 22:36:12 +00:00
|
|
|
const std::string & method_,
|
|
|
|
OutStreamCallback out_stream_callback_,
|
|
|
|
const ConnectionTimeouts & timeouts,
|
2020-07-16 22:08:44 +00:00
|
|
|
const UInt64 max_redirects = 0,
|
2019-09-16 17:28:41 +00:00
|
|
|
const Poco::Net::HTTPBasicCredentials & credentials_ = {},
|
2019-09-25 08:08:46 +00:00
|
|
|
size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE,
|
2019-10-23 11:58:35 +00:00
|
|
|
const HTTPHeaderEntries & http_header_entries_ = {},
|
|
|
|
const RemoteHostFilter & remote_host_filter_ = {})
|
|
|
|
: Parent(std::make_shared<UpdatableSession>(uri_, timeouts, max_redirects), uri_, method_, out_stream_callback_, credentials_, buffer_size_, http_header_entries_, remote_host_filter_)
|
2019-09-16 17:28:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class UpdatablePooledSession : public UpdatableSessionBase<PooledHTTPSessionPtr>
|
|
|
|
{
|
|
|
|
using Parent = UpdatableSessionBase<PooledHTTPSessionPtr>;
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t per_endpoint_pool_size;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit UpdatablePooledSession(const Poco::URI uri,
|
|
|
|
const ConnectionTimeouts & timeouts_,
|
2020-07-16 22:08:44 +00:00
|
|
|
const UInt64 max_redirects_,
|
2019-09-16 17:28:41 +00:00
|
|
|
size_t per_endpoint_pool_size_)
|
|
|
|
: Parent(uri, timeouts_, max_redirects_)
|
|
|
|
, per_endpoint_pool_size { per_endpoint_pool_size_ }
|
2018-11-16 13:15:17 +00:00
|
|
|
{
|
2019-09-16 17:28:41 +00:00
|
|
|
session = makePooledHTTPSession(initial_uri, timeouts, per_endpoint_pool_size);
|
|
|
|
}
|
|
|
|
|
2019-09-18 08:49:46 +00:00
|
|
|
void buildNewSession(const Poco::URI & uri) override
|
2019-09-16 17:28:41 +00:00
|
|
|
{
|
|
|
|
session = makePooledHTTPSession(uri, timeouts, per_endpoint_pool_size);
|
2018-11-16 13:15:17 +00:00
|
|
|
}
|
|
|
|
};
|
2019-09-16 17:28:41 +00:00
|
|
|
|
|
|
|
class PooledReadWriteBufferFromHTTP : public detail::ReadWriteBufferFromHTTPBase<std::shared_ptr<UpdatablePooledSession>>
|
2018-11-16 13:15:17 +00:00
|
|
|
{
|
2019-09-16 17:28:41 +00:00
|
|
|
using Parent = detail::ReadWriteBufferFromHTTPBase<std::shared_ptr<UpdatablePooledSession>>;
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
public:
|
2019-01-07 10:40:58 +00:00
|
|
|
explicit PooledReadWriteBufferFromHTTP(Poco::URI uri_,
|
|
|
|
const std::string & method_ = {},
|
2019-09-16 17:28:41 +00:00
|
|
|
OutStreamCallback out_stream_callback_ = {},
|
|
|
|
const ConnectionTimeouts & timeouts_ = {},
|
|
|
|
const Poco::Net::HTTPBasicCredentials & credentials_ = {},
|
2018-11-16 13:15:17 +00:00
|
|
|
size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE,
|
2020-07-16 22:08:44 +00:00
|
|
|
const UInt64 max_redirects = 0,
|
2018-11-16 13:33:43 +00:00
|
|
|
size_t max_connections_per_endpoint = DEFAULT_COUNT_OF_HTTP_CONNECTIONS_PER_ENDPOINT)
|
2019-09-16 17:28:41 +00:00
|
|
|
: Parent(std::make_shared<UpdatablePooledSession>(uri_, timeouts_, max_redirects, max_connections_per_endpoint),
|
2019-01-07 10:40:58 +00:00
|
|
|
uri_,
|
|
|
|
method_,
|
2019-09-16 17:28:41 +00:00
|
|
|
out_stream_callback_,
|
|
|
|
credentials_,
|
2018-11-16 13:15:17 +00:00
|
|
|
buffer_size_)
|
|
|
|
{
|
|
|
|
}
|
2016-11-19 00:07:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|