|
| 1 | +#pragma once |
| 2 | +#include "fastmcpp/server/middleware.hpp" |
| 3 | +#include "fastmcpp/types.hpp" |
| 4 | + |
| 5 | +#include <atomic> |
| 6 | +#include <chrono> |
| 7 | +#include <deque> |
| 8 | +#include <functional> |
| 9 | +#include <mutex> |
| 10 | +#include <string> |
| 11 | +#include <unordered_map> |
| 12 | + |
| 13 | +namespace fastmcpp::server |
| 14 | +{ |
| 15 | + |
| 16 | +/// Log entry for a request |
| 17 | +struct RequestLogEntry |
| 18 | +{ |
| 19 | + std::chrono::system_clock::time_point timestamp; |
| 20 | + std::string route; |
| 21 | + size_t payload_size; |
| 22 | + bool success; |
| 23 | + std::string error_message; // Empty if success |
| 24 | +}; |
| 25 | + |
| 26 | +/// Logging callback function type |
| 27 | +using LogCallback = std::function<void(const RequestLogEntry&)>; |
| 28 | + |
| 29 | +/// Logging middleware for audit trail (v2.13.0+) |
| 30 | +/// |
| 31 | +/// Provides optional request logging to track all route/tool invocations. |
| 32 | +/// Can be used as both BeforeHook and AfterHook for comprehensive logging. |
| 33 | +/// |
| 34 | +/// Usage: |
| 35 | +/// ```cpp |
| 36 | +/// auto logger = std::make_shared<LoggingMiddleware>( |
| 37 | +/// [](const RequestLogEntry& entry) { |
| 38 | +/// std::cout << entry.timestamp << " " << entry.route << std::endl; |
| 39 | +/// }); |
| 40 | +/// srv.add_before(logger->create_before_hook()); |
| 41 | +/// srv.add_after(logger->create_after_hook()); |
| 42 | +/// ``` |
| 43 | +class LoggingMiddleware |
| 44 | +{ |
| 45 | + public: |
| 46 | + explicit LoggingMiddleware(LogCallback callback) : callback_(std::move(callback)) {} |
| 47 | + |
| 48 | + /// Create a BeforeHook that logs incoming requests |
| 49 | + BeforeHook create_before_hook(); |
| 50 | + |
| 51 | + /// Create an AfterHook that logs completed requests |
| 52 | + AfterHook create_after_hook(); |
| 53 | + |
| 54 | + private: |
| 55 | + LogCallback callback_; |
| 56 | + std::mutex mutex_; |
| 57 | + std::unordered_map<std::string, size_t> request_sizes_; // Track sizes for after hook |
| 58 | +}; |
| 59 | + |
| 60 | +/// Rate limiting middleware for DoS prevention (v2.13.0+) |
| 61 | +/// |
| 62 | +/// Enforces per-route request limits using a sliding window algorithm. |
| 63 | +/// Rejects requests that exceed the configured rate. |
| 64 | +/// |
| 65 | +/// Usage: |
| 66 | +/// ```cpp |
| 67 | +/// auto limiter = std::make_shared<RateLimitMiddleware>( |
| 68 | +/// 100, // max requests |
| 69 | +/// std::chrono::minutes(1) // per time window |
| 70 | +/// ); |
| 71 | +/// srv.add_before(limiter->create_hook()); |
| 72 | +/// ``` |
| 73 | +class RateLimitMiddleware |
| 74 | +{ |
| 75 | + public: |
| 76 | + /// Construct rate limiter |
| 77 | + /// @param max_requests Maximum requests allowed in time window |
| 78 | + /// @param window Time window for rate limiting |
| 79 | + RateLimitMiddleware(size_t max_requests, |
| 80 | + std::chrono::steady_clock::duration window = std::chrono::minutes(1)) |
| 81 | + : max_requests_(max_requests), window_(window) |
| 82 | + { |
| 83 | + } |
| 84 | + |
| 85 | + /// Create a BeforeHook that enforces rate limits |
| 86 | + BeforeHook create_hook(); |
| 87 | + |
| 88 | + /// Get current request count for a route |
| 89 | + size_t get_request_count(const std::string& route); |
| 90 | + |
| 91 | + /// Reset rate limit counters (for testing) |
| 92 | + void reset(); |
| 93 | + |
| 94 | + private: |
| 95 | + size_t max_requests_; |
| 96 | + std::chrono::steady_clock::duration window_; |
| 97 | + std::mutex mutex_; |
| 98 | + |
| 99 | + struct RouteStats |
| 100 | + { |
| 101 | + std::deque<std::chrono::steady_clock::time_point> timestamps; |
| 102 | + }; |
| 103 | + |
| 104 | + std::unordered_map<std::string, RouteStats> stats_; |
| 105 | + |
| 106 | + void cleanup_old_entries(RouteStats& stats); |
| 107 | +}; |
| 108 | + |
| 109 | +/// Concurrency limiting middleware for resource control (v2.13.0+) |
| 110 | +/// |
| 111 | +/// Limits the number of concurrent route handler executions. |
| 112 | +/// Uses atomic counters for thread-safe tracking. |
| 113 | +/// |
| 114 | +/// Usage: |
| 115 | +/// ```cpp |
| 116 | +/// auto limiter = std::make_shared<ConcurrencyLimitMiddleware>(10); // Max 10 parallel |
| 117 | +/// srv.add_before(limiter->create_before_hook()); |
| 118 | +/// srv.add_after(limiter->create_after_hook()); |
| 119 | +/// ``` |
| 120 | +class ConcurrencyLimitMiddleware |
| 121 | +{ |
| 122 | + public: |
| 123 | + /// Construct concurrency limiter |
| 124 | + /// @param max_concurrent Maximum number of concurrent handler executions |
| 125 | + explicit ConcurrencyLimitMiddleware(size_t max_concurrent) : max_concurrent_(max_concurrent) {} |
| 126 | + |
| 127 | + /// Create a BeforeHook that checks concurrency limit |
| 128 | + BeforeHook create_before_hook(); |
| 129 | + |
| 130 | + /// Create an AfterHook that releases concurrency slot |
| 131 | + AfterHook create_after_hook(); |
| 132 | + |
| 133 | + /// Get current concurrent request count |
| 134 | + size_t get_current_count() const |
| 135 | + { |
| 136 | + return current_count_.load(); |
| 137 | + } |
| 138 | + |
| 139 | + private: |
| 140 | + size_t max_concurrent_; |
| 141 | + std::atomic<size_t> current_count_{0}; |
| 142 | +}; |
| 143 | + |
| 144 | +} // namespace fastmcpp::server |
0 commit comments