diff --git a/http/server/HttpHandler.cpp b/http/server/HttpHandler.cpp index 54a05c71a..34853a5b4 100644 --- a/http/server/HttpHandler.cpp +++ b/http/server/HttpHandler.cpp @@ -42,6 +42,7 @@ HttpHandler::HttpHandler(hio_t* io) : tid(0), // for http io(io), + server(NULL), service(NULL), api_handler(NULL), // for websocket diff --git a/http/server/HttpHandler.h b/http/server/HttpHandler.h index fca6848c4..3b49bd7d6 100644 --- a/http/server/HttpHandler.h +++ b/http/server/HttpHandler.h @@ -51,6 +51,7 @@ class HttpHandler { // for http hio_t *io; + void *server; HttpService *service; HttpRequestPtr req; HttpResponsePtr resp; diff --git a/http/server/HttpServer.cpp b/http/server/HttpServer.cpp index eff0a865c..5e1b8c4fc 100644 --- a/http/server/HttpServer.cpp +++ b/http/server/HttpServer.cpp @@ -37,14 +37,17 @@ static void on_recv(hio_t* io, void* buf, int readbytes) { 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; EventLoop* loop = currentThreadEventLoop; if (loop) { + if (server->onClose) { + server->onClose(io); + } --loop->connectionNum; } + hevent_set_userdata(io, NULL); + delete handler; } static void on_accept(hio_t* io) { @@ -65,6 +68,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 +91,8 @@ 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; // 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;