From f98c8dabb02a44f0fa41eb6db200fc90c8d05409 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 12 Aug 2021 16:13:36 +0200 Subject: [PATCH] Http: add Status::ToString method, use to report errors to sentry for custom fingerprint --- include/Http.h | 3 ++ src/Http.cpp | 75 ++++++++++++++++++++++++++++++++++++++++ src/THeartbeatThread.cpp | 4 +-- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/include/Http.h b/include/Http.h index 2e439fc..35916bf 100644 --- a/include/Http.h +++ b/include/Http.h @@ -6,4 +6,7 @@ namespace Http { std::string GET(const std::string& host, int port, const std::string& target); std::string POST(const std::string& host, const std::string& target, const std::unordered_map& fields, const std::string& body, bool json, int* status = nullptr); +namespace Status { + std::string ToString(int code); +} } diff --git a/src/Http.cpp b/src/Http.cpp index a9090de..2a4c11b 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace beast = boost::beast; // from namespace http = beast::http; // from @@ -166,3 +167,77 @@ std::string Http::POST(const std::string& host, const std::string& target, const return "-1"; } } + +// RFC 2616, RFC 7231 +static std::map Map = { + { 100, "Continue" }, + { 101, "Switching Protocols" }, + { 102, "Processing" }, + { 103, "Early Hints" }, + { 200, "OK" }, + { 201, "Created" }, + { 202, "Accepted" }, + { 203, "Non-Authoritative Information" }, + { 204, "No Content" }, + { 205, "Reset Content" }, + { 206, "Partial Content" }, + { 207, "Multi-Status" }, + { 208, "Already Reported" }, + { 226, "IM Used" }, + { 300, "Multiple Choices" }, + { 301, "Moved Permanently" }, + { 302, "Found" }, + { 303, "See Other" }, + { 304, "Not Modified" }, + { 305, "Use Proxy" }, + { 306, "(Unused)" }, + { 307, "Temporary Redirect" }, + { 308, "Permanent Redirect" }, + { 400, "Bad Request" }, + { 401, "Unauthorized" }, + { 402, "Payment Required" }, + { 403, "Forbidden" }, + { 404, "Not Found" }, + { 405, "Method Not Allowed" }, + { 406, "Not Acceptable" }, + { 407, "Proxy Authentication Required" }, + { 408, "Request Timeout" }, + { 409, "Conflict" }, + { 410, "Gone" }, + { 411, "Length Required" }, + { 412, "Precondition Failed" }, + { 413, "Payload Too Large" }, + { 414, "URI Too Long" }, + { 415, "Unsupported Media Type" }, + { 416, "Range Not Satisfiable" }, + { 417, "Expectation Failed" }, + { 421, "Misdirected Request" }, + { 422, "Unprocessable Entity" }, + { 423, "Locked" }, + { 424, "Failed Dependency" }, + { 425, "Too Early" }, + { 426, "Upgrade Required" }, + { 428, "Precondition Required" }, + { 429, "Too Many Requests" }, + { 431, "Request Header Fields Too Large" }, + { 451, "Unavailable For Legal Reasons" }, + { 500, "Internal Server Error" }, + { 501, "Not Implemented" }, + { 502, "Bad Gateway" }, + { 503, "Service Unavailable" }, + { 504, "Gateway Timeout" }, + { 505, "HTTP Version Not Supported" }, + { 506, "Variant Also Negotiates" }, + { 507, "Insufficient Storage" }, + { 508, "Loop Detected" }, + { 510, "Not Extended" }, + { 511, "Network Authentication Required" }, +}; + +std::string Http::Status::ToString(int code) { + if (Map.find(code) != Map.end()) { + return Map.at(code); + } else { + return "Unassigned"; + } +} diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index e6c63cd..181f40f 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -47,7 +47,7 @@ void THeartbeatThread::operator()() { #if DEBUG debug("sending log to sentry: " + std::to_string(status) + " for " + transaction); #endif // DEBUG - Sentry.Log(SentryLevel::Error, "default", "unexpected backend response (" + std::to_string(status) + ")"); + Sentry.Log(SentryLevel::Error, "default", Http::Status::ToString(status) + " (" + std::to_string(status) + ")"); }; auto Target = "/heartbeat"; @@ -66,7 +66,7 @@ void THeartbeatThread::operator()() { std::this_thread::sleep_for(std::chrono::milliseconds(500)); T = Http::POST(Application::GetBackup2Hostname(), Target, {}, Body, false, &ResponseCode); if (T.substr(0, 2) != "20" || ResponseCode != 200) { - warn("Backend system refused server! Server might not show in the public list"); + warn("Backend system refused server! Server will not show in the public server list."); isAuth = false; SentryReportError(Application::GetBackup2Hostname() + Target, ResponseCode); }