2017-04-01 09:19:00 +00:00
|
|
|
#include <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-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/HTTPCommon.h>
|
|
|
|
#include <Common/NetException.h>
|
|
|
|
#include <Common/Stopwatch.h>
|
|
|
|
#include <Core/Progress.h>
|
2017-01-30 05:13:58 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2017-01-30 05:13:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromHTTPServerResponse::startSendHeaders()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!headers_started_sending)
|
|
|
|
{
|
|
|
|
headers_started_sending = true;
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (add_cors_header)
|
|
|
|
response.set("Access-Control-Allow-Origin", "*");
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
setResponseDefaultHeaders(response);
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-02-07 19:45:55 +00:00
|
|
|
#if POCO_CLICKHOUSE_PATCH
|
2017-09-01 21:46:12 +00:00
|
|
|
if (request.getMethod() != Poco::Net::HTTPRequest::HTTP_HEAD)
|
|
|
|
std::tie(response_header_ostr, response_body_ostr) = response.beginSend();
|
2017-02-07 19:45:55 +00:00
|
|
|
#endif
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-01-30 05:13:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromHTTPServerResponse::finishSendHeaders()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!headers_finished_sending)
|
|
|
|
{
|
|
|
|
headers_finished_sending = true;
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-09-01 21:46:12 +00:00
|
|
|
if (request.getMethod() != Poco::Net::HTTPRequest::HTTP_HEAD) {
|
2017-02-07 19:45:55 +00:00
|
|
|
#if POCO_CLICKHOUSE_PATCH
|
2017-09-01 21:46:12 +00:00
|
|
|
/// Send end of headers delimiter.
|
|
|
|
if (response_header_ostr)
|
|
|
|
*response_header_ostr << "\r\n" << std::flush;
|
2017-02-07 19:45:55 +00:00
|
|
|
#else
|
2017-09-01 21:46:12 +00:00
|
|
|
/// Newline autosent by response.send()
|
|
|
|
/// if nothing to send in body:
|
|
|
|
if (!response_body_ostr)
|
|
|
|
response_body_ostr = &(response.send());
|
2017-02-07 19:45:55 +00:00
|
|
|
#endif
|
2017-09-01 21:46:12 +00:00
|
|
|
} else {
|
|
|
|
if (!response_body_ostr)
|
|
|
|
response_body_ostr = &(response.send());
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-01-30 05:13:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromHTTPServerResponse::nextImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
startSendHeaders();
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-09-01 21:46:12 +00:00
|
|
|
if (!out && request.getMethod() != Poco::Net::HTTPRequest::HTTP_HEAD)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (compress)
|
|
|
|
{
|
|
|
|
if (compression_method == ZlibCompressionMethod::Gzip)
|
|
|
|
{
|
2017-02-07 19:45:55 +00:00
|
|
|
#if POCO_CLICKHOUSE_PATCH
|
2017-04-01 07:20:54 +00:00
|
|
|
*response_header_ostr << "Content-Encoding: gzip\r\n";
|
2017-02-07 19:45:55 +00:00
|
|
|
#else
|
2017-04-01 07:20:54 +00:00
|
|
|
response.set("Content-Encoding", "gzip");
|
2017-02-07 19:45:55 +00:00
|
|
|
#endif
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else if (compression_method == ZlibCompressionMethod::Zlib)
|
|
|
|
{
|
2017-02-07 19:45:55 +00:00
|
|
|
#if POCO_CLICKHOUSE_PATCH
|
2017-04-01 07:20:54 +00:00
|
|
|
*response_header_ostr << "Content-Encoding: deflate\r\n";
|
2017-02-07 19:45:55 +00:00
|
|
|
#else
|
2017-04-01 07:20:54 +00:00
|
|
|
response.set("Content-Encoding", "deflate");
|
2017-02-07 19:45:55 +00:00
|
|
|
#endif
|
2017-04-01 07:20:54 +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
|
2017-04-01 07:20:54 +00:00
|
|
|
response_body_ostr = &(response.send());
|
2017-02-07 19:45:55 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-01 07:20:54 +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
|
2017-04-01 07:20:54 +00:00
|
|
|
response_body_ostr = &(response.send());
|
2017-02-07 19:45:55 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out_raw.emplace(*response_body_ostr, working_buffer.size(), working_buffer.begin());
|
|
|
|
out = &out_raw.value();
|
|
|
|
}
|
|
|
|
}
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
finishSendHeaders();
|
2017-02-07 19:45:55 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-09-01 21:46:12 +00:00
|
|
|
if (out) {
|
|
|
|
out->position() = position();
|
|
|
|
out->next();
|
|
|
|
}
|
2017-01-30 05:13:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WriteBufferFromHTTPServerResponse::WriteBufferFromHTTPServerResponse(
|
2017-09-01 21:46:12 +00:00
|
|
|
Poco::Net::HTTPServerRequest & request_,
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Net::HTTPServerResponse & response_,
|
|
|
|
bool compress_,
|
|
|
|
ZlibCompressionMethod compression_method_,
|
|
|
|
size_t size)
|
2017-09-01 21:46:12 +00:00
|
|
|
: BufferWithOwnMemory<WriteBuffer>(size), request(request_), response(response_),
|
2017-04-01 07:20:54 +00:00
|
|
|
compress(compress_), compression_method(compression_method_)
|
2017-01-30 05:13:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromHTTPServerResponse::onProgress(const Progress & progress)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Cannot add new headers if body was started to send.
|
|
|
|
if (headers_finished_sending)
|
|
|
|
return;
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
accumulated_progress.incrementPiecewiseAtomically(progress);
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (progress_watch.elapsed() >= send_progress_interval_ms * 1000000)
|
|
|
|
{
|
|
|
|
progress_watch.restart();
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Send all common headers before our special progress headers.
|
|
|
|
startSendHeaders();
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
WriteBufferFromOwnString progress_string_writer;
|
|
|
|
accumulated_progress.writeJSON(progress_string_writer);
|
2017-01-30 05:13:58 +00:00
|
|
|
|
2017-02-07 19:45:55 +00:00
|
|
|
#if POCO_CLICKHOUSE_PATCH
|
2017-07-31 21:39:24 +00:00
|
|
|
*response_header_ostr << "X-ClickHouse-Progress: " << progress_string_writer.str() << "\r\n" << std::flush;
|
2017-02-07 19:45:55 +00:00
|
|
|
#endif
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-01-30 05:13:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromHTTPServerResponse::finalize()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (offset())
|
|
|
|
{
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// If no remaining data, just send headers.
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
startSendHeaders();
|
|
|
|
finishSendHeaders();
|
|
|
|
}
|
2017-01-30 05:13:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WriteBufferFromHTTPServerResponse::~WriteBufferFromHTTPServerResponse()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
finalize();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
2017-01-30 05:13:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|