| 10 | { |
| 11 | |
| 12 | void drainRequestIfNeeded(HTTPServerRequest & request, HTTPServerResponse & response) noexcept |
| 13 | { |
| 14 | auto input_stream = request.getStream(); |
| 15 | if (input_stream->isCanceled()) |
| 16 | { |
| 17 | response.setKeepAlive(false); |
| 18 | LOG_WARNING(getLogger("sendExceptionToHTTPClient"), "Cannot read remaining request body during exception handling. The request read buffer is canceled. Set keep alive to false on the response."); |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | LOG_DEBUG(getLogger("sendExceptionToHTTPClient"), "Draining connection ({}, Transfer-Encoding: {}, Content-Length: {}, Keep-Alive: {})", |
| 23 | request.getVersion(), request.getTransferEncoding(), request.getContentLength(), request.getKeepAlive()); |
| 24 | |
| 25 | /// If HTTP method is POST and Keep-Alive is turned on, we should try to read the whole request body |
| 26 | /// to avoid reading part of the current request body in the next request. |
| 27 | /// Or we have to close connection after this request. |
| 28 | if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST |
| 29 | && (request.getChunkedTransferEncoding() || request.hasContentLength()) |
| 30 | && response.getKeepAlive()) |
| 31 | { |
| 32 | /// If the client expects 100 Continue, but we never sent it, don't attempt to read the body and |
| 33 | /// don't reuse the connection. |
| 34 | if (request.getExpectContinue() && response.getStatus() != Poco::Net::HTTPResponse::HTTP_CONTINUE) |
| 35 | { |
| 36 | response.setKeepAlive(false); |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | try |
| 41 | { |
| 42 | if (!input_stream->eof()) |
| 43 | { |
| 44 | size_t ignored_bytes = input_stream->ignoreAll(); |
| 45 | LOG_DEBUG(getLogger("sendExceptionToHTTPClient"), "Drained {} bytes", ignored_bytes); |
| 46 | } |
| 47 | } |
| 48 | catch (...) |
| 49 | { |
| 50 | tryLogCurrentException("sendExceptionToHTTPClient", "Cannot read remaining request body during exception handling. Set keep alive to false on the response."); |
| 51 | response.setKeepAlive(false); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | } |
no test coverage detected