2017-04-06 22:35:06 +00:00
|
|
|
/* 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/>.
|
|
|
|
*/
|
|
|
|
|
2017-04-06 21:45:44 +00:00
|
|
|
#include "submit_paste_response.hpp"
|
2017-06-05 20:57:14 +00:00
|
|
|
#include "storage.hpp"
|
2017-04-06 20:27:38 +00:00
|
|
|
#include "cgi_post.hpp"
|
2017-04-24 18:09:43 +00:00
|
|
|
#include "settings_bag.hpp"
|
2017-04-24 23:21:44 +00:00
|
|
|
#include "duckhandy/compatibility.h"
|
|
|
|
#include "duckhandy/lexical_cast.hpp"
|
2017-05-16 17:54:00 +00:00
|
|
|
#include "tawashi_exception.hpp"
|
2017-05-24 08:34:52 +00:00
|
|
|
#include "ip_utils.hpp"
|
2017-04-06 21:42:43 +00:00
|
|
|
#include <ciso646>
|
2017-04-24 23:21:44 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
2017-05-11 08:48:18 +00:00
|
|
|
#include <cstdint>
|
2017-05-11 17:50:56 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2017-05-11 08:48:18 +00:00
|
|
|
|
2017-04-04 19:58:40 +00:00
|
|
|
namespace tawashi {
|
2017-04-06 20:27:38 +00:00
|
|
|
namespace {
|
2017-04-06 21:42:43 +00:00
|
|
|
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";
|
|
|
|
|
2017-05-16 17:54:00 +00:00
|
|
|
class MissingPostVarError : public TawashiException {
|
2017-04-24 23:21:44 +00:00
|
|
|
public:
|
2017-06-06 22:04:40 +00:00
|
|
|
explicit MissingPostVarError(const boost::string_view& parKey) :
|
2017-05-16 17:54:00 +00:00
|
|
|
TawashiException(
|
|
|
|
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);
|
2017-05-16 17:54:00 +00:00
|
|
|
if (parPost.end() == post_data_it)
|
|
|
|
throw MissingPostVarError(parKey);
|
2017-04-24 23:21:44 +00:00
|
|
|
return post_data_it->second;
|
|
|
|
}
|
2017-05-11 08:48:18 +00:00
|
|
|
|
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) {
|
2017-05-18 18:12:28 +00:00
|
|
|
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-05-18 18:12:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-06 20:27:38 +00:00
|
|
|
} //unnamed namespace
|
|
|
|
|
2017-06-06 18:40:52 +00:00
|
|
|
#if defined(TAWASHI_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
|
|
|
|
|
2017-05-06 18:48:44 +00:00
|
|
|
SubmitPasteResponse::SubmitPasteResponse (
|
|
|
|
const Kakoune::SafePtr<SettingsBag>& parSettings,
|
|
|
|
std::ostream* parStreamOut,
|
|
|
|
const Kakoune::SafePtr<cgi::Env>& parCgiEnv
|
|
|
|
) :
|
2017-06-06 18:40:52 +00:00
|
|
|
Response(parSettings, parStreamOut, parCgiEnv, true)
|
2017-04-04 19:58:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-05-18 17:50:01 +00:00
|
|
|
HttpHeader SubmitPasteResponse::on_process() {
|
2017-06-06 22:04:40 +00:00
|
|
|
boost::string_view pastie;
|
|
|
|
boost::string_view lang;
|
|
|
|
boost::string_view duration;
|
2017-05-16 17:54:00 +00:00
|
|
|
|
|
|
|
auto statuslog = spdlog::get("statuslog");
|
|
|
|
assert(statuslog);
|
|
|
|
|
2017-06-02 08:23:35 +00:00
|
|
|
const SettingsBag& settings = this->settings();
|
2017-04-24 23:21:44 +00:00
|
|
|
try {
|
2017-06-05 22:56:59 +00:00
|
|
|
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));
|
2017-06-02 08:23:35 +00:00
|
|
|
}
|
|
|
|
catch (const UnsupportedContentTypeException& err) {
|
|
|
|
statuslog->info(
|
|
|
|
"Unsupported content type exception: \"{}\"",
|
|
|
|
err.what()
|
|
|
|
);
|
|
|
|
return make_error_redirect(ErrorReasons::UnsupportedContentType);
|
2017-04-24 23:21:44 +00:00
|
|
|
}
|
2017-05-16 17:54:00 +00:00
|
|
|
catch (const TawashiException& 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
|
|
|
}
|
2017-05-18 18:12:28 +00:00
|
|
|
|
2017-04-24 18:09:43 +00:00
|
|
|
const auto max_sz = settings.as<uint32_t>("max_pastie_size");
|
2017-05-16 23:03:43 +00:00
|
|
|
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-05-16 23:03:43 +00:00
|
|
|
}
|
2017-04-24 23:21:44 +00:00
|
|
|
if (max_sz and pastie.size() > max_sz) {
|
2017-05-12 21:46:54 +00:00
|
|
|
if (settings.as<bool>("truncate_long_pasties")) {
|
2017-04-24 18:09:43 +00:00
|
|
|
pastie = pastie.substr(0, max_sz);
|
2017-05-12 21:46:54 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-05-18 21:35:31 +00:00
|
|
|
return make_error_redirect(ErrorReasons::PostLengthNotInRange);
|
2017-05-12 21:46:54 +00:00
|
|
|
}
|
2017-04-24 18:09:43 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2017-06-05 20:57:14 +00:00
|
|
|
StringOrHeader submit_result = submit_to_storage(pastie, duration_int, lang);
|
2017-05-18 17:50:01 +00:00
|
|
|
const auto& token = submit_result.first;
|
2017-05-12 21:46:54 +00:00
|
|
|
|
2017-04-06 23:31:06 +00:00
|
|
|
if (token) {
|
|
|
|
std::ostringstream oss;
|
2017-05-18 17:50:01 +00:00
|
|
|
oss << *token;
|
2017-04-24 23:21:44 +00:00
|
|
|
if (not lang.empty())
|
|
|
|
oss << '?' << lang;
|
2017-05-23 19:29:17 +00:00
|
|
|
|
2017-06-05 22:02:39 +00:00
|
|
|
std::string redirect = oss.str();
|
|
|
|
statuslog->info("Pastie token=\"{}\" redirect=\"{}\"", *token, redirect);
|
|
|
|
|
|
|
|
return this->make_success_response(std::move(redirect));
|
2017-04-06 21:42:43 +00:00
|
|
|
}
|
2017-05-12 21:46:54 +00:00
|
|
|
else {
|
2017-05-16 23:03:43 +00:00
|
|
|
statuslog->info("Empty pastie token (possibly due to a previous failure)");
|
2017-05-18 17:50:01 +00:00
|
|
|
return submit_result.second;
|
2017-05-12 21:46:54 +00:00
|
|
|
}
|
2017-04-06 21:42:43 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 20:57:14 +00:00
|
|
|
auto SubmitPasteResponse::submit_to_storage (
|
2017-06-06 22:04:40 +00:00
|
|
|
const boost::string_view& parText,
|
2017-05-18 17:50:01 +00:00
|
|
|
uint32_t parExpiry,
|
2017-06-06 22:04:40 +00:00
|
|
|
const boost::string_view& parLang
|
2017-05-18 17:50:01 +00:00
|
|
|
) -> StringOrHeader {
|
2017-06-05 20:57:14 +00:00
|
|
|
auto& storage = this->storage();
|
2017-05-24 08:34:52 +00:00
|
|
|
std::string remote_ip = guess_real_remote_ip(cgi_env());
|
2017-06-05 20:57:14 +00:00
|
|
|
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)), HttpHeader());
|
|
|
|
else
|
|
|
|
return std::make_pair(boost::optional<std::string>(), make_error_redirect(*submission_res.error));
|
2017-05-12 21:46:54 +00:00
|
|
|
}
|
2017-05-23 19:29:17 +00:00
|
|
|
|
2017-05-24 08:05:03 +00:00
|
|
|
HttpHeader SubmitPasteResponse::make_success_response (std::string&& parPastieParam) {
|
|
|
|
return this->make_redirect(HttpStatusCodes::Code303_SeeOther, std::move(parPastieParam));
|
2017-05-23 19:29:17 +00:00
|
|
|
}
|
2017-04-04 19:58:40 +00:00
|
|
|
} //namespace tawashi
|