2012-10-29 07:19:47 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-02-11 21:40:51 +00:00
|
|
|
#include <experimental/optional>
|
|
|
|
|
2012-10-29 07:19:47 +00:00
|
|
|
#include <Poco/Net/HTTPServerResponse.h>
|
|
|
|
|
2015-10-05 01:35:28 +00:00
|
|
|
#include <DB/Common/Exception.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>
|
2015-10-05 01:26:43 +00:00
|
|
|
#include <DB/Common/NetException.h>
|
2012-10-29 07:19:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2016-02-11 21:40:51 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 07:19:47 +00:00
|
|
|
|
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).
|
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
|
|
|
|
2016-08-23 18:35:23 +00:00
|
|
|
bool add_cors_header;
|
2016-02-11 21:40:51 +00:00
|
|
|
bool compress;
|
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-07 16:11:30 +00:00
|
|
|
std::unique_ptr<WriteBufferFromOStream> out_raw;
|
|
|
|
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
|
|
|
|
|
|
|
void sendHeaders()
|
2012-10-29 07:19:47 +00:00
|
|
|
{
|
2017-01-07 16:11:30 +00:00
|
|
|
if (!out)
|
2016-02-11 21:40:51 +00:00
|
|
|
{
|
2016-08-23 18:35:23 +00:00
|
|
|
if (add_cors_header)
|
|
|
|
{
|
|
|
|
response.set("Access-Control-Allow-Origin","*");
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:11:30 +00:00
|
|
|
if (compress && offset()) /// Empty response need not be compressed.
|
2016-02-11 21:40:51 +00:00
|
|
|
{
|
2017-01-07 16:11:30 +00:00
|
|
|
if (compression_method == ZlibCompressionMethod::Gzip)
|
2016-02-11 21:40:51 +00:00
|
|
|
response.set("Content-Encoding", "gzip");
|
2017-01-07 16:11:30 +00:00
|
|
|
else if (compression_method == ZlibCompressionMethod::Zlib)
|
2016-02-11 21:40:51 +00:00
|
|
|
response.set("Content-Encoding", "deflate");
|
|
|
|
else
|
|
|
|
throw Exception("Logical error: unknown compression method passed to WriteBufferFromHTTPServerResponse",
|
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2017-01-07 16:11:30 +00:00
|
|
|
out_raw = std::make_unique<WriteBufferFromOStream>(response.send());
|
|
|
|
/// Use memory allocated for the outer buffer in the buffer pointed to by out. This avoids extra allocation and copy.
|
|
|
|
deflating_buf.emplace(*out_raw, compression_method, compression_level, working_buffer.size(), working_buffer.begin());
|
|
|
|
out = &deflating_buf.value();
|
2016-02-11 21:40:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-07 16:11:30 +00:00
|
|
|
out_raw = std::make_unique<WriteBufferFromOStream>(response.send(), working_buffer.size(), working_buffer.begin());
|
|
|
|
out = out_raw.get();
|
2016-02-11 21:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 01:54:23 +00:00
|
|
|
void nextImpl() override
|
2016-02-11 21:40:51 +00:00
|
|
|
{
|
2012-10-29 07:19:47 +00:00
|
|
|
if (!offset())
|
|
|
|
return;
|
2016-02-11 23:37:16 +00:00
|
|
|
|
2016-02-16 16:33:12 +00:00
|
|
|
sendHeaders();
|
|
|
|
|
2017-01-07 16:11:30 +00:00
|
|
|
out->position() = position();
|
|
|
|
out->next();
|
2012-10-29 07:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-02-11 21:40:51 +00:00
|
|
|
WriteBufferFromHTTPServerResponse(
|
|
|
|
Poco::Net::HTTPServerResponse & response_,
|
2017-01-07 16:11:30 +00:00
|
|
|
bool compress_ = false, /// If true - set Content-Encoding header and compress the result.
|
|
|
|
ZlibCompressionMethod compression_method_ = ZlibCompressionMethod::Gzip,
|
2016-02-11 21:40:51 +00:00
|
|
|
size_t size = DBMS_DEFAULT_BUFFER_SIZE)
|
|
|
|
: BufferWithOwnMemory<WriteBuffer>(size), response(response_),
|
|
|
|
compress(compress_), compression_method(compression_method_) {}
|
2012-10-29 07:19:47 +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.
|
2013-12-17 19:45:18 +00:00
|
|
|
void finalize()
|
|
|
|
{
|
2016-02-11 21:40:51 +00:00
|
|
|
sendHeaders();
|
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;
|
|
|
|
}
|
|
|
|
|
2012-10-29 07:19:47 +00:00
|
|
|
~WriteBufferFromHTTPServerResponse()
|
|
|
|
{
|
2013-11-28 20:05:54 +00:00
|
|
|
if (!offset())
|
|
|
|
return;
|
|
|
|
|
2013-11-18 17:17:45 +00:00
|
|
|
try
|
2012-11-19 04:16:00 +00:00
|
|
|
{
|
2012-10-29 07:19:47 +00:00
|
|
|
next();
|
2012-11-19 04:16:00 +00:00
|
|
|
}
|
2013-11-18 17:17:45 +00:00
|
|
|
catch (...)
|
|
|
|
{
|
2013-11-18 19:18:03 +00:00
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
2013-11-18 17:17:45 +00:00
|
|
|
}
|
2012-10-29 07:19:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|