From: Remi Gacogne Date: Tue, 4 Jul 2017 13:51:12 +0000 (+0200) Subject: Wrap the webserver's objects into smart pointers X-Git-Tag: dnsdist-1.2.0~52^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d4c53d8c770057f7bc4585e6d6b47074d9f739bf;p=pdns Wrap the webserver's objects into smart pointers --- diff --git a/pdns/webserver.cc b/pdns/webserver.cc index f6c77c06b..d89268cb5 100644 --- a/pdns/webserver.cc +++ b/pdns/webserver.cc @@ -25,6 +25,7 @@ #include "utility.hh" #include "webserver.hh" #include "misc.hh" +#include #include #include "logger.hh" #include @@ -36,7 +37,7 @@ struct connectionThreadData { WebServer* webServer{nullptr}; - Socket* client{nullptr}; + std::shared_ptr client{nullptr}; }; json11::Json HttpRequest::json() @@ -198,15 +199,9 @@ void WebServer::registerWebHandler(const string& url, HandlerFunction handler) { registerBareHandler(url, f); } -static void *WebServerConnectionThreadStart(void *p) { - connectionThreadData* data = static_cast(p); - pthread_detach(pthread_self()); +static void *WebServerConnectionThreadStart(std::shared_ptr data) { data->webServer->serveConnection(data->client); - - delete data->client; // close socket - delete data; - - return NULL; + return nullptr; } void WebServer::handleRequest(HttpRequest& req, HttpResponse& resp) @@ -286,7 +281,7 @@ void WebServer::handleRequest(HttpRequest& req, HttpResponse& resp) } } -void WebServer::serveConnection(Socket *client) +void WebServer::serveConnection(std::shared_ptr client) try { HttpRequest req; YaHTTP::AsyncRequestLoader yarl; @@ -354,41 +349,32 @@ void WebServer::go() if(!d_server) return; try { - pthread_t tid; - NetmaskGroup acl; acl.toMasks(::arg()["webserver-allow-from"]); while(true) { // data and data->client will be freed by thread - connectionThreadData *data = new connectionThreadData; + auto data = std::make_shared(); data->webServer = this; try { data->client = d_server->accept(); if (data->client->acl(acl)) { - pthread_create(&tid, 0, &WebServerConnectionThreadStart, (void *)data); + std::thread webHandler(WebServerConnectionThreadStart, data); + webHandler.detach(); } else { ComboAddress remote; if (data->client->getRemote(remote)) L<client; // close socket - delete data; } } catch(PDNSException &e) { L<client; - delete data; } catch(std::exception &e) { L<client; - delete data; } catch(...) { L<client; - delete data; } } } diff --git a/pdns/webserver.hh b/pdns/webserver.hh index d784cdf9f..3972162bc 100644 --- a/pdns/webserver.hh +++ b/pdns/webserver.hh @@ -126,8 +126,8 @@ public: ComboAddress d_local; - Socket *accept() { - return d_server_socket.accept(); + std::shared_ptr accept() { + return std::shared_ptr(d_server_socket.accept()); } protected: @@ -142,7 +142,7 @@ public: void bind(); void go(); - void serveConnection(Socket *client); + void serveConnection(std::shared_ptr client); void handleRequest(HttpRequest& request, HttpResponse& resp); typedef boost::function HandlerFunction; diff --git a/pdns/ws-recursor.cc b/pdns/ws-recursor.cc index c495918d7..404c5f4f7 100644 --- a/pdns/ws-recursor.cc +++ b/pdns/ws-recursor.cc @@ -540,9 +540,8 @@ void RecursorWebServer::jsonstat(HttpRequest* req, HttpResponse *resp) void AsyncServerNewConnectionMT(void *p) { AsyncServer *server = (AsyncServer*)p; try { - Socket* socket = server->accept(); + auto socket = server->accept(); server->d_asyncNewConnectionCallback(socket); - delete socket; } catch (NetworkError &e) { // we're running in a shared process/thread, so can't just terminate/abort. return; @@ -561,7 +560,7 @@ void AsyncServer::newConnection() } // This is an entry point from FDM, so it needs to catch everything. -void AsyncWebServer::serveConnection(Socket *client) +void AsyncWebServer::serveConnection(std::shared_ptr client) try { HttpRequest req; YaHTTP::AsyncRequestLoader yarl; @@ -571,7 +570,7 @@ try { string data; try { while(!req.complete) { - int bytes = arecvtcp(data, 16384, client, true); + int bytes = arecvtcp(data, 16384, client.get(), true); if (bytes > 0) { req.complete = yarl.feed(data); } else { @@ -591,7 +590,7 @@ try { data = ss.str(); // now send the reply - if (asendtcp(data, client) == -1 || data.empty()) { + if (asendtcp(data, client.get()) == -1 || data.empty()) { L< newconnectioncb_t; + typedef boost::function< void(std::shared_ptr) > newconnectioncb_t; void asyncWaitForConnections(FDMultiplexer* fdm, const newconnectioncb_t& callback); private: @@ -54,7 +54,7 @@ public: private: FDMultiplexer* d_fdm; - void serveConnection(Socket *socket); + void serveConnection(std::shared_ptr socket); protected: virtual std::shared_ptr createServer() override {