Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/http_server_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ int main(int argc, char** argv) {
writer->End();
});

// curl -v http://ip:port/close
// Test HTTP_STATUS_CLOSE: closes connection without sending any response
router.GET("/close", [](HttpRequest* req, HttpResponse* resp) {
return HTTP_STATUS_CLOSE;
});

// middleware
router.AllowCORS();
router.Use([](HttpRequest* req, HttpResponse* resp) {
Expand Down
23 changes: 20 additions & 3 deletions http/server/HttpHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ void HttpHandler::onHeadersComplete() {
handleRequestHeaders();
if (service->headerHandler) {
const int status_code = customHttpHandler(service->headerHandler);
if (status_code == HTTP_STATUS_CLOSE) {
state = WANT_CLOSE;
return;
}
if (status_code != HTTP_STATUS_OK && status_code != HTTP_STATUS_NEXT) {
SetError(ERR_REQUEST, static_cast<http_status>(status_code));
return;
Expand Down Expand Up @@ -338,6 +342,10 @@ void HttpHandler::onMessageComplete() {
}
} else {
status_code = HandleHttpRequest();
if (status_code == HTTP_STATUS_CLOSE) {
state = WANT_CLOSE;
return;
}
if (status_code != HTTP_STATUS_NEXT) {
SendHttpResponse();
}
Expand Down Expand Up @@ -478,12 +486,17 @@ 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();
}
}
}
// Handle HTTP_STATUS_CLOSE: close connection without response
if (status_code == HTTP_STATUS_CLOSE) {
state = WANT_CLOSE;
return HTTP_STATUS_CLOSE;
}
if (fc) {
pResp->content = fc->filebuf.base;
pResp->content_length = fc->filebuf.len;
Expand All @@ -492,7 +505,11 @@ int HttpHandler::HandleHttpRequest() {
pResp->headers["Etag"] = fc->etag;
}
if (service->postprocessor) {
customHttpHandler(service->postprocessor);
status_code = customHttpHandler(service->postprocessor);
if (status_code == HTTP_STATUS_CLOSE) {
state = WANT_CLOSE;
return HTTP_STATUS_CLOSE;
}
}

if (writer && writer->state != hv::HttpResponseWriter::SEND_BEGIN) {
Expand Down Expand Up @@ -760,7 +777,7 @@ int HttpHandler::GetSendData(char** data, size_t* len) {
if (parser->IsComplete()) state = WANT_SEND;
else return 0;
case HANDLE_END:
state = WANT_SEND;
state = WANT_SEND;
case WANT_SEND:
state = SEND_HEADER;
case SEND_HEADER:
Expand Down
6 changes: 4 additions & 2 deletions http/server/HttpService.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
/*
* @param[in] req: parsed structured http request
* @param[out] resp: structured http response
* @return 0: handle next
* http_status_code: handle done
* @return HTTP_STATUS_NEXT: handle next
* HTTP_STATUS_CLOSE: close connection
* http_status_code: handle done
*/
#define HTTP_STATUS_NEXT 0
#define HTTP_STATUS_UNFINISHED 0
#define HTTP_STATUS_CLOSE -100
// NOTE: http_sync_handler run on IO thread
typedef std::function<int(HttpRequest* req, HttpResponse* resp)> http_sync_handler;
// NOTE: http_async_handler run on hv::async threadpool
Expand Down
Loading