@@ -618,9 +618,10 @@ build, `webserver(create_webserver{}.use_ssl(true))` throws
618618 digest auth.
619619* **`.auth_handler(auth_handler_ptr cb)`** — install a centralized
620620 authentication handler that runs before every resource's `render_*`
621- call. The callback returns `nullptr` to allow the request to proceed,
622- or an `http_response` (typically `http_response::unauthorized(realm)`)
623- to reject it. Single source of truth for auth — see [Centralized
621+ call. The callback returns `std::nullopt` to allow the request to
622+ proceed, or an `http_response` (typically
623+ `http_response::unauthorized(realm)`) to reject it. Single source of
624+ truth for auth — see [Centralized
624625 authentication](#using-centralized-authentication).
625626* **`.auth_skip_paths(const std::vector<std::string>& paths)`** — paths
626627 to bypass `auth_handler`. Exact match (`"/health"`) and wildcard
@@ -1226,14 +1227,13 @@ resource. Centralized authentication lets you define the policy once and
12261227have it applied automatically to every request:
12271228
12281229``` cpp
1229- // auth runs once per request before any render_*; return nullptr to allow
1230+ // auth runs once per request before any render_*; return std::nullopt to allow
12301231auto auth = [](const httpserver::http_request& req)
1231- -> std::shared_ptr <httpserver::http_response> {
1232+ -> std::optional <httpserver::http_response> {
12321233 if (req.get_user() != "admin" || req.get_pass() != "secret") {
1233- return std::make_shared<httpserver::http_response>(
1234- httpserver::http_response::unauthorized ("MyRealm"));
1234+ return httpserver::http_response::unauthorized("MyRealm");
12351235 }
1236- return nullptr ;
1236+ return std::nullopt ;
12371237};
12381238
12391239httpserver::webserver ws{httpserver::create_webserver (8080)
@@ -1248,9 +1248,10 @@ ws.start(true);
12481248The `auth_handler` callback runs for every request before the resource's
12491249render method. It receives the `http_request` and can:
12501250
1251- * Return `nullptr ` to allow the request to proceed normally.
1251+ * Return `std::nullopt ` to allow the request to proceed normally.
12521252* Return an `http_response` (typically `http_response::unauthorized`) to
1253- reject the request.
1253+ reject the request. The response is moved onto the wire by the
1254+ dispatcher; no heap allocation is required.
12541255
12551256`auth_skip_paths` accepts a vector of paths that should bypass the
12561257handler:
0 commit comments