From c0bff45554647e60a93ee2806099429f628d6084 Mon Sep 17 00:00:00 2001 From: Adrien Devresse Date: Thu, 10 Jan 2019 16:18:05 +0100 Subject: [PATCH] Add "overwrite" option to the handle() method in mux This allows to add several method (POST,DELETE,PUT) to an existing handler without headache or having to keep a reference to every handle --- src/served/multiplexer.cpp | 12 ++++++++++-- src/served/multiplexer.hpp | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/served/multiplexer.cpp b/src/served/multiplexer.cpp index e550791..f962acc 100644 --- a/src/served/multiplexer.cpp +++ b/src/served/multiplexer.cpp @@ -121,14 +121,22 @@ multiplexer::get_segments(const std::string & path) // ----- http request handlers ----- served::methods_handler & -multiplexer::handle(const std::string & path, const std::string info /* = "" */) +multiplexer::handle(const std::string & path, const std::string info /* = "" */, bool overwrite) { + // Remove any duplicates. for ( auto it = _handler_candidates.begin(); it != _handler_candidates.end(); ) { if ( std::get<2>(*it) == path ) { - it = _handler_candidates.erase(it); + if(overwrite) + { + it = _handler_candidates.erase(it); + } + else + { + return std::get<1>(*it); + } } else { diff --git a/src/served/multiplexer.hpp b/src/served/multiplexer.hpp index c822d59..9628b86 100644 --- a/src/served/multiplexer.hpp +++ b/src/served/multiplexer.hpp @@ -139,7 +139,7 @@ class multiplexer * * @return returns a methods_handler used to specify handlers per HTTP method */ - served::methods_handler & handle(const std::string & path, const std::string info = ""); + served::methods_handler & handle(const std::string & path, const std::string info = "", bool overwrite = true); // ----- request forwarding -----