2017-02-07 19:45:55 +00:00
|
|
|
#include <DB/IO/WriteBufferFromHTTPServerResponse.h>
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-02-07 19:45:55 +00:00
|
|
|
#include <Poco/Version.h>
|
|
|
|
#include <Poco/Net/HTTPServerResponse.h>
|
2017-01-30 05:13:58 +00:00
|
|
|
#include <DB/Common/Exception.h>
|
|
|
|
#include <DB/IO/WriteBufferFromString.h>
|
|
|
|
#include <DB/IO/HTTPCommon.h>
|
|
|
|
#include <DB/Common/NetException.h>
|
|
|
|
#include <DB/Common/Stopwatch.h>
|
|
|
|
#include <DB/Core/Progress.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromHTTPServerResponse::startSendHeaders()
|
|
|
|
{
|
|
|
|
if (!headers_started_sending)
|
|
|
|
{
|
|
|
|
headers_started_sending = true;
|
|
|
|
|
|
|
|
if (add_cors_header)
|
|
|
|
response.set("Access-Control-Allow-Origin", "*");
|
|
|
|
|
|
|
|
setResponseDefaultHeaders(response);
|
|
|
|
|
2017-02-07 19:45:55 +00:00
|
|
|
#if POCO_CLICKHOUSE_PATCH
|
2017-01-30 05:13:58 +00:00
|
|
|
std::tie(response_header_ostr, response_body_ostr) = response.beginSend();
|
2017-02-07 19:45:55 +00:00
|
|
|
#endif
|
2017-01-30 05:13:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromHTTPServerResponse::finishSendHeaders()
|
|
|
|
{
|
|
|
|
if (!headers_finished_sending)
|
|
|
|
{
|
|
|
|
headers_finished_sending = true;
|
|
|
|
|
2017-02-07 19:45:55 +00:00
|
|
|
#if POCO_CLICKHOUSE_PATCH
|
2017-01-30 05:13:58 +00:00
|
|
|
/// Send end of headers delimiter.
|
|
|
|
*response_header_ostr << "\r\n" << std::flush;
|
2017-02-07 19:45:55 +00:00
|
|
|
#else
|
|
|
|
/// Newline autosent by response.send()
|
|
|
|
#endif
|
2017-01-30 05:13:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromHTTPServerResponse::nextImpl()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
startSendHeaders();
|
|
|
|
|
|
|
|
if (!out)
|
|
|
|
{
|
|
|
|
if (compress)
|
|
|
|
{
|
|
|
|
if (compression_method == ZlibCompressionMethod::Gzip)
|
2017-02-07 19:45:55 +00:00
|
|
|
{
|
|
|
|
#if POCO_CLICKHOUSE_PATCH
|
2017-01-30 05:13:58 +00:00
|
|
|
*response_header_ostr << "Content-Encoding: gzip\r\n";
|
2017-02-07 19:45:55 +00:00
|
|
|
#else
|
|
|
|
response.set("Content-Encoding", "gzip");
|
|
|
|
#endif
|
|
|
|
}
|
2017-01-30 05:13:58 +00:00
|
|
|
else if (compression_method == ZlibCompressionMethod::Zlib)
|
2017-02-07 19:45:55 +00:00
|
|
|
{
|
|
|
|
#if POCO_CLICKHOUSE_PATCH
|
2017-01-30 05:13:58 +00:00
|
|
|
*response_header_ostr << "Content-Encoding: deflate\r\n";
|
2017-02-07 19:45:55 +00:00
|
|
|
#else
|
|
|
|
response.set("Content-Encoding", "deflate");
|
|
|
|
#endif
|
|
|
|
}
|
2017-01-30 05:13:58 +00:00
|
|
|
else
|
|
|
|
throw Exception("Logical error: unknown compression method passed to WriteBufferFromHTTPServerResponse",
|
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
|
|
|
/// Use memory allocated for the outer buffer in the buffer pointed to by out. This avoids extra allocation and copy.
|
2017-02-07 19:45:55 +00:00
|
|
|
|
|
|
|
#if !POCO_CLICKHOUSE_PATCH
|
|
|
|
response_body_ostr = &(response.send());
|
|
|
|
#endif
|
|
|
|
|
2017-01-30 05:13:58 +00:00
|
|
|
out_raw.emplace(*response_body_ostr);
|
|
|
|
deflating_buf.emplace(out_raw.value(), compression_method, compression_level, working_buffer.size(), working_buffer.begin());
|
|
|
|
out = &deflating_buf.value();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-07 19:45:55 +00:00
|
|
|
#if !POCO_CLICKHOUSE_PATCH
|
|
|
|
response_body_ostr = &(response.send());
|
|
|
|
#endif
|
|
|
|
|
2017-01-30 05:13:58 +00:00
|
|
|
out_raw.emplace(*response_body_ostr, working_buffer.size(), working_buffer.begin());
|
|
|
|
out = &out_raw.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
finishSendHeaders();
|
2017-02-07 19:45:55 +00:00
|
|
|
|
2017-01-30 05:13:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out->position() = position();
|
|
|
|
out->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WriteBufferFromHTTPServerResponse::WriteBufferFromHTTPServerResponse(
|
|
|
|
Poco::Net::HTTPServerResponse & response_,
|
|
|
|
bool compress_,
|
|
|
|
ZlibCompressionMethod compression_method_,
|
|
|
|
size_t size)
|
|
|
|
: BufferWithOwnMemory<WriteBuffer>(size), response(response_),
|
|
|
|
compress(compress_), compression_method(compression_method_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromHTTPServerResponse::onProgress(const Progress & progress)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
/// Cannot add new headers if body was started to send.
|
|
|
|
if (headers_finished_sending)
|
|
|
|
return;
|
|
|
|
|
|
|
|
accumulated_progress.incrementPiecewiseAtomically(progress);
|
|
|
|
|
|
|
|
if (progress_watch.elapsed() >= send_progress_interval_ms * 1000000)
|
|
|
|
{
|
|
|
|
progress_watch.restart();
|
|
|
|
|
|
|
|
/// Send all common headers before our special progress headers.
|
|
|
|
startSendHeaders();
|
|
|
|
|
|
|
|
std::string progress_string;
|
|
|
|
{
|
|
|
|
WriteBufferFromString progress_string_writer(progress_string);
|
|
|
|
accumulated_progress.writeJSON(progress_string_writer);
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:45:55 +00:00
|
|
|
#if POCO_CLICKHOUSE_PATCH
|
2017-01-30 05:13:58 +00:00
|
|
|
*response_header_ostr << "X-ClickHouse-Progress: " << progress_string << "\r\n" << std::flush;
|
2017-02-07 19:45:55 +00:00
|
|
|
#endif
|
2017-01-30 05:13:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromHTTPServerResponse::finalize()
|
|
|
|
{
|
|
|
|
if (offset())
|
|
|
|
{
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// If no remaining data, just send headers.
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
startSendHeaders();
|
|
|
|
finishSendHeaders();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WriteBufferFromHTTPServerResponse::~WriteBufferFromHTTPServerResponse()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
finalize();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|