From ef9e74c473187a0461b642ffe7deb595453653d2 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 7 Apr 2017 00:31:06 +0100 Subject: [PATCH] Redirect to the new page if pastie was successful. --- src/index_response.cpp | 2 +- src/pastie_response.cpp | 2 +- src/response.cpp | 33 ++++++++++++++++++++++++++++---- src/response.hpp | 13 +++++++++++-- src/submit_paste_response.cpp | 36 ++++++++++++++++++++++++----------- src/submit_paste_response.hpp | 5 ++++- 6 files changed, 71 insertions(+), 20 deletions(-) diff --git a/src/index_response.cpp b/src/index_response.cpp index 95314b4..8cd023a 100644 --- a/src/index_response.cpp +++ b/src/index_response.cpp @@ -19,7 +19,7 @@ namespace tawashi { IndexResponse::IndexResponse() : - Response("text/html") + Response(Response::ContentType, "text/html") { } diff --git a/src/pastie_response.cpp b/src/pastie_response.cpp index 833ea67..a78ffc5 100644 --- a/src/pastie_response.cpp +++ b/src/pastie_response.cpp @@ -21,7 +21,7 @@ namespace tawashi { PastieResponse::PastieResponse (redis::IncRedis& parRedis) : - Response("text/plain"), + Response(Response::ContentType, "text/plain"), m_redis(parRedis) { } diff --git a/src/response.cpp b/src/response.cpp index c8bf93c..f327668 100644 --- a/src/response.cpp +++ b/src/response.cpp @@ -17,22 +17,47 @@ #include "response.hpp" #include +#include 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 diff --git a/src/response.hpp b/src/response.hpp index b0d7e03..083f3ad 100644 --- a/src/response.hpp +++ b/src/response.hpp @@ -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 diff --git a/src/submit_paste_response.cpp b/src/submit_paste_response.cpp index b26986c..a7e1343 100644 --- a/src/submit_paste_response.cpp +++ b/src/submit_paste_response.cpp @@ -20,6 +20,7 @@ #include "cgi_post.hpp" #include "num_to_token.hpp" #include +#include 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 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? :/
\n" << + m_error_message << '\n'; + } + + boost::optional 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(); } 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(); + } } } //namespace tawashi diff --git a/src/submit_paste_response.hpp b/src/submit_paste_response.hpp index 77fbe06..78de7f3 100644 --- a/src/submit_paste_response.hpp +++ b/src/submit_paste_response.hpp @@ -19,6 +19,7 @@ #include "response.hpp" #include +#include 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 submit_to_redis (const std::string& parText) const; redis::IncRedis& m_redis; + std::string m_error_message; }; } //namespace tawashi