Skip to content

Commit 6c526a3

Browse files
committed
support http handler chain return HTTP_STATUS_WANT_CLOSE
1 parent 1e3dd55 commit 6c526a3

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

http/server/HttpHandler.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ void HttpHandler::onMessageComplete() {
331331

332332
if (status_code != HTTP_STATUS_NEXT) {
333333
// keepalive ? Reset : Close
334-
if (keepalive) {
334+
if (error == 0 && keepalive) {
335335
Reset();
336336
} else {
337337
state = WANT_CLOSE;
@@ -467,9 +467,9 @@ int HttpHandler::HandleHttpRequest() {
467467
pResp->status_code = (http_status)status_code;
468468
if (pResp->status_code >= 400 && pResp->body.size() == 0 && pReq->method != HTTP_HEAD) {
469469
if (service->errorHandler) {
470-
customHttpHandler(service->errorHandler);
470+
status_code = customHttpHandler(service->errorHandler);
471471
} else {
472-
defaultErrorHandler();
472+
status_code = defaultErrorHandler();
473473
}
474474
}
475475
}
@@ -481,7 +481,10 @@ int HttpHandler::HandleHttpRequest() {
481481
pResp->headers["Etag"] = fc->etag;
482482
}
483483
if (service->postprocessor) {
484-
customHttpHandler(service->postprocessor);
484+
status_code = customHttpHandler(service->postprocessor);
485+
}
486+
if (status_code == HTTP_STATUS_WANT_CLOSE) {
487+
error = ERR_REQUEST;
485488
}
486489

487490
if (writer && writer->state != hv::HttpResponseWriter::SEND_BEGIN) {
@@ -853,7 +856,7 @@ int HttpHandler::SendHttpStatusResponse(http_status status_code) {
853856
if (state > WANT_SEND) return 0;
854857
resp->status_code = status_code;
855858
addResponseHeaders();
856-
HandleHttpRequest();
859+
if (HandleHttpRequest() == HTTP_STATUS_NEXT) return 0;
857860
state = WANT_SEND;
858861
return SendHttpResponse();
859862
}

http/server/HttpService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
*/
3535
#define HTTP_STATUS_NEXT 0
3636
#define HTTP_STATUS_UNFINISHED 0
37+
#define HTTP_STATUS_WANT_CLOSE 1
3738
// NOTE: http_sync_handler run on IO thread
3839
typedef std::function<int(HttpRequest* req, HttpResponse* resp)> http_sync_handler;
3940
// NOTE: http_async_handler run on hv::async threadpool

0 commit comments

Comments
 (0)