From 06517ba0a236bd19025cd2e54273487937bd705e Mon Sep 17 00:00:00 2001 From: nekobit Date: Tue, 11 Oct 2022 01:31:19 +0000 Subject: [PATCH] URL logging FossilOrigin-Name: 2e0af08207be9087bce0d16e13f97f909eb2e40dedec828e12e6909d6e356f44 --- src/http/httpserver.cpp | 6 ++++++ src/http/httpserver.h | 7 +++++++ src/http/microhttpd_server.cpp | 2 ++ src/http/request.cpp | 12 ++++++++++++ src/http/request.h | 1 + src/logger.h | 9 +++++++++ 6 files changed, 37 insertions(+) diff --git a/src/http/httpserver.cpp b/src/http/httpserver.cpp index 7a1d30a..9253c01 100644 --- a/src/http/httpserver.cpp +++ b/src/http/httpserver.cpp @@ -20,6 +20,7 @@ #include "http/request.h" #include #include +#include "logger.h" using namespace HTTP; @@ -65,3 +66,8 @@ std::optional Server::handle_request(HTTP::Request &reques return std::nullopt; } +void Server::log_request(const HTTP::Request& request) +{ + using namespace std::string_literals; + Logger::instance() << std::string(request.get_type_string()) + request.get_url(); +} diff --git a/src/http/httpserver.h b/src/http/httpserver.h index f5b1d29..dea6434 100644 --- a/src/http/httpserver.h +++ b/src/http/httpserver.h @@ -69,6 +69,13 @@ namespace HTTP */ std::optional handle_request(HTTP::Request& request); + /** + * @brief Logs a request to the Logger instance + * + * Mainly for debugging and seeing if things are working + */ + void log_request(const HTTP::Request& request); + inline uint16_t get_port() const noexcept { return port; } protected: std::vector req_map; diff --git a/src/http/microhttpd_server.cpp b/src/http/microhttpd_server.cpp index 4dc22d2..febdce7 100644 --- a/src/http/microhttpd_server.cpp +++ b/src/http/microhttpd_server.cpp @@ -76,6 +76,8 @@ namespace HTTP::Request req{str_to_method(method), url}; MHD_get_connection_values_n(conn, MHD_GET_ARGUMENT_KIND, key_val_iterator, &req); + + that->log_request(req); // Call response, ensure that the request above is proprogated well! std::optional resp = that->handle_request(req); diff --git a/src/http/request.cpp b/src/http/request.cpp index 8d24602..77feecd 100644 --- a/src/http/request.cpp +++ b/src/http/request.cpp @@ -113,3 +113,15 @@ bool Request::try_strip_slash() return ret; } +std::string_view Request::get_type_string() const noexcept +{ + switch (type) + { + case Request::Type::GET: return "GET"; + case Request::Type::POST: return "POST"; + case Request::Type::PUT: return "PUT"; + case Request::Type::DELETE: return "DELETE"; + case Request::Type::PATCH: return "PATCH"; + } + return "???"; +} diff --git a/src/http/request.h b/src/http/request.h index 48fcc64..58bf05e 100644 --- a/src/http/request.h +++ b/src/http/request.h @@ -108,6 +108,7 @@ namespace HTTP // Type shouldn't be modifyable inline Type get_type() const noexcept { return type; } + std::string_view get_type_string() const noexcept; // For consistancy... inline const std::string& get_url() const noexcept { return url; } KeyStrVarMap get; diff --git a/src/logger.h b/src/logger.h index b4b7e2e..4bd7dd0 100644 --- a/src/logger.h +++ b/src/logger.h @@ -26,6 +26,15 @@ #include #include +#define RED "\x1B[31m" +#define GREEN "\x1B[32m" +#define YELLOW "\x1B[33m" +#define BLUE "\x1B[34m" +#define MAGENTA "\x1B[35m" +#define CYAN "\x1B[36m" +#define WHITE "\x1B[37m" +#define RESET "\x1B[0m" + namespace Logger { using clock_type = typename std::chrono::system_clock;