From 96dd0a52d93219a214130c18fe34bb81ce5acdbd Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Wed, 11 Feb 2015 17:09:59 +0100 Subject: Convert the HTTPd ACL event to use the new cross-module event system --- src/modules/m_httpd.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/modules/m_httpd.cpp') diff --git a/src/modules/m_httpd.cpp b/src/modules/m_httpd.cpp index bbd9f1275..0dd028924 100644 --- a/src/modules/m_httpd.cpp +++ b/src/modules/m_httpd.cpp @@ -31,6 +31,7 @@ class ModuleHttpServer; static ModuleHttpServer* HttpModule; static bool claimed; static insp::intrusive_list sockets; +static Events::ModuleEventProvider* aclevprov; /** HTTP socket states */ @@ -323,9 +324,10 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru InternalState = HTTP_SERVE_SEND_DATA; claimed = false; + ModResult MOD_RESULT; HTTPRequest acl((Module*)HttpModule, "httpd_acl", request_type, uri, &headers, this, ip, postdata); - acl.Send(); - if (!claimed) + 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(); @@ -372,11 +374,14 @@ class ModuleHttpServer : public Module { HTTPdAPIImpl APIImpl; unsigned int timeoutsec; + Events::ModuleEventProvider acleventprov; public: ModuleHttpServer() : APIImpl(this) + , acleventprov(this, "event/http-acl") { + aclevprov = &acleventprov; } void init() CXX11_OVERRIDE -- cgit v1.2.3 From 1e8e379dce7a3311eff7161269593f1655f577eb Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Wed, 11 Feb 2015 17:13:08 +0100 Subject: Convert the HTTPd request event to use the new cross-module event system --- include/modules/httpd.h | 15 +++++++++++++-- src/modules/m_httpd.cpp | 8 ++++++-- src/modules/m_httpd_config.cpp | 16 +++++++++++----- src/modules/m_httpd_stats.cpp | 16 +++++++++++----- 4 files changed, 41 insertions(+), 14 deletions(-) (limited to 'src/modules/m_httpd.cpp') diff --git a/include/modules/httpd.h b/include/modules/httpd.h index 5e3e780dc..60032cb16 100644 --- a/include/modules/httpd.h +++ b/include/modules/httpd.h @@ -108,7 +108,7 @@ class HttpServerSocket; /** This class represents a HTTP request. */ -class HTTPRequest : public Event +class HTTPRequest { protected: std::string type; @@ -137,7 +137,7 @@ class HTTPRequest : public Event */ HTTPRequest(Module* me, const std::string &eventid, const std::string &request_type, const std::string &uri, HTTPHeaders* hdr, HttpServerSocket* socket, const std::string &ip, const std::string &pdata) - : Event(me, eventid), type(request_type), document(uri), ipaddr(ip), postdata(pdata), headers(hdr), sock(socket) + : type(request_type), document(uri), ipaddr(ip), postdata(pdata), headers(hdr), sock(socket) { } @@ -249,3 +249,14 @@ class HTTPACLEventListener : public Events::ModuleEventListener virtual ModResult OnHTTPACLCheck(HTTPRequest& req) = 0; }; + +class HTTPRequestEventListener : public Events::ModuleEventListener +{ + public: + HTTPRequestEventListener(Module* mod) + : ModuleEventListener(mod, "event/http-request") + { + } + + virtual ModResult OnHTTPRequest(HTTPRequest& req) = 0; +}; diff --git a/src/modules/m_httpd.cpp b/src/modules/m_httpd.cpp index 0dd028924..3558b0a2b 100644 --- a/src/modules/m_httpd.cpp +++ b/src/modules/m_httpd.cpp @@ -32,6 +32,7 @@ static ModuleHttpServer* HttpModule; static bool claimed; static insp::intrusive_list sockets; static Events::ModuleEventProvider* aclevprov; +static Events::ModuleEventProvider* reqevprov; /** HTTP socket states */ @@ -330,8 +331,8 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru if (MOD_RESULT != MOD_RES_DENY) { HTTPRequest url((Module*)HttpModule, "httpd_url", request_type, uri, &headers, this, ip, postdata); - url.Send(); - if (!claimed) + FIRST_MOD_RESULT_CUSTOM(*reqevprov, HTTPRequestEventListener, OnHTTPRequest, MOD_RESULT, (url)); + if (MOD_RESULT == MOD_RES_PASSTHRU) { SendHTTPError(404); } @@ -375,13 +376,16 @@ 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 diff --git a/src/modules/m_httpd_config.cpp b/src/modules/m_httpd_config.cpp index 3c4680799..6fd7f4050 100644 --- a/src/modules/m_httpd_config.cpp +++ b/src/modules/m_httpd_config.cpp @@ -21,13 +21,14 @@ #include "inspircd.h" #include "modules/httpd.h" -class ModuleHttpConfig : public Module +class ModuleHttpConfig : public Module, public HTTPRequestEventListener { HTTPdAPI API; public: ModuleHttpConfig() - : API(this) + : HTTPRequestEventListener(this) + , API(this) { } @@ -65,14 +66,12 @@ class ModuleHttpConfig : public Module return ret; } - void OnEvent(Event& event) CXX11_OVERRIDE + ModResult HandleRequest(HTTPRequest* http) { std::stringstream data(""); - if (event.id == "httpd_url") { ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event"); - HTTPRequest* http = (HTTPRequest*)&event; if ((http->GetURI() == "/config") || (http->GetURI() == "/config/")) { @@ -96,8 +95,15 @@ class ModuleHttpConfig : public Module response.headers.SetHeader("X-Powered-By", MODNAME); response.headers.SetHeader("Content-Type", "text/html"); API->SendResponse(response); + return MOD_RES_DENY; // Handled } } + return MOD_RES_PASSTHRU; + } + + ModResult OnHTTPRequest(HTTPRequest& req) CXX11_OVERRIDE + { + return HandleRequest(&req); } Version GetVersion() CXX11_OVERRIDE diff --git a/src/modules/m_httpd_stats.cpp b/src/modules/m_httpd_stats.cpp index 30eacd7a7..983566e55 100644 --- a/src/modules/m_httpd_stats.cpp +++ b/src/modules/m_httpd_stats.cpp @@ -25,14 +25,15 @@ #include "modules/httpd.h" #include "xline.h" -class ModuleHttpStats : public Module +class ModuleHttpStats : public Module, public HTTPRequestEventListener { static const insp::flat_map& entities; HTTPdAPI API; public: ModuleHttpStats() - : API(this) + : HTTPRequestEventListener(this) + , API(this) { } @@ -87,14 +88,12 @@ class ModuleHttpStats : public Module data << ""; } - void OnEvent(Event& event) CXX11_OVERRIDE + ModResult HandleRequest(HTTPRequest* http) { std::stringstream data(""); - if (event.id == "httpd_url") { ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event"); - HTTPRequest* http = (HTTPRequest*)&event; if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/")) { @@ -231,8 +230,15 @@ class ModuleHttpStats : public Module response.headers.SetHeader("X-Powered-By", MODNAME); response.headers.SetHeader("Content-Type", "text/xml"); API->SendResponse(response); + return MOD_RES_DENY; // Handled } } + return MOD_RES_PASSTHRU; + } + + ModResult OnHTTPRequest(HTTPRequest& req) CXX11_OVERRIDE + { + return HandleRequest(&req); } Version GetVersion() CXX11_OVERRIDE -- cgit v1.2.3 From 3b83968416bcc5710c394892bb2ac1cc79f0298a Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Wed, 11 Feb 2015 17:15:57 +0100 Subject: Remove unused parameters from HTTPRequest constructor --- include/modules/httpd.h | 2 +- src/modules/m_httpd.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/modules/m_httpd.cpp') diff --git a/include/modules/httpd.h b/include/modules/httpd.h index 60032cb16..b4b88bed5 100644 --- a/include/modules/httpd.h +++ b/include/modules/httpd.h @@ -135,7 +135,7 @@ class HTTPRequest * @param ip The IP address making the web request. * @param pdata The post data (content after headers) received with the request, up to Content-Length in size */ - HTTPRequest(Module* me, const std::string &eventid, const std::string &request_type, const std::string &uri, + HTTPRequest(const std::string& request_type, const std::string& uri, HTTPHeaders* hdr, HttpServerSocket* socket, const std::string &ip, const std::string &pdata) : type(request_type), document(uri), ipaddr(ip), postdata(pdata), headers(hdr), sock(socket) { diff --git a/src/modules/m_httpd.cpp b/src/modules/m_httpd.cpp index 3558b0a2b..47416406e 100644 --- a/src/modules/m_httpd.cpp +++ b/src/modules/m_httpd.cpp @@ -326,11 +326,11 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru claimed = false; ModResult MOD_RESULT; - HTTPRequest acl((Module*)HttpModule, "httpd_acl", request_type, uri, &headers, this, ip, postdata); + 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); + 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) { -- cgit v1.2.3 From 7918febc630d7d6f94fda1a918fe7b98c2d0e742 Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Wed, 11 Feb 2015 17:26:16 +0100 Subject: m_httpd Remove now unused variable "claimed" --- src/modules/m_httpd.cpp | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/modules/m_httpd.cpp') diff --git a/src/modules/m_httpd.cpp b/src/modules/m_httpd.cpp index 47416406e..e09ca3fa2 100644 --- a/src/modules/m_httpd.cpp +++ b/src/modules/m_httpd.cpp @@ -29,7 +29,6 @@ class ModuleHttpServer; static ModuleHttpServer* HttpModule; -static bool claimed; static insp::intrusive_list sockets; static Events::ModuleEventProvider* aclevprov; static Events::ModuleEventProvider* reqevprov; @@ -324,7 +323,6 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru { InternalState = HTTP_SERVE_SEND_DATA; - claimed = false; ModResult MOD_RESULT; HTTPRequest acl(request_type, uri, &headers, this, ip, postdata); FIRST_MOD_RESULT_CUSTOM(*aclevprov, HTTPACLEventListener, OnHTTPACLCheck, MOD_RESULT, (acl)); @@ -366,7 +364,6 @@ class HTTPdAPIImpl : public HTTPdAPIBase void SendResponse(HTTPDocumentResponse& resp) CXX11_OVERRIDE { - claimed = true; resp.src.sock->Page(resp.document, resp.responsecode, &resp.headers); } }; -- cgit v1.2.3