Open
Description
In the handler functions I'd like to know which route matched:
server.Get("/user/:id", [](const httplib::Request &req, httplib::Response &res)
{
std::cout << req.matched_route << std::endl; // /user/:id
});
For the example above it doesn't make sense because the handler specifically gets added to that route, but if you re-use the handler then it's unclear.
It would also come in handy for set_pre_routing_handler
. I only know the req.path
and would have to write something myself to find out which route matched while the internals of the library (probably) just know which route matched.
Background:
I've wrapped the server.(Get|Post|...)
methods and gave it an additional parameter, needed_permissions
which is a std::vector<std::string>
void HTTPServer::AddRoute(const std::string &method, const std::string &route, httplib::Server::Handler handler, StringList needed_permissions = {})
{
// add route
// ...
// map "route -> needed_permissions"
// ...
}
then:
server.set_pre_routing_handler([](const httplib::Request& req, httplib::Response& res)
{
// look up needed_permissions for req.matched_route
// ...
// respond with "401 Unauthorized" if there's no auth header or "403 Forbidden" if the permissions of the requester don't fulfill needed_permissions
// ...
// return httplib::Server::HandlerResponse::Handled;
// otherwise
return httplib::Server::HandlerResponse::Unhandled;
});