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

73 lines
2.2 KiB
C++
Raw Normal View History

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"
#include "num_to_token.hpp"
#include <ciso646>
#include <sstream>
2017-04-04 19:58:40 +00:00
namespace tawashi {
2017-04-06 20:27:38 +00:00
namespace {
const char g_post_key[] = "pastie";
2017-04-06 20:27:38 +00:00
} //unnamed namespace
SubmitPasteResponse::SubmitPasteResponse (const IniFile& parIni) :
Response(Response::ContentType, "text/plain", "paste", parIni, true)
2017-04-04 19:58:40 +00:00
{
}
void SubmitPasteResponse::on_process() {
2017-04-06 20:27:38 +00:00
auto post = cgi::read_post(cgi_env());
auto post_data_it = post.find(g_post_key);
if (post.end() == post_data_it) {
m_error_message = "can't find POST data";
return;
2017-04-06 20:27:38 +00:00
}
boost::optional<std::string> token = submit_to_redis(post_data_it->second);
if (token) {
std::ostringstream oss;
oss << "http://127.0.0.1:8080/" << *token;
this->change_type(Response::Location, oss.str());
}
}
void SubmitPasteResponse::on_send (std::ostream& parStream) {
assert(not m_error_message.empty());
parStream << "something happened? :/\n" <<
m_error_message << '\n';
}
boost::optional<std::string> SubmitPasteResponse::submit_to_redis (const std::string& parText) const {
auto& redis = this->redis();
if (not redis.is_connected())
return boost::optional<std::string>();
const auto next_id = redis.incr("paste_counter");
const std::string token = num_to_token(next_id);
assert(not token.empty());
if (redis.set(token, parText)) {
return boost::make_optional(token);
}
else {
return boost::optional<std::string>();
}
2017-04-04 19:58:40 +00:00
}
} //namespace tawashi