diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2021-06-21 19:55:06 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2021-06-21 19:55:06 +0200 |
commit | ec7cfa85530082127703278cf1ae5167990c0f45 (patch) | |
tree | 81799c532da3c213609471cfcfa785bfda3b2ee0 | |
parent | 93d69841064fd5c3156d989caf7ddec46bfb8685 (diff) |
Request/Response handling w/ libevent2
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | src/Content.cpp | 8 | ||||
-rw-r--r-- | src/Content.hpp | 15 | ||||
-rw-r--r-- | src/ContentManager.cpp | 2 | ||||
-rw-r--r-- | src/EventManager.cpp | 27 | ||||
-rw-r--r-- | src/EventManager.hpp | 18 | ||||
-rw-r--r-- | src/RequestResponse.cpp | 47 | ||||
-rw-r--r-- | src/RequestResponse.hpp | 28 | ||||
-rw-r--r-- | src/content/blog/Blog.cpp | 14 | ||||
-rw-r--r-- | src/content/blog/Blog.hpp | 10 | ||||
-rw-r--r-- | src/main.cpp | 2 |
10 files changed, 129 insertions, 42 deletions
diff --git a/src/Content.cpp b/src/Content.cpp index 36138a7..b2977bb 100644 --- a/src/Content.cpp +++ b/src/Content.cpp @@ -13,14 +13,16 @@ void TemplatedContent::Shutdown() { } -bool TemplatedContent::Render(std::string & out) +bool TemplatedContent::Render(RequestResponse & rr, std::string & out) { + (void)rr; + out = "tmpl"; return false; } -void TemplatedContent::SetTemplateData(nlohmann::json & data) +void TemplatedContent::GetRequiredFiles(std::vector<std::string> & requiredFiles) const { - m_TemplateData = data; + requiredFiles.push_back(m_FilesystemPath); } diff --git a/src/Content.hpp b/src/Content.hpp index 6a1d422..c379522 100644 --- a/src/Content.hpp +++ b/src/Content.hpp @@ -1,6 +1,8 @@ #ifndef CONTENT_H #define CONTENT_H 1 +#include "RequestResponse.hpp" + #include <inja/inja.hpp> #include <string> #include <tuple> @@ -12,25 +14,24 @@ class Content { public: virtual bool Init() = 0; virtual void Shutdown() = 0; - virtual bool Render(std::string & out) = 0; + virtual bool Render(RequestResponse & rr, std::string & out) = 0; - virtual std::string const & GetBasePath() const = 0; + virtual std::string const & GetBaseUri() const = 0; virtual Redirections const & GetRedirections() const = 0; + virtual void GetRequiredFiles(std::vector<std::string> & requiredFiles) const = 0; }; class TemplatedContent : public Content { public: - explicit TemplatedContent(std::string filesystemPath); + explicit TemplatedContent(std::string mainTemplate); virtual bool Init(); virtual void Shutdown(); - virtual bool Render(std::string & out); - - void SetTemplateData(nlohmann::json & data); + virtual bool Render(RequestResponse & rr, std::string & out); + virtual void GetRequiredFiles(std::vector<std::string> & requiredFiles) const; private: std::string m_FilesystemPath; - nlohmann::json m_TemplateData; }; #endif diff --git a/src/ContentManager.cpp b/src/ContentManager.cpp index 951c10e..a1787bd 100644 --- a/src/ContentManager.cpp +++ b/src/ContentManager.cpp @@ -2,7 +2,7 @@ bool ContentManager::RegisterModule(std::shared_ptr<Content> ctnt) { - std::string const & basePath = ctnt->GetBasePath(); + std::string const & basePath = ctnt->GetBaseUri(); Redirections const & rs = ctnt->GetRedirections(); m_ContentModules[basePath] = ctnt; diff --git a/src/EventManager.cpp b/src/EventManager.cpp index 6921b82..ad480bc 100644 --- a/src/EventManager.cpp +++ b/src/EventManager.cpp @@ -37,10 +37,11 @@ static void EvContentManagerInterceptor(struct evhttp_request * const req, void * ev_c_callback) { if (ev_c_callback != nullptr) { Content * const cntnt = (Content *)ev_c_callback; + RequestResponse rr(req); std::string out; - if (cntnt->Render(out) == false) { + if (cntnt->Render(rr, out) == false) { std::string text; - text = "ContentModule(\"" + cntnt->GetBasePath() + "\")->Render() failed.\n"; + text = "ContentModule(\"" + cntnt->GetBaseUri() + "\")->Render() failed.\n"; GenerateInternalErrorPage(req, text); } else { evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", @@ -49,7 +50,7 @@ static void EvContentManagerInterceptor(struct evhttp_request * const req, struct evbuffer *const output = evbuffer_new(); if (output != nullptr) { evbuffer_add(output, out.c_str(), out.size()); - evhttp_send_reply(req, 200, "OK", output); + evhttp_send_reply(req, HTTP_OK, "OK", output); evbuffer_free(output); } } @@ -66,6 +67,26 @@ static void do_term(int sig, short events, void *arg) { } } +static inline void default_evhttp_callback(struct evhttp_request * const req, + EvUserData ud) { + (void)ud; + + evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", + "text/html"); + + struct evbuffer *const output = evbuffer_new(); + if (output != nullptr) { + evbuffer_add_printf(output, "%s\n", + "<html><body><b>default page</b></body></html>"); + evhttp_send_reply(req, 200, "OK", output); + evbuffer_free(output); + } +} + +EventManager::EventManager() : m_DefaultCallback({default_evhttp_callback, nullptr}) +{ +} + EventManager::~EventManager() { if (m_EvConfig != nullptr) event_config_free(m_EvConfig); diff --git a/src/EventManager.hpp b/src/EventManager.hpp index eafaf0d..db9d461 100644 --- a/src/EventManager.hpp +++ b/src/EventManager.hpp @@ -23,25 +23,9 @@ struct ev_callback { typedef std::tuple<std::string, struct ev_callback> EvUrlCallback; -static inline void default_evhttp_callback(struct evhttp_request * const req, - EvUserData ud) { - (void)ud; - - evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", - "text/html"); - - struct evbuffer *const output = evbuffer_new(); - if (output != nullptr) { - evbuffer_add_printf(output, "%s\n", - "<html><body><b>default page</b></body></html>"); - evhttp_send_reply(req, 200, "OK", output); - evbuffer_free(output); - } -} - class EventManager { public: - EventManager() : m_DefaultCallback({default_evhttp_callback, nullptr}) {} + EventManager(); ~EventManager(); bool Init(std::string = "127.0.0.1", uint16_t port = 9000); diff --git a/src/RequestResponse.cpp b/src/RequestResponse.cpp new file mode 100644 index 0000000..be8a0d0 --- /dev/null +++ b/src/RequestResponse.cpp @@ -0,0 +1,47 @@ +#include "RequestResponse.hpp" + +RequestResponse::RequestResponse(struct evhttp_request * const req) : m_Request(req) +{ +} + +RequestResponse::~RequestResponse() +{ +} + +void RequestResponse::UseInputHeader() +{ + m_InputHeader = evhttp_request_get_input_headers(m_Request); +} + +void RequestResponse::UseOutputHeader() +{ + m_OutputHeader = evhttp_request_get_output_headers(m_Request); +} + +bool RequestResponse::AddOutputHeader(std::string & key, std::string & value) +{ + return evhttp_add_header(m_OutputHeader, key.c_str(), value.c_str()); +} + +bool RequestResponse::AddOutputHeader2(std::string key, std::string value) +{ + return AddOutputHeader(key, value); +} + +bool RequestResponse::GetInputHeader(std::string & key, std::string & value) +{ + char const * const v = evhttp_find_header(m_InputHeader, key.c_str()); + + if (v == nullptr) + { + return false; + } + + value = v; + return true; +} + +bool RequestResponse::GetInputHeader2(std::string key, std::string value) +{ + return GetInputHeader(key, value); +} diff --git a/src/RequestResponse.hpp b/src/RequestResponse.hpp new file mode 100644 index 0000000..24577aa --- /dev/null +++ b/src/RequestResponse.hpp @@ -0,0 +1,28 @@ +#ifndef REQUEST_RESPONSE_H +#define REQUEST_RESPONSE_H 1 + +#include <event2/http.h> + +#include <string> + +class RequestResponse { +public: + RequestResponse(struct evhttp_request * const req); + ~RequestResponse(); + + void UseInputHeader(); + void UseOutputHeader(); + + bool AddOutputHeader(std::string & key, std::string & value); + bool AddOutputHeader2(std::string key, std::string value); + + bool GetInputHeader(std::string & key, std::string & value); + bool GetInputHeader2(std::string key, std::string value); + +private: + struct evhttp_request * const m_Request; + struct evkeyvalq * m_InputHeader; + struct evkeyvalq * m_OutputHeader; +}; + +#endif diff --git a/src/content/blog/Blog.cpp b/src/content/blog/Blog.cpp index 3f1433d..f72ef70 100644 --- a/src/content/blog/Blog.cpp +++ b/src/content/blog/Blog.cpp @@ -1,8 +1,8 @@ #include "Blog.hpp" -Blog::Blog(std::string basePath) : m_BasePath(basePath), m_Redirections() +Blog::Blog(std::string baseUri, std::string templatePath) : TemplatedContent(templatePath), m_BaseUri(baseUri), m_Redirections() { - m_Redirections.push_back(basePath + "-data"); + m_Redirections.push_back(baseUri + "-data"); } bool Blog::Init(void) @@ -14,16 +14,20 @@ void Blog::Shutdown(void) { } -bool Blog::Render(std::string & out) +bool Blog::Render(RequestResponse & rr, std::string & out) { + (void)rr; + + rr.UseOutputHeader(); + rr.AddOutputHeader2("bla", "blubb"); out = "blog-bla"; return true; } -std::string const & Blog::GetBasePath(void) const +std::string const & Blog::GetBaseUri(void) const { - return m_BasePath; + return m_BaseUri; } Redirections const & diff --git a/src/content/blog/Blog.hpp b/src/content/blog/Blog.hpp index 316ce92..1de6e0f 100644 --- a/src/content/blog/Blog.hpp +++ b/src/content/blog/Blog.hpp @@ -3,20 +3,20 @@ #include "../../Content.hpp" -class Blog : public Content +class Blog : public TemplatedContent { public: - explicit Blog(std::string basePath); + explicit Blog(std::string baseUri, std::string templatePath); bool Init(void); void Shutdown(void); - bool Render(std::string & out); + bool Render(RequestResponse & rr, std::string & out); - std::string const & GetBasePath() const; + std::string const & GetBaseUri() const; Redirections const & GetRedirections() const; private: - std::string m_BasePath; + std::string m_BaseUri; Redirections m_Redirections; }; diff --git a/src/main.cpp b/src/main.cpp index cfa08c6..198f065 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -87,7 +87,7 @@ int main(int argc, char **argv) { fs.Scan(); ContentManager ctmgr; - ctmgr.RegisterModule(std::make_shared<Blog>("/blog")); + ctmgr.RegisterModule(std::make_shared<Blog>("/blog", "index.html")); if (ctmgr.InitAll() == false) { |