From 975064f3f9e1cee80cce4636655cf4d729d38495 Mon Sep 17 00:00:00 2001 From: caocao Date: Thu, 21 Aug 2025 10:04:21 +0800 Subject: [PATCH 1/2] add gitignore .idea --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index aa1525284..5dd7f6754 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,7 @@ MODULE.bazel.lock .vs .vscode .DS_Store +.idea tags cscope* From 4c247ed1d2722f31233732e61b71b76621c3b0f3 Mon Sep 17 00:00:00 2001 From: caocao Date: Thu, 21 Aug 2025 10:07:52 +0800 Subject: [PATCH 2/2] add an HTTP protocol accept callback and pass userdata in the context. --- http/server/HttpHandler.cpp | 3 +++ http/server/HttpHandler.h | 2 ++ http/server/HttpServer.cpp | 19 ++++++++++++++++--- http/server/HttpServer.h | 2 ++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/http/server/HttpHandler.cpp b/http/server/HttpHandler.cpp index 747d8a1a0..4c58ca933 100644 --- a/http/server/HttpHandler.cpp +++ b/http/server/HttpHandler.cpp @@ -42,6 +42,8 @@ HttpHandler::HttpHandler(hio_t* io) : tid(0), // for http io(io), + server(NULL), + userdata(NULL), service(NULL), api_handler(NULL), // for websocket @@ -224,6 +226,7 @@ const HttpContextPtr& HttpHandler::context() { ctx->response = resp; ctx->writer = writer; } + ctx->userdata = this->userdata; return ctx; } diff --git a/http/server/HttpHandler.h b/http/server/HttpHandler.h index b72f07772..486406215 100644 --- a/http/server/HttpHandler.h +++ b/http/server/HttpHandler.h @@ -51,6 +51,8 @@ class HttpHandler { // for http hio_t *io; + void *server; + void *userdata; HttpService *service; HttpRequestPtr req; HttpResponsePtr resp; diff --git a/http/server/HttpServer.cpp b/http/server/HttpServer.cpp index aa296fe8b..18b6dbfc5 100644 --- a/http/server/HttpServer.cpp +++ b/http/server/HttpServer.cpp @@ -38,13 +38,17 @@ static void on_close(hio_t* io) { HttpHandler* handler = (HttpHandler*)hevent_userdata(io); if (handler == NULL) return; - hevent_set_userdata(io, NULL); - delete handler; - + http_server_t* server = (http_server_t*)handler->server; + if (server->onClose) { + hevent_set_userdata(io, handler->userdata); + server->onClose(io); + } EventLoop* loop = currentThreadEventLoop; if (loop) { --loop->connectionNum; } + hevent_set_userdata(io, NULL); + delete handler; } static void on_accept(hio_t* io) { @@ -65,6 +69,12 @@ static void on_accept(hio_t* io) { hio_close(io); return; } + if (server->onAccept) { + if (!server->onAccept(io)) { + hio_close(io); + return; + } + } ++loop->connectionNum; hio_setcb_close(io, on_close); @@ -82,6 +92,9 @@ static void on_accept(hio_t* io) { sockaddr_u* peeraddr = (sockaddr_u*)hio_peeraddr(io); sockaddr_ip(peeraddr, handler->ip, sizeof(handler->ip)); handler->port = sockaddr_port(peeraddr); + // http server + handler->server = server; + handler->userdata = hevent_userdata(io); // http service handler->service = service; // websocket service diff --git a/http/server/HttpServer.h b/http/server/HttpServer.h index 79b3772da..62c61e251 100644 --- a/http/server/HttpServer.h +++ b/http/server/HttpServer.h @@ -29,6 +29,8 @@ typedef struct http_server_s { // hooks std::function onWorkerStart; std::function onWorkerStop; + std::function onAccept; + std::function onClose; // SSL/TLS hssl_ctx_t ssl_ctx; unsigned alloced_ssl_ctx: 1;