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.
102 lines
4.6 KiB
Markdown
102 lines
4.6 KiB
Markdown
---
|
||
title: "Cookbook"
|
||
order: 0
|
||
status: "draft"
|
||
---
|
||
|
||
「〇〇をするには?」という問いに答えるレシピ集です。各レシピは独立しているので、必要なページだけ読めます。基本的な使い方は[Tour](../tour/)で紹介しています。
|
||
|
||
## クライアント
|
||
|
||
### 基本
|
||
- [C01. レスポンスボディを取得する / ファイルに保存する](c01-get-response-body)
|
||
- [C02. JSONを送受信する](c02-json)
|
||
- [C03. デフォルトヘッダーを設定する](c03-default-headers)
|
||
- [C04. リダイレクトを追従する](c04-follow-location)
|
||
|
||
### 認証
|
||
- [C05. Basic認証を使う](c05-basic-auth)
|
||
- [C06. BearerトークンでAPIを呼ぶ](c06-bearer-token)
|
||
|
||
### ファイル送信
|
||
- [C07. ファイルをマルチパートフォームとしてアップロードする](c07-multipart-upload)
|
||
- [C08. ファイルを生バイナリとしてPOSTする](c08-post-file-body)
|
||
- [C09. チャンク転送でボディを送る](c09-chunked-upload)
|
||
|
||
### ストリーミング・進捗
|
||
- [C10. レスポンスをストリーミングで受信する](c10-stream-response)
|
||
- [C11. 進捗コールバックを使う](c11-progress-callback)
|
||
|
||
### 接続・パフォーマンス
|
||
- [C12. タイムアウトを設定する](c12-timeouts)
|
||
- [C13. 全体タイムアウトを設定する](c13-max-timeout)
|
||
- [C14. 接続の再利用とKeep-Aliveの挙動を理解する](c14-keep-alive)
|
||
- [C15. 圧縮を有効にする](c15-compression)
|
||
- [C16. プロキシを経由してリクエストを送る](c16-proxy)
|
||
|
||
### エラー処理・デバッグ
|
||
- [C17. エラーコードをハンドリングする](c17-error-codes)
|
||
- [C18. SSLエラーをハンドリングする](c18-ssl-errors)
|
||
- [C19. クライアントにログを設定する](c19-client-logger)
|
||
|
||
## サーバー
|
||
|
||
### 基本
|
||
- [S01. GET / POST / PUT / DELETEハンドラを登録する](s01-handlers)
|
||
- [S02. JSONリクエストを受け取りJSONレスポンスを返す](s02-json-api)
|
||
- [S03. パスパラメーターを使う](s03-path-params)
|
||
- [S04. 静的ファイルサーバーを設定する](s04-static-files)
|
||
|
||
### ストリーミング・ファイル
|
||
- [S05. 大きなファイルをストリーミングで返す](s05-stream-response)
|
||
- [S06. ファイルダウンロードレスポンスを返す](s06-download-response)
|
||
- [S07. マルチパートデータをストリーミングで受け取る](s07-multipart-reader)
|
||
- [S08. レスポンスを圧縮して返す](s08-compress-response)
|
||
|
||
### ハンドラチェーン
|
||
- [S09. 全ルートに共通の前処理をする](s09-pre-routing)
|
||
- [S10. Post-routing handlerでレスポンスヘッダーを追加する](s10-post-routing)
|
||
- [S11. Pre-request handlerでルート単位の認証を行う](s11-pre-request)
|
||
- [S12. `res.user_data`でハンドラ間データを渡す](s12-user-data)
|
||
|
||
### エラー処理・デバッグ
|
||
- [S13. カスタムエラーページを返す](s13-error-handler)
|
||
- [S14. 例外をキャッチする](s14-exception-handler)
|
||
- [S15. リクエストをログに記録する](s15-server-logger)
|
||
- [S16. クライアントが切断したか検出する](s16-disconnect)
|
||
|
||
### 運用・チューニング
|
||
- [S17. ポートを動的に割り当てる](s17-bind-any-port)
|
||
- [S18. `listen_after_bind`で起動順序を制御する](s18-listen-after-bind)
|
||
- [S19. グレースフルシャットダウンする](s19-graceful-shutdown)
|
||
- [S20. Keep-Aliveを調整する](s20-keep-alive)
|
||
- [S21. マルチスレッド数を設定する](s21-thread-pool)
|
||
- [S22. Unix domain socketで通信する](s22-unix-socket)
|
||
|
||
### プロトコル拡張
|
||
- [S23. カスタムHTTPメソッドを扱う](s23-custom-methods)
|
||
|
||
## TLS / セキュリティ
|
||
|
||
- [T01. OpenSSL・mbedTLS・wolfSSLの選択指針](t01-tls-backends)
|
||
- [T02. SSL証明書の検証を制御する](t02-cert-verification)
|
||
- [T03. SSL/TLSサーバーを立ち上げる](t03-ssl-server)
|
||
- [T04. mTLSを設定する](t04-mtls)
|
||
- [T05. サーバー側でピア証明書を参照する](t05-peer-cert)
|
||
|
||
## SSE
|
||
|
||
- [E01. SSEサーバーを実装する](e01-sse-server)
|
||
- [E02. SSEでイベント名を使い分ける](e02-sse-event-names)
|
||
- [E03. SSEの再接続を処理する](e03-sse-reconnect)
|
||
- [E04. SSEをクライアントで受信する](e04-sse-client)
|
||
|
||
## WebSocket
|
||
|
||
- [W01. WebSocketエコーサーバー/クライアントを実装する](w01-websocket-echo)
|
||
- [W02. ハートビートを設定する](w02-websocket-ping)
|
||
- [W03. 接続クローズをハンドリングする](w03-websocket-close)
|
||
- [W04. バイナリフレームを送受信する](w04-websocket-binary)
|
||
- [W05. wss接続でTLSを設定する](w05-websocket-tls)
|
||
- [W06. タイムアウトを設定する](w06-websocket-timeouts)
|