1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-12-27 21:35:41 +00:00

Assign the actual numbers to the HTTP status codes enum.

This allows me to get rid of a good deal of crap.
This commit is contained in:
King_DuckZ 2017-05-18 19:00:25 +01:00
parent 4eb4261f4a
commit aaa28d3454
5 changed files with 28 additions and 72 deletions

View file

@ -42,36 +42,6 @@ namespace tawashi {
return "INVALID STATUS CODE";
}
inline constexpr uint16_t single_status_code_to_code (const char* parCode) {
std::size_t idx = 0;
uint16_t ret_num = 0;
uint16_t digits = 0;
char cur_char = 0;
while ((cur_char = parCode[idx++]) and digits < 3) {
if (cur_char >= '0' and cur_char <= '9') {
uint16_t multip = 1;
for (uint16_t z = 1; z < (3 - digits); ++z) {
multip *= 10;
}
ret_num += multip * (cur_char - '0');
++digits;
}
}
return ret_num;
}
template <int... Values>
inline constexpr sprout::array<uint16_t, sizeof...(Values)> make_status_codes_lookup (dhandy::bt::number_seq<int, Values...>) {
return sprout::array<uint16_t, sizeof...(Values)> {
single_status_code_to_code(HttpStatusCodes::_names()[Values])...
};
}
uint16_t status_code_name_to_num (HttpStatusCodes parCode) {
constexpr const auto status_codes = make_status_codes_lookup(dhandy::bt::number_range<int, 0, HttpStatusCodes::_size() - 1>());
return status_codes[parCode._to_integral()];
}
constexpr auto g_status_code_descriptions = ::better_enums::make_map(get_status_code_desc);
} //unnamed namespace
@ -102,12 +72,16 @@ namespace tawashi {
m_param = std::move(parParameter);
}
bool HttpHeader::body_required() const {
return type() == ContentType;
}
std::ostream& operator<< (std::ostream& parStream, const HttpHeader& parHeader) {
const HttpStatusCodes code_none = HttpStatusCodes::CodeNone;
if (parHeader.status_code() != code_none) {
parStream <<
"Status: " <<
status_code_name_to_num(parHeader.status_code()) <<
parHeader.status_code()._to_integral() <<
g_status_code_descriptions[parHeader.status_code()] <<
'\n'
;
@ -133,21 +107,4 @@ namespace tawashi {
HttpHeader make_header_type_text_utf8() {
return HttpHeader(HttpHeader::ContentType, HttpStatusCodes::CodeNone, "text/plain; charset=utf-8");
}
HttpStatusCodes int_to_status_code (uint16_t parCode) {
constexpr const auto status_codes = make_status_codes_lookup(dhandy::bt::number_range<int, 0, HttpStatusCodes::_size() - 1>());
auto it_found_code = std::find(status_codes.begin(), status_codes.end(), parCode);
if (it_found_code != status_codes.end()) {
const auto index = it_found_code - status_codes.begin();
assert(index < HttpStatusCodes::_size() - 1);
return HttpStatusCodes::_from_integral(index);
}
else {
return HttpStatusCodes::CodeNone;
}
}
bool HttpHeader::body_required() const {
return type() == ContentType;
}
} //namespace tawashi

View file

@ -23,20 +23,20 @@
#include <ostream>
namespace tawashi {
SLOW_ENUM(HttpStatusCodes, uint16_t,
Code301_MovedPermanently,
Code302_Found,
Code303_SeeOther,
Code400_BadRequest,
Code403_Forbidden,
Code404_NotFound,
Code413_PayloadTooLarge,
Code429_TooManyRequests,
Code431_RequestHeaderFieldsTooLarge,
Code500_InternalServerError,
Code501_NotImplemented,
Code503_ServiceUnavailable,
CodeNone
BETTER_ENUM(HttpStatusCodes, uint16_t,
Code301_MovedPermanently = 301,
Code302_Found = 302,
Code303_SeeOther = 303,
Code400_BadRequest = 400,
Code403_Forbidden = 403,
Code404_NotFound = 404,
Code413_PayloadTooLarge = 413,
Code429_TooManyRequests = 429,
Code431_RequestHeaderFieldsTooLarge = 431,
Code500_InternalServerError = 500,
Code501_NotImplemented = 501,
Code503_ServiceUnavailable = 503,
CodeNone = 0
)
class HttpHeader {
public:
@ -70,5 +70,4 @@ namespace tawashi {
std::ostream& operator<< (std::ostream& parStream, const HttpHeader& parHeader);
HttpHeader make_header_type_html();
HttpHeader make_header_type_text_utf8();
HttpStatusCodes int_to_status_code (uint16_t parCode);
} //namespace tawashi

View file

@ -240,13 +240,13 @@ namespace tawashi {
return HttpHeader(HttpHeader::Location, parCode, oss.str());
}
HttpHeader Response::make_error_redirect (uint16_t parCode, ErrorReasons parReason) {
HttpHeader Response::make_error_redirect (HttpStatusCodes parCode, ErrorReasons parReason) {
auto statuslog = spdlog::get("statuslog");
assert(statuslog);
statuslog->info("Redirecting to error page, code={} reason={}", parCode, parReason);
std::ostringstream oss;
oss << "error.cgi?code=" << parCode << "&reason=" << parReason._to_integral();
return make_redirect(int_to_status_code(parCode), oss.str());
return make_redirect(parCode, oss.str());
}
} //namespace tawashi

View file

@ -58,7 +58,7 @@ namespace tawashi {
const SettingsBag& settings() const;
virtual std::string load_mustache() const;
HttpHeader make_redirect (HttpStatusCodes parCode, const std::string& parLocation);
HttpHeader make_error_redirect (uint16_t parCode, ErrorReasons parReason);
HttpHeader make_error_redirect (HttpStatusCodes parCode, ErrorReasons parReason);
private:
virtual HttpHeader on_process();

View file

@ -113,7 +113,7 @@ namespace tawashi {
}
catch (const TawashiException& e) {
statuslog->error(e.what());
return make_error_redirect(500, e.reason());
return make_error_redirect(HttpStatusCodes::Code500_InternalServerError, e.reason());
}
try {
lang = get_value_from_post(post, make_string_ref(g_language_key));
@ -126,14 +126,14 @@ namespace tawashi {
const SettingsBag& settings = this->settings();
const auto max_sz = settings.as<uint32_t>("max_pastie_size");
if (pastie.size() < settings.as<uint32_t>("min_pastie_size")) {
return make_error_redirect(431, ErrorReasons::PostLengthNotInRange);
return make_error_redirect(HttpStatusCodes::Code431_RequestHeaderFieldsTooLarge, ErrorReasons::PostLengthNotInRange);
}
if (max_sz and pastie.size() > max_sz) {
if (settings.as<bool>("truncate_long_pasties")) {
pastie = pastie.substr(0, max_sz);
}
else {
return make_error_redirect(431, ErrorReasons::PostLengthNotInRange);
return make_error_redirect(HttpStatusCodes::Code431_RequestHeaderFieldsTooLarge, ErrorReasons::PostLengthNotInRange);
}
}
@ -164,13 +164,13 @@ namespace tawashi {
) -> StringOrHeader {
auto& redis = this->redis();
if (not redis.is_connected()) {
return std::make_pair(boost::optional<std::string>(), make_error_redirect(503, ErrorReasons::RedisDisconnected));
return std::make_pair(boost::optional<std::string>(), make_error_redirect(HttpStatusCodes::Code503_ServiceUnavailable, ErrorReasons::RedisDisconnected));
}
std::string ip_hash = hashed_ip(cgi_env().remote_addr());
if (redis.get(ip_hash)) {
//please wait and submit again
return std::make_pair(boost::optional<std::string>(), make_error_redirect(429, ErrorReasons::UserFlooding));
return std::make_pair(boost::optional<std::string>(), make_error_redirect(HttpStatusCodes::Code429_TooManyRequests, ErrorReasons::UserFlooding));
}
const auto next_id = redis.incr("paste_counter");
@ -187,6 +187,6 @@ namespace tawashi {
return std::make_pair(boost::make_optional(token), HttpHeader());
}
return std::make_pair(boost::optional<std::string>(), make_error_redirect(500, ErrorReasons::PastieNotSaved));
return std::make_pair(boost::optional<std::string>(), make_error_redirect(HttpStatusCodes::Code500_InternalServerError, ErrorReasons::PastieNotSaved));
}
} //namespace tawashi