1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2025-01-13 19:56:40 +00:00
kamokan/src/kamokan_impl/submit_paste_response.cpp

181 lines
5.9 KiB
C++
Raw Normal View History

2017-04-06 22:35:06 +00:00
/* Copyright 2017, Michele Santullo
* This file is part of "kamokan".
2017-04-06 22:35:06 +00:00
*
* "kamokan" is free software: you can redistribute it and/or modify
2017-04-06 22:35:06 +00:00
* 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.
*
* "kamokan" is distributed in the hope that it will be useful,
2017-04-06 22:35:06 +00:00
* 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 "kamokan". If not, see <http://www.gnu.org/licenses/>.
2017-04-06 22:35:06 +00:00
*/
2017-04-06 21:45:44 +00:00
#include "submit_paste_response.hpp"
#include "storage.hpp"
2017-04-06 20:27:38 +00:00
#include "cgi_post.hpp"
#include "settings_bag.hpp"
2017-04-24 23:21:44 +00:00
#include "duckhandy/compatibility.h"
#include "duckhandy/lexical_cast.hpp"
#include "tawashi_exception.hpp"
#include "ip_utils.hpp"
#include <ciso646>
2017-04-24 23:21:44 +00:00
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <cstdint>
#include <spdlog/spdlog.h>
namespace kamokan {
2017-04-06 20:27:38 +00:00
namespace {
const char g_post_key[] = "pastie";
2017-04-24 23:21:44 +00:00
const char g_language_key[] = "lang";
const char g_duration_key[] = "ttl";
class MissingPostVarError : public tawashi::Exception {
2017-04-24 23:21:44 +00:00
public:
2017-06-06 22:04:40 +00:00
explicit MissingPostVarError(const boost::string_view& parKey) :
tawashi::Exception(
tawashi::ErrorReasons::MissingPostVariable,
"Error retrieving POST variable \"" + std::string(parKey.begin(), parKey.end()) + "\""
)
{}
2017-04-24 23:21:44 +00:00
};
template <std::size_t N>
2017-06-06 22:04:40 +00:00
inline boost::string_view make_string_view (const char (&parStr)[N]) a_always_inline;
2017-04-24 23:21:44 +00:00
template <std::size_t N>
2017-06-06 22:04:40 +00:00
boost::string_view make_string_view (const char (&parStr)[N]) {
2017-04-24 23:21:44 +00:00
static_assert(N > 0, "wat?");
2017-06-06 22:04:40 +00:00
return boost::string_view(parStr, N - 1);
2017-04-24 23:21:44 +00:00
}
2017-06-06 22:04:40 +00:00
boost::string_view get_value_from_post (const cgi::PostMapType& parPost, boost::string_view parKey) {
2017-06-06 23:19:53 +00:00
std::string key(parKey);
2017-04-24 23:21:44 +00:00
auto post_data_it = parPost.find(key);
if (parPost.end() == post_data_it)
throw MissingPostVarError(parKey);
2017-04-24 23:21:44 +00:00
return post_data_it->second;
}
2017-06-06 22:04:40 +00:00
boost::string_view get_value_from_post_log_failure (const cgi::PostMapType& parPost, boost::string_view parKey) {
try {
return get_value_from_post(parPost, parKey);
}
catch (const MissingPostVarError& e) {
spdlog::get("statuslog")->info(e.what());
2017-06-06 22:04:40 +00:00
return boost::string_view();
}
}
2017-04-06 20:27:38 +00:00
} //unnamed namespace
#if defined(KAMOKAN_WITH_TESTING)
SubmitPasteResponse::SubmitPasteResponse (
const Kakoune::SafePtr<SettingsBag>& parSettings,
std::ostream* parStreamOut,
const Kakoune::SafePtr<cgi::Env>& parCgiEnv,
bool parInitStorage
) :
Response(parSettings, parStreamOut, parCgiEnv, parInitStorage)
{
}
#endif
SubmitPasteResponse::SubmitPasteResponse (
const Kakoune::SafePtr<SettingsBag>& parSettings,
std::ostream* parStreamOut,
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
) :
Response(parSettings, parStreamOut, parCgiEnv, true)
2017-04-04 19:58:40 +00:00
{
}
tawashi::HttpHeader SubmitPasteResponse::on_process() {
using tawashi::ErrorReasons;
2017-06-06 22:04:40 +00:00
boost::string_view pastie;
boost::string_view lang;
boost::string_view duration;
auto statuslog = spdlog::get("statuslog");
assert(statuslog);
const SettingsBag& settings = this->settings();
2017-04-24 23:21:44 +00:00
try {
auto& post = this->cgi_post();
2017-06-06 22:04:40 +00:00
pastie = get_value_from_post(post, make_string_view(g_post_key));
lang = get_value_from_post_log_failure(post, make_string_view(g_language_key));
duration = get_value_from_post_log_failure(post, make_string_view(g_duration_key));
}
catch (const tawashi::UnsupportedContentTypeException& err) {
statuslog->info(
"Unsupported content type exception: \"{}\"",
err.what()
);
return make_error_redirect(ErrorReasons::UnsupportedContentType);
2017-04-24 23:21:44 +00:00
}
catch (const tawashi::Exception& e) {
statuslog->error(e.what());
2017-05-18 21:35:31 +00:00
return make_error_redirect(e.reason());
2017-04-06 20:27:38 +00:00
}
const auto max_sz = settings.as<uint32_t>("max_pastie_size");
if (pastie.size() < settings.as<uint32_t>("min_pastie_size")) {
2017-05-18 21:35:31 +00:00
return make_error_redirect(ErrorReasons::PostLengthNotInRange);
}
2017-04-24 23:21:44 +00:00
if (max_sz and pastie.size() > max_sz) {
if (settings.as<bool>("truncate_long_pasties")) {
pastie = pastie.substr(0, max_sz);
}
else {
2017-05-18 21:35:31 +00:00
return make_error_redirect(ErrorReasons::PostLengthNotInRange);
}
}
2017-04-24 23:21:44 +00:00
//TODO: replace boost's lexical_cast with mine when I have some checks
2017-04-25 21:08:03 +00:00
//over invalid inputs
2017-04-24 23:21:44 +00:00
const uint32_t duration_int = std::max(std::min((duration.empty() ? 86400U : boost::lexical_cast<uint32_t>(duration)), 2628000U), 1U);
StringOrHeader submit_result = submit_to_storage(pastie, duration_int, lang);
const auto& token = submit_result.first;
if (token) {
std::ostringstream oss;
oss << *token;
2017-04-24 23:21:44 +00:00
if (not lang.empty())
oss << '?' << lang;
std::string redirect = oss.str();
statuslog->info("Pastie token=\"{}\" redirect=\"{}\"", *token, redirect);
return this->make_success_response(std::move(redirect));
}
else {
statuslog->info("Empty pastie token (possibly due to a previous failure)");
return submit_result.second;
}
}
auto SubmitPasteResponse::submit_to_storage (
2017-06-06 22:04:40 +00:00
const boost::string_view& parText,
uint32_t parExpiry,
2017-06-06 22:04:40 +00:00
const boost::string_view& parLang
) -> StringOrHeader {
auto& storage = this->storage();
std::string remote_ip = tawashi::guess_real_remote_ip(cgi_env());
Storage::SubmissionResult submission_res = storage.submit_pastie(parText, parExpiry, parLang, remote_ip);
if (not submission_res.error)
return std::make_pair(boost::make_optional(std::move(submission_res.token)), tawashi::HttpHeader());
else
return std::make_pair(boost::optional<std::string>(), make_error_redirect(*submission_res.error));
}
tawashi::HttpHeader SubmitPasteResponse::make_success_response (std::string&& parPastieParam) {
using tawashi::HttpStatusCodes;
return this->make_redirect(HttpStatusCodes::Code303_SeeOther, std::move(parPastieParam));
}
} //namespace kamokan