Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
Add "overwrite" option to the handle() method in mux
Browse files Browse the repository at this point in the history
This allows to add several method (POST,DELETE,PUT) to an existing handler without headache
or having to keep a reference to every handle
  • Loading branch information
adevress committed Jan 10, 2019
1 parent 4b85e42 commit c0bff45
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/served/multiplexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/served/multiplexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----

Expand Down

0 comments on commit c0bff45

Please sign in to comment.