]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Add timeouts to the http module. Two seperate timeouts, 60 seconds to receive headers...
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>
Fri, 15 Sep 2006 18:36:38 +0000 (18:36 +0000)
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>
Fri, 15 Sep 2006 18:36:38 +0000 (18:36 +0000)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@5260 e03df62e-2008-0410-955e-edbf42e46eb7

include/timer.h
src/modules/m_httpd.cpp
src/timer.cpp

index ef9427abfb3d4c7a31c1f639a5bfb038b2d8c9bc..1f6da45213dd4dba852f5c95f0ae7217d3eb5c64 100644 (file)
@@ -84,6 +84,10 @@ class TimerManager : public Extensible
         * @param T an InspTimer derived class to add
         */
        void AddTimer(InspTimer* T);
+       /** Delete and InspTImer
+        * @param T an InspTimer derived class to delete
+        */
+       void DelTimer(InspTimer* T);
        /** Tick any timers that have been missed due to lag
         * @param TIME the current system time
         */
index 96fdfc8f25e6a94975d83dfae280c806980f20f0..ba8990afb4001b307b6642e2a20f2ba01c201856 100644 (file)
@@ -41,6 +41,18 @@ enum HttpState
        HTTP_SERVE_SEND_DATA = 3
 };
 
+class HttpSocket;
+
+class HTTPTimeout : public InspTimer
+{
+ private:
+       HttpSocket* s;
+       SocketEngine* SE;
+ public:
+       HTTPTimeout(HttpSocket* sock, SocketEngine* engine);
+       void Tick(time_t TIME);
+};
+
 /** A socket used for HTTP transport
  */
 class HttpSocket : public InspSocket
@@ -54,6 +66,7 @@ class HttpSocket : public InspSocket
        std::string http_version;
        unsigned int postsize;
        unsigned int amount;
+       HTTPTimeout* Timeout;
 
  public:
 
@@ -61,11 +74,21 @@ class HttpSocket : public InspSocket
        {
                SI->Log(DEBUG,"HttpSocket constructor");
                InternalState = HTTP_LISTEN;
+               Timeout = NULL;
        }
 
        HttpSocket(InspIRCd* SI, int newfd, char* ip, FileReader* ind) : InspSocket(SI, newfd, ip), index(ind), postsize(0)
        {
                InternalState = HTTP_SERVE_WAIT_REQUEST;
+               Timeout = new HTTPTimeout(this, Instance->SE);
+               Instance->Timers->AddTimer(Timeout);
+       }
+
+       ~HttpSocket()
+       {
+               if (Timeout)
+                       Instance->Timers->DelTimer(Timeout);
+               Timeout = NULL;
        }
 
        virtual int OnIncomingConnection(int newsock, char* ip)
@@ -228,6 +251,8 @@ class HttpSocket : public InspSocket
                                        {
                                                InternalState = HTTP_SERVE_SEND_DATA;
                                                SendHeaders(0, 400, "");
+                                               Timeout = new HTTPTimeout(this, Instance->SE);
+                                               Instance->Timers->AddTimer(Timeout);
                                        }
                                        else
                                        {
@@ -266,6 +291,9 @@ class HttpSocket : public InspSocket
        {
                /* Headers are complete */
                InternalState = HTTP_SERVE_SEND_DATA;
+
+               Instance->Timers->DelTimer(Timeout);
+               Timeout = NULL;
        
                if ((http_version != "HTTP/1.1") && (http_version != "HTTP/1.0"))
                {
@@ -291,6 +319,8 @@ class HttpSocket : public InspSocket
                                }
                        }
                }
+               Timeout = new HTTPTimeout(this, Instance->SE);
+               Instance->Timers->AddTimer(Timeout);
        }
 
        void Page(std::stringstream* n, int response, std::string& extraheaders)
@@ -301,6 +331,16 @@ class HttpSocket : public InspSocket
        }
 };
 
+HTTPTimeout::HTTPTimeout(HttpSocket* sock, SocketEngine* engine) : InspTimer(60, time(NULL)), s(sock), SE(engine)
+{
+}
+
+void HTTPTimeout::Tick(time_t TIME)
+{
+       SE->DelFd(s);
+       s->Close();
+}
+
 class ModuleHttp : public Module
 {
        int port;
index 9157c152187b6ba0994854cf9f0fac7ed237bddc..bc08ae1d015f1238017bec1b5e76c595aebafea8 100644 (file)
@@ -39,6 +39,28 @@ void TimerManager::TickTimers(time_t TIME)
        }
 }
 
+void TimerManager::DelTimer(InspTimer* T)
+{
+       timerlist::iterator found = Timers.find(T->GetTimer());
+
+       if (found != Timers.end())
+       {
+               timergroup* x = found->second;
+               for (timergroup::iterator y = x->begin(); y != x->end(); y++)
+               {
+                       InspTimer* n = *y;
+                       if (n == T)
+                       {
+                               DELETE(n);
+                               x->erase(y);
+                               if (!x->size())
+                                       Timers.erase(found);
+                               return;
+                       }
+               }
+       }
+}
+
 /*
  * Because some muppets may do odd things, and their ircd may lock up due
  * to crappy 3rd party modules, or they may change their system time a bit,