OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
HttpTraceMiddleware.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <chrono>
5#include <cstdint>
6#include <sstream>
7#include <string>
8
9#include "crow.h"
10#include <crow/logging.h>
11
13{
15 {
16 public:
17 struct context
18 {
19 std::chrono::steady_clock::time_point start;
20 };
21
22 static void SetEnabled(bool enabled) noexcept
23 {
24 s_enabled.store(enabled, std::memory_order_release);
25 }
26
27 void before_handle(crow::request &req, crow::response & /*res*/, context &ctx)
28 {
29 if (!s_enabled.load(std::memory_order_acquire))
30 {
31 return;
32 }
33
34 ctx.start = std::chrono::steady_clock::now();
35
36 CROW_LOG_INFO << "[http] -> " << method_to_string(req)
37 << " " << req.url
38 << " host=" << header_or_empty(req, "Host")
39 << " from=" << remote_addr_or_unknown(req);
40 }
41
42 void after_handle(crow::request &req, crow::response &res, context &ctx)
43 {
44 if (!s_enabled.load(std::memory_order_acquire))
45 {
46 return;
47 }
48
49 const auto end = std::chrono::steady_clock::now();
50 const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - ctx.start).count();
51
52 CROW_LOG_INFO << "[http] <- " << method_to_string(req)
53 << " " << req.url
54 << " status=" << res.code
55 << " dur_ms=" << ms
56 << " from=" << remote_addr_or_unknown(req);
57 }
58
59 private:
60 static std::atomic<bool> s_enabled;
61
62 static std::string header_or_empty(const crow::request &req, const char *header)
63 {
64 try
65 {
66 return req.get_header_value(header);
67 }
68 catch (...)
69 {
70 return {};
71 }
72 }
73
74 // Best-effort remote address extraction across Crow versions.
75 static std::string remote_addr_or_unknown(const crow::request &req)
76 {
77 // Try to get remote IP address using various Crow APIs
78 // Method 1: remote_ip_address field (if available)
79 // Method 2: remote_ip_address() method (if available)
80 // Method 3: raw socket info from req.ci_ (crow internals)
81
82 // First, try to get from headers (X-Forwarded-For, X-Real-IP)
83 std::string ip = get_header_value_safe(req, "X-Forwarded-For");
84 if (!ip.empty())
85 {
86 // X-Forwarded-For can contain multiple IPs, take the first one
87 auto comma_pos = ip.find(',');
88 if (comma_pos != std::string::npos)
89 {
90 ip = ip.substr(0, comma_pos);
91 }
92 return trim(ip);
93 }
94
95 ip = get_header_value_safe(req, "X-Real-IP");
96 if (!ip.empty())
97 {
98 return trim(ip);
99 }
100
101 // If no headers, use a placeholder (actual socket IP requires Crow internals)
102 return "unknown";
103 }
104
105 static std::string get_header_value_safe(const crow::request &req, const char *header)
106 {
107 try
108 {
109 return req.get_header_value(header);
110 }
111 catch (...)
112 {
113 return {};
114 }
115 }
116
117 static std::string trim(const std::string &s)
118 {
119 size_t start = s.find_first_not_of(" \t\r\n");
120 if (start == std::string::npos) return {};
121 size_t end = s.find_last_not_of(" \t\r\n");
122 return s.substr(start, end - start + 1);
123 }
124
125 static std::string method_to_string(const crow::request &req)
126 {
127 // Best-effort; if Crow doesn't have method_name(), fall back to integer.
128 return method_name_if_available(req);
129 }
130
131 template <typename T>
132 static auto method_name_if_available(const T &r) -> decltype(crow::method_name(r.method), std::string{})
133 {
134 return std::string(crow::method_name(r.method));
135 }
136 static std::string method_name_if_available(const crow::request &r)
137 {
138 return std::to_string(static_cast<int>(r.method));
139 }
140 };
141}
142
143inline std::atomic<bool> Utils::Tracing::HttpTraceMiddleware::s_enabled{false};
void after_handle(crow::request &req, crow::response &res, context &ctx)
void before_handle(crow::request &req, crow::response &, context &ctx)
static void SetEnabled(bool enabled) noexcept
std::chrono::steady_clock::time_point start