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
3.7 KiB
Markdown
102 lines
3.7 KiB
Markdown
---
|
|
title: "Cookbook"
|
|
order: 0
|
|
status: "draft"
|
|
---
|
|
|
|
A collection of recipes that answer "How do I...?" questions. Each recipe is self-contained — read only what you need. For an introduction to the basics, see the [Tour](../tour/).
|
|
|
|
## Client
|
|
|
|
### Basics
|
|
- [C01. Get the response body / save to a file](c01-get-response-body)
|
|
- [C02. Send and receive JSON](c02-json)
|
|
- [C03. Set default headers](c03-default-headers)
|
|
- [C04. Follow redirects](c04-follow-location)
|
|
|
|
### Authentication
|
|
- [C05. Use Basic authentication](c05-basic-auth)
|
|
- [C06. Call an API with a Bearer token](c06-bearer-token)
|
|
|
|
### File Upload
|
|
- [C07. Upload a file as multipart form data](c07-multipart-upload)
|
|
- [C08. POST a file as raw binary](c08-post-file-body)
|
|
- [C09. Send the body with chunked transfer](c09-chunked-upload)
|
|
|
|
### Streaming & Progress
|
|
- [C10. Receive a response as a stream](c10-stream-response)
|
|
- [C11. Use the progress callback](c11-progress-callback)
|
|
|
|
### Connection & Performance
|
|
- [C12. Set timeouts](c12-timeouts)
|
|
- [C13. Set an overall timeout](c13-max-timeout)
|
|
- [C14. Understand connection reuse and Keep-Alive behavior](c14-keep-alive)
|
|
- [C15. Enable compression](c15-compression)
|
|
- [C16. Send requests through a proxy](c16-proxy)
|
|
|
|
### Error Handling & Debugging
|
|
- [C17. Handle error codes](c17-error-codes)
|
|
- [C18. Handle SSL errors](c18-ssl-errors)
|
|
- [C19. Set up client logging](c19-client-logger)
|
|
|
|
## Server
|
|
|
|
### Basics
|
|
- [S01. Register GET / POST / PUT / DELETE handlers](s01-handlers)
|
|
- [S02. Receive JSON requests and return JSON responses](s02-json-api)
|
|
- [S03. Use path parameters](s03-path-params)
|
|
- [S04. Set up a static file server](s04-static-files)
|
|
|
|
### Streaming & Files
|
|
- [S05. Stream a large file in the response](s05-stream-response)
|
|
- [S06. Return a file download response](s06-download-response)
|
|
- [S07. Receive multipart data as a stream](s07-multipart-reader)
|
|
- [S08. Return a compressed response](s08-compress-response)
|
|
|
|
### Handler Chain
|
|
- [S09. Add pre-processing to all routes](s09-pre-routing)
|
|
- [S10. Add response headers with a post-routing handler](s10-post-routing)
|
|
- [S11. Authenticate per route with a pre-request handler](s11-pre-request)
|
|
- [S12. Pass data between handlers with `res.user_data`](s12-user-data)
|
|
|
|
### Error Handling & Debugging
|
|
- [S13. Return custom error pages](s13-error-handler)
|
|
- [S14. Catch exceptions](s14-exception-handler)
|
|
- [S15. Log requests](s15-server-logger)
|
|
- [S16. Detect client disconnection](s16-disconnect)
|
|
|
|
### Operations & Tuning
|
|
- [S17. Bind to any available port](s17-bind-any-port)
|
|
- [S18. Control startup order with `listen_after_bind`](s18-listen-after-bind)
|
|
- [S19. Shut down gracefully](s19-graceful-shutdown)
|
|
- [S20. Tune Keep-Alive](s20-keep-alive)
|
|
- [S21. Configure the thread pool](s21-thread-pool)
|
|
- [S22. Talk over a Unix domain socket](s22-unix-socket)
|
|
|
|
### Protocol Extensions
|
|
- [S23. Handle custom HTTP methods](s23-custom-methods)
|
|
|
|
## TLS / Security
|
|
|
|
- [T01. Choosing between OpenSSL, mbedTLS, and wolfSSL](t01-tls-backends)
|
|
- [T02. Control SSL certificate verification](t02-cert-verification)
|
|
- [T03. Start an SSL/TLS server](t03-ssl-server)
|
|
- [T04. Configure mTLS](t04-mtls)
|
|
- [T05. Access the peer certificate on the server](t05-peer-cert)
|
|
|
|
## SSE
|
|
|
|
- [E01. Implement an SSE server](e01-sse-server)
|
|
- [E02. Use named events in SSE](e02-sse-event-names)
|
|
- [E03. Handle SSE reconnection](e03-sse-reconnect)
|
|
- [E04. Receive SSE on the client](e04-sse-client)
|
|
|
|
## WebSocket
|
|
|
|
- [W01. Implement a WebSocket echo server and client](w01-websocket-echo)
|
|
- [W02. Set a WebSocket heartbeat](w02-websocket-ping)
|
|
- [W03. Handle connection close](w03-websocket-close)
|
|
- [W04. Send and receive binary frames](w04-websocket-binary)
|
|
- [W05. Configure TLS for wss:// connections](w05-websocket-tls)
|
|
- [W06. Set timeouts](w06-websocket-timeouts)
|