From 1507c7950363e7fc81eacd821d72b5b4d57caaad Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 24 May 2017 09:05:03 +0100 Subject: [PATCH] Clean up and make a separate class for pasties from curl-style requests. --- src/tawashi/main.cpp | 4 +- src/tawashi_implem/CMakeLists.txt | 1 + .../quick_submit_paste_response.cpp | 42 ++++++++++++++++++ .../quick_submit_paste_response.hpp | 44 +++++++++++++++++++ src/tawashi_implem/submit_paste_response.cpp | 17 ++----- src/tawashi_implem/submit_paste_response.hpp | 10 ++--- 6 files changed, 96 insertions(+), 22 deletions(-) create mode 100644 src/tawashi_implem/quick_submit_paste_response.cpp create mode 100644 src/tawashi_implem/quick_submit_paste_response.hpp diff --git a/src/tawashi/main.cpp b/src/tawashi/main.cpp index 1307953..1e0f148 100644 --- a/src/tawashi/main.cpp +++ b/src/tawashi/main.cpp @@ -17,6 +17,7 @@ #include "tawashiConfig.h" #include "submit_paste_response.hpp" +#include "quick_submit_paste_response.hpp" #include "pastie_response.hpp" #include "index_response.hpp" #include "error_response.hpp" @@ -133,6 +134,7 @@ int main (int parArgc, char* parArgv[], char* parEnvp[]) { using curry::SafeStackObject; using tawashi::IndexResponse; using tawashi::SubmitPasteResponse; + using tawashi::QuickSubmitPasteResponse; using tawashi::PastieResponse; using tawashi::ErrorResponse; using tawashi::Response; @@ -158,7 +160,7 @@ int main (int parArgc, char* parArgv[], char* parEnvp[]) { SPDLOG_TRACE(statuslog, "Registering makers in the response factory"); resp_factory.register_maker("index.cgi", RequestMethodType::GET, &make_response); resp_factory.register_maker("", RequestMethodType::GET, &make_response); - resp_factory.register_maker("", RequestMethodType::POST, &make_response); + resp_factory.register_maker("", RequestMethodType::POST, &make_response); resp_factory.register_maker("paste.cgi", RequestMethodType::POST, &make_response); resp_factory.register_maker("error.cgi", RequestMethodType::GET, &make_response); resp_factory.register_jolly_maker(&make_response); diff --git a/src/tawashi_implem/CMakeLists.txt b/src/tawashi_implem/CMakeLists.txt index 6c89619..9ea67ea 100644 --- a/src/tawashi_implem/CMakeLists.txt +++ b/src/tawashi_implem/CMakeLists.txt @@ -27,6 +27,7 @@ add_library(${PROJECT_NAME} STATIC error_response.cpp tawashi_exception.cpp http_header.cpp + quick_submit_paste_response.cpp ) target_include_directories(${PROJECT_NAME} diff --git a/src/tawashi_implem/quick_submit_paste_response.cpp b/src/tawashi_implem/quick_submit_paste_response.cpp new file mode 100644 index 0000000..26e49f8 --- /dev/null +++ b/src/tawashi_implem/quick_submit_paste_response.cpp @@ -0,0 +1,42 @@ +/* Copyright 2017, Michele Santullo + * This file is part of "tawashi". + * + * "tawashi" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "tawashi" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "tawashi". If not, see . + */ + +#include "quick_submit_paste_response.hpp" + +namespace tawashi { + QuickSubmitPasteResponse::QuickSubmitPasteResponse ( + const Kakoune::SafePtr& parSettings, + std::ostream* parStreamOut, + const Kakoune::SafePtr& parCgiEnv + ) : + SubmitPasteResponse(parSettings, parStreamOut, parCgiEnv) + { + } + + void QuickSubmitPasteResponse::on_mustache_prepare (mstch::map& parContext) { + parContext["redirect_to_address"] = m_redirect_to; + } + + std::string QuickSubmitPasteResponse::on_mustache_retrieve() { + return "{{base_uri}}/{{redirect_to_address}}\n"; + } + + HttpHeader QuickSubmitPasteResponse::make_success_response (std::string&& parPastieParam) { + m_redirect_to = std::move(parPastieParam); + return HttpHeader(); + } +} //namespace tawashi diff --git a/src/tawashi_implem/quick_submit_paste_response.hpp b/src/tawashi_implem/quick_submit_paste_response.hpp new file mode 100644 index 0000000..d65ba20 --- /dev/null +++ b/src/tawashi_implem/quick_submit_paste_response.hpp @@ -0,0 +1,44 @@ +/* Copyright 2017, Michele Santullo + * This file is part of "tawashi". + * + * "tawashi" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "tawashi" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "tawashi". If not, see . + */ + +#pragma once + +#include "submit_paste_response.hpp" +#include +#include +#include + +namespace tawashi { + class QuickSubmitPasteResponse : public SubmitPasteResponse { + public: + QuickSubmitPasteResponse ( + const Kakoune::SafePtr& parSettings, + std::ostream* parStreamOut, + const Kakoune::SafePtr& parCgiEnv + ); + + protected: + virtual boost::string_ref page_basename() const override { assert(false); return boost::string_ref(""); } + virtual HttpHeader make_success_response (std::string&& parPastieParam) override; + + private: + virtual void on_mustache_prepare (mstch::map& parContext) override; + virtual std::string on_mustache_retrieve() override; + + std::string m_redirect_to; + }; +} //namespace tawashi diff --git a/src/tawashi_implem/submit_paste_response.cpp b/src/tawashi_implem/submit_paste_response.cpp index 33a11f4..8c9a13f 100644 --- a/src/tawashi_implem/submit_paste_response.cpp +++ b/src/tawashi_implem/submit_paste_response.cpp @@ -156,14 +156,7 @@ namespace tawashi { if (not lang.empty()) oss << '?' << lang; - //TODO: clean up this hack, make a separate class or something - if (cgi_env().path_info().empty()) { - m_redirect_to = oss.str(); - return HttpHeader(); - } - else { - return this->make_redirect(HttpStatusCodes::Code303_SeeOther, oss.str()); - } + return this->make_success_response(oss.str()); } else { statuslog->info("Empty pastie token (possibly due to a previous failure)"); @@ -204,11 +197,7 @@ namespace tawashi { return std::make_pair(boost::optional(), make_error_redirect(ErrorReasons::PastieNotSaved)); } - void SubmitPasteResponse::on_mustache_prepare (mstch::map& parContext) { - parContext["redirect_to_address"] = m_redirect_to; - } - - std::string SubmitPasteResponse::on_mustache_retrieve() { - return "{{base_uri}}/{{redirect_to_address}}\n"; + HttpHeader SubmitPasteResponse::make_success_response (std::string&& parPastieParam) { + return this->make_redirect(HttpStatusCodes::Code303_SeeOther, std::move(parPastieParam)); } } //namespace tawashi diff --git a/src/tawashi_implem/submit_paste_response.hpp b/src/tawashi_implem/submit_paste_response.hpp index e6c242d..0522fdf 100644 --- a/src/tawashi_implem/submit_paste_response.hpp +++ b/src/tawashi_implem/submit_paste_response.hpp @@ -35,16 +35,12 @@ namespace tawashi { protected: virtual boost::string_ref page_basename() const override { assert(false); return boost::string_ref(""); } + virtual HttpHeader make_success_response (std::string&& parPastieParam); private: typedef std::pair, HttpHeader> StringOrHeader; - virtual HttpHeader on_process() override; - virtual void on_mustache_prepare (mstch::map& parContext) override; - virtual std::string on_mustache_retrieve() override; - StringOrHeader submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang); - //TODO: remove, this shouldn't be in this class, nor on_mustache_prepare - //or on_mustache_retrieve. - std::string m_redirect_to; + virtual HttpHeader on_process() override; + StringOrHeader submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang); }; } //namespace tawashi