1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2025-07-02 14:04:16 +00:00

Clean up and make a separate class for pasties from curl-style requests.

This commit is contained in:
King_DuckZ 2017-05-24 09:05:03 +01:00
parent a2676ad5c7
commit 1507c79503
6 changed files with 96 additions and 22 deletions

View file

@ -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<IndexResponse>);
resp_factory.register_maker("", RequestMethodType::GET, &make_response<IndexResponse>);
resp_factory.register_maker("", RequestMethodType::POST, &make_response<SubmitPasteResponse>);
resp_factory.register_maker("", RequestMethodType::POST, &make_response<QuickSubmitPasteResponse>);
resp_factory.register_maker("paste.cgi", RequestMethodType::POST, &make_response<SubmitPasteResponse>);
resp_factory.register_maker("error.cgi", RequestMethodType::GET, &make_response<ErrorResponse>);
resp_factory.register_jolly_maker(&make_response<PastieResponse>);

View file

@ -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}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "quick_submit_paste_response.hpp"
namespace tawashi {
QuickSubmitPasteResponse::QuickSubmitPasteResponse (
const Kakoune::SafePtr<SettingsBag>& parSettings,
std::ostream* parStreamOut,
const Kakoune::SafePtr<cgi::Env>& 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

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "submit_paste_response.hpp"
#include <boost/optional.hpp>
#include <boost/utility/string_ref.hpp>
#include <cassert>
namespace tawashi {
class QuickSubmitPasteResponse : public SubmitPasteResponse {
public:
QuickSubmitPasteResponse (
const Kakoune::SafePtr<SettingsBag>& parSettings,
std::ostream* parStreamOut,
const Kakoune::SafePtr<cgi::Env>& 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

View file

@ -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<std::string>(), 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

View file

@ -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<boost::optional<std::string>, 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