mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2024-12-27 21:35:41 +00:00
Redirect to the new page if pastie was successful.
This commit is contained in:
parent
4354f4bf29
commit
ef9e74c473
6 changed files with 71 additions and 20 deletions
|
@ -19,7 +19,7 @@
|
|||
|
||||
namespace tawashi {
|
||||
IndexResponse::IndexResponse() :
|
||||
Response("text/html")
|
||||
Response(Response::ContentType, "text/html")
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
|
||||
namespace tawashi {
|
||||
PastieResponse::PastieResponse (redis::IncRedis& parRedis) :
|
||||
Response("text/plain"),
|
||||
Response(Response::ContentType, "text/plain"),
|
||||
m_redis(parRedis)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -17,22 +17,47 @@
|
|||
|
||||
#include "response.hpp"
|
||||
#include <utility>
|
||||
#include <cassert>
|
||||
|
||||
namespace tawashi {
|
||||
Response::Response (std::string&& parType) :
|
||||
m_content_type(std::move(parType))
|
||||
Response::Response (Types parRespType, std::string&& parValue) :
|
||||
m_resp_value(std::move(parValue)),
|
||||
m_resp_type(parRespType),
|
||||
m_header_sent(false)
|
||||
{
|
||||
}
|
||||
|
||||
Response::~Response() noexcept = default;
|
||||
|
||||
void Response::on_process() {
|
||||
}
|
||||
|
||||
void Response::send() {
|
||||
std::cout << "Content-type:" << m_content_type << "\n\n";
|
||||
this->on_send(std::cout);
|
||||
this->on_process();
|
||||
|
||||
m_header_sent = true;
|
||||
switch (m_resp_type) {
|
||||
case ContentType:
|
||||
std::cout << "Content-type: " << m_resp_value << "\n\n";
|
||||
break;
|
||||
case Location:
|
||||
std::cout << "Location: " << m_resp_value << "\n\n";
|
||||
break;
|
||||
}
|
||||
|
||||
if (ContentType == m_resp_type)
|
||||
this->on_send(std::cout);
|
||||
std::cout.flush();
|
||||
}
|
||||
|
||||
const cgi::Env& Response::cgi_env() const {
|
||||
return m_cgi_env;
|
||||
}
|
||||
|
||||
void Response::change_type (Types parRespType, std::string&& parValue) {
|
||||
assert(not m_header_sent);
|
||||
assert(not parValue.empty());
|
||||
m_resp_type = parRespType;
|
||||
m_resp_value = std::move(parValue);
|
||||
}
|
||||
} //namespace tawashi
|
||||
|
|
|
@ -29,13 +29,22 @@ namespace tawashi {
|
|||
void send();
|
||||
|
||||
protected:
|
||||
Response (std::string&& parType);
|
||||
enum Types {
|
||||
ContentType,
|
||||
Location
|
||||
};
|
||||
|
||||
Response (Types parRespType, std::string&& parValue);
|
||||
const cgi::Env& cgi_env() const;
|
||||
void change_type (Types parRespType, std::string&& parValue);
|
||||
|
||||
private:
|
||||
virtual void on_process();
|
||||
virtual void on_send (std::ostream& parStream) = 0;
|
||||
|
||||
cgi::Env m_cgi_env;
|
||||
std::string m_content_type;
|
||||
std::string m_resp_value;
|
||||
Types m_resp_type;
|
||||
bool m_header_sent;
|
||||
};
|
||||
} //namespace tawashi
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "cgi_post.hpp"
|
||||
#include "num_to_token.hpp"
|
||||
#include <ciso646>
|
||||
#include <sstream>
|
||||
|
||||
namespace tawashi {
|
||||
namespace {
|
||||
|
@ -27,36 +28,49 @@ namespace tawashi {
|
|||
} //unnamed namespace
|
||||
|
||||
SubmitPasteResponse::SubmitPasteResponse (redis::IncRedis& parRedis) :
|
||||
Response("text/plain"),
|
||||
Response(Response::ContentType, "text/plain"),
|
||||
m_redis(parRedis)
|
||||
{
|
||||
}
|
||||
|
||||
void SubmitPasteResponse::on_send (std::ostream& parStream) {
|
||||
void SubmitPasteResponse::on_process() {
|
||||
auto post = cgi::read_post(cgi_env());
|
||||
auto post_data_it = post.find(g_post_key);
|
||||
if (post.end() == post_data_it) {
|
||||
parStream << "can't find POST data\n";
|
||||
}
|
||||
else if (submit_to_redis(post_data_it->second)) {
|
||||
parStream << "post submitted correctly\n";
|
||||
m_error_message = "can't find POST data";
|
||||
return;
|
||||
}
|
||||
else {
|
||||
parStream << "something happened? :/\n";
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
bool SubmitPasteResponse::submit_to_redis (const std::string& parText) const {
|
||||
void SubmitPasteResponse::on_send (std::ostream& parStream) {
|
||||
assert(not m_error_message.empty());
|
||||
parStream << "something happened? :/<br>\n" <<
|
||||
m_error_message << '\n';
|
||||
}
|
||||
|
||||
boost::optional<std::string> SubmitPasteResponse::submit_to_redis (const std::string& parText) const {
|
||||
if (not m_redis.is_connected()) {
|
||||
m_redis.connect();
|
||||
m_redis.wait_for_connect();
|
||||
if (not m_redis.is_connected())
|
||||
return false;
|
||||
return boost::optional<std::string>();
|
||||
}
|
||||
|
||||
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);
|
||||
if (m_redis.set(token, parText)) {
|
||||
return boost::make_optional(token);
|
||||
}
|
||||
else {
|
||||
return boost::optional<std::string>();
|
||||
}
|
||||
}
|
||||
} //namespace tawashi
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
#include "response.hpp"
|
||||
#include <string>
|
||||
#include <boost/optional.hpp>
|
||||
|
||||
namespace redis {
|
||||
class IncRedis;
|
||||
|
@ -30,9 +31,11 @@ namespace tawashi {
|
|||
explicit SubmitPasteResponse (redis::IncRedis& parRedis);
|
||||
|
||||
private:
|
||||
virtual void on_process() override;
|
||||
virtual void on_send (std::ostream& parStream) override;
|
||||
bool submit_to_redis (const std::string& parText) const;
|
||||
boost::optional<std::string> submit_to_redis (const std::string& parText) const;
|
||||
|
||||
redis::IncRedis& m_redis;
|
||||
std::string m_error_message;
|
||||
};
|
||||
} //namespace tawashi
|
||||
|
|
Loading…
Reference in a new issue