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"
|
|
|
|
#include <ciso646>
|
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-06 20:27:38 +00:00
|
|
|
} //unnamed namespace
|
|
|
|
|
2017-04-06 21:45:44 +00:00
|
|
|
SubmitPasteResponse::SubmitPasteResponse (redis::IncRedis& parRedis) :
|
2017-04-06 21:42:43 +00:00
|
|
|
Response("text/plain"),
|
2017-04-06 20:27:38 +00:00
|
|
|
m_redis(parRedis)
|
2017-04-04 19:58:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:45:44 +00:00
|
|
|
void SubmitPasteResponse::on_send (std::ostream& parStream) {
|
2017-04-06 20:27:38 +00:00
|
|
|
auto post = cgi::read_post(cgi_env());
|
|
|
|
auto post_data_it = post.find(g_post_key);
|
2017-04-06 21:42:43 +00:00
|
|
|
if (post.end() == post_data_it) {
|
2017-04-06 20:27:38 +00:00
|
|
|
parStream << "can't find POST data\n";
|
|
|
|
}
|
|
|
|
else if (submit_to_redis(post_data_it->second)) {
|
|
|
|
parStream << "post submitted correctly\n";
|
|
|
|
}
|
2017-04-06 21:42:43 +00:00
|
|
|
else {
|
|
|
|
parStream << "something happened? :/\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:45:44 +00:00
|
|
|
bool SubmitPasteResponse::submit_to_redis (const std::string& parText) const {
|
2017-04-06 21:42:43 +00:00
|
|
|
if (not m_redis.is_connected()) {
|
|
|
|
m_redis.connect();
|
|
|
|
m_redis.wait_for_connect();
|
|
|
|
if (not m_redis.is_connected())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto next_id = m_redis.incr("paste_counter");
|
|
|
|
const std::string token = num_to_token(next_id);
|
|
|
|
assert(not token.empty());
|
|
|
|
return m_redis.set(token, parText);
|
2017-04-04 19:58:40 +00:00
|
|
|
}
|
|
|
|
} //namespace tawashi
|