ClickHouse/dbms/src/Server/InterserverIOHTTPHandler.cpp

104 lines
2.8 KiB
C++
Raw Normal View History

2017-08-09 14:33:07 +00:00
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <common/logger_useful.h>
#include <Common/HTMLForm.h>
#include <IO/CompressedWriteBuffer.h>
#include <IO/ReadBufferFromIStream.h>
2017-08-09 14:33:07 +00:00
#include <IO/WriteBufferFromHTTPServerResponse.h>
#include <Interpreters/InterserverIOHandler.h>
2014-03-21 13:42:14 +00:00
2017-08-09 14:33:07 +00:00
#include "InterserverIOHTTPHandler.h"
2014-03-21 13:42:14 +00:00
namespace DB
{
2016-01-12 02:21:15 +00:00
namespace ErrorCodes
{
extern const int ABORTED;
extern const int POCO_EXCEPTION;
extern const int STD_EXCEPTION;
extern const int UNKNOWN_EXCEPTION;
extern const int TOO_MUCH_SIMULTANEOUS_QUERIES;
2016-01-12 02:21:15 +00:00
}
2014-03-21 13:42:14 +00:00
void InterserverIOHTTPHandler::processQuery(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response)
{
HTMLForm params(request);
2014-03-21 13:42:14 +00:00
LOG_TRACE(log, "Request URI: " << request.getURI());
2014-03-21 13:42:14 +00:00
/// NOTE: You can do authentication here if you need to.
2014-03-21 13:42:14 +00:00
String endpoint_name = params.get("endpoint");
bool compress = params.get("compress") == "true";
2014-03-21 13:42:14 +00:00
ReadBufferFromIStream body(request.stream());
2016-03-01 17:47:53 +00:00
WriteBufferFromHTTPServerResponse out(request, response);
2014-03-21 13:42:14 +00:00
2017-08-09 11:57:09 +00:00
auto endpoint = server.context().getInterserverIOHandler().getEndpoint(endpoint_name);
2014-03-21 13:42:14 +00:00
if (compress)
{
CompressedWriteBuffer compressed_out(out);
endpoint->processQuery(params, body, compressed_out, response);
}
else
{
endpoint->processQuery(params, body, out, response);
}
2014-03-21 13:42:14 +00:00
out.finalize();
2014-03-21 13:42:14 +00:00
}
void InterserverIOHTTPHandler::handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response)
{
/// In order to work keep-alive.
if (request.getVersion() == Poco::Net::HTTPServerRequest::HTTP_1_1)
response.setChunkedTransferEncoding(true);
try
{
processQuery(request, response);
LOG_INFO(log, "Done processing query");
}
catch (Exception & e)
{
if (e.code() == ErrorCodes::TOO_MUCH_SIMULTANEOUS_QUERIES)
{
if (!response.sent())
response.send();
return;
}
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
/// Sending to remote server was cancelled due to server shutdown or drop table.
bool is_real_error = e.code() != ErrorCodes::ABORTED;
std::string message = getCurrentExceptionMessage(is_real_error);
if (!response.sent())
response.send() << message << std::endl;
if (is_real_error)
LOG_ERROR(log, message);
else
LOG_INFO(log, message);
}
catch (...)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
std::string message = getCurrentExceptionMessage(false);
if (!response.sent())
response.send() << message << std::endl;
LOG_ERROR(log, message);
}
2014-03-21 13:42:14 +00:00
}
}