diff --git a/http/server/HttpHandler.cpp b/http/server/HttpHandler.cpp index 54a05c71a..903b807d6 100644 --- a/http/server/HttpHandler.cpp +++ b/http/server/HttpHandler.cpp @@ -331,7 +331,7 @@ void HttpHandler::onMessageComplete() { if (status_code != HTTP_STATUS_NEXT) { // keepalive ? Reset : Close - if (keepalive) { + if (error == 0 && keepalive) { Reset(); } else { state = WANT_CLOSE; @@ -467,9 +467,9 @@ int HttpHandler::HandleHttpRequest() { pResp->status_code = (http_status)status_code; if (pResp->status_code >= 400 && pResp->body.size() == 0 && pReq->method != HTTP_HEAD) { if (service->errorHandler) { - customHttpHandler(service->errorHandler); + status_code = customHttpHandler(service->errorHandler); } else { - defaultErrorHandler(); + status_code = defaultErrorHandler(); } } } @@ -481,7 +481,10 @@ int HttpHandler::HandleHttpRequest() { pResp->headers["Etag"] = fc->etag; } if (service->postprocessor) { - customHttpHandler(service->postprocessor); + status_code = customHttpHandler(service->postprocessor); + } + if (status_code == HTTP_STATUS_WANT_CLOSE) { + error = ERR_REQUEST; } if (writer && writer->state != hv::HttpResponseWriter::SEND_BEGIN) { @@ -671,7 +674,7 @@ int HttpHandler::defaultErrorHandler() { resp->content_type = TEXT_HTML; make_http_status_page(resp->status_code, resp->body); } - return 0; + return resp->status_code; } int HttpHandler::FeedRecvData(const char* data, size_t len) { @@ -853,7 +856,7 @@ int HttpHandler::SendHttpStatusResponse(http_status status_code) { if (state > WANT_SEND) return 0; resp->status_code = status_code; addResponseHeaders(); - HandleHttpRequest(); + if (HandleHttpRequest() == HTTP_STATUS_NEXT) return 0; state = WANT_SEND; return SendHttpResponse(); } diff --git a/http/server/HttpService.h b/http/server/HttpService.h index d85c7606f..4f5cead7c 100644 --- a/http/server/HttpService.h +++ b/http/server/HttpService.h @@ -34,6 +34,7 @@ */ #define HTTP_STATUS_NEXT 0 #define HTTP_STATUS_UNFINISHED 0 +#define HTTP_STATUS_WANT_CLOSE 1 // NOTE: http_sync_handler run on IO thread typedef std::function http_sync_handler; // NOTE: http_async_handler run on hv::async threadpool