2016-11-19 00:07:58 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-02-07 15:38:57 +00:00
|
|
|
#include <functional>
|
2018-11-16 13:15:17 +00:00
|
|
|
#include <Core/Types.h>
|
|
|
|
#include <IO/ConnectionTimeouts.h>
|
|
|
|
#include <IO/HTTPCommon.h>
|
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/ReadBufferFromIStream.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>
|
|
|
|
#include <Common/config.h>
|
|
|
|
#include <common/logger_useful.h>
|
|
|
|
|
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
|
|
|
|
|
|
|
namespace detail
|
2016-11-19 00:07:58 +00:00
|
|
|
{
|
2018-11-16 13:15:17 +00:00
|
|
|
template <typename SessionPtr>
|
|
|
|
class ReadWriteBufferFromHTTPBase : public ReadBuffer
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
Poco::URI uri;
|
|
|
|
std::string method;
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
SessionPtr session;
|
|
|
|
std::istream * istr; /// owned by session
|
|
|
|
std::unique_ptr<ReadBuffer> impl;
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
public:
|
|
|
|
using OutStreamCallback = std::function<void(std::ostream &)>;
|
|
|
|
|
|
|
|
explicit ReadWriteBufferFromHTTPBase(SessionPtr session_,
|
|
|
|
Poco::URI uri,
|
|
|
|
const std::string & method = {},
|
|
|
|
OutStreamCallback out_stream_callback = {},
|
|
|
|
const Poco::Net::HTTPBasicCredentials & credentials = {},
|
|
|
|
size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE)
|
|
|
|
: ReadBuffer(nullptr, 0)
|
|
|
|
, uri {uri}
|
|
|
|
, method {!method.empty() ? method : out_stream_callback ? Poco::Net::HTTPRequest::HTTP_POST : Poco::Net::HTTPRequest::HTTP_GET}
|
|
|
|
, session {std::move(session_)}
|
|
|
|
{
|
|
|
|
// With empty path poco will send "POST HTTP/1.1" its bug.
|
|
|
|
if (uri.getPath().empty())
|
|
|
|
uri.setPath("/");
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if (out_stream_callback)
|
|
|
|
request.setChunkedTransferEncoding(true);
|
|
|
|
|
|
|
|
if (!credentials.getUsername().empty())
|
|
|
|
credentials.authenticate(request);
|
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
|
|
|
|
LOG_TRACE((&Logger::get("ReadWriteBufferFromHTTP")), "Sending request to " << uri.toString());
|
|
|
|
|
|
|
|
auto & stream_out = session->sendRequest(request);
|
|
|
|
|
|
|
|
if (out_stream_callback)
|
|
|
|
out_stream_callback(stream_out);
|
2016-11-25 00:16:20 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
istr = receiveResponse(*session, request, response);
|
|
|
|
|
|
|
|
impl = std::make_unique<ReadBufferFromIStream>(*istr, buffer_size_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool nextImpl() override
|
|
|
|
{
|
|
|
|
if (!impl->next())
|
|
|
|
return false;
|
|
|
|
internal_buffer = impl->buffer();
|
|
|
|
working_buffer = internal_buffer;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class ReadWriteBufferFromHTTP : public detail::ReadWriteBufferFromHTTPBase<HTTPSessionPtr>
|
|
|
|
{
|
|
|
|
using Parent = detail::ReadWriteBufferFromHTTPBase<HTTPSessionPtr>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ReadWriteBufferFromHTTP(Poco::URI uri,
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::string & method = {},
|
|
|
|
OutStreamCallback out_stream_callback = {},
|
2017-12-27 17:58:52 +00:00
|
|
|
const ConnectionTimeouts & timeouts = {},
|
2018-07-26 15:10:57 +00:00
|
|
|
const Poco::Net::HTTPBasicCredentials & credentials = {},
|
2018-11-16 13:15:17 +00:00
|
|
|
size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE)
|
|
|
|
: Parent(makeHTTPSession(uri, timeouts), uri, method, out_stream_callback, credentials, buffer_size_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
class PooledReadWriteBufferFromHTTP : public detail::ReadWriteBufferFromHTTPBase<PooledHTTPSessionPtr>
|
|
|
|
{
|
|
|
|
using Parent = detail::ReadWriteBufferFromHTTPBase<PooledHTTPSessionPtr>;
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
public:
|
|
|
|
explicit PooledReadWriteBufferFromHTTP(Poco::URI uri,
|
|
|
|
const std::string & method = {},
|
|
|
|
OutStreamCallback out_stream_callback = {},
|
|
|
|
const ConnectionTimeouts & timeouts = {},
|
|
|
|
const Poco::Net::HTTPBasicCredentials & credentials = {},
|
|
|
|
size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE,
|
2018-11-16 13:33:43 +00:00
|
|
|
size_t max_connections_per_endpoint = DEFAULT_COUNT_OF_HTTP_CONNECTIONS_PER_ENDPOINT)
|
2018-11-16 13:15:17 +00:00
|
|
|
: Parent(makePooledHTTPSession(uri, timeouts, max_connections_per_endpoint),
|
|
|
|
uri,
|
|
|
|
method,
|
|
|
|
out_stream_callback,
|
|
|
|
credentials,
|
|
|
|
buffer_size_)
|
|
|
|
{
|
|
|
|
}
|
2016-11-19 00:07:58 +00:00
|
|
|
};
|
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
|
2016-11-19 00:07:58 +00:00
|
|
|
}
|