mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-04-11 19:28:30 +00:00
Update documentation to clarify progress callback usage and user data handling in examples
This commit is contained in:
@@ -24,7 +24,7 @@ A collection of recipes that answer "How do I...?" questions. Each recipe is sel
|
|||||||
|
|
||||||
### Streaming & Progress
|
### Streaming & Progress
|
||||||
- Receive a response as a stream
|
- Receive a response as a stream
|
||||||
- Use the progress callback (`set_progress`)
|
- Use the progress callback (`DownloadProgress` / `UploadProgress`)
|
||||||
|
|
||||||
### Connection & Performance
|
### Connection & Performance
|
||||||
- Set timeouts (`set_connection_timeout` / `set_read_timeout`)
|
- 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
|
## WebSocket
|
||||||
|
|
||||||
- Implement a WebSocket echo server and client
|
- 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
|
- Handle connection close
|
||||||
- Send and receive binary frames
|
- Send and receive binary frames
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ For uploading large files, `make_file_provider()` comes in handy. It streams the
|
|||||||
```cpp
|
```cpp
|
||||||
httplib::Client cli("http://localhost:8080");
|
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")
|
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
|
```cpp
|
||||||
svr.set_pre_routing_handler([](const auto &req, auto &res) {
|
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;
|
return httplib::Server::HandlerResponse::Unhandled;
|
||||||
});
|
});
|
||||||
|
|
||||||
svr.Get("/me", [](const auto &req, auto &res) {
|
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");
|
res.set_content("Hello, " + user, "text/plain");
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ order: 0
|
|||||||
|
|
||||||
### ストリーミング・進捗
|
### ストリーミング・進捗
|
||||||
- レスポンスをストリーミングで受信する
|
- レスポンスをストリーミングで受信する
|
||||||
- 進捗コールバックを使う(`set_progress`)
|
- 進捗コールバックを使う(`DownloadProgress` / `UploadProgress`)
|
||||||
|
|
||||||
### 接続・パフォーマンス
|
### 接続・パフォーマンス
|
||||||
- タイムアウトを設定する(`set_connection_timeout` / `set_read_timeout`)
|
- タイムアウトを設定する(`set_connection_timeout` / `set_read_timeout`)
|
||||||
@@ -90,6 +90,6 @@ order: 0
|
|||||||
## WebSocket
|
## WebSocket
|
||||||
|
|
||||||
- WebSocket エコーサーバー/クライアントを実装する
|
- WebSocket エコーサーバー/クライアントを実装する
|
||||||
- ハートビートを設定する(`set_websocket_ping_interval`)
|
- ハートビートを設定する(`set_websocket_ping_interval` / `CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND`)
|
||||||
- 接続クローズをハンドリングする
|
- 接続クローズをハンドリングする
|
||||||
- バイナリフレームを送受信する
|
- バイナリフレームを送受信する
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ svr.Get("/stream", [](const auto &, auto &res) {
|
|||||||
```cpp
|
```cpp
|
||||||
httplib::Client cli("http://localhost:8080");
|
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")
|
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
|
```cpp
|
||||||
svr.set_pre_routing_handler([](const auto &req, auto &res) {
|
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;
|
return httplib::Server::HandlerResponse::Unhandled;
|
||||||
});
|
});
|
||||||
|
|
||||||
svr.Get("/me", [](const auto &req, auto &res) {
|
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");
|
res.set_content("Hello, " + user, "text/plain");
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user