2012-10-29 07:19:47 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-02-11 21:40:51 +00:00
|
|
|
#include <experimental/optional>
|
2017-01-22 15:03:55 +00:00
|
|
|
#include <mutex>
|
2017-02-07 19:45:55 +00:00
|
|
|
#include <Poco/Version.h>
|
2012-10-29 07:19:47 +00:00
|
|
|
#include <DB/IO/WriteBuffer.h>
|
|
|
|
#include <DB/IO/BufferWithOwnMemory.h>
|
2017-01-07 16:11:30 +00:00
|
|
|
#include <DB/IO/WriteBufferFromOStream.h>
|
|
|
|
#include <DB/IO/ZlibDeflatingWriteBuffer.h>
|
2016-12-30 20:52:56 +00:00
|
|
|
#include <DB/IO/HTTPCommon.h>
|
2015-10-05 01:26:43 +00:00
|
|
|
#include <DB/Common/NetException.h>
|
2017-01-22 16:34:17 +00:00
|
|
|
#include <DB/Common/Stopwatch.h>
|
2017-01-22 15:03:55 +00:00
|
|
|
#include <DB/Core/Progress.h>
|
2012-10-29 07:19:47 +00:00
|
|
|
|
|
|
|
|
2017-01-30 05:13:58 +00:00
|
|
|
namespace Poco
|
2016-01-11 21:46:36 +00:00
|
|
|
{
|
2017-01-30 05:13:58 +00:00
|
|
|
namespace Net
|
|
|
|
{
|
|
|
|
class HTTPServerResponse;
|
|
|
|
}
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 07:19:47 +00:00
|
|
|
|
2017-01-30 05:13:58 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-01-07 16:11:30 +00:00
|
|
|
/// The difference from WriteBufferFromOStream is that this buffer gets the underlying std::ostream
|
|
|
|
/// (using response.send()) only after data is flushed for the first time. This is needed in HTTP
|
|
|
|
/// servers to change some HTTP headers (e.g. response code) before any data is sent to the client
|
|
|
|
/// (headers can't be changed after response.send() is called).
|
|
|
|
///
|
|
|
|
/// In short, it allows delaying the call to response.send().
|
|
|
|
///
|
|
|
|
/// Additionally, supports HTTP response compression (in this case corresponding Content-Encoding
|
|
|
|
/// header will be set).
|
2017-01-29 22:53:29 +00:00
|
|
|
///
|
|
|
|
/// Also this class write and flush special X-ClickHouse-Progress HTTP headers
|
|
|
|
/// if no data was sent at the time of progress notification.
|
|
|
|
/// This allows to implement progress bar in HTTP clients.
|
2012-10-29 07:19:47 +00:00
|
|
|
class WriteBufferFromHTTPServerResponse : public BufferWithOwnMemory<WriteBuffer>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
Poco::Net::HTTPServerResponse & response;
|
2017-01-07 16:11:30 +00:00
|
|
|
|
2017-01-29 22:53:29 +00:00
|
|
|
bool add_cors_header = false;
|
|
|
|
bool compress = false;
|
2017-01-07 16:11:30 +00:00
|
|
|
ZlibCompressionMethod compression_method;
|
2016-02-12 02:26:04 +00:00
|
|
|
int compression_level = Z_DEFAULT_COMPRESSION;
|
2012-10-29 07:19:47 +00:00
|
|
|
|
2017-01-22 15:42:42 +00:00
|
|
|
std::ostream * response_body_ostr = nullptr;
|
2017-02-07 19:45:55 +00:00
|
|
|
|
|
|
|
#if POCO_CLICKHOUSE_PATCH
|
2017-01-22 15:42:42 +00:00
|
|
|
std::ostream * response_header_ostr = nullptr;
|
2017-02-07 19:45:55 +00:00
|
|
|
#endif
|
2017-01-22 15:42:42 +00:00
|
|
|
|
2017-01-22 15:03:55 +00:00
|
|
|
std::experimental::optional<WriteBufferFromOStream> out_raw;
|
2017-01-07 16:11:30 +00:00
|
|
|
std::experimental::optional<ZlibDeflatingWriteBuffer> deflating_buf;
|
|
|
|
|
|
|
|
WriteBuffer * out = nullptr; /// Uncompressed HTTP body is written to this buffer. Points to out_raw or possibly to deflating_buf.
|
2016-02-11 21:40:51 +00:00
|
|
|
|
2017-01-22 17:09:00 +00:00
|
|
|
bool headers_started_sending = false;
|
|
|
|
bool headers_finished_sending = false; /// If true, you could not add any headers.
|
2017-01-22 15:03:55 +00:00
|
|
|
|
2017-01-22 16:19:24 +00:00
|
|
|
Progress accumulated_progress;
|
2017-01-22 16:34:17 +00:00
|
|
|
size_t send_progress_interval_ms = 100;
|
|
|
|
Stopwatch progress_watch;
|
2017-01-22 16:19:24 +00:00
|
|
|
|
2017-01-22 15:03:55 +00:00
|
|
|
std::mutex mutex; /// progress callback could be called from different threads.
|
|
|
|
|
|
|
|
|
|
|
|
/// Must be called under locked mutex.
|
2017-01-22 15:42:42 +00:00
|
|
|
/// This method send headers, if this was not done already,
|
|
|
|
/// but not finish them with \r\n, allowing to send more headers subsequently.
|
2017-01-30 05:13:58 +00:00
|
|
|
void startSendHeaders();
|
2016-02-11 21:40:51 +00:00
|
|
|
|
2017-01-22 17:09:00 +00:00
|
|
|
/// This method finish headers with \r\n, allowing to start to send body.
|
2017-01-30 05:13:58 +00:00
|
|
|
void finishSendHeaders();
|
2017-01-22 17:09:00 +00:00
|
|
|
|
2017-01-30 05:13:58 +00:00
|
|
|
void nextImpl() override;
|
2012-10-29 07:19:47 +00:00
|
|
|
|
2017-01-22 15:13:50 +00:00
|
|
|
public:
|
|
|
|
WriteBufferFromHTTPServerResponse(
|
|
|
|
Poco::Net::HTTPServerResponse & response_,
|
|
|
|
bool compress_ = false, /// If true - set Content-Encoding header and compress the result.
|
|
|
|
ZlibCompressionMethod compression_method_ = ZlibCompressionMethod::Gzip,
|
2017-01-30 05:13:58 +00:00
|
|
|
size_t size = DBMS_DEFAULT_BUFFER_SIZE);
|
2017-01-22 15:13:50 +00:00
|
|
|
|
2017-01-22 15:03:55 +00:00
|
|
|
/// Writes progess in repeating HTTP headers.
|
2017-01-30 05:13:58 +00:00
|
|
|
void onProgress(const Progress & progress);
|
2017-01-22 15:03:55 +00:00
|
|
|
|
2017-01-07 16:11:30 +00:00
|
|
|
/// Send at least HTTP headers if no data has been sent yet.
|
|
|
|
/// Use after the data has possibly been sent and no error happened (and thus you do not plan
|
|
|
|
/// to change response HTTP code.
|
2017-01-23 21:21:38 +00:00
|
|
|
/// This method is idempotent.
|
2017-01-30 05:13:58 +00:00
|
|
|
void finalize();
|
2013-12-17 19:45:18 +00:00
|
|
|
|
2017-01-07 16:11:30 +00:00
|
|
|
/// Turn compression on or off.
|
|
|
|
/// The setting has any effect only if HTTP headers haven't been sent yet.
|
2016-02-19 19:02:20 +00:00
|
|
|
void setCompression(bool enable_compression)
|
|
|
|
{
|
|
|
|
compress = enable_compression;
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:11:30 +00:00
|
|
|
/// Set compression level if the compression is turned on.
|
|
|
|
/// The setting has any effect only if HTTP headers haven't been sent yet.
|
2016-02-12 02:26:04 +00:00
|
|
|
void setCompressionLevel(int level)
|
|
|
|
{
|
|
|
|
compression_level = level;
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:11:30 +00:00
|
|
|
/// Turn CORS on or off.
|
|
|
|
/// The setting has any effect only if HTTP headers haven't been sent yet.
|
2016-08-23 18:35:23 +00:00
|
|
|
void addHeaderCORS(bool enable_cors)
|
|
|
|
{
|
|
|
|
add_cors_header = enable_cors;
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:34:17 +00:00
|
|
|
/// Don't send HTTP headers with progress more frequently.
|
|
|
|
void setSendProgressInterval(size_t send_progress_interval_ms_)
|
|
|
|
{
|
|
|
|
send_progress_interval_ms = send_progress_interval_ms_;
|
|
|
|
}
|
|
|
|
|
2017-01-30 05:13:58 +00:00
|
|
|
~WriteBufferFromHTTPServerResponse();
|
2012-10-29 07:19:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|