2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadWriteBufferFromHTTP.h>
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/SimpleCache.h>
|
2017-04-08 01:32:05 +00:00
|
|
|
#include <Common/config.h>
|
|
|
|
#include <Core/Types.h>
|
2017-04-18 10:17:37 +00:00
|
|
|
#include <IO/ReadBufferFromIStream.h>
|
|
|
|
#include <Poco/Net/DNS.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <Poco/Version.h>
|
2016-11-19 00:07:58 +00:00
|
|
|
#include <common/logger_useful.h>
|
|
|
|
|
2017-03-28 20:30:57 +00:00
|
|
|
#if Poco_NetSSL_FOUND
|
|
|
|
#include <Poco/Net/HTTPSClientSession.h>
|
|
|
|
#endif
|
|
|
|
|
2017-04-08 01:32:05 +00:00
|
|
|
|
2016-11-19 00:07:58 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int RECEIVED_ERROR_FROM_REMOTE_IO_SERVER;
|
2017-04-06 13:03:23 +00:00
|
|
|
extern const int RECEIVED_ERROR_TOO_MANY_REQUESTS;
|
2016-11-19 00:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Poco::Net::IPAddress resolveHostImpl(const String & host)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return Poco::Net::DNS::resolveOne(host);
|
2016-11-19 00:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Poco::Net::IPAddress resolveHost(const String & host)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static SimpleCache<decltype(resolveHostImpl), &resolveHostImpl> cache;
|
|
|
|
return cache(host);
|
2016-11-19 00:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 10:17:37 +00:00
|
|
|
ReadWriteBufferFromHTTP::ReadWriteBufferFromHTTP(const Poco::URI & uri,
|
2017-07-11 18:59:01 +00:00
|
|
|
const std::string & method_,
|
2017-04-18 10:17:37 +00:00
|
|
|
OutStreamCallback out_stream_callback,
|
|
|
|
size_t buffer_size_,
|
|
|
|
const HTTPTimeouts & timeouts)
|
|
|
|
: ReadBuffer(nullptr, 0),
|
|
|
|
uri{uri},
|
2017-07-11 18:59:01 +00:00
|
|
|
method{!method_.empty() ? method_ : out_stream_callback ? Poco::Net::HTTPRequest::HTTP_POST : Poco::Net::HTTPRequest::HTTP_GET},
|
2017-04-18 10:17:37 +00:00
|
|
|
timeouts{timeouts},
|
|
|
|
is_ssl{uri.getScheme() == "https"},
|
|
|
|
session
|
|
|
|
{
|
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession>(
|
2017-03-28 20:30:57 +00:00
|
|
|
#if Poco_NetSSL_FOUND
|
2017-04-01 07:20:54 +00:00
|
|
|
is_ssl ? new Poco::Net::HTTPSClientSession :
|
2017-03-28 20:30:57 +00:00
|
|
|
#endif
|
2017-04-18 10:17:37 +00:00
|
|
|
new Poco::Net::HTTPClientSession)
|
|
|
|
}
|
2016-11-19 00:07:58 +00:00
|
|
|
{
|
2017-04-18 10:17:37 +00:00
|
|
|
session->setHost(resolveHost(uri.getHost()).toString()); /// Cache DNS forever (until server restart)
|
2017-04-01 07:20:54 +00:00
|
|
|
session->setPort(uri.getPort());
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2017-01-13 11:25:44 +00:00
|
|
|
#if POCO_CLICKHOUSE_PATCH || POCO_VERSION >= 0x02000000
|
2017-04-01 07:20:54 +00:00
|
|
|
session->setTimeout(timeouts.connection_timeout, timeouts.send_timeout, timeouts.receive_timeout);
|
2017-01-13 11:25:44 +00:00
|
|
|
#else
|
2017-04-01 07:20:54 +00:00
|
|
|
session->setTimeout(timeouts.connection_timeout);
|
2017-01-13 11:25:44 +00:00
|
|
|
#endif
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +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
|
2017-02-27 21:09:57 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (out_stream_callback)
|
|
|
|
request.setChunkedTransferEncoding(true);
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Net::HTTPResponse response;
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
LOG_TRACE((&Logger::get("ReadWriteBufferFromHTTP")), "Sending request to " << uri.toString());
|
2016-11-24 19:57:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto & stream_out = session->sendRequest(request);
|
2016-11-22 15:03:54 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (out_stream_callback)
|
|
|
|
out_stream_callback(stream_out);
|
2016-11-22 15:03:54 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
istr = &session->receiveResponse(response);
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto status = response.getStatus();
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (status != Poco::Net::HTTPResponse::HTTP_OK)
|
|
|
|
{
|
|
|
|
std::stringstream error_message;
|
2017-04-18 10:17:37 +00:00
|
|
|
error_message << "Received error from remote server " << uri.toString() << ". HTTP status code: " << status << " "
|
|
|
|
<< response.getReason() << ", body: " << istr->rdbuf();
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2017-04-18 10:17:37 +00:00
|
|
|
throw Exception(error_message.str(),
|
|
|
|
status == HTTP_TOO_MANY_REQUESTS ? ErrorCodes::RECEIVED_ERROR_TOO_MANY_REQUESTS
|
|
|
|
: ErrorCodes::RECEIVED_ERROR_FROM_REMOTE_IO_SERVER);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-11-19 00:07:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
impl = std::make_unique<ReadBufferFromIStream>(*istr, buffer_size_);
|
2016-11-19 00:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ReadWriteBufferFromHTTP::nextImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!impl->next())
|
|
|
|
return false;
|
|
|
|
internal_buffer = impl->buffer();
|
|
|
|
working_buffer = internal_buffer;
|
|
|
|
return true;
|
2016-11-19 00:07:58 +00:00
|
|
|
}
|
|
|
|
}
|