ClickHouse/src/Server/StaticRequestHandler.cpp

182 lines
7.2 KiB
C++
Raw Normal View History

2020-04-04 08:57:16 +00:00
#include "StaticRequestHandler.h"
2020-10-20 00:28:33 +00:00
#include "IServer.h"
2020-04-04 08:57:16 +00:00
#include "HTTPHandlerFactory.h"
#include "HTTPHandlerRequestFilter.h"
#include <IO/HTTPCommon.h>
#include <IO/ReadBufferFromFile.h>
#include <IO/WriteBufferFromString.h>
#include <IO/copyData.h>
2020-04-26 11:06:14 +00:00
#include <IO/WriteHelpers.h>
#include <Server/HTTP/WriteBufferFromHTTPServerResponse.h>
#include <Interpreters/Context.h>
2020-04-04 08:57:16 +00:00
#include <Common/Exception.h>
#include <Poco/Path.h>
#include <Poco/File.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Net/HTTPRequestHandlerFactory.h>
2020-10-20 00:28:33 +00:00
#include <Poco/Util/LayeredConfiguration.h>
2020-04-04 08:57:16 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int INCORRECT_FILE_NAME;
2020-04-26 11:06:14 +00:00
extern const int HTTP_LENGTH_REQUIRED;
2020-04-04 08:57:16 +00:00
extern const int INVALID_CONFIG_PARAMETER;
}
static inline WriteBufferPtr
responseWriteBuffer(HTTPServerRequest & request, HTTPServerResponse & response, unsigned int keep_alive_timeout)
2020-04-26 11:06:14 +00:00
{
/// The client can pass a HTTP header indicating supported compression method (gzip or deflate).
String http_response_compression_methods = request.get("Accept-Encoding", "");
CompressionMethod http_response_compression_method = CompressionMethod::None;
if (!http_response_compression_methods.empty())
{
/// If client supports brotli - it's preferred.
/// Both gzip and deflate are supported. If the client supports both, gzip is preferred.
/// NOTE parsing of the list of methods is slightly incorrect.
if (std::string::npos != http_response_compression_methods.find("br"))
http_response_compression_method = CompressionMethod::Brotli;
else if (std::string::npos != http_response_compression_methods.find("gzip"))
http_response_compression_method = CompressionMethod::Gzip;
else if (std::string::npos != http_response_compression_methods.find("deflate"))
http_response_compression_method = CompressionMethod::Zlib;
}
bool client_supports_http_compression = http_response_compression_method != CompressionMethod::None;
return std::make_shared<WriteBufferFromHTTPServerResponse>(
response,
request.getMethod() == Poco::Net::HTTPRequest::HTTP_HEAD,
keep_alive_timeout,
client_supports_http_compression,
http_response_compression_method);
2020-04-26 11:06:14 +00:00
}
static inline void trySendExceptionToClient(
const std::string & s, int exception_code, HTTPServerRequest & request, HTTPServerResponse & response, WriteBuffer & out)
2020-04-04 08:57:16 +00:00
{
try
{
2020-04-26 11:06:14 +00:00
response.set("X-ClickHouse-Exception-Code", toString<int>(exception_code));
/// If HTTP method is POST and Keep-Alive is turned on, we should read the whole request body
/// to avoid reading part of the current request body in the next request.
if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST
&& response.getKeepAlive() && !request.getStream().eof() && exception_code != ErrorCodes::HTTP_LENGTH_REQUIRED)
request.getStream().ignore(std::numeric_limits<std::streamsize>::max());
2020-04-26 11:06:14 +00:00
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
if (!response.sent())
*response.send() << s << std::endl;
2020-04-26 11:06:14 +00:00
else
{
if (out.count() != out.offset())
out.position() = out.buffer().begin();
writeString(s, out);
writeChar('\n', out);
out.next();
out.finalize();
}
}
catch (...)
{
tryLogCurrentException("StaticRequestHandler", "Cannot send exception to client");
}
}
void StaticRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response)
2020-04-26 11:06:14 +00:00
{
auto keep_alive_timeout = server.config().getUInt("keep_alive_timeout", 10);
const auto & out = responseWriteBuffer(request, response, keep_alive_timeout);
2020-04-04 08:57:16 +00:00
2020-04-26 11:06:14 +00:00
try
{
2020-04-04 08:57:16 +00:00
response.setContentType(content_type);
2020-04-26 11:06:14 +00:00
if (request.getVersion() == Poco::Net::HTTPServerRequest::HTTP_1_1)
response.setChunkedTransferEncoding(true);
/// Workaround. Poco does not detect 411 Length Required case.
if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST && !request.getChunkedTransferEncoding() && !request.hasContentLength())
throw Exception("The Transfer-Encoding is not chunked and there is no Content-Length header for POST request", ErrorCodes::HTTP_LENGTH_REQUIRED);
setResponseDefaultHeaders(response, keep_alive_timeout);
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTPStatus(status));
writeResponse(*out);
2020-04-04 08:57:16 +00:00
}
catch (...)
{
tryLogCurrentException("StaticRequestHandler");
2020-04-26 11:06:14 +00:00
int exception_code = getCurrentExceptionCode();
std::string exception_message = getCurrentExceptionMessage(false, true);
trySendExceptionToClient(exception_message, exception_code, request, response, *out);
2020-04-04 08:57:16 +00:00
}
out->finalize();
2020-04-04 08:57:16 +00:00
}
2020-04-26 11:06:14 +00:00
void StaticRequestHandler::writeResponse(WriteBuffer & out)
2020-04-04 08:57:16 +00:00
{
static const String file_prefix = "file://";
static const String config_prefix = "config://";
2020-04-26 11:06:14 +00:00
if (startsWith(response_expression, file_prefix))
2020-04-04 08:57:16 +00:00
{
const auto & user_files_absolute_path = Poco::Path(server.context()->getUserFilesPath()).makeAbsolute().makeDirectory().toString();
2020-04-26 11:06:14 +00:00
const auto & file_name = response_expression.substr(file_prefix.size(), response_expression.size() - file_prefix.size());
2020-04-04 08:57:16 +00:00
2020-04-26 11:06:14 +00:00
const auto & file_path = Poco::Path(user_files_absolute_path, file_name).makeAbsolute().toString();
2020-04-04 08:57:16 +00:00
if (!Poco::File(file_path).exists())
2020-04-26 11:06:14 +00:00
throw Exception("Invalid file name " + file_path + " for static HTTPHandler. ", ErrorCodes::INCORRECT_FILE_NAME);
2020-04-04 08:57:16 +00:00
ReadBufferFromFile in(file_path);
copyData(in, out);
}
2020-04-26 11:06:14 +00:00
else if (startsWith(response_expression, config_prefix))
2020-04-04 08:57:16 +00:00
{
2020-04-26 11:06:14 +00:00
if (response_expression.size() <= config_prefix.size())
2020-04-27 20:51:39 +00:00
throw Exception( "Static handling rule handler must contain a complete configuration path, for example: config://config_key",
2020-04-04 08:57:16 +00:00
ErrorCodes::INVALID_CONFIG_PARAMETER);
2020-04-26 11:06:14 +00:00
const auto & config_path = response_expression.substr(config_prefix.size(), response_expression.size() - config_prefix.size());
writeString(server.config().getRawString(config_path, "Ok.\n"), out);
2020-04-04 08:57:16 +00:00
}
else
2020-04-26 11:06:14 +00:00
writeString(response_expression, out);
}
StaticRequestHandler::StaticRequestHandler(IServer & server_, const String & expression, int status_, const String & content_type_)
: server(server_), status(status_), content_type(content_type_), response_expression(expression)
{
2020-04-04 08:57:16 +00:00
}
HTTPRequestHandlerFactoryPtr createStaticHandlerFactory(IServer & server, const std::string & config_prefix)
2020-04-04 08:57:16 +00:00
{
int status = server.config().getInt(config_prefix + ".handler.status", 200);
std::string response_content = server.config().getRawString(config_prefix + ".handler.response_content", "Ok.\n");
std::string response_content_type = server.config().getString(config_prefix + ".handler.content_type", "text/plain; charset=UTF-8");
auto factory = std::make_shared<HandlingRuleHTTPHandlerFactory<StaticRequestHandler>>(
server, std::move(response_content), std::move(status), std::move(response_content_type));
2020-04-04 08:57:16 +00:00
factory->addFiltersFromConfig(server.config(), config_prefix);
return factory;
2020-04-04 08:57:16 +00:00
}
}