]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Change the API of m_httpd to be dynamic_reference-based
authorattilamolnar <attilamolnar@hush.com>
Fri, 24 May 2013 17:34:25 +0000 (19:34 +0200)
committerattilamolnar <attilamolnar@hush.com>
Thu, 6 Jun 2013 23:00:10 +0000 (01:00 +0200)
include/modules/httpd.h
src/modules/m_httpd.cpp
src/modules/m_httpd_acl.cpp
src/modules/m_httpd_config.cpp
src/modules/m_httpd_stats.cpp

index d1746e862f99afc80fd598e68e54f6de40b3c4c6..3d94b3f42a687c6adcdc0b6305217d15a0860991 100644 (file)
@@ -177,27 +177,63 @@ class HTTPRequest : public Event
        }
 };
 
-/** You must return a HTTPDocument to the httpd module by using the Request class.
- * When you initialize this class you may initialize it with all components required to
- * form a valid HTTP response, including document data, headers, and a response code.
+/** If you want to reply to HTTP requests, you must return a HTTPDocumentResponse to
+ * the httpd module via the HTTPdAPI.
+ * When you initialize this class you initialize it with all components required to
+ * form a valid HTTP response: the document data and a response code.
+ * You can add additional HTTP headers, if you want.
  */
-class HTTPDocumentResponse : public Request
+class HTTPDocumentResponse
 {
+       /** Module that generated this reply
+        */
+       Module* const module;
+
  public:
        std::stringstream* document;
-       int responsecode;
+       unsigned int responsecode;
+
+       /** Any extra headers to include with the defaults
+        */
        HTTPHeaders headers;
+
        HTTPRequest& src;
 
-       /** Initialize a HTTPRequest ready for sending to m_httpd.so.
-        * @param opaque The socket pointer you obtained from the HTTPRequest at an earlier time
+       /** Initialize a HTTPDocumentResponse ready for sending to the httpd module.
+        * @param mod A pointer to the module who responded to the request
+        * @param req The request you obtained from the HTTPRequest at an earlier time
         * @param doc A stringstream containing the document body
         * @param response A valid HTTP/1.0 or HTTP/1.1 response code. The response text will be determined for you
         * based upon the response code.
-        * @param extra Any extra headers to include with the defaults, seperated by carriage return and linefeed.
         */
-       HTTPDocumentResponse(Module* me, HTTPRequest& req, std::stringstream* doc, int response)
-               : Request(me, req.source, "HTTP-DOC"), document(doc), responsecode(response), src(req)
+       HTTPDocumentResponse(Module* mod, HTTPRequest& req, std::stringstream* doc, unsigned int response)
+               : module(mod), document(doc), responsecode(response), src(req)
+       {
+       }
+};
+
+class HTTPdAPIBase : public DataProvider
+{
+ public:
+       HTTPdAPIBase(Module* parent)
+               : DataProvider(parent, "m_httpd_api")
+       {
+       }
+
+       /** Answer an incoming HTTP request with the provided document
+        * @param response The response created by your module that will be sent to the client
+        */
+       virtual void SendResponse(HTTPDocumentResponse& response) = 0;
+};
+
+/** The API provided by the httpd module that allows other modules to respond to incoming
+ * HTTP requests
+ */
+class HTTPdAPI : public dynamic_reference<HTTPdAPIBase>
+{
+ public:
+       HTTPdAPI(Module* parent)
+               : dynamic_reference<HTTPdAPIBase>(parent, "m_httpd_api")
        {
        }
 };
index 9d437bd1c248822845eaab1e2afc1b3932edd0d9..7cbef13e255ea2c4a6a3957537d0feebe99d06ac 100644 (file)
@@ -331,24 +331,37 @@ class HttpServerSocket : public BufferedSocket
        }
 };
 
