ClickHouse/dbms/src/Server/InterserverIOHTTPHandler.cpp

108 lines
3.1 KiB
C++
Raw Normal View History

2014-03-21 13:42:14 +00:00
#include "InterserverIOHTTPHandler.h"
2015-04-16 06:12:35 +00:00
#include <DB/Interpreters/InterserverIOHandler.h>
2014-03-21 13:42:14 +00:00
#include <DB/IO/WriteBufferFromHTTPServerResponse.h>
#include <DB/IO/CompressedWriteBuffer.h>
2016-03-01 17:47:53 +00:00
#include <DB/IO/ReadBufferFromIStream.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;
}
2014-03-21 13:42:14 +00:00
void InterserverIOHTTPHandler::processQuery(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response)
{
HTMLForm params(request);
LOG_TRACE(log, "Request URI: " << request.getURI());
/// NOTE: Тут можно сделать аутентификацию, если понадобится.
String endpoint_name = params.get("endpoint");
bool compress = params.get("compress") == "true";
2016-03-01 17:47:53 +00:00
ReadBufferFromIStream body(request.stream());
2014-03-21 13:42:14 +00:00
WriteBufferFromHTTPServerResponse out(response);
auto endpoint = server.global_context->getInterserverIOHandler().getEndpoint(endpoint_name);
if (compress)
{
CompressedWriteBuffer compressed_out(out);
2016-03-01 17:47:53 +00:00
endpoint->processQuery(params, body, compressed_out);
2014-03-21 13:42:14 +00:00
}
else
{
2016-03-01 17:47:53 +00:00
endpoint->processQuery(params, body, out);
2014-03-21 13:42:14 +00:00
}
out.finalize();
}
void InterserverIOHTTPHandler::handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response)
{
/// Для того, чтобы работал 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)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
std::stringstream s;
s << "Code: " << e.code()
<< ", e.displayText() = " << e.displayText() << ", e.what() = " << e.what();
if (!response.sent())
response.send() << s.str() << std::endl;
if (e.code() == ErrorCodes::ABORTED)
LOG_INFO(log, s.str()); /// Отдача куска на удалённый сервер была остановлена из-за остановки сервера или удаления таблицы.
else
LOG_ERROR(log, s.str());
2014-03-21 13:42:14 +00:00
}
catch (Poco::Exception & e)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
std::stringstream s;
s << "Code: " << ErrorCodes::POCO_EXCEPTION << ", e.code() = " << e.code()
<< ", e.displayText() = " << e.displayText() << ", e.what() = " << e.what();
if (!response.sent())
response.send() << s.str() << std::endl;
LOG_ERROR(log, s.str());
}
catch (std::exception & e)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
std::stringstream s;
s << "Code: " << ErrorCodes::STD_EXCEPTION << ". " << e.what();
if (!response.sent())
response.send() << s.str() << std::endl;
LOG_ERROR(log, s.str());
}
catch (...)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
std::stringstream s;
s << "Code: " << ErrorCodes::UNKNOWN_EXCEPTION << ". Unknown exception.";
if (!response.sent())
response.send() << s.str() << std::endl;
LOG_ERROR(log, s.str());
}
}
}