mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-09-02 14:53:46 +00:00
* Add Server::CustomRoute() for HTTP methods outside the built-in set parse_request_line validates the request method against a fixed whitelist and rejects anything else with 400 before routing runs. That blocks WebDAV, where PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK and UNLOCK are ordinary methods defined by RFC 4918, and it blocks extension methods such as UPnP's SUBSCRIBE. The need has been open since #847. Registering a handler is now what makes the server accept a method: svr.CustomRoute("PROPFIND", "/dav/:id", handler); Because custom methods go through the normal dispatch path, patterns work the way they do for Get() and friends, and the request body is available in req.body. Serving these methods through set_pre_routing_handler was never enough: the body has not been read at that point, so PROPPATCH and LOCK, which require one, could not be implemented at all. A HandlerWithContentReader overload is available too. The content reader gate in routing() also fires when a custom method carries no body, matching what expect_content() does unconditionally for POST/PUT/PATCH/DELETE, so a body-less PROPFIND (RFC 4918 treats one as allprop) reaches its handler instead of falling through to 404. Method names are validated as RFC 9110 tokens, and the ten built-in methods are refused. Seven of them are dispatched by the if/else chain in routing() before the custom tables are consulted, so a route registered for one could never fire; CONNECT, TRACE and PRI carry protocol-level meaning this library does not route. A refused registration makes is_valid() return false, so listen() fails rather than starting a server holding a handler that would never run. This is also why SSLServer::is_valid() now chains to Server::is_valid() instead of only checking ctx_. Servers that never call CustomRoute() keep the previous per-request cost: the built-in method set is checked first and short-circuits, and the custom lookup returns early on an empty map. * Add cookbook recipe for custom HTTP methods The CustomRoute() docs were a section inside S01, which pushed that page to 90 lines, the longest in the cookbook, and mixed a separate feature into a page about registering GET/POST/PUT/DELETE handlers. Move the section into its own recipe and give it room for the part that was missing: the OPTIONS handler returning DAV: and Allow, which WebDAV clients probe for before anything else. S01 goes back to 68 lines and keeps a pointer to the new page. The recipe is titled after the API rather than after WebDAV, and says outright that generating the 207 Multi-Status XML, interpreting Depth and managing locks are the reader's job. Routing the method is all the library does. S23 takes order 42, so the TLS, SSE and WebSocket recipes shift to 43-57. That only moves the sort key. Filenames, the T01/E01/W01 labels, the published URLs and every cross-reference are untouched.
79 lines
2.8 KiB
Markdown
79 lines
2.8 KiB
Markdown
---
|
||
title: "T03. SSL/TLSサーバーを立ち上げる"
|
||
order: 45
|
||
status: "draft"
|
||
---
|
||
|
||
HTTPSサーバーを立ち上げるには、`httplib::Server`の代わりに`httplib::SSLServer`を使います。サーバー証明書と秘密鍵をコンストラクタに渡せば、あとは`Server`とまったく同じように使えます。
|
||
|
||
## 基本の使い方
|
||
|
||
```cpp
|
||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||
#include <httplib.h>
|
||
|
||
int main() {
|
||
httplib::SSLServer svr("cert.pem", "key.pem");
|
||
|
||
svr.Get("/", [](const auto &req, auto &res) {
|
||
res.set_content("hello over TLS", "text/plain");
|
||
});
|
||
|
||
svr.listen("0.0.0.0", 443);
|
||
}
|
||
```
|
||
|
||
コンストラクタにサーバー証明書(PEM形式)と秘密鍵のファイルパスを渡します。これだけでTLS対応のサーバーが立ちます。ハンドラの登録も`listen()`の呼び方も、通常の`Server`と同じです。
|
||
|
||
## 秘密鍵がパスワード保護されている場合
|
||
|
||
第5引数に秘密鍵のパスワードを渡せます。
|
||
|
||
```cpp
|
||
httplib::SSLServer svr("cert.pem", "key.pem",
|
||
nullptr, nullptr, "password");
|
||
```
|
||
|
||
第3、第4引数はクライアント証明書検証用(mTLS、[T04. mTLSを設定する](../t04-mtls)参照)なので、今は`nullptr`を指定します。
|
||
|
||
## メモリ上のPEMから立ち上げる
|
||
|
||
ファイルではなくメモリ上のPEMデータから起動したいときは、`PemMemory`構造体を使います。
|
||
|
||
```cpp
|
||
httplib::SSLServer::PemMemory pem{};
|
||
pem.cert_pem = cert_data.data();
|
||
pem.cert_pem_len = cert_data.size();
|
||
pem.key_pem = key_data.data();
|
||
pem.key_pem_len = key_data.size();
|
||
|
||
httplib::SSLServer svr(pem);
|
||
```
|
||
|
||
環境変数やシークレットマネージャから証明書を取得する場合に便利です。
|
||
|
||
## 証明書の更新
|
||
|
||
証明書の有効期限が切れる前に、サーバーを再起動せずに新しい証明書に差し替えたいことがあります。`update_certs_pem()`が使えます。
|
||
|
||
```cpp
|
||
svr.update_certs_pem(new_cert_pem, new_key_pem);
|
||
```
|
||
|
||
既存の接続はそのまま、これから確立する接続は新しい証明書で動きます。
|
||
|
||
## 証明書の準備
|
||
|
||
テスト用の自己署名証明書は、OpenSSLのコマンドで作れます。
|
||
|
||
```sh
|
||
openssl req -x509 -newkey rsa:2048 -days 365 -nodes \
|
||
-keyout key.pem -out cert.pem -subj "/CN=localhost"
|
||
```
|
||
|
||
本番では、Let's Encryptや社内CAから発行された証明書を使いましょう。
|
||
|
||
> **Warning:** HTTPSサーバーを443番ポートで立ち上げるにはroot権限が必要です。安全に立ち上げる方法は[S18. `listen_after_bind`で起動順序を制御する](../s18-listen-after-bind)の「特権降格」を参照してください。
|
||
|
||
> クライアント証明書による相互認証(mTLS)は[T04. mTLSを設定する](../t04-mtls)を参照してください。
|