+class HTTPdAPIImpl : public HTTPdAPIBase
+{
+ public:
+       HTTPdAPIImpl(Module* parent)
+               : HTTPdAPIBase(parent)
+       {
+       }
+
+       void SendResponse(HTTPDocumentResponse& resp) CXX11_OVERRIDE
+       {
+               claimed = true;
+               resp.src.sock->Page(resp.document, resp.responsecode, &resp.headers);
+       }
+};
+
 class ModuleHttpServer : public Module
 {
        std::vector<HttpServerSocket *> httpsocks;
+       HTTPdAPIImpl APIImpl;
 
  public:
-       void init() CXX11_OVERRIDE
+       ModuleHttpServer()
+               : APIImpl(this)
        {
-               HttpModule = this;
-               ServerInstance->Modules->Attach(I_OnAcceptConnection, this);
        }
 
-       void OnRequest(Request& request) CXX11_OVERRIDE
+       void init() CXX11_OVERRIDE
        {
-               if (strcmp(request.id, "HTTP-DOC") != 0)
-                       return;
-               HTTPDocumentResponse& resp = static_cast<HTTPDocumentResponse&>(request);
-               claimed = true;
-               resp.src.sock->Page(resp.document, resp.responsecode, &resp.headers);
+               HttpModule = this;
+               ServerInstance->Modules->AddService(APIImpl);
+               ServerInstance->Modules->Attach(I_OnAcceptConnection, this);
        }
 
        ModResult OnAcceptConnection(int nfd, ListenSocket* from, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) CXX11_OVERRIDE
index 061d56f254ec5082657e5daa75c71f073f3d1b45..676b35ca4ec1ab7d029ea306608e5dd89c0f8ad7 100644 (file)
@@ -43,8 +43,14 @@ class ModuleHTTPAccessList : public Module
 {
        std::string stylesheet;
        std::vector<HTTPACL> acl_list;
+       HTTPdAPI API;
 
  public:
+       ModuleHTTPAccessList()
+               : API(this)
+       {
+       }
+
        void OnRehash(User* user)
        {
                acl_list.clear();
@@ -105,7 +111,7 @@ class ModuleHTTPAccessList : public Module
                response.headers.SetHeader("X-Powered-By", "m_httpd_acl.so");
                if (!extraheaderkey.empty())
                        response.headers.SetHeader(extraheaderkey, extraheaderval);
-               response.Send();
+               API->SendResponse(response);
        }
 
        void OnEvent(Event& event) CXX11_OVERRIDE
index a9fdd44fb3b96e3a18f172c8fcc285f461f64a01..3396361d085b5b3c52f98662c9f768497a337dcc 100644 (file)
 
 class ModuleHttpConfig : public Module
 {
+       HTTPdAPI API;
+
  public:
+       ModuleHttpConfig()
+               : API(this)
+       {
+       }
+
        void init() CXX11_OVERRIDE
        {
                Implementation eventlist[] = { I_OnEvent };
@@ -97,7 +104,7 @@ class ModuleHttpConfig : public Module
                                HTTPDocumentResponse response(this, *http, &data, 200);
                                response.headers.SetHeader("X-Powered-By", "m_httpd_config.so");
                                response.headers.SetHeader("Content-Type", "text/html");
-                               response.Send();
+                               API->SendResponse(response);
                        }
                }
        }
index 99909d97fac76c47fc72ad3e0f8e222f47e30bbb..faf42d071e5793d7f0c94aab7f936b154f08221c 100644 (file)
 class ModuleHttpStats : public Module
 {
        static std::map<char, char const*> const &entities;
+       HTTPdAPI API;
 
  public:
+       ModuleHttpStats()
+               : API(this)
+       {
+       }
 
        void init() CXX11_OVERRIDE
        {
@@ -233,7 +238,7 @@ class ModuleHttpStats : public Module
                                HTTPDocumentResponse response(this, *http, &data, 200);
                                response.headers.SetHeader("X-Powered-By", "m_httpd_stats.so");
                                response.headers.SetHeader("Content-Type", "text/xml");
-                               response.Send();
+                               API->SendResponse(response);
                        }
                }
        }