Update documentation to clarify progress callback usage and user data handling in examples

This commit is contained in:
yhirose
2026-03-13 22:54:29 -04:00
parent 3ad4a4243a
commit 2e61fd3e6e
4 changed files with 12 additions and 12 deletions

View File

@@ -24,7 +24,7 @@ A collection of recipes that answer "How do I...?" questions. Each recipe is sel
### Streaming & Progress
- Receive a response as a stream
- Use the progress callback (`set_progress`)
- Use the progress callback (`DownloadProgress` / `UploadProgress`)
### Connection & Performance
- Set timeouts (`set_connection_timeout` / `set_read_timeout`)
@@ -90,6 +90,6 @@ A collection of recipes that answer "How do I...?" questions. Each recipe is sel
## WebSocket
- Implement a WebSocket echo server and client
- Configure heartbeats (`set_websocket_ping_interval`)
- Configure heartbeats (`set_websocket_ping_interval` / `CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND`)
- Handle connection close
- Send and receive binary frames

View File

@@ -60,7 +60,7 @@ For uploading large files, `make_file_provider()` comes in handy. It streams the
```cpp
httplib::Client cli("http://localhost:8080");
auto res = cli.Post("/upload", {}, {
auto res = cli.Post("/upload", {}, {}, {
httplib::make_file_provider("file", "/path/to/large-file.zip")
});
```
@@ -160,16 +160,16 @@ svr.set_post_routing_handler([](const auto &req, auto &res) {
});
```
Use `req.user_data` to pass data from middleware to handlers. This is useful for sharing things like decoded auth tokens.
Use `res.user_data` to pass data from middleware to handlers. This is useful for sharing things like decoded auth tokens.
```cpp
svr.set_pre_routing_handler([](const auto &req, auto &res) {
req.user_data["auth_user"] = std::string("alice");
res.user_data["auth_user"] = std::string("alice");
return httplib::Server::HandlerResponse::Unhandled;
});
svr.Get("/me", [](const auto &req, auto &res) {
auto user = std::any_cast<std::string>(req.user_data.at("auth_user"));
auto user = std::any_cast<std::string>(res.user_data.at("auth_user"));
res.set_content("Hello, " + user, "text/plain");
});
```

View File

@@ -24,7 +24,7 @@ order: 0
### ストリーミング・進捗
- レスポンスをストリーミングで受信する
- 進捗コールバックを使う(`set_progress`
- 進捗コールバックを使う(`DownloadProgress` / `UploadProgress`
### 接続・パフォーマンス
- タイムアウトを設定する(`set_connection_timeout` / `set_read_timeout`
@@ -90,6 +90,6 @@ order: 0
## WebSocket
- WebSocket エコーサーバー/クライアントを実装する
- ハートビートを設定する(`set_websocket_ping_interval`
- ハートビートを設定する(`set_websocket_ping_interval` / `CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND`
- 接続クローズをハンドリングする
- バイナリフレームを送受信する

View File

@@ -60,7 +60,7 @@ svr.Get("/stream", [](const auto &, auto &res) {
```cpp
httplib::Client cli("http://localhost:8080");
auto res = cli.Post("/upload", {}, {
auto res = cli.Post("/upload", {}, {}, {
httplib::make_file_provider("file", "/path/to/large-file.zip")
});
```
@@ -160,16 +160,16 @@ svr.set_post_routing_handler([](const auto &req, auto &res) {
});
```
`req.user_data` を使うと、ミドルウェアからハンドラーにデータを渡せます。認証トークンのデコード結果を共有するときに便利です。
`res.user_data` を使うと、ミドルウェアからハンドラーにデータを渡せます。認証トークンのデコード結果を共有するときに便利です。
```cpp
svr.set_pre_routing_handler([](const auto &req, auto &res) {
req.user_data["auth_user"] = std::string("alice");
res.user_data["auth_user"] = std::string("alice");
return httplib::Server::HandlerResponse::Unhandled;
});
svr.Get("/me", [](const auto &req, auto &res) {
auto user = std::any_cast<std::string>(req.user_data.at("auth_user"));
auto user = std::any_cast<std::string>(res.user_data.at("auth_user"));
res.set_content("Hello, " + user, "text/plain");
});
```