From 5377d25c72a12c0d5370a45708650e6d9de963fc Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 26 Apr 2017 09:17:57 +0100 Subject: [PATCH] Simplify the response pipeline. There is no on_send() anymore, instead responses can override the way the top-level mustache file is retrieved. --- src/pastie_response.cpp | 61 +++++++++++++---------------------- src/pastie_response.hpp | 2 +- src/response.cpp | 25 ++++---------- src/response.hpp | 6 ++-- src/submit_paste_response.cpp | 6 ---- src/submit_paste_response.hpp | 1 - 6 files changed, 32 insertions(+), 69 deletions(-) diff --git a/src/pastie_response.cpp b/src/pastie_response.cpp index 7ce2fba..9b3ae8c 100644 --- a/src/pastie_response.cpp +++ b/src/pastie_response.cpp @@ -51,49 +51,34 @@ namespace tawashi { using opt_string = redis::IncRedis::opt_string; using opt_string_list = redis::IncRedis::opt_string_list; - if (not m_plain_text) { - call_on_send(false); - - auto token = boost::string_ref(cgi_env().path_info()).substr(1); - auto& redis = this->redis(); - opt_string_list pastie_reply = redis.hmget(token, "pastie"); - opt_string pastie = (pastie_reply and not pastie_reply->empty() ? (*pastie_reply)[0] : opt_string()); - - srchilite::SourceHighlight highlighter; - highlighter.setDataDir(settings().as("langmap_dir")); - highlighter.setGenerateEntireDoc(false); - highlighter.setGenerateLineNumbers(true); - const auto lang = m_lang_file; - //Escapist houdini; - //std::istringstream iss(houdini.escape_html(*pastie)); - std::istringstream iss(*pastie); - - std::ostringstream oss; - highlighter.highlight(iss, oss, lang); - - parContext["pastie"] = oss.str(); - } - } - - void PastieResponse::on_send (std::ostream& parStream) { - using opt_string = redis::IncRedis::opt_string; - using opt_string_list = redis::IncRedis::opt_string_list; - - if (cgi_env().path_info().empty()) { - return; - } - - assert(m_plain_text); - auto token = boost::string_ref(cgi_env().path_info()).substr(1); auto& redis = this->redis(); opt_string_list pastie_reply = redis.hmget(token, "pastie"); opt_string pastie = (pastie_reply and not pastie_reply->empty() ? (*pastie_reply)[0] : opt_string()); - if (not pastie) { - assert(false); - } + srchilite::SourceHighlight highlighter; + highlighter.setDataDir(settings().as("langmap_dir")); + highlighter.setGenerateEntireDoc(false); + highlighter.setGenerateLineNumbers(true); + const auto lang = m_lang_file; + //Escapist houdini; + //std::istringstream iss(houdini.escape_html(*pastie)); - parStream << *pastie; + if (m_plain_text) { + parContext["pastie"] = *pastie; + } + else { + std::istringstream iss(*pastie); + std::ostringstream oss; + highlighter.highlight(iss, oss, lang); + parContext["pastie"] = oss.str(); + } + } + + std::string PastieResponse::on_mustache_retrieve() { + if (m_plain_text) + return "{{pastie}}"; + else + return load_mustache(); } } //namespace tawashi diff --git a/src/pastie_response.hpp b/src/pastie_response.hpp index 1df5136..0a3da11 100644 --- a/src/pastie_response.hpp +++ b/src/pastie_response.hpp @@ -28,8 +28,8 @@ namespace tawashi { private: virtual void on_process() override; - virtual void on_send (std::ostream& parStream) override; virtual void on_mustache_prepare (mstch::map& parContext) override; + virtual std::string on_mustache_retrieve() override; std::string m_lang_file; std::string m_langmap_dir; diff --git a/src/response.cpp b/src/response.cpp index a7678ea..9fcf947 100644 --- a/src/response.cpp +++ b/src/response.cpp @@ -110,8 +110,7 @@ namespace tawashi { m_website_root(make_root_path(*parSettings)), m_page_basename(std::move(parPageBaseName)), m_resp_type(parRespType), - m_header_sent(false), - m_call_derived_on_send(true) + m_header_sent(false) { if (parWantRedis) { m_redis = std::make_unique(make_incredis(*parSettings)); @@ -126,10 +125,6 @@ namespace tawashi { void Response::on_process() { } - void Response::on_send (std::ostream& parStream) { - parStream << load_mustache(); - } - void Response::on_mustache_prepare (mstch::map&) { } @@ -161,16 +156,8 @@ namespace tawashi { break; } - std::ostringstream stream_out; - if (ContentType == m_resp_type) { - if (m_call_derived_on_send) - this->on_send(stream_out); - else - Response::on_send(stream_out); - } - std::cout << mstch::render( - stream_out.str(), + on_mustache_retrieve(), mustache_context, std::bind( &load_whole_file, @@ -183,6 +170,10 @@ namespace tawashi { std::cout.flush(); } + std::string Response::on_mustache_retrieve() { + return load_mustache(); + } + const cgi::Env& Response::cgi_env() const { return m_cgi_env; } @@ -216,8 +207,4 @@ namespace tawashi { assert(m_settings); return *m_settings; } - - void Response::call_on_send (bool parCall) { - m_call_derived_on_send = parCall; - } } //namespace tawashi diff --git a/src/response.hpp b/src/response.hpp index fb6111b..05bd8ea 100644 --- a/src/response.hpp +++ b/src/response.hpp @@ -49,15 +49,14 @@ namespace tawashi { void change_type (Types parRespType, std::string&& parValue); const boost::string_ref& base_uri() const; const std::string& page_basename() const; - std::string load_mustache() const; redis::IncRedis& redis() const; const SettingsBag& settings() const; - void call_on_send (bool parCall); + virtual std::string load_mustache() const; private: virtual void on_process(); - virtual void on_send (std::ostream& parStream); virtual void on_mustache_prepare (mstch::map& parContext); + virtual std::string on_mustache_retrieve(); cgi::Env m_cgi_env; std::string m_resp_value; @@ -67,6 +66,5 @@ namespace tawashi { Types m_resp_type; std::unique_ptr m_redis; bool m_header_sent; - bool m_call_derived_on_send; }; } //namespace tawashi diff --git a/src/submit_paste_response.cpp b/src/submit_paste_response.cpp index d609c8a..4bc0fc2 100644 --- a/src/submit_paste_response.cpp +++ b/src/submit_paste_response.cpp @@ -108,12 +108,6 @@ namespace tawashi { } } - void SubmitPasteResponse::on_send (std::ostream& parStream) { - assert(not m_error_message.empty()); - parStream << "something happened? :/\n" << - m_error_message << '\n'; - } - boost::optional SubmitPasteResponse::submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang) const { auto& redis = this->redis(); if (not redis.is_connected()) diff --git a/src/submit_paste_response.hpp b/src/submit_paste_response.hpp index 9641277..e6e44a8 100644 --- a/src/submit_paste_response.hpp +++ b/src/submit_paste_response.hpp @@ -29,7 +29,6 @@ namespace tawashi { private: virtual void on_process() override; - virtual void on_send (std::ostream& parStream) override; boost::optional submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang) const; std::string m_error_message;