mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2025-06-07 00:51: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 {
|
namespace tawashi {
|
||||||
IndexResponse::IndexResponse() :
|
IndexResponse::IndexResponse() :
|
||||||
Response("text/html")
|
Response(Response::ContentType, "text/html")
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
PastieResponse::PastieResponse (redis::IncRedis& parRedis) :
|
PastieResponse::PastieResponse (redis::IncRedis& parRedis) :
|
||||||
Response("text/plain"),
|
Response(Response::ContentType, "text/plain"),
|
||||||
m_redis(parRedis)
|
m_redis(parRedis)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,17 +17,35 @@
|
||||||
|
|
||||||
#include "response.hpp"
|
#include "response.hpp"
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
Response::Response (std::string&& parType) :
|
Response::Response (Types parRespType, std::string&& parValue) :
|
||||||
m_content_type(std::move(parType))
|
m_resp_value(std::move(parValue)),
|
||||||
|
m_resp_type(parRespType),
|
||||||
|
m_header_sent(false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Response::~Response() noexcept = default;
|
Response::~Response() noexcept = default;
|
||||||
|
|
||||||
|
void Response::on_process() {
|
||||||
|
}
|
||||||
|
|
||||||
void Response::send() {
|
void Response::send() {
|
||||||
std::cout << "Content-type:" << m_content_type << "\n\n";
|
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);
|
this->on_send(std::cout);
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
}
|
}
|
||||||
|
@ -35,4 +53,11 @@ namespace tawashi {
|
||||||
const cgi::Env& Response::cgi_env() const {
|
const cgi::Env& Response::cgi_env() const {
|
||||||
return m_cgi_env;
|
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
|
} //namespace tawashi
|
||||||
|
|
|
@ -29,13 +29,22 @@ namespace tawashi {
|
||||||
void send();
|
void send();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Response (std::string&& parType);
|
enum Types {
|
||||||
|
ContentType,
|
||||||
|
Location
|
||||||
|
};
|
||||||
|
|
||||||
|
Response (Types parRespType, std::string&& parValue);
|
||||||
const cgi::Env& cgi_env() const;
|
const cgi::Env& cgi_env() const;
|
||||||
|
void change_type (Types parRespType, std::string&& parValue);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
virtual void on_process();
|
||||||
virtual void on_send (std::ostream& parStream) = 0;
|
virtual void on_send (std::ostream& parStream) = 0;
|
||||||
|
|
||||||
cgi::Env m_cgi_env;
|
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
|
} //namespace tawashi
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
#include "cgi_post.hpp"
|
#include "cgi_post.hpp"
|
||||||
#include "num_to_token.hpp"
|
#include "num_to_token.hpp"
|
||||||
#include <ciso646>
|
#include <ciso646>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -27,36 +28,49 @@ namespace tawashi {
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
SubmitPasteResponse::SubmitPasteResponse (redis::IncRedis& parRedis) :
|
SubmitPasteResponse::SubmitPasteResponse (redis::IncRedis& parRedis) :
|
||||||
Response("text/plain"),
|
Response(Response::ContentType, "text/plain"),
|
||||||
m_redis(parRedis)
|
m_redis(parRedis)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void SubmitPasteResponse::on_send (std::ostream& parStream) {
|
void SubmitPasteResponse::on_process() {
|
||||||
auto post = cgi::read_post(cgi_env());
|
auto post = cgi::read_post(cgi_env());
|
||||||
auto post_data_it = post.find(g_post_key);
|
auto post_data_it = post.find(g_post_key);
|
||||||
if (post.end() == post_data_it) {
|
if (post.end() == post_data_it) {
|
||||||
parStream << "can't find POST data\n";
|
m_error_message = "can't find POST data";
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else if (submit_to_redis(post_data_it->second)) {
|
|
||||||
parStream << "post submitted correctly\n";
|
boost::optional<std::string> token = submit_to_redis(post_data_it->second);
|
||||||
}
|
if (token) {
|
||||||
else {
|
std::ostringstream oss;
|
||||||
parStream << "something happened? :/\n";
|
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()) {
|
if (not m_redis.is_connected()) {
|
||||||
m_redis.connect();
|
m_redis.connect();
|
||||||
m_redis.wait_for_connect();
|
m_redis.wait_for_connect();
|
||||||
if (not m_redis.is_connected())
|
if (not m_redis.is_connected())
|
||||||
return false;
|
return boost::optional<std::string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto next_id = m_redis.incr("paste_counter");
|
const auto next_id = m_redis.incr("paste_counter");
|
||||||
const std::string token = num_to_token(next_id);
|
const std::string token = num_to_token(next_id);
|
||||||
assert(not token.empty());
|
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
|
} //namespace tawashi
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include "response.hpp"
|
#include "response.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <boost/optional.hpp>
|
||||||
|
|
||||||
namespace redis {
|
namespace redis {
|
||||||
class IncRedis;
|
class IncRedis;
|
||||||
|
@ -30,9 +31,11 @@ namespace tawashi {
|
||||||
explicit SubmitPasteResponse (redis::IncRedis& parRedis);
|
explicit SubmitPasteResponse (redis::IncRedis& parRedis);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
virtual void on_process() override;
|
||||||
virtual void on_send (std::ostream& parStream) 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;
|
redis::IncRedis& m_redis;
|
||||||
|
std::string m_error_message;
|
||||||
};
|
};
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
Loading…
Add table
Reference in a new issue