mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-04-12 19:58:29 +00:00
Add Cookbook S01-S22 (draft)
This commit is contained in:
54
docs-src/pages/en/cookbook/s09-pre-routing.md
Normal file
54
docs-src/pages/en/cookbook/s09-pre-routing.md
Normal file
@@ -0,0 +1,54 @@
|
||||
---
|
||||
title: "S09. Add Pre-Processing to All Routes"
|
||||
order: 28
|
||||
status: "draft"
|
||||
---
|
||||
|
||||
Sometimes you want the same logic to run before every request — auth checks, logging, rate limiting. Register those with `set_pre_routing_handler()`.
|
||||
|
||||
## Basic usage
|
||||
|
||||
```cpp
|
||||
svr.set_pre_routing_handler(
|
||||
[](const httplib::Request &req, httplib::Response &res) {
|
||||
std::cout << req.method << " " << req.path << std::endl;
|
||||
return httplib::Server::HandlerResponse::Unhandled;
|
||||
});
|
||||
```
|
||||
|
||||
The pre-routing handler runs **before routing**. It catches every request — including ones that don't match any handler.
|
||||
|
||||
The `HandlerResponse` return value is key:
|
||||
|
||||
- Return `Unhandled` → continue normally (routing and the actual handler run)
|
||||
- Return `Handled` → the response is considered complete, skip the rest
|
||||
|
||||
## Use it for authentication
|
||||
|
||||
Put your shared auth check in one place.
|
||||
|
||||
```cpp
|
||||
svr.set_pre_routing_handler(
|
||||
[](const httplib::Request &req, httplib::Response &res) {
|
||||
if (req.path.rfind("/public", 0) == 0) {
|
||||
return httplib::Server::HandlerResponse::Unhandled; // no auth needed
|
||||
}
|
||||
|
||||
auto auth = req.get_header_value("Authorization");
|
||||
if (auth.empty()) {
|
||||
res.status = 401;
|
||||
res.set_content("unauthorized", "text/plain");
|
||||
return httplib::Server::HandlerResponse::Handled;
|
||||
}
|
||||
|
||||
return httplib::Server::HandlerResponse::Unhandled;
|
||||
});
|
||||
```
|
||||
|
||||
If auth fails, return `Handled` to respond with 401 immediately. If it passes, return `Unhandled` and let routing take over.
|
||||
|
||||
## For per-route auth
|
||||
|
||||
If you want different auth rules per route rather than a single global check, `set_pre_request_handler()` is a better fit. See S11. Authenticate Per Route with a Pre-Request Handler.
|
||||
|
||||
> **Note:** If all you want is to modify the response, `set_post_routing_handler()` is the right tool. See S10. Add Response Headers with a Post-Routing Handler.
|
||||
Reference in New Issue
Block a user