| 65 | } |
| 66 | |
| 67 | static inline void trySendExceptionToClient( |
| 68 | const std::string & s, int exception_code, HTTPServerRequest & request, HTTPServerResponse & response, WriteBuffer & out) |
| 69 | { |
| 70 | try |
| 71 | { |
| 72 | response.set("X-ClickHouse-Exception-Code", toString<int>(exception_code)); |
| 73 | |
| 74 | /// If HTTP method is POST and Keep-Alive is turned on, we should read the whole request body |
| 75 | /// to avoid reading part of the current request body in the next request. |
| 76 | if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST |
| 77 | && response.getKeepAlive() && !request.getStream().eof() && exception_code != ErrorCodes::HTTP_LENGTH_REQUIRED) |
| 78 | request.getStream().ignore(std::numeric_limits<std::streamsize>::max()); |
| 79 | |
| 80 | response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); |
| 81 | |
| 82 | if (!response.sent()) |
| 83 | *response.send() << s << std::endl; |
| 84 | else |
| 85 | { |
| 86 | if (out.count() != out.offset()) |
| 87 | out.position() = out.buffer().begin(); |
| 88 | |
| 89 | writeString(s, out); |
| 90 | writeChar('\n', out); |
| 91 | |
| 92 | out.next(); |
| 93 | out.finalize(); |
| 94 | } |
| 95 | } |
| 96 | catch (...) |
| 97 | { |
| 98 | tryLogCurrentException("StaticRequestHandler", "Cannot send exception to client"); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void StaticRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response) |
| 103 | { |