URL logging

FossilOrigin-Name: 2e0af08207be9087bce0d16e13f97f909eb2e40dedec828e12e6909d6e356f44
This commit is contained in:
nekobit 2022-10-11 01:31:19 +00:00
parent 6b047d9160
commit 06517ba0a2
6 changed files with 37 additions and 0 deletions

View file

@ -20,6 +20,7 @@
#include "http/request.h"
#include <cstdint>
#include <iostream>
#include "logger.h"
using namespace HTTP;
@ -65,3 +66,8 @@ std::optional<HTTP::Response const> 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();
}

View file

@ -69,6 +69,13 @@ namespace HTTP
*/
std::optional<HTTP::Response const> 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<RequestCallbackPair_t> req_map;

View file

@ -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<HTTP::Response> resp = that->handle_request(req);

View file

@ -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 "???";
}

View file

@ -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;

View file

@ -26,6 +26,15 @@
#include <iterator>
#include <cstddef>
#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;