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-04-06 20:27:38 +00:00
|
|
|
#include "incredis/incredis.hpp"
|
|
|
|
#include "cgi_post.hpp"
|
2017-04-06 21:42:43 +00:00
|
|
|
#include "num_to_token.hpp"
|
2017-04-24 18:09:43 +00:00
|
|
|
#include "settings_bag.hpp"
|
2017-04-24 18:41:38 +00:00
|
|
|
#include "curl_wrapper.hpp"
|
2017-04-24 23:21:44 +00:00
|
|
|
#include "duckhandy/compatibility.h"
|
|
|
|
#include "duckhandy/lexical_cast.hpp"
|
2017-04-06 21:42:43 +00:00
|
|
|
#include <ciso646>
|
2017-04-06 23:31:06 +00:00
|
|
|
#include <sstream>
|
2017-04-24 23:21:44 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
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";
|
|
|
|
|
|
|
|
class MissingPostVarError : public std::runtime_error {
|
|
|
|
public:
|
|
|
|
MissingPostVarError (const std::string& parMsg) : std::runtime_error(parMsg) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <std::size_t N>
|
|
|
|
inline boost::string_ref make_string_ref (const char (&parStr)[N]) a_always_inline;
|
|
|
|
|
|
|
|
template <std::size_t N>
|
|
|
|
boost::string_ref make_string_ref (const char (&parStr)[N]) {
|
|
|
|
static_assert(N > 0, "wat?");
|
|
|
|
return boost::string_ref(parStr, N - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::string_ref get_value_from_post (const cgi::PostMapType& parPost, boost::string_ref parKey) {
|
|
|
|
std::string key(parKey.data(), parKey.size());
|
|
|
|
auto post_data_it = parPost.find(key);
|
|
|
|
if (parPost.end() == post_data_it) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "can't find POST data field \"" << parKey << '"';
|
|
|
|
throw MissingPostVarError(oss.str());
|
|
|
|
}
|
|
|
|
return post_data_it->second;
|
|
|
|
}
|
2017-04-06 20:27:38 +00:00
|
|
|
} //unnamed namespace
|
|
|
|
|
2017-04-23 12:40:48 +00:00
|
|
|
SubmitPasteResponse::SubmitPasteResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) :
|
2017-04-21 22:10:16 +00:00
|
|
|
Response(Response::ContentType, "text/plain", "paste", parSettings, true)
|
2017-04-04 19:58:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-06 23:31:06 +00:00
|
|
|
void SubmitPasteResponse::on_process() {
|
2017-04-06 20:27:38 +00:00
|
|
|
auto post = cgi::read_post(cgi_env());
|
2017-04-24 23:21:44 +00:00
|
|
|
boost::string_ref pastie;
|
|
|
|
boost::string_ref lang;
|
|
|
|
boost::string_ref duration;
|
|
|
|
try {
|
|
|
|
pastie = get_value_from_post(post, make_string_ref(g_post_key));
|
|
|
|
}
|
|
|
|
catch (const MissingPostVarError& e) {
|
|
|
|
m_error_message = e.what();
|
2017-04-06 23:31:06 +00:00
|
|
|
return;
|
2017-04-06 20:27:38 +00:00
|
|
|
}
|
2017-04-24 23:21:44 +00:00
|
|
|
try {
|
|
|
|
lang = get_value_from_post(post, make_string_ref(g_language_key));
|
|
|
|
duration = get_value_from_post(post, make_string_ref(g_duration_key));
|
|
|
|
}
|
|
|
|
catch (const MissingPostVarError&) {
|
|
|
|
}
|
2017-04-06 23:31:06 +00:00
|
|
|
|
2017-04-24 18:09:43 +00:00
|
|
|
const SettingsBag& settings = this->settings();
|
|
|
|
const auto max_sz = settings.as<uint32_t>("max_pastie_size");
|
2017-04-24 23:21:44 +00:00
|
|
|
if (pastie.size() < settings.as<uint32_t>("min_pastie_size"))
|
2017-04-24 18:09:43 +00:00
|
|
|
return;
|
2017-04-24 23:21:44 +00:00
|
|
|
if (max_sz and pastie.size() > max_sz) {
|
2017-04-24 18:09:43 +00:00
|
|
|
if (settings.as<bool>("truncate_long_pasties"))
|
|
|
|
pastie = pastie.substr(0, max_sz);
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-24 23:21:44 +00:00
|
|
|
//TODO: replace boost's lexical_cast with mine when I have some checks
|
|
|
|
//oven invalid inputs
|
|
|
|
const uint32_t duration_int = std::max(std::min((duration.empty() ? 86400U : boost::lexical_cast<uint32_t>(duration)), 2628000U), 1U);
|
2017-04-24 18:41:38 +00:00
|
|
|
CurlWrapper curl;
|
2017-04-24 23:21:44 +00:00
|
|
|
boost::optional<std::string> token = submit_to_redis(curl.escape(pastie), duration_int, lang);
|
2017-04-06 23:31:06 +00:00
|
|
|
if (token) {
|
|
|
|
std::ostringstream oss;
|
2017-04-23 12:08:27 +00:00
|
|
|
oss << base_uri() << '/' << *token;
|
2017-04-24 23:21:44 +00:00
|
|
|
if (not lang.empty())
|
|
|
|
oss << '?' << lang;
|
2017-04-06 23:31:06 +00:00
|
|
|
this->change_type(Response::Location, oss.str());
|
2017-04-06 21:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 23:31:06 +00:00
|
|
|
void SubmitPasteResponse::on_send (std::ostream& parStream) {
|
|
|
|
assert(not m_error_message.empty());
|
2017-04-18 19:10:36 +00:00
|
|
|
parStream << "something happened? :/\n" <<
|
2017-04-06 23:31:06 +00:00
|
|
|
m_error_message << '\n';
|
|
|
|
}
|
|
|
|
|
2017-04-24 23:21:44 +00:00
|
|
|
boost::optional<std::string> SubmitPasteResponse::submit_to_redis (const std::string& parText, uint32_t parExpiry, const boost::string_ref& parLang) const {
|
2017-04-15 02:18:33 +00:00
|
|
|
auto& redis = this->redis();
|
|
|
|
if (not redis.is_connected())
|
|
|
|
return boost::optional<std::string>();
|
2017-04-06 21:42:43 +00:00
|
|
|
|
2017-04-15 02:18:33 +00:00
|
|
|
const auto next_id = redis.incr("paste_counter");
|
2017-04-06 21:42:43 +00:00
|
|
|
const std::string token = num_to_token(next_id);
|
|
|
|
assert(not token.empty());
|
2017-04-24 23:21:44 +00:00
|
|
|
if (redis.hmset(token,
|
|
|
|
"pastie", parText,
|
|
|
|
"max_ttl", dhandy::lexical_cast<std::string>(parExpiry),
|
|
|
|
"lang", parLang)
|
|
|
|
) {
|
|
|
|
if (redis.expire(token, parExpiry))
|
|
|
|
return boost::make_optional(token);
|
2017-04-06 23:31:06 +00:00
|
|
|
}
|
2017-04-24 23:21:44 +00:00
|
|
|
|
|
|
|
return boost::optional<std::string>();
|
2017-04-04 19:58:40 +00:00
|
|
|
}
|
|
|
|
} //namespace tawashi
|