1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-11-23 00:33:44 +00:00

Add http status codes and save dummy error into the mstch context.

This commit is contained in:
King_DuckZ 2017-05-14 03:38:41 +01:00
parent 4d31323bb1
commit ec80734625
4 changed files with 29 additions and 9 deletions

View file

@ -17,10 +17,14 @@
#pragma once #pragma once
#include "enum.h"
namespace tawashi { namespace tawashi {
enum ErrorReasons : int { BETTER_ENUM(ErrorReasons, int,
PostLengthNotInRange, PostLengthNotInRange,
PastieNotSaved, PastieNotSaved,
UserFlooding UserFlooding,
}; UnkownReason,
RedisDisconnected
)
} //namespace tawashi } //namespace tawashi

View file

@ -16,7 +16,12 @@
*/ */
#include "error_response.hpp" #include "error_response.hpp"
#include "error_reasons.hpp"
#include "cgi_env.hpp"
#include <boost/algorithm/string/replace.hpp> #include <boost/algorithm/string/replace.hpp>
#include <boost/lexical_cast.hpp>
#include <ciso646>
#include <string>
namespace tawashi { namespace tawashi {
ErrorResponse::ErrorResponse ( ErrorResponse::ErrorResponse (
@ -29,6 +34,15 @@ namespace tawashi {
} }
void ErrorResponse::on_mustache_prepare (mstch::map& parContext) { void ErrorResponse::on_mustache_prepare (mstch::map& parContext) {
auto get = cgi_env().query_string_split();
auto err_code = boost::lexical_cast<int>(get["code"]);
const int reason_int = boost::lexical_cast<int>(get["reason"]);
ErrorReasons reason_code(ErrorReasons::UnkownReason);
if (reason_int >= 0 and reason_int < ErrorReasons::_size())
reason_code = ErrorReasons::_from_integral(reason_int);
parContext["error_message"] = "No error"; parContext["error_message"] = "No error";
parContext["error_code"] = std::to_string(err_code);
parContext["error_id"] = std::to_string(reason_code);
} }
} //namespace tawashi } //namespace tawashi

View file

@ -43,9 +43,9 @@ namespace tawashi {
} }
void PastieResponse::on_process() { void PastieResponse::on_process() {
auto env = cgi_env().query_string_split(); auto get = cgi_env().query_string_split();
const std::string& query_str(cgi_env().query_string()); const std::string& query_str(cgi_env().query_string());
if (env["m"] == "plain" or query_str.empty()) { if (get["m"] == "plain" or query_str.empty()) {
this->change_type(Response::ContentType, "text/plain; charset=utf-8"); this->change_type(Response::ContentType, "text/plain; charset=utf-8");
m_plain_text = true; m_plain_text = true;
} }

View file

@ -127,7 +127,7 @@ namespace tawashi {
pastie = pastie.substr(0, max_sz); pastie = pastie.substr(0, max_sz);
} }
else { else {
error_redirect(1, ErrorReasons::PostLengthNotInRange); error_redirect(431, ErrorReasons::PostLengthNotInRange);
return; return;
} }
} }
@ -151,13 +151,15 @@ namespace tawashi {
boost::optional<std::string> SubmitPasteResponse::submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang) { boost::optional<std::string> SubmitPasteResponse::submit_to_redis (const boost::string_ref& parText, uint32_t parExpiry, const boost::string_ref& parLang) {
auto& redis = this->redis(); auto& redis = this->redis();
if (not redis.is_connected()) if (not redis.is_connected()) {
error_redirect(503, ErrorReasons::RedisDisconnected);
return boost::optional<std::string>(); return boost::optional<std::string>();
}
std::string ip_hash = hashed_ip(cgi_env().remote_addr()); std::string ip_hash = hashed_ip(cgi_env().remote_addr());
if (redis.get(ip_hash)) { if (redis.get(ip_hash)) {
//please wait and submit again //please wait and submit again
error_redirect(1, ErrorReasons::UserFlooding); error_redirect(429, ErrorReasons::UserFlooding);
return boost::optional<std::string>(); return boost::optional<std::string>();
} }
@ -175,7 +177,7 @@ namespace tawashi {
return boost::make_optional(token); return boost::make_optional(token);
} }
error_redirect(1, ErrorReasons::PastieNotSaved); error_redirect(500, ErrorReasons::PastieNotSaved);
return boost::optional<std::string>(); return boost::optional<std::string>();
} }