]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_httpd.cpp
Change allocation of UserManager::clientlist to be physically part of the object...
[user/henk/code/inspircd.git] / src / modules / m_httpd.cpp
index 88dd3f47b19b1dc861a9a848994bba54c28e244d..d0291b8cc0f98c251db1cec7ac0f16ebcaa05358 100644 (file)
 #include "iohook.h"
 #include "modules/httpd.h"
 
-/* $ModDep: modules/httpd.h */
-
 class ModuleHttpServer;
 
 static ModuleHttpServer* HttpModule;
 static bool claimed;
+static std::set<HttpServerSocket*> 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(I_OnHookIO, 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
@@ -216,7 +222,7 @@ class HttpServerSocket : public BufferedSocket
 
                        if (reqbuffer.length() >= 8192)
                        {
-                               ServerInstance->Logs->Log("m_httpd", LOG_DEBUG, "m_httpd dropped connection due to an oversized request buffer");
+                               ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "m_httpd dropped connection due to an oversized request buffer");
                                reqbuffer.clear();
                                SetError("Buffer");
                        }
@@ -349,6 +355,7 @@ class ModuleHttpServer : public Module
 {
        std::vector<HttpServerSocket *> 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<HttpServerSocket*>::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<HttpServerSocket*> local;
+               local.swap(sockets);
+               for (std::set<HttpServerSocket*>::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