mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-04-11 19:28:30 +00:00
Documentation Site on GitHub Pages (#2376)
* Add initial documentations * Update documentation for Basic Client and add WebSocket section * feat: add a static site generator with multi-language support - Introduced a new Rust-based static site generator in the `docs-gen` directory. - Implemented core functionality for building sites from markdown files, including: - Configuration loading from `config.toml`. - Markdown rendering with frontmatter support. - Navigation generation based on page structure. - Static file copying and output directory management. - Added templates for base layout, pages, and portal. - Created a CSS file for styling and a JavaScript file for interactive features like language selection and theme toggling. - Updated documentation source with new configuration and example pages in English and Japanese. - Added a `justfile` target for building the documentation site. * Add language/theme toggle functionality - Created a new Japanese tour index page at docs/ja/tour/index.html - Implemented navigation links for various sections of the cpp-httplib tutorial - Added a language selector to switch between English and Japanese - Introduced theme toggle functionality to switch between light and dark modes - Added mobile sidebar toggle for better navigation on smaller screens
This commit is contained in:
88
docs-src/pages/ja/tour/01-getting-started.md
Normal file
88
docs-src/pages/ja/tour/01-getting-started.md
Normal file
@@ -0,0 +1,88 @@
|
||||
---
|
||||
title: "Getting Started"
|
||||
order: 1
|
||||
---
|
||||
|
||||
cpp-httplibを始めるのに必要なのは、`httplib.h`とC++コンパイラーだけです。ファイルをダウンロードして、Hello Worldサーバーを動かすところまでやってみましょう。
|
||||
|
||||
## httplib.h の入手
|
||||
|
||||
GitHubから直接ダウンロードできます。常に最新版を使ってください。
|
||||
|
||||
```sh
|
||||
curl -LO https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/httplib.h
|
||||
```
|
||||
|
||||
ダウンロードした `httplib.h` をプロジェクトのディレクトリに置けば、準備完了です。
|
||||
|
||||
## コンパイラーの準備
|
||||
|
||||
| OS | 開発環境 | セットアップ |
|
||||
| -- | -------- | ------------ |
|
||||
| macOS | Apple Clang | Xcode Command Line Tools (`xcode-select --install`) |
|
||||
| Ubuntu | clang++ または g++ | `apt install clang` または `apt install g++` |
|
||||
| Windows | MSVC | Visual Studio 2022 以降(C++ コンポーネントを含めてインストール) |
|
||||
|
||||
## Hello World サーバー
|
||||
|
||||
次のコードを `server.cpp` として保存しましょう。
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
|
||||
int main() {
|
||||
httplib::Server svr;
|
||||
|
||||
svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
|
||||
res.set_content("Hello, World!", "text/plain");
|
||||
});
|
||||
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
たった数行で、HTTPリクエストに応答するサーバーが書けます。
|
||||
|
||||
## コンパイルと実行
|
||||
|
||||
このチュートリアルのサンプルコードは、コードを簡潔に書けるC++17で書いています。cpp-httplib自体はC++11でもコンパイルできます。
|
||||
|
||||
```sh
|
||||
# macOS
|
||||
clang++ -std=c++17 -o server server.cpp
|
||||
|
||||
# Linux
|
||||
# `-pthread`: cpp-httplibは内部でスレッドを使用
|
||||
clang++ -std=c++17 -pthread -o server server.cpp
|
||||
|
||||
# Windows (Developer Command Prompt)
|
||||
# `/EHsc`: C++例外処理を有効化
|
||||
cl /EHsc /std:c++17 server.cpp
|
||||
```
|
||||
|
||||
コンパイルできたら実行します。
|
||||
|
||||
```sh
|
||||
# macOS / Linux
|
||||
./server
|
||||
|
||||
# Windows
|
||||
server.exe
|
||||
```
|
||||
|
||||
ブラウザで `http://localhost:8080` を開いてください。"Hello, World!" と表示されれば成功です。
|
||||
|
||||
`curl` でも確認できます。
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/
|
||||
# Hello, World!
|
||||
```
|
||||
|
||||
サーバーを停止するには、ターミナルで `Ctrl+C` を押します。
|
||||
|
||||
## 次のステップ
|
||||
|
||||
サーバーの基本がわかりましたね。次は、クライアント側を見てみましょう。cpp-httplibはHTTPクライアント機能も備えています。
|
||||
|
||||
**次:** [Basic Client](../02-basic-client)
|
||||
266
docs-src/pages/ja/tour/02-basic-client.md
Normal file
266
docs-src/pages/ja/tour/02-basic-client.md
Normal file
@@ -0,0 +1,266 @@
|
||||
---
|
||||
title: "Basic Client"
|
||||
order: 2
|
||||
---
|
||||
|
||||
cpp-httplibはサーバーだけでなく、HTTPクライアント機能も備えています。`httplib::Client` を使って、GETやPOSTリクエストを送ってみましょう。
|
||||
|
||||
## テスト用サーバーの準備
|
||||
|
||||
クライアントの動作を確認するために、リクエストを受け付けるサーバーを用意します。次のコードを保存し、前章と同じ手順でコンパイル・実行してください。サーバーの詳しい解説は次章で行います。
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Server svr;
|
||||
|
||||
svr.Get("/hi", [](const auto &, auto &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
|
||||
svr.Get("/search", [](const auto &req, auto &res) {
|
||||
auto q = req.get_param_value("q");
|
||||
res.set_content("Query: " + q, "text/plain");
|
||||
});
|
||||
|
||||
svr.Post("/post", [](const auto &req, auto &res) {
|
||||
res.set_content(req.body, "text/plain");
|
||||
});
|
||||
|
||||
svr.Post("/submit", [](const auto &req, auto &res) {
|
||||
std::string result;
|
||||
for (auto &[key, val] : req.params) {
|
||||
result += key + " = " + val + "\n";
|
||||
}
|
||||
res.set_content(result, "text/plain");
|
||||
});
|
||||
|
||||
svr.Post("/upload", [](const auto &req, auto &res) {
|
||||
auto f = req.form.get_file("file");
|
||||
auto content = f.filename + " (" + std::to_string(f.content.size()) + " bytes)";
|
||||
res.set_content(content, "text/plain");
|
||||
});
|
||||
|
||||
svr.Get("/users/:id", [](const auto &req, auto &res) {
|
||||
auto id = req.path_params.at("id");
|
||||
res.set_content("User ID: " + id, "text/plain");
|
||||
});
|
||||
|
||||
svr.Get(R"(/files/(\d+))", [](const auto &req, auto &res) {
|
||||
auto id = req.matches[1];
|
||||
res.set_content("File ID: " + std::string(id), "text/plain");
|
||||
});
|
||||
|
||||
std::cout << "Listening on port 8080..." << std::endl;
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
## GETリクエスト
|
||||
|
||||
サーバーが起動したら、別のターミナルを開いて試してみましょう。まず、最もシンプルなGETリクエストです。
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
|
||||
auto res = cli.Get("/hi");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 200
|
||||
std::cout << res->body << std::endl; // Hello!
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`httplib::Client` のコンストラクターにサーバーのアドレスを渡し、`Get()` でリクエストを送ります。戻り値の `res` からステータスコードやボディを取得できます。
|
||||
|
||||
対応する `curl` コマンドはこうなります。
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/hi
|
||||
# Hello!
|
||||
```
|
||||
|
||||
## レスポンスの確認
|
||||
|
||||
レスポンスには、ステータスコードとボディ以外にもヘッダー情報が含まれています。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/hi");
|
||||
if (res) {
|
||||
// ステータスコード
|
||||
std::cout << res->status << std::endl; // 200
|
||||
|
||||
// ボディ
|
||||
std::cout << res->body << std::endl; // Hello!
|
||||
|
||||
// ヘッダー
|
||||
std::cout << res->get_header_value("Content-Type") << std::endl; // text/plain
|
||||
}
|
||||
```
|
||||
|
||||
`res->body` は `std::string` なので、JSON レスポンスをパースしたい場合は [nlohmann/json](https://github.com/nlohmann/json) などの JSON ライブラリにそのまま渡せます。
|
||||
|
||||
## クエリパラメーター
|
||||
|
||||
GETリクエストにクエリパラメーターを付けるには、URLに直接書くか、`httplib::Params` を使います。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/search", httplib::Params{{"q", "cpp-httplib"}});
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // Query: cpp-httplib
|
||||
}
|
||||
```
|
||||
|
||||
`httplib::Params` を使うと、特殊文字のURLエンコードを自動で行ってくれます。
|
||||
|
||||
```sh
|
||||
curl "http://localhost:8080/search?q=cpp-httplib"
|
||||
# Query: cpp-httplib
|
||||
```
|
||||
|
||||
## パスパラメーター
|
||||
|
||||
URLのパスに値を直接埋め込む場合も、クライアント側は特別なAPIは不要です。パスをそのまま `Get()` に渡すだけです。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/users/42");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // User ID: 42
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/users/42
|
||||
# User ID: 42
|
||||
```
|
||||
|
||||
テスト用サーバーには、正規表現でIDを数字のみに絞った `/files/(\d+)` もあります。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/files/42");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // File ID: 42
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/files/42
|
||||
# File ID: 42
|
||||
```
|
||||
|
||||
`/files/abc` のように数字以外を渡すと404が返ります。仕組みは次章で解説します。
|
||||
|
||||
## リクエストヘッダー
|
||||
|
||||
カスタムHTTPヘッダーを付けるには、`httplib::Headers` を渡します。`Get()` や `Post()` のどちらでも使えます。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Get("/hi", httplib::Headers{
|
||||
{"Authorization", "Bearer my-token"}
|
||||
});
|
||||
```
|
||||
|
||||
```sh
|
||||
curl -H "Authorization: Bearer my-token" http://localhost:8080/hi
|
||||
```
|
||||
|
||||
## POSTリクエスト
|
||||
|
||||
テキストデータをPOSTしてみましょう。`Post()` の第2引数にボディ、第3引数にContent-Typeを指定します。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Post("/post", "Hello, Server!", "text/plain");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 200
|
||||
std::cout << res->body << std::endl; // Hello, Server!
|
||||
}
|
||||
```
|
||||
|
||||
テスト用サーバーの `/post` はボディをそのまま返すので、送った文字列がそのまま返ってきます。
|
||||
|
||||
```sh
|
||||
curl -X POST -H "Content-Type: text/plain" -d "Hello, Server!" http://localhost:8080/post
|
||||
# Hello, Server!
|
||||
```
|
||||
|
||||
## フォームデータの送信
|
||||
|
||||
HTMLフォームのように、キーと値のペアを送ることもできます。`httplib::Params` を使います。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Post("/submit", httplib::Params{
|
||||
{"name", "Alice"},
|
||||
{"age", "30"}
|
||||
});
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl;
|
||||
// age = 30
|
||||
// name = Alice
|
||||
}
|
||||
```
|
||||
|
||||
これは `application/x-www-form-urlencoded` 形式で送信されます。
|
||||
|
||||
```sh
|
||||
curl -X POST -d "name=Alice&age=30" http://localhost:8080/submit
|
||||
```
|
||||
|
||||
## ファイルのPOST
|
||||
|
||||
ファイルをアップロードするには、`httplib::UploadFormDataItems` を使ってマルチパートフォームデータとして送信します。
|
||||
|
||||
```cpp
|
||||
auto res = cli.Post("/upload", httplib::UploadFormDataItems{
|
||||
{"file", "Hello, File!", "hello.txt", "text/plain"}
|
||||
});
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // hello.txt (12 bytes)
|
||||
}
|
||||
```
|
||||
|
||||
`UploadFormDataItems` の各要素は `{name, content, filename, content_type}` の4つのフィールドで構成されます。
|
||||
|
||||
```sh
|
||||
curl -F "file=Hello, File!;filename=hello.txt;type=text/plain" http://localhost:8080/upload
|
||||
```
|
||||
|
||||
## エラーハンドリング
|
||||
|
||||
ネットワーク通信では、サーバーに接続できない場合があります。`res` が有効かどうかを必ず確認しましょう。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:9999"); // 存在しないポート
|
||||
auto res = cli.Get("/hi");
|
||||
|
||||
if (!res) {
|
||||
// 接続エラー
|
||||
std::cout << "Error: " << httplib::to_string(res.error()) << std::endl;
|
||||
// Error: Connection
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ここに到達すればレスポンスを受信できている
|
||||
if (res->status != 200) {
|
||||
std::cout << "HTTP Error: " << res->status << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << res->body << std::endl;
|
||||
```
|
||||
|
||||
エラーには2つのレベルがあります。
|
||||
|
||||
- **接続エラー**: サーバーに到達できなかった場合。`res` が偽になり、`res.error()` でエラーの種類を取得できます
|
||||
- **HTTPエラー**: サーバーからエラーステータス(404、500など)が返ってきた場合。`res` は真ですが、`res->status` を確認する必要があります
|
||||
|
||||
## 次のステップ
|
||||
|
||||
クライアントからリクエストを送る方法がわかりました。次は、サーバー側をもっと詳しく見てみましょう。ルーティングやパスパラメータなど、サーバーの機能を掘り下げます。
|
||||
|
||||
**次:** [Basic Server](../03-basic-server)
|
||||
280
docs-src/pages/ja/tour/03-basic-server.md
Normal file
280
docs-src/pages/ja/tour/03-basic-server.md
Normal file
@@ -0,0 +1,280 @@
|
||||
---
|
||||
title: "Basic Server"
|
||||
order: 3
|
||||
---
|
||||
|
||||
前章ではクライアントからリクエストを送りました。そのとき、テスト用サーバーを用意しましたね。この章では、あのサーバーの仕組みをひとつずつ紐解いていきます。
|
||||
|
||||
## サーバーの起動
|
||||
|
||||
ルーティングを登録したら、最後に `svr.listen()` を呼んでサーバーを起動します。
|
||||
|
||||
```cpp
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
```
|
||||
|
||||
第1引数はホスト、第2引数はポート番号です。`"0.0.0.0"` を指定すると、すべてのネットワークインターフェースでリクエストを受け付けます。自分のマシンからのアクセスだけに限定したいときは `"127.0.0.1"` を使います。
|
||||
|
||||
`listen()` はブロッキング呼び出しです。サーバーが停止するまで、この行から先には進みません。ターミナルで `Ctrl+C` を押すか、別スレッドから `svr.stop()` を呼ぶまでサーバーは動き続けます。
|
||||
|
||||
## ルーティング
|
||||
|
||||
サーバーの核になるのは「ルーティング」です。どのURLに、どのHTTPメソッドでアクセスされたら、何をするか。それを登録する仕組みです。
|
||||
|
||||
```cpp
|
||||
httplib::Server svr;
|
||||
|
||||
svr.Get("/hi", [](const httplib::Request &req, httplib::Response &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`svr.Get()` は、GETリクエストに対するハンドラーを登録します。第1引数がパス、第2引数がハンドラー関数です。`/hi` にGETリクエストが来たら、このラムダが呼ばれます。
|
||||
|
||||
HTTPメソッドごとにメソッドが用意されています。
|
||||
|
||||
```cpp
|
||||
svr.Get("/path", handler); // GET
|
||||
svr.Post("/path", handler); // POST
|
||||
svr.Put("/path", handler); // PUT
|
||||
svr.Delete("/path", handler); // DELETE
|
||||
```
|
||||
|
||||
ハンドラーのシグネチャは `(const httplib::Request &req, httplib::Response &res)` です。`auto` を使って短く書くこともできます。
|
||||
|
||||
```cpp
|
||||
svr.Get("/hi", [](const auto &req, auto &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
パスが一致したときだけハンドラーが呼ばれます。登録されていないパスにアクセスすると、自動的に404が返ります。
|
||||
|
||||
## リクエストオブジェクト
|
||||
|
||||
ハンドラーの第1引数 `req` から、クライアントが送ってきた情報を読み取れます。
|
||||
|
||||
### ボディ
|
||||
|
||||
`req.body` でリクエストボディを取得できます。型は `std::string` です。
|
||||
|
||||
```cpp
|
||||
svr.Post("/post", [](const auto &req, auto &res) {
|
||||
// クライアントが送ったボディをそのまま返す
|
||||
res.set_content(req.body, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### ヘッダー
|
||||
|
||||
`req.get_header_value()` でリクエストヘッダーの値を取得できます。
|
||||
|
||||
```cpp
|
||||
svr.Get("/check", [](const auto &req, auto &res) {
|
||||
auto auth = req.get_header_value("Authorization");
|
||||
res.set_content("Auth: " + auth, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### クエリパラメーターとフォームデータ
|
||||
|
||||
`req.get_param_value()` でパラメーターを取得できます。GETのクエリパラメーターと、POSTのフォームデータの両方に使えます。
|
||||
|
||||
```cpp
|
||||
svr.Get("/search", [](const auto &req, auto &res) {
|
||||
auto q = req.get_param_value("q");
|
||||
res.set_content("Query: " + q, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`/search?q=cpp-httplib` にアクセスすると、`q` の値は `"cpp-httplib"` になります。
|
||||
|
||||
すべてのパラメーターをループで処理したいときは、`req.params` を使います。
|
||||
|
||||
```cpp
|
||||
svr.Post("/submit", [](const auto &req, auto &res) {
|
||||
std::string result;
|
||||
for (auto &[key, val] : req.params) {
|
||||
result += key + " = " + val + "\n";
|
||||
}
|
||||
res.set_content(result, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### ファイルアップロード
|
||||
|
||||
マルチパートフォームでアップロードされたファイルは、`req.form.get_file()` で取得します。
|
||||
|
||||
```cpp
|
||||
svr.Post("/upload", [](const auto &req, auto &res) {
|
||||
auto f = req.form.get_file("file");
|
||||
auto content = f.filename + " (" + std::to_string(f.content.size()) + " bytes)";
|
||||
res.set_content(content, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`f.filename` でファイル名、`f.content` でファイルの中身にアクセスできます。
|
||||
|
||||
## パスパラメーター
|
||||
|
||||
URLの一部を変数として受け取りたいことがあります。たとえば `/users/42` の `42` を取得したい場合です。`:param` 記法を使うと、URLの一部をキャプチャできます。
|
||||
|
||||
```cpp
|
||||
svr.Get("/users/:id", [](const auto &req, auto &res) {
|
||||
auto id = req.path_params.at("id");
|
||||
res.set_content("User ID: " + id, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`/users/42` にアクセスすると、`req.path_params.at("id")` は `"42"` を返します。`/users/100` なら `"100"` です。
|
||||
|
||||
複数のパスパラメーターも使えます。
|
||||
|
||||
```cpp
|
||||
svr.Get("/users/:user_id/posts/:post_id", [](const auto &req, auto &res) {
|
||||
auto user_id = req.path_params.at("user_id");
|
||||
auto post_id = req.path_params.at("post_id");
|
||||
res.set_content("User: " + user_id + ", Post: " + post_id, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### 正規表現パターン
|
||||
|
||||
`:param` の代わりに正規表現をパスに書くこともできます。キャプチャグループの値は `req.matches` で取得します。型は `std::smatch` です。
|
||||
|
||||
```cpp
|
||||
// 数字のみのIDを受け付ける
|
||||
svr.Get(R"(/files/(\d+))", [](const auto &req, auto &res) {
|
||||
auto id = req.matches[1]; // 最初のキャプチャグループ
|
||||
res.set_content("File ID: " + std::string(id), "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`/files/42` にはマッチしますが、`/files/abc` にはマッチしません。入力値を絞り込みたいときに便利です。
|
||||
|
||||
## レスポンスの組み立て
|
||||
|
||||
ハンドラーの第2引数 `res` を使って、クライアントに返すレスポンスを組み立てます。
|
||||
|
||||
### ボディとContent-Type
|
||||
|
||||
`res.set_content()` でボディとContent-Typeを設定します。これだけでステータスコード200のレスポンスが返ります。
|
||||
|
||||
```cpp
|
||||
svr.Get("/hi", [](const auto &req, auto &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### ステータスコード
|
||||
|
||||
ステータスコードを変えたいときは、`res.status` に代入します。
|
||||
|
||||
```cpp
|
||||
svr.Get("/not-found", [](const auto &req, auto &res) {
|
||||
res.status = 404;
|
||||
res.set_content("Not found", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
### レスポンスヘッダー
|
||||
|
||||
`res.set_header()` でレスポンスヘッダーを追加できます。
|
||||
|
||||
```cpp
|
||||
svr.Get("/with-header", [](const auto &req, auto &res) {
|
||||
res.set_header("X-Custom", "my-value");
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
## 前章のサーバーを読み解く
|
||||
|
||||
ここまでの知識を使って、前章で用意したテスト用サーバーを改めて見てみましょう。
|
||||
|
||||
### GET /hi
|
||||
|
||||
```cpp
|
||||
svr.Get("/hi", [](const auto &, auto &res) {
|
||||
res.set_content("Hello!", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
最もシンプルなハンドラーです。リクエストの情報は使わないので、`req` の変数名を省略しています。`"Hello!"` というテキストをそのまま返します。
|
||||
|
||||
### GET /search
|
||||
|
||||
```cpp
|
||||
svr.Get("/search", [](const auto &req, auto &res) {
|
||||
auto q = req.get_param_value("q");
|
||||
res.set_content("Query: " + q, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`req.get_param_value("q")` でクエリパラメーター `q` の値を取り出します。`/search?q=cpp-httplib` なら、レスポンスは `"Query: cpp-httplib"` になります。
|
||||
|
||||
### POST /post
|
||||
|
||||
```cpp
|
||||
svr.Post("/post", [](const auto &req, auto &res) {
|
||||
res.set_content(req.body, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
クライアントが送ったリクエストボディを、そのままレスポンスとして返すエコーサーバーです。`req.body` にボディが丸ごと入っています。
|
||||
|
||||
### POST /submit
|
||||
|
||||
```cpp
|
||||
svr.Post("/submit", [](const auto &req, auto &res) {
|
||||
std::string result;
|
||||
for (auto &[key, val] : req.params) {
|
||||
result += key + " = " + val + "\n";
|
||||
}
|
||||
res.set_content(result, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
フォームデータとして送られたキーと値のペアを、`req.params` でループ処理しています。構造化束縛 `auto &[key, val]` を使って、各ペアを取り出しています。
|
||||
|
||||
### POST /upload
|
||||
|
||||
```cpp
|
||||
svr.Post("/upload", [](const auto &req, auto &res) {
|
||||
auto f = req.form.get_file("file");
|
||||
auto content = f.filename + " (" + std::to_string(f.content.size()) + " bytes)";
|
||||
res.set_content(content, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
マルチパートフォームで送られたファイルを受け取ります。`req.form.get_file("file")` で `"file"` という名前のフィールドを取得し、`f.filename` と `f.content.size()` でファイル名とサイズを返しています。
|
||||
|
||||
### GET /users/:id
|
||||
|
||||
```cpp
|
||||
svr.Get("/users/:id", [](const auto &req, auto &res) {
|
||||
auto id = req.path_params.at("id");
|
||||
res.set_content("User ID: " + id, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
`:id` の部分がパスパラメーターです。`req.path_params.at("id")` で値を取り出しています。`/users/42` なら `"42"`、`/users/alice` なら `"alice"` が得られます。
|
||||
|
||||
### GET /files/(\d+)
|
||||
|
||||
```cpp
|
||||
svr.Get(R"(/files/(\d+))", [](const auto &req, auto &res) {
|
||||
auto id = req.matches[1];
|
||||
res.set_content("File ID: " + std::string(id), "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
正規表現 `(\d+)` で数字だけのIDにマッチします。`/files/42` にはマッチしますが、`/files/abc` は404になります。`req.matches[1]` で最初のキャプチャグループの値を取得しています。
|
||||
|
||||
## 次のステップ
|
||||
|
||||
サーバーの基本がわかりましたね。ルーティング、リクエストの読み取り、レスポンスの組み立て。これだけで、十分に実用的なAPIサーバーが作れます。
|
||||
|
||||
次は、静的ファイルの配信を見てみましょう。HTMLやCSSを配信するサーバーを作ります。
|
||||
|
||||
**次:** [Static File Server](../04-static-file-server)
|
||||
134
docs-src/pages/ja/tour/04-static-file-server.md
Normal file
134
docs-src/pages/ja/tour/04-static-file-server.md
Normal file
@@ -0,0 +1,134 @@
|
||||
---
|
||||
title: "Static File Server"
|
||||
order: 4
|
||||
---
|
||||
|
||||
cpp-httplibは、HTMLやCSS、画像ファイルなどの静的ファイルも配信できます。面倒な設定は要りません。`set_mount_point()` を1行呼ぶだけです。
|
||||
|
||||
## set_mount_point の基本
|
||||
|
||||
さっそくやってみましょう。`set_mount_point()` は、URLのパスとローカルディレクトリを紐づけます。
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Server svr;
|
||||
|
||||
svr.set_mount_point("/", "./html");
|
||||
|
||||
std::cout << "Listening on port 8080..." << std::endl;
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
第1引数がURLのマウントポイント、第2引数がローカルのディレクトリパスです。この例だと、`/` へのリクエストを `./html` ディレクトリから配信します。
|
||||
|
||||
試してみましょう。まず `html` ディレクトリを作って、`index.html` を置きます。
|
||||
|
||||
```sh
|
||||
mkdir html
|
||||
```
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head><title>My Page</title></head>
|
||||
<body>
|
||||
<h1>Hello from cpp-httplib!</h1>
|
||||
<p>This is a static file.</p>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
コンパイルして起動します。
|
||||
|
||||
```sh
|
||||
g++ -std=c++17 -o server server.cpp -pthread
|
||||
./server
|
||||
```
|
||||
|
||||
ブラウザで `http://localhost:8080` を開いてみてください。`html/index.html` の内容が表示されるはずです。`http://localhost:8080/index.html` でも同じページが返ります。
|
||||
|
||||
もちろん、前章のクライアントコードや `curl` でもアクセスできますよ。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
auto res = cli.Get("/");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // HTMLが表示される
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080
|
||||
```
|
||||
|
||||
## 複数のマウントポイント
|
||||
|
||||
`set_mount_point()` は何回でも呼べます。URLのパスごとに、別々のディレクトリを割り当てられます。
|
||||
|
||||
```cpp
|
||||
svr.set_mount_point("/", "./public");
|
||||
svr.set_mount_point("/assets", "./static/assets");
|
||||
svr.set_mount_point("/docs", "./documentation");
|
||||
```
|
||||
|
||||
`/assets/style.css` なら `./static/assets/style.css` を、`/docs/guide.html` なら `./documentation/guide.html` を配信します。
|
||||
|
||||
## ハンドラーとの組み合わせ
|
||||
|
||||
静的ファイルの配信と、前章で学んだルーティングハンドラーは共存できます。
|
||||
|
||||
```cpp
|
||||
httplib::Server svr;
|
||||
|
||||
// APIエンドポイント
|
||||
svr.Get("/api/hello", [](const auto &, auto &res) {
|
||||
res.set_content(R"({"message":"Hello!"})", "application/json");
|
||||
});
|
||||
|
||||
// 静的ファイル配信
|
||||
svr.set_mount_point("/", "./public");
|
||||
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
```
|
||||
|
||||
ハンドラーが先に評価されます。`/api/hello` にはハンドラーが応答し、それ以外のパスは `./public` ディレクトリからファイルを探します。
|
||||
|
||||
## レスポンスヘッダーの追加
|
||||
|
||||
`set_mount_point()` の第3引数にヘッダーを渡すと、静的ファイルのレスポンスにカスタムヘッダーを付けられます。キャッシュ制御に便利です。
|
||||
|
||||
```cpp
|
||||
svr.set_mount_point("/", "./public", {
|
||||
{"Cache-Control", "max-age=3600"}
|
||||
});
|
||||
```
|
||||
|
||||
こうすると、ブラウザは配信されたファイルを1時間キャッシュします。
|
||||
|
||||
## 静的ファイルサーバー用のDockerファイル
|
||||
|
||||
cpp-httplibのリポジトリには、静的ファイルサーバー用の `Dockerfile` が含まれています。Docker Hubにビルド済みイメージも公開しているので、1コマンドで起動できます。
|
||||
|
||||
```sh
|
||||
> docker run -p 8080:80 -v ./my-site:/html yhirose4dockerhub/cpp-httplib-server
|
||||
Serving HTTP on 0.0.0.0:80
|
||||
Mount point: / -> ./html
|
||||
Press Ctrl+C to shutdown gracefully...
|
||||
192.168.65.1 - - [22/Feb/2026:12:00:00 +0000] "GET / HTTP/1.1" 200 256 "-" "Mozilla/5.0 ..."
|
||||
192.168.65.1 - - [22/Feb/2026:12:00:00 +0000] "GET /style.css HTTP/1.1" 200 1024 "-" "Mozilla/5.0 ..."
|
||||
192.168.65.1 - - [22/Feb/2026:12:00:01 +0000] "GET /favicon.ico HTTP/1.1" 404 152 "-" "Mozilla/5.0 ..."
|
||||
```
|
||||
|
||||
`./my-site` ディレクトリの中身が、そのままポート8080で配信されます。NGINXと同じログ形式で、アクセスの様子を確認できますよ。
|
||||
|
||||
## 次のステップ
|
||||
|
||||
静的ファイルの配信ができるようになりましたね。HTMLやCSS、JavaScriptを配信するWebサーバーが、これだけのコードで作れます。
|
||||
|
||||
次は、HTTPSで暗号化通信をしてみましょう。まずはTLSライブラリのセットアップからです。
|
||||
|
||||
**次:** [TLS Setup](../05-tls-setup)
|
||||
88
docs-src/pages/ja/tour/05-tls-setup.md
Normal file
88
docs-src/pages/ja/tour/05-tls-setup.md
Normal file
@@ -0,0 +1,88 @@
|
||||
---
|
||||
title: "TLS Setup"
|
||||
order: 5
|
||||
---
|
||||
|
||||
ここまではHTTP(平文)でやってきましたが、実際のWebではHTTPS(暗号化通信)が当たり前ですよね。cpp-httplibでHTTPSを使うには、TLSライブラリが必要です。
|
||||
|
||||
このTourではOpenSSLを使います。最も広く使われていて、情報も豊富です。
|
||||
|
||||
## OpenSSLのインストール
|
||||
|
||||
お使いのOSに合わせてインストールしましょう。
|
||||
|
||||
| OS | インストール方法 |
|
||||
| -- | ---------------- |
|
||||
| macOS | [Homebrew](https://brew.sh/) (`brew install openssl`) |
|
||||
| Ubuntu / Debian | `sudo apt install libssl-dev` |
|
||||
| Windows | [vcpkg](https://vcpkg.io/) (`vcpkg install openssl`) |
|
||||
|
||||
## コンパイルオプション
|
||||
|
||||
TLS機能を有効にするには、`CPPHTTPLIB_OPENSSL_SUPPORT` マクロを定義してコンパイルします。前章までのコンパイルコマンドに、いくつかオプションが増えます。
|
||||
|
||||
```sh
|
||||
# macOS (Homebrew)
|
||||
clang++ -std=c++17 -DCPPHTTPLIB_OPENSSL_SUPPORT \
|
||||
-I$(brew --prefix openssl)/include \
|
||||
-L$(brew --prefix openssl)/lib \
|
||||
-lssl -lcrypto \
|
||||
-framework CoreFoundation -framework Security \
|
||||
-o server server.cpp
|
||||
|
||||
# Linux
|
||||
clang++ -std=c++17 -pthread -DCPPHTTPLIB_OPENSSL_SUPPORT \
|
||||
-lssl -lcrypto \
|
||||
-o server server.cpp
|
||||
|
||||
# Windows (Developer Command Prompt)
|
||||
cl /EHsc /std:c++17 /DCPPHTTPLIB_OPENSSL_SUPPORT server.cpp libssl.lib libcrypto.lib
|
||||
```
|
||||
|
||||
それぞれのオプションの役割を見てみましょう。
|
||||
|
||||
- **`-DCPPHTTPLIB_OPENSSL_SUPPORT`** — TLS機能を有効にするマクロ定義
|
||||
- **`-lssl -lcrypto`** — OpenSSLのライブラリをリンク
|
||||
- **`-I` / `-L`**(macOSのみ)— Homebrew版OpenSSLのパスを指定
|
||||
- **`-framework CoreFoundation -framework Security`**(macOSのみ)— Keychainからシステム証明書を自動で読み込むために必要です
|
||||
|
||||
## 動作確認
|
||||
|
||||
ちゃんと動くか確認してみましょう。`httplib::Client` にHTTPSのURLを渡してアクセスするだけのプログラムです。
|
||||
|
||||
```cpp
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("https://www.google.com");
|
||||
|
||||
auto res = cli.Get("/");
|
||||
if (res) {
|
||||
std::cout << "Status: " << res->status << std::endl;
|
||||
} else {
|
||||
std::cout << "Error: " << httplib::to_string(res.error()) << std::endl;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
コンパイルして実行してみてください。`Status: 200` と表示されれば、セットアップ完了です。
|
||||
|
||||
## 他のTLSバックエンド
|
||||
|
||||
cpp-httplibはOpenSSL以外にも、Mbed TLSとwolfSSLに対応しています。マクロ定義とリンクするライブラリを変えるだけで切り替えられます。
|
||||
|
||||
| バックエンド | マクロ定義 | リンクするライブラリ |
|
||||
| :--- | :--- | :--- |
|
||||
| OpenSSL | `CPPHTTPLIB_OPENSSL_SUPPORT` | `libssl`, `libcrypto` |
|
||||
| Mbed TLS | `CPPHTTPLIB_MBEDTLS_SUPPORT` | `libmbedtls`, `libmbedx509`, `libmbedcrypto` |
|
||||
| wolfSSL | `CPPHTTPLIB_WOLFSSL_SUPPORT` | `libwolfssl` |
|
||||
|
||||
このTourではOpenSSLを前提に進めますが、APIはどのバックエンドでも共通です。
|
||||
|
||||
## 次のステップ
|
||||
|
||||
TLSの準備ができましたね。次は、HTTPSサイトにリクエストを送ってみましょう。
|
||||
|
||||
**次:** [HTTPS Client](../06-https-client)
|
||||
122
docs-src/pages/ja/tour/06-https-client.md
Normal file
122
docs-src/pages/ja/tour/06-https-client.md
Normal file
@@ -0,0 +1,122 @@
|
||||
---
|
||||
title: "HTTPS Client"
|
||||
order: 6
|
||||
---
|
||||
|
||||
前章でOpenSSLのセットアップが済んだので、さっそくHTTPSクライアントを使ってみましょう。2章で使った `httplib::Client` がそのまま使えます。コンストラクタに `https://` 付きのURLを渡すだけです。
|
||||
|
||||
## GETリクエスト
|
||||
|
||||
実在するHTTPSサイトにアクセスしてみましょう。
|
||||
|
||||
```cpp
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("https://nghttp2.org");
|
||||
|
||||
auto res = cli.Get("/");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 200
|
||||
std::cout << res->body.substr(0, 100) << std::endl; // HTMLの先頭部分
|
||||
} else {
|
||||
std::cout << "Error: " << httplib::to_string(res.error()) << std::endl;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2章では `httplib::Client cli("http://localhost:8080")` と書きましたよね。スキームを `https://` に変えるだけです。`Get()` や `Post()` など、2章で学んだAPIはすべてそのまま使えます。
|
||||
|
||||
```sh
|
||||
curl https://nghttp2.org/
|
||||
```
|
||||
|
||||
## ポートの指定
|
||||
|
||||
HTTPSのデフォルトポートは443です。別のポートを使いたい場合は、URLにポートを含めます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://localhost:8443");
|
||||
```
|
||||
|
||||
## CA証明書の検証
|
||||
|
||||
`httplib::Client` はHTTPS接続時、デフォルトでサーバー証明書を検証します。信頼できるCA(認証局)が発行した証明書を持つサーバーにしか接続しません。
|
||||
|
||||
CA証明書は、macOSならKeychain、LinuxならシステムのCA証明書ストア、WindowsならWindowsの証明書ストアから自動で読み込みます。ほとんどの場合、追加の設定は要りません。
|
||||
|
||||
### CA証明書ファイルの指定
|
||||
|
||||
環境によってはシステムのCA証明書が見つからないこともあります。そのときは `set_ca_cert_path()` でパスを直接指定してください。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://nghttp2.org");
|
||||
cli.set_ca_cert_path("/etc/ssl/certs/ca-certificates.crt");
|
||||
|
||||
auto res = cli.Get("/");
|
||||
```
|
||||
|
||||
```sh
|
||||
curl --cacert /etc/ssl/certs/ca-certificates.crt https://nghttp2.org/
|
||||
```
|
||||
|
||||
### 証明書検証の無効化
|
||||
|
||||
開発中、自己署名証明書のサーバーに接続したいときは、検証を無効にできます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://localhost:8443");
|
||||
cli.enable_server_certificate_verification(false);
|
||||
|
||||
auto res = cli.Get("/");
|
||||
```
|
||||
|
||||
```sh
|
||||
curl -k https://localhost:8443/
|
||||
```
|
||||
|
||||
本番では絶対に無効にしないでください。中間者攻撃のリスクがあります。
|
||||
|
||||
## リダイレクトの追跡
|
||||
|
||||
HTTPSサイトへのアクセスでは、リダイレクトに遭遇することがよくあります。たとえば `http://` から `https://` へ、あるいは `www` なしから `www` ありへ転送されるケースです。
|
||||
|
||||
デフォルトではリダイレクトを追跡しません。リダイレクト先は `Location` ヘッダーで確認できます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://nghttp2.org");
|
||||
|
||||
auto res = cli.Get("/httpbin/redirect/3");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 302
|
||||
std::cout << res->get_header_value("Location") << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl https://nghttp2.org/httpbin/redirect/3
|
||||
```
|
||||
|
||||
`set_follow_location(true)` を設定すると、リダイレクトを自動で追跡して、最終的なレスポンスを返してくれます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://nghttp2.org");
|
||||
cli.set_follow_location(true);
|
||||
|
||||
auto res = cli.Get("/httpbin/redirect/3");
|
||||
if (res) {
|
||||
std::cout << res->status << std::endl; // 200(最終的なレスポンス)
|
||||
}
|
||||
```
|
||||
|
||||
```sh
|
||||
curl -L https://nghttp2.org/httpbin/redirect/3
|
||||
```
|
||||
|
||||
## 次のステップ
|
||||
|
||||
HTTPSクライアントの使い方がわかりましたね。次は自分でHTTPSサーバーを立ててみましょう。自己署名証明書の作り方から始めます。
|
||||
|
||||
**次:** [HTTPS Server](../07-https-server)
|
||||
124
docs-src/pages/ja/tour/07-https-server.md
Normal file
124
docs-src/pages/ja/tour/07-https-server.md
Normal file
@@ -0,0 +1,124 @@
|
||||
---
|
||||
title: "HTTPS Server"
|
||||
order: 7
|
||||
---
|
||||
|
||||
前章ではHTTPSクライアントを使いました。今度は自分でHTTPSサーバーを立ててみましょう。3章の `httplib::Server` を `httplib::SSLServer` に置き換えるだけです。
|
||||
|
||||
ただし、TLSサーバーにはサーバー証明書と秘密鍵が必要です。まずはそこから準備しましょう。
|
||||
|
||||
## 自己署名証明書の作成
|
||||
|
||||
開発やテスト用なら、自己署名証明書(いわゆるオレオレ証明書)で十分です。OpenSSLのコマンドでサクッと作れます。
|
||||
|
||||
```sh
|
||||
openssl req -x509 -noenc -keyout key.pem -out cert.pem -subj /CN=localhost
|
||||
```
|
||||
|
||||
これで2つのファイルができます。
|
||||
|
||||
- **`cert.pem`** — サーバー証明書
|
||||
- **`key.pem`** — 秘密鍵
|
||||
|
||||
## 最小のHTTPSサーバー
|
||||
|
||||
証明書ができたら、さっそくサーバーを書いてみましょう。
|
||||
|
||||
```cpp
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::SSLServer svr("cert.pem", "key.pem");
|
||||
|
||||
svr.Get("/", [](const auto &, auto &res) {
|
||||
res.set_content("Hello, HTTPS!", "text/plain");
|
||||
});
|
||||
|
||||
std::cout << "Listening on https://localhost:8443" << std::endl;
|
||||
svr.listen("0.0.0.0", 8443);
|
||||
}
|
||||
```
|
||||
|
||||
`httplib::SSLServer` のコンストラクタに証明書と秘密鍵のパスを渡すだけです。ルーティングの書き方は3章の `httplib::Server` とまったく同じですよ。
|
||||
|
||||
コンパイルして起動しましょう。
|
||||
|
||||
## 動作確認
|
||||
|
||||
サーバーが起動したら、`curl` でアクセスしてみましょう。自己署名証明書なので、`-k` オプションで証明書検証をスキップします。
|
||||
|
||||
```sh
|
||||
curl -k https://localhost:8443/
|
||||
# Hello, HTTPS!
|
||||
```
|
||||
|
||||
ブラウザで `https://localhost:8443` を開くと、「この接続は安全ではありません」と警告が出ます。自己署名証明書なので正常です。気にせず進めてください。
|
||||
|
||||
## クライアントからの接続
|
||||
|
||||
前章の `httplib::Client` で接続してみましょう。自己署名証明書のサーバーに接続するには、2つの方法があります。
|
||||
|
||||
### 方法1: 証明書検証を無効にする
|
||||
|
||||
開発時の手軽な方法です。
|
||||
|
||||
```cpp
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("https://localhost:8443");
|
||||
cli.enable_server_certificate_verification(false);
|
||||
|
||||
auto res = cli.Get("/");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // Hello, HTTPS!
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 方法2: 自己署名証明書をCA証明書として指定する
|
||||
|
||||
こちらのほうが安全です。`cert.pem` をCA証明書として信頼するよう指定します。
|
||||
|
||||
```cpp
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Client cli("https://localhost:8443");
|
||||
cli.set_ca_cert_path("cert.pem");
|
||||
|
||||
auto res = cli.Get("/");
|
||||
if (res) {
|
||||
std::cout << res->body << std::endl; // Hello, HTTPS!
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
この方法なら、指定した証明書のサーバーにだけ接続を許可して、なりすましを防げます。テスト環境でもなるべくこちらを使いましょう。
|
||||
|
||||
## Server と SSLServer の比較
|
||||
|
||||
3章で学んだ `httplib::Server` のAPIは、`httplib::SSLServer` でもそのまま使えます。違いはコンストラクタだけです。
|
||||
|
||||
| | `httplib::Server` | `httplib::SSLServer` |
|
||||
| -- | ------------------ | -------------------- |
|
||||
| コンストラクタ | 引数なし | 証明書と秘密鍵のパス |
|
||||
| プロトコル | HTTP | HTTPS |
|
||||
| ポート(慣例) | 8080 | 8443 |
|
||||
| ルーティング | 共通 | 共通 |
|
||||
|
||||
HTTPサーバーをHTTPSに切り替えるには、コンストラクタを変えるだけです。
|
||||
|
||||
## 次のステップ
|
||||
|
||||
HTTPSサーバーが動きましたね。これでHTTP/HTTPSのクライアントとサーバー、両方の基本がそろいました。
|
||||
|
||||
次は、cpp-httplibに新しく加わったWebSocket機能を見てみましょう。
|
||||
|
||||
**次:** [WebSocket](../08-websocket)
|
||||
139
docs-src/pages/ja/tour/08-websocket.md
Normal file
139
docs-src/pages/ja/tour/08-websocket.md
Normal file
@@ -0,0 +1,139 @@
|
||||
---
|
||||
title: "WebSocket"
|
||||
order: 8
|
||||
---
|
||||
|
||||
cpp-httplibはWebSocketにも対応しています。HTTPのリクエスト/レスポンスと違い、WebSocketはサーバーとクライアントが双方向にメッセージをやり取りできます。チャットやリアルタイム通知に便利です。
|
||||
|
||||
さっそく、エコーサーバーとクライアントを作ってみましょう。
|
||||
|
||||
## エコーサーバー
|
||||
|
||||
受け取ったメッセージをそのまま返すエコーサーバーです。
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::Server svr;
|
||||
|
||||
svr.WebSocket("/ws", [](const httplib::Request &, httplib::ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
ws.send(msg); // 受け取ったメッセージをそのまま返す
|
||||
}
|
||||
});
|
||||
|
||||
std::cout << "Listening on port 8080..." << std::endl;
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
```
|
||||
|
||||
`svr.WebSocket()` でWebSocketハンドラーを登録します。3章の `svr.Get()` や `svr.Post()` と同じ感覚ですね。
|
||||
|
||||
ハンドラーの中では、`ws.read(msg)` でメッセージを待ちます。接続が閉じられると `read()` が `false` を返すので、ループを抜けます。`ws.send(msg)` でメッセージを送り返します。
|
||||
|
||||
## クライアントからの接続
|
||||
|
||||
`httplib::ws::WebSocketClient` を使ってサーバーに接続してみましょう。
|
||||
|
||||
```cpp
|
||||
#include "httplib.h"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
httplib::ws::WebSocketClient client("ws://localhost:8080/ws");
|
||||
|
||||
if (!client.connect()) {
|
||||
std::cout << "Connection failed" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// メッセージを送信
|
||||
client.send("Hello, WebSocket!");
|
||||
|
||||
// サーバーからの応答を受信
|
||||
std::string msg;
|
||||
if (client.read(msg)) {
|
||||
std::cout << msg << std::endl; // Hello, WebSocket!
|
||||
}
|
||||
|
||||
client.close();
|
||||
}
|
||||
```
|
||||
|
||||
コンストラクタには `ws://host:port/path` 形式のURLを渡します。`connect()` で接続を開始し、`send()` と `read()` でメッセージをやり取りします。
|
||||
|
||||
## テキストとバイナリ
|
||||
|
||||
WebSocketにはテキストとバイナリの2種類のメッセージがあります。`read()` の戻り値で区別できます。
|
||||
|
||||
```cpp
|
||||
svr.WebSocket("/ws", [](const httplib::Request &, httplib::ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
httplib::ws::ReadResult ret;
|
||||
while ((ret = ws.read(msg))) {
|
||||
if (ret == httplib::ws::Binary) {
|
||||
ws.send(msg.data(), msg.size()); // バイナリとして送信
|
||||
} else {
|
||||
ws.send(msg); // テキストとして送信
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
- `ws.send(const std::string &)` — テキストメッセージとして送信
|
||||
- `ws.send(const char *, size_t)` — バイナリメッセージとして送信
|
||||
|
||||
クライアント側も同じAPIです。
|
||||
|
||||
## リクエスト情報へのアクセス
|
||||
|
||||
ハンドラーの第1引数 `req` から、ハンドシェイク時のHTTPリクエスト情報を読み取れます。認証トークンの確認などに便利です。
|
||||
|
||||
```cpp
|
||||
svr.WebSocket("/ws", [](const httplib::Request &req, httplib::ws::WebSocket &ws) {
|
||||
auto token = req.get_header_value("Authorization");
|
||||
if (token.empty()) {
|
||||
ws.close(httplib::ws::CloseStatus::PolicyViolation, "unauthorized");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
ws.send(msg);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
## WSSで使う
|
||||
|
||||
HTTPS上のWebSocket(WSS)にも対応しています。サーバー側は `httplib::SSLServer` にWebSocketハンドラーを登録するだけです。
|
||||
|
||||
```cpp
|
||||
httplib::SSLServer svr("cert.pem", "key.pem");
|
||||
|
||||
svr.WebSocket("/ws", [](const httplib::Request &, httplib::ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
ws.send(msg);
|
||||
}
|
||||
});
|
||||
|
||||
svr.listen("0.0.0.0", 8443);
|
||||
```
|
||||
|
||||
クライアント側は `wss://` スキームを使います。
|
||||
|
||||
```cpp
|
||||
httplib::ws::WebSocketClient client("wss://localhost:8443/ws");
|
||||
```
|
||||
|
||||
## 次のステップ
|
||||
|
||||
WebSocketの基本がわかりましたね。ここまでで Tourは終わりです。
|
||||
|
||||
次のページでは、Tourで取り上げなかった機能をまとめて紹介します。
|
||||
|
||||
**次:** [What's Next](../09-whats-next)
|
||||
228
docs-src/pages/ja/tour/09-whats-next.md
Normal file
228
docs-src/pages/ja/tour/09-whats-next.md
Normal file
@@ -0,0 +1,228 @@
|
||||
---
|
||||
title: "What's Next"
|
||||
order: 9
|
||||
---
|
||||
|
||||
Tourお疲れさまでした! cpp-httplibの基本はひと通り押さえましたね。でも、まだまだ便利な機能があります。Tourで取り上げなかった機能をカテゴリー別に紹介します。
|
||||
|
||||
## Streaming API
|
||||
|
||||
LLMのストリーミング応答や大きなファイルのダウンロードでは、レスポンス全体をメモリに載せたくないですよね。`stream::Get()` を使えば、データをチャンクごとに処理できます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:11434");
|
||||
|
||||
auto result = httplib::stream::Get(cli, "/api/generate");
|
||||
|
||||
if (result) {
|
||||
while (result.next()) {
|
||||
std::cout.write(result.data(), result.size());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`Get()` に `content_receiver` コールバックを渡す方法もあります。こちらはKeep-Aliveと併用できます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
|
||||
cli.Get("/stream", [](const char *data, size_t len) {
|
||||
std::cout.write(data, len);
|
||||
return true;
|
||||
});
|
||||
```
|
||||
|
||||
サーバー側には `set_content_provider()` と `set_chunked_content_provider()` があります。サイズがわかっているなら前者、不明なら後者を使ってください。
|
||||
|
||||
```cpp
|
||||
// サイズ指定あり(Content-Length が設定される)
|
||||
svr.Get("/file", [](const auto &, auto &res) {
|
||||
auto size = get_file_size("large.bin");
|
||||
res.set_content_provider(size, "application/octet-stream",
|
||||
[](size_t offset, size_t length, httplib::DataSink &sink) {
|
||||
// offset から length バイト分を送る
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
// サイズ不明(Chunked Transfer Encoding)
|
||||
svr.Get("/stream", [](const auto &, auto &res) {
|
||||
res.set_chunked_content_provider("text/plain",
|
||||
[](size_t offset, httplib::DataSink &sink) {
|
||||
sink.write("chunk\n", 6);
|
||||
return true; // falseを返すと終了
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
大きなファイルのアップロードには `make_file_provider()` が便利です。ファイルを全部メモリに読み込まず、ストリーミングで送れます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
|
||||
auto res = cli.Post("/upload", {}, {
|
||||
httplib::make_file_provider("file", "/path/to/large-file.zip")
|
||||
});
|
||||
```
|
||||
|
||||
## Server-Sent Events (SSE)
|
||||
|
||||
SSEクライアントも用意しています。自動再接続や `Last-Event-ID` による再開にも対応しています。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("http://localhost:8080");
|
||||
httplib::sse::SSEClient sse(cli, "/events");
|
||||
|
||||
sse.on_message([](const httplib::sse::SSEMessage &msg) {
|
||||
std::cout << msg.event << ": " << msg.data << std::endl;
|
||||
});
|
||||
|
||||
sse.start(); // ブロッキング、自動再接続あり
|
||||
```
|
||||
|
||||
イベントタイプごとにハンドラーを分けることもできますよ。
|
||||
|
||||
```cpp
|
||||
sse.on_event("update", [](const httplib::sse::SSEMessage &msg) {
|
||||
// "update" イベントだけ処理
|
||||
});
|
||||
```
|
||||
|
||||
## 認証
|
||||
|
||||
クライアントにはBasic認証、Bearer Token認証、Digest認証のヘルパーを用意しています。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://api.example.com");
|
||||
cli.set_basic_auth("user", "password");
|
||||
cli.set_bearer_token_auth("my-token");
|
||||
```
|
||||
|
||||
## 圧縮
|
||||
|
||||
gzip、Brotli、Zstandardによる圧縮・展開に対応しています。使いたい方式のマクロを定義してコンパイルしましょう。
|
||||
|
||||
| 圧縮方式 | マクロ定義 |
|
||||
| -- | -- |
|
||||
| gzip | `CPPHTTPLIB_ZLIB_SUPPORT` |
|
||||
| Brotli | `CPPHTTPLIB_BROTLI_SUPPORT` |
|
||||
| Zstandard | `CPPHTTPLIB_ZSTD_SUPPORT` |
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://example.com");
|
||||
cli.set_compress(true); // リクエストボディを圧縮
|
||||
cli.set_decompress(true); // レスポンスボディを展開
|
||||
```
|
||||
|
||||
## プロキシ
|
||||
|
||||
HTTPプロキシ経由で接続できます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://example.com");
|
||||
cli.set_proxy("proxy.example.com", 8080);
|
||||
cli.set_proxy_basic_auth("user", "password");
|
||||
```
|
||||
|
||||
## タイムアウト
|
||||
|
||||
接続・読み取り・書き込みのタイムアウトを個別に設定できます。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://example.com");
|
||||
cli.set_connection_timeout(5, 0); // 5秒
|
||||
cli.set_read_timeout(10, 0); // 10秒
|
||||
cli.set_write_timeout(10, 0); // 10秒
|
||||
```
|
||||
|
||||
## Keep-Alive
|
||||
|
||||
同じサーバーに何度もリクエストするなら、Keep-Aliveを有効にしましょう。TCP接続を再利用するので効率的です。
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://example.com");
|
||||
cli.set_keep_alive(true);
|
||||
```
|
||||
|
||||
## サーバーのミドルウェア
|
||||
|
||||
リクエスト処理の前後にフックを挟めます。
|
||||
|
||||
```cpp
|
||||
svr.set_pre_routing_handler([](const auto &req, auto &res) {
|
||||
// すべてのリクエストの前に実行される
|
||||
return httplib::Server::HandlerResponse::Unhandled; // 通常のルーティングに進む
|
||||
});
|
||||
|
||||
svr.set_post_routing_handler([](const auto &req, auto &res) {
|
||||
// レスポンスが返された後に実行される
|
||||
res.set_header("X-Server", "cpp-httplib");
|
||||
});
|
||||
```
|
||||
|
||||
`req.user_data` を使うと、ミドルウェアからハンドラーにデータを渡せます。認証トークンのデコード結果を共有するときに便利です。
|
||||
|
||||
```cpp
|
||||
svr.set_pre_routing_handler([](const auto &req, auto &res) {
|
||||
req.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"));
|
||||
res.set_content("Hello, " + user, "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
エラーや例外のハンドラーもカスタマイズできますよ。
|
||||
|
||||
```cpp
|
||||
svr.set_error_handler([](const auto &req, auto &res) {
|
||||
res.set_content("Custom Error Page", "text/html");
|
||||
});
|
||||
|
||||
svr.set_exception_handler([](const auto &req, auto &res, std::exception_ptr ep) {
|
||||
res.status = 500;
|
||||
res.set_content("Internal Server Error", "text/plain");
|
||||
});
|
||||
```
|
||||
|
||||
## ロギング
|
||||
|
||||
サーバーでもクライアントでもロガーを設定できます。
|
||||
|
||||
```cpp
|
||||
svr.set_logger([](const auto &req, const auto &res) {
|
||||
std::cout << req.method << " " << req.path << " " << res.status << std::endl;
|
||||
});
|
||||
```
|
||||
|
||||
## Unix Domain Socket
|
||||
|
||||
TCP以外に、Unix Domain Socketでの通信にも対応しています。同じマシン上のプロセス間通信に使えます。
|
||||
|
||||
```cpp
|
||||
// サーバー
|
||||
httplib::Server svr;
|
||||
svr.set_address_family(AF_UNIX);
|
||||
svr.listen("/tmp/httplib.sock", 0);
|
||||
```
|
||||
|
||||
```cpp
|
||||
// クライアント
|
||||
httplib::Client cli("http://localhost");
|
||||
cli.set_address_family(AF_UNIX);
|
||||
cli.set_hostname_addr_map({{"localhost", "/tmp/httplib.sock"}});
|
||||
|
||||
auto res = cli.Get("/");
|
||||
```
|
||||
|
||||
## さらに詳しく
|
||||
|
||||
もっと詳しく知りたいときは、以下を参照してください。
|
||||
|
||||
- Cookbook — よくあるユースケースのレシピ集
|
||||
- [README](https://github.com/yhirose/cpp-httplib/blob/master/README.md) — 全APIのリファレンス
|
||||
- [README-sse](https://github.com/yhirose/cpp-httplib/blob/master/README-sse.md) — Server-Sent Eventsの使い方
|
||||
- [README-stream](https://github.com/yhirose/cpp-httplib/blob/master/README-stream.md) — Streaming APIの使い方
|
||||
- [README-websocket](https://github.com/yhirose/cpp-httplib/blob/master/README-websocket.md) — WebSocketサーバーの使い方
|
||||
16
docs-src/pages/ja/tour/index.md
Normal file
16
docs-src/pages/ja/tour/index.md
Normal file
@@ -0,0 +1,16 @@
|
||||
---
|
||||
title: "A Tour of cpp-httplib"
|
||||
order: 1
|
||||
---
|
||||
|
||||
cpp-httplibの基本を、順番に学んでいくチュートリアルです。各章は前の章の内容を踏まえて進む構成なので、1章から順に読んでください。
|
||||
|
||||
1. [Getting Started](01-getting-started) — httplib.h の入手とHello Worldサーバー
|
||||
2. [Basic Client](02-basic-client) — GET/POST・パスパラメーターのリクエスト送信
|
||||
3. [Basic Server](03-basic-server) — ルーティング、パスパラメーター、レスポンスの組み立て
|
||||
4. [Static File Server](04-static-file-server) — 静的ファイルの配信
|
||||
5. [TLS Setup](05-tls-setup) — OpenSSL / mbedTLS のセットアップ
|
||||
6. [HTTPS Client](06-https-client) — HTTPSサイトへのリクエスト
|
||||
7. [HTTPS Server](07-https-server) — HTTPSサーバーの構築
|
||||
8. [WebSocket](08-websocket) — WebSocket通信の基本
|
||||
9. [What's Next](09-whats-next) — さらなる機能の紹介
|
||||
Reference in New Issue
Block a user