-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: let the Request& know which route matched #2101
Comments
I assume you mean post-routing handler? I second the request to store the route pattern in the matcher – a prerequisite to making this work. But please don't add another |
Alright, it's easy enough to implement, and I know I'll need it for removable endpoints in the WebSockets code. So here it is: #2104 |
Thanks! This will solve me some headaches. |
Thanks! And no, I meant pre-routing, since I want to check required permissions for routes, it'd be too late for that in the post-routing handler |
You can still reject requests in post-routing; handlers are invoked after. In pre-routing, we don't know which handler we're going to invoke … because that's decided during routing. |
@zon7 Not officially. I have a (nearly) fully working and compliant implementation of the client and server (the SSL client is the last to-do item, and it's very minor). There are a few PRs that need to be merged first; after that, I hope to convince @yhirose to host (but not maintain) the WebSocket implementation in this repo. I'll ping you when it's time, and you can add your support if you're interested. :-) |
Ah, ok then. It's post-routing and not post-request after all so it makes sense. The example in |
Nope, sorry, I made a mistake. This needs some more thought to fully address your scenario. @yhirose What use case did you have in mind for the post-routing handler? Why is that not invoked before the handlers? Edit: Would this make sense? (in if (matcher->match(req)) {
req.matched_route = matcher->pattern().c_str();
if(pre_handler_ && pre_handler_(req, res) == HandlerResponse::Handled)
return true;
handler(req, res);
return true;
} |
I hope this questions wasn't addressed to me because I don't know :) While having the |
I was partly thinking out loud and partly asking @yhirose. :-)
I'm thinking about adding |
@betsch85 sorry that I wasn't able to reply sooner... Could you please explain a bit more about |
Becuase I want to know which route matched, not what the user typed in the browser If I have a route, lets say I'd have to split the path and check the route myself which makes everything unnecessarily more complicated, especially if a static route and a route with a parameter overlap, e.g. In the pre-routing handler I'd like to do: if (user_permissions.contains(needed_permissions[req.matched_route]) {
return httplib::Server::HandlerResponse::Unhandled;
} else {
res.status = 401;
return httplib::Server::HandlerResponse::Handled;
} instead of: auto matched_route = find_out_what_the_route_probably_was(req.path);
if (user_permissions.contains(needed_permissions[matched_route]) {
return httplib::Server::HandlerResponse::Unhandled;
} else {
res.status = 401;
return httplib::Server::HandlerResponse::Handled;
} I hope that makes sense |
In the handler functions I'd like to know which route matched:
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 thereq.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 astd::vector<std::string>
then:
The text was updated successfully, but these errors were encountered: