X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_httpd.cpp;h=d0291b8cc0f98c251db1cec7ac0f16ebcaa05358;hb=ea590a5d80741c3bc030cb0a2fcb3c59da4fd078;hp=bb5d5c0b0867111c8d69db22a27f0034e5773a77;hpb=8710724b5518ae9858309e548514f76e620a8459;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_httpd.cpp b/src/modules/m_httpd.cpp index bb5d5c0b0..d0291b8cc 100644 --- a/src/modules/m_httpd.cpp +++ b/src/modules/m_httpd.cpp @@ -26,12 +26,11 @@ #include "iohook.h" #include "modules/httpd.h" -/* $ModDep: modules/httpd.h */ - class ModuleHttpServer; static ModuleHttpServer* HttpModule; static bool claimed; +static std::set sockets; /** HTTP socket states */ @@ -58,14 +57,21 @@ class HttpServerSocket : public BufferedSocket std::string http_version; public: + const time_t createtime; + HttpServerSocket(int newfd, const std::string& IP, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) : BufferedSocket(newfd), ip(IP), postsize(0) + , createtime(ServerInstance->Time()) { InternalState = HTTP_SERVE_WAIT_REQUEST; - FOREACH_MOD(OnHookIO, (this, via)); - if (GetIOHook()) - GetIOHook()->OnStreamSocketAccept(this, client, server); + if (via->iohookprov) + via->iohookprov->OnAccept(this, client, server); + } + + ~HttpServerSocket() + { + sockets.erase(this); } void OnError(BufferedSocketError) CXX11_OVERRIDE @@ -349,6 +355,7 @@ class ModuleHttpServer : public Module { std::vector httpsocks; HTTPdAPIImpl APIImpl; + unsigned int timeoutsec; public: ModuleHttpServer() @@ -359,8 +366,12 @@ class ModuleHttpServer : public Module void init() CXX11_OVERRIDE { HttpModule = this; - ServerInstance->Modules->AddService(APIImpl); - ServerInstance->Modules->Attach(I_OnAcceptConnection, this); + } + + void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE + { + ConfigTag* tag = ServerInstance->Config->ConfValue("httpd"); + timeoutsec = tag->getInt("timeout"); } ModResult OnAcceptConnection(int nfd, ListenSocket* from, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) CXX11_OVERRIDE @@ -370,17 +381,39 @@ class ModuleHttpServer : public Module int port; std::string incomingip; irc::sockets::satoap(*client, incomingip, port); - new HttpServerSocket(nfd, incomingip, from, client, server); + sockets.insert(new HttpServerSocket(nfd, incomingip, from, client, server)); return MOD_RES_ALLOW; } - ~ModuleHttpServer() + void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE + { + if (!timeoutsec) + return; + + time_t oldest_allowed = curtime - timeoutsec; + for (std::set::const_iterator i = sockets.begin(); i != sockets.end(); ) + { + HttpServerSocket* sock = *i; + ++i; + if (sock->createtime < oldest_allowed) + { + sock->cull(); + delete sock; + } + } + } + + CullResult cull() CXX11_OVERRIDE { - for (size_t i = 0; i < httpsocks.size(); i++) + std::set local; + local.swap(sockets); + for (std::set::const_iterator i = local.begin(); i != local.end(); ++i) { - httpsocks[i]->cull(); - delete httpsocks[i]; + HttpServerSocket* sock = *i; + sock->cull(); + delete sock; } + return Module::cull(); } Version GetVersion() CXX11_OVERRIDE