]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_httpd.cpp
Merge pull request #1443 from B00mX0r/master+filtershun
[user/henk/code/inspircd.git] / src / modules / m_httpd.cpp
index bbd9f1275d98f365812009f6d25fcd4918c944d8..a20b85f9d003e7cc9e26e73d4c598046ec68b04e 100644 (file)
@@ -29,8 +29,9 @@
 class ModuleHttpServer;
 
 static ModuleHttpServer* HttpModule;
-static bool claimed;
 static insp::intrusive_list<HttpServerSocket> sockets;
+static Events::ModuleEventProvider* aclevprov;
+static Events::ModuleEventProvider* reqevprov;
 
 /** HTTP socket states
  */
@@ -75,10 +76,18 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
                , postsize(0)
                , waitingcull(false)
        {
-               ServerInstance->Timers.AddTimer(this);
+               if ((!via->iohookprovs.empty()) && (via->iohookprovs.back()))
+               {
+                       via->iohookprovs.back()->OnAccept(this, client, server);
+                       // IOHook may have errored
+                       if (!getError().empty())
+                       {
+                               AddToCull();
+                               return;
+                       }
+               }
 
-               if (via->iohookprov)
-                       via->iohookprov->OnAccept(this, client, server);
+               ServerInstance->Timers.AddTimer(this);
        }
 
        ~HttpServerSocket()
@@ -91,7 +100,7 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
                AddToCull();
        }
 
-       std::string Response(int response)
+       std::string Response(unsigned int response)
        {
                switch (response)
                {
@@ -182,7 +191,7 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
                }
        }
 
-       void SendHTTPError(int response)
+       void SendHTTPError(unsigned int response)
        {
                HTTPHeaders empty;
                std::string data = "<html><head></head><body>Server error "+ConvToStr(response)+": "+Response(response)+"<br>"+
@@ -192,7 +201,7 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
                WriteData(data);
        }
 
-       void SendHeaders(unsigned long size, int response, HTTPHeaders &rheaders)
+       void SendHeaders(unsigned long size, unsigned int response, HTTPHeaders &rheaders)
        {
 
                WriteData(http_version + " "+ConvToStr(response)+" "+Response(response)+"\r\n");
@@ -215,7 +224,7 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
                WriteData("\r\n");
        }
 
-       void OnDataReady()
+       void OnDataReady() CXX11_OVERRIDE
        {
                if (InternalState == HTTP_SERVE_RECV_POSTDATA)
                {
@@ -322,21 +331,21 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
        {
                InternalState = HTTP_SERVE_SEND_DATA;
 
-               claimed = false;
-               HTTPRequest acl((Module*)HttpModule, "httpd_acl", request_type, uri, &headers, this, ip, postdata);
-               acl.Send();
-               if (!claimed)
+               ModResult MOD_RESULT;
+               HTTPRequest acl(request_type, uri, &headers, this, ip, postdata);
+               FIRST_MOD_RESULT_CUSTOM(*aclevprov, HTTPACLEventListener, OnHTTPACLCheck, MOD_RESULT, (acl));
+               if (MOD_RESULT != MOD_RES_DENY)
                {
-                       HTTPRequest url((Module*)HttpModule, "httpd_url", request_type, uri, &headers, this, ip, postdata);
-                       url.Send();
-                       if (!claimed)
+                       HTTPRequest url(request_type, uri, &headers, this, ip, postdata);
+                       FIRST_MOD_RESULT_CUSTOM(*reqevprov, HTTPRequestEventListener, OnHTTPRequest, MOD_RESULT, (url));
+                       if (MOD_RESULT == MOD_RES_PASSTHRU)
                        {
                                SendHTTPError(404);
                        }
                }
        }
 
-       void Page(std::stringstream* n, int response, HTTPHeaders *hheaders)
+       void Page(std::stringstream* n, unsigned int response, HTTPHeaders *hheaders)
        {
                SendHeaders(n->str().length(), response, *hheaders);
                WriteData(n->str());
@@ -363,7 +372,6 @@ class HTTPdAPIImpl : public HTTPdAPIBase
 
        void SendResponse(HTTPDocumentResponse& resp) CXX11_OVERRIDE
        {
-               claimed = true;
                resp.src.sock->Page(resp.document, resp.responsecode, &resp.headers);
        }
 };
@@ -372,11 +380,17 @@ class ModuleHttpServer : public Module
 {
        HTTPdAPIImpl APIImpl;
        unsigned int timeoutsec;
+       Events::ModuleEventProvider acleventprov;
+       Events::ModuleEventProvider reqeventprov;
 
  public:
        ModuleHttpServer()
                : APIImpl(this)
+               , acleventprov(this, "event/http-acl")
+               , reqeventprov(this, "event/http-request")
        {
+               aclevprov = &acleventprov;
+               reqevprov = &reqeventprov;
        }
 
        void init() CXX11_OVERRIDE
@@ -387,20 +401,32 @@ class ModuleHttpServer : public Module
        void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
        {
                ConfigTag* tag = ServerInstance->Config->ConfValue("httpd");
-               timeoutsec = tag->getInt("timeout", 10, 1);
+               timeoutsec = tag->getDuration("timeout", 10, 1);
        }
 
        ModResult OnAcceptConnection(int nfd, ListenSocket* from, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) CXX11_OVERRIDE
        {
                if (from->bind_tag->getString("type") != "httpd")
                        return MOD_RES_PASSTHRU;
-               int port;
-               std::string incomingip;
-               irc::sockets::satoap(*client, incomingip, port);
-               sockets.push_front(new HttpServerSocket(nfd, incomingip, from, client, server, timeoutsec));
+
+               sockets.push_front(new HttpServerSocket(nfd, client->addr(), from, client, server, timeoutsec));
                return MOD_RES_ALLOW;
        }
 
+       void OnUnloadModule(Module* mod) CXX11_OVERRIDE
+       {
+               for (insp::intrusive_list<HttpServerSocket>::const_iterator i = sockets.begin(); i != sockets.end(); )
+               {
+                       HttpServerSocket* sock = *i;
+                       ++i;
+                       if (sock->GetModHook(mod))
+                       {
+                               sock->cull();
+                               delete sock;
+                       }
+               }
+       }
+
        CullResult cull() CXX11_OVERRIDE
        {
                for (insp::intrusive_list<HttpServerSocket>::const_iterator i = sockets.begin(); i != sockets.end(); ++i)