@@ -636,6 +636,7 @@ using Ranges = std::vector<Range>;
636636struct Request {
637637 std::string method;
638638 std::string path;
639+ std::string matched_route;
639640 Params params;
640641 Headers headers;
641642 std::string body;
@@ -887,10 +888,16 @@ namespace detail {
887888
888889class MatcherBase {
889890public:
891+ MatcherBase (std::string pattern) : pattern_(pattern) {}
890892 virtual ~MatcherBase () = default ;
891893
894+ const std::string &pattern () const { return pattern_; }
895+
892896 // Match request path and populate its matches and
893897 virtual bool match (Request &request) const = 0;
898+
899+ private:
900+ std::string pattern_;
894901};
895902
896903/* *
@@ -942,7 +949,8 @@ class PathParamsMatcher final : public MatcherBase {
942949 */
943950class RegexMatcher final : public MatcherBase {
944951public:
945- RegexMatcher (const std::string &pattern) : regex_(pattern) {}
952+ RegexMatcher (const std::string &pattern)
953+ : MatcherBase(pattern), regex_(pattern) {}
946954
947955 bool match (Request &request) const override ;
948956
@@ -1009,9 +1017,12 @@ class Server {
10091017 }
10101018
10111019 Server &set_exception_handler (ExceptionHandler handler);
1020+
10121021 Server &set_pre_routing_handler (HandlerWithResponse handler);
10131022 Server &set_post_routing_handler (Handler handler);
10141023
1024+ Server &set_pre_request_handler (HandlerWithResponse handler);
1025+
10151026 Server &set_expect_100_continue_handler (Expect100ContinueHandler handler);
10161027 Server &set_logger (Logger logger);
10171028
@@ -1153,6 +1164,7 @@ class Server {
11531164 ExceptionHandler exception_handler_;
11541165 HandlerWithResponse pre_routing_handler_;
11551166 Handler post_routing_handler_;
1167+ HandlerWithResponse pre_request_handler_;
11561168 Expect100ContinueHandler expect_100_continue_handler_;
11571169
11581170 Logger logger_;
@@ -6224,7 +6236,8 @@ inline time_t BufferStream::duration() const { return 0; }
62246236
62256237inline const std::string &BufferStream::get_buffer () const { return buffer; }
62266238
6227- inline PathParamsMatcher::PathParamsMatcher (const std::string &pattern) {
6239+ inline PathParamsMatcher::PathParamsMatcher (const std::string &pattern)
6240+ : MatcherBase(pattern) {
62286241 constexpr const char marker[] = " /:" ;
62296242
62306243 // One past the last ending position of a path param substring
@@ -6475,6 +6488,11 @@ inline Server &Server::set_post_routing_handler(Handler handler) {
64756488 return *this ;
64766489}
64776490
6491+ inline Server &Server::set_pre_request_handler (HandlerWithResponse handler) {
6492+ pre_request_handler_ = std::move (handler);
6493+ return *this ;
6494+ }
6495+
64786496inline Server &Server::set_logger (Logger logger) {
64796497 logger_ = std::move (logger);
64806498 return *this ;
@@ -7129,7 +7147,11 @@ inline bool Server::dispatch_request(Request &req, Response &res,
71297147 const auto &handler = x.second ;
71307148
71317149 if (matcher->match (req)) {
7132- handler (req, res);
7150+ req.matched_route = matcher->pattern ();
7151+ if (!pre_request_handler_ ||
7152+ pre_request_handler_ (req, res) != HandlerResponse::Handled) {
7153+ handler (req, res);
7154+ }
71337155 return true ;
71347156 }
71357157 }
@@ -7256,7 +7278,11 @@ inline bool Server::dispatch_request_for_content_reader(
72567278 const auto &handler = x.second ;
72577279
72587280 if (matcher->match (req)) {
7259- handler (req, res, content_reader);
7281+ req.matched_route = matcher->pattern ();
7282+ if (!pre_request_handler_ ||
7283+ pre_request_handler_ (req, res) != HandlerResponse::Handled) {
7284+ handler (req, res, content_reader);
7285+ }
72607286 return true ;
72617287 }
72627288 }
0 commit comments