diff --git a/docs-src/pages/en/cookbook/c02-json.md b/docs-src/pages/en/cookbook/c02-json.md index 1af39b4..4dfc540 100644 --- a/docs-src/pages/en/cookbook/c02-json.md +++ b/docs-src/pages/en/cookbook/c02-json.md @@ -17,7 +17,7 @@ auto res = cli.Post("/api/users", j.dump(), "application/json"); Pass the JSON string as the second argument to `Post()` and the Content-Type as the third. The same pattern works with `Put()` and `Patch()`. -> **Warning:** If you omit the Content-Type (the third argument), the server may not recognize the body as JSON. Always specify `"application/json"`. +> **Warning:** If the Content-Type (the third argument) is anything other than `"application/json"`, the server may not recognize the body as JSON. Always specify `"application/json"`. ## Receive a JSON response diff --git a/docs-src/pages/en/cookbook/c11-progress-callback.md b/docs-src/pages/en/cookbook/c11-progress-callback.md index 6efcf96..606f42b 100644 --- a/docs-src/pages/en/cookbook/c11-progress-callback.md +++ b/docs-src/pages/en/cookbook/c11-progress-callback.md @@ -21,7 +21,7 @@ auto res = cli.Get("/large-file", std::cout << std::endl; ``` -The callback fires each time data arrives. `total` comes from the Content-Length header — if the server doesn't send one, it may be `0`. In that case, you can't compute a percentage, so just display bytes received. +The callback fires each time data arrives. `total` comes from the Content-Length header. For a response without one (chunked transfer, for example), the progress callback is not called at all. ## Upload progress @@ -54,6 +54,8 @@ auto res = cli.Get("/large-file", }); ``` +> **Note:** A response without Content-Length never calls the progress callback, so it cannot be cancelled this way. Return `false` from a `ContentReceiver` instead. + > **Note:** `ContentReceiver` and the progress callback can be used together. When you want to stream to a file and show progress at the same time, pass both. > For a concrete example of saving to a file, see [C01. Get the response body / save to a file](../c01-get-response-body). diff --git a/docs-src/pages/en/cookbook/c13-max-timeout.md b/docs-src/pages/en/cookbook/c13-max-timeout.md index aa657b7..aaf49f0 100644 --- a/docs-src/pages/en/cookbook/c13-max-timeout.md +++ b/docs-src/pages/en/cookbook/c13-max-timeout.md @@ -16,7 +16,7 @@ cli.set_max_timeout(5000); // 5 seconds (in milliseconds) auto res = cli.Get("/slow-endpoint"); ``` -The value is in milliseconds. Connection, send, and receive together — the whole request is aborted if it exceeds the limit. +The value is in milliseconds. Once that much time has passed since the request started, waiting for the response is cut off. The limit does not shorten the connection and write waits themselves, so bound those with `set_connection_timeout` and `set_write_timeout`. ## Use `std::chrono` diff --git a/docs-src/pages/en/cookbook/c14-keep-alive.md b/docs-src/pages/en/cookbook/c14-keep-alive.md index 9a35b8d..ebc2eea 100644 --- a/docs-src/pages/en/cookbook/c14-keep-alive.md +++ b/docs-src/pages/en/cookbook/c14-keep-alive.md @@ -4,30 +4,29 @@ order: 14 status: "draft" --- -When you send multiple requests through the same `httplib::Client` instance, the TCP connection is reused automatically. HTTP/1.1 Keep-Alive does the work for you — you don't pay the TCP and TLS handshake cost on every call. +By default, `httplib::Client` closes the connection after every request (it sends `Connection: close`). Call `set_keep_alive(true)` and the requests you send through the same instance share one TCP connection, so you don't pay the TCP and TLS handshake cost on every call. -## Connections are reused automatically +## Enable Keep-Alive ```cpp httplib::Client cli("https://api.example.com"); +cli.set_keep_alive(true); auto res1 = cli.Get("/users/1"); auto res2 = cli.Get("/users/2"); // reuses the same connection auto res3 = cli.Get("/users/3"); // reuses the same connection ``` -No special config required. Just hold on to `cli` — internally, the socket stays open across calls. The effect is especially noticeable over HTTPS, where the TLS handshake is expensive. +After that, just hold on to `cli`. Internally, the socket stays open across calls. The effect is especially noticeable over HTTPS, where the TLS handshake is expensive. -## Disable Keep-Alive explicitly +## Turn Keep-Alive back off -To force a fresh connection every time, call `set_keep_alive(false)`. Mostly useful for testing. +To go back to a fresh connection every time, call `set_keep_alive(false)`. This is the default behavior. ```cpp cli.set_keep_alive(false); ``` -For normal use, leave it on (the default). - ## Don't create a `Client` per request If you create a `Client` inside a loop and let it fall out of scope each iteration, you lose the reuse benefit. Create the instance outside the loop. @@ -36,11 +35,13 @@ If you create a `Client` inside a loop and let it fall out of scope each iterati // Bad: a new connection every iteration for (auto id : ids) { httplib::Client cli("https://api.example.com"); + cli.set_keep_alive(true); cli.Get("/users/" + id); } // Good: the connection is reused httplib::Client cli("https://api.example.com"); +cli.set_keep_alive(true); for (auto id : ids) { cli.Get("/users/" + id); } @@ -50,4 +51,4 @@ for (auto id : ids) { If you want to send requests in parallel from multiple threads, give each thread its own `Client` instance. A single `Client` uses a single TCP connection, so firing concurrent requests at the same instance ends up serializing them anyway. -> **Note:** If the server closes the connection after its Keep-Alive timeout, cpp-httplib reconnects and retries transparently. You don't need to handle this in application code. +> **Note:** If the server closes the connection after its Keep-Alive timeout, cpp-httplib notices before it sends the next request and reconnects. You don't need to handle this in application code. diff --git a/docs-src/pages/en/cookbook/c15-compression.md b/docs-src/pages/en/cookbook/c15-compression.md index 8933ac2..cbd9e40 100644 --- a/docs-src/pages/en/cookbook/c15-compression.md +++ b/docs-src/pages/en/cookbook/c15-compression.md @@ -28,7 +28,7 @@ std::string big_payload = build_payload(); auto res = cli.Post("/api/data", big_payload, "application/json"); ``` -With `set_compress(true)`, the body of POST or PUT requests gets gzipped before sending. The server needs to handle compressed bodies too. +With `set_compress(true)`, the body of POST or PUT requests is compressed before sending. The encoding is the first one enabled in your build, in the order Brotli, gzip, Zstd. The server needs to handle compressed bodies too. ## Decompress the response @@ -44,4 +44,4 @@ With `set_decompress(true)`, the client automatically decompresses responses tha It's on by default, so normally you don't need to do anything. Set it to `false` only if you want the raw compressed bytes. -> **Warning:** If you build without `CPPHTTPLIB_ZLIB_SUPPORT`, calling `set_compress()` or `set_decompress()` does nothing. If compression isn't working, check the macro definition first. +> **Warning:** If you build without any compression library, `set_compress(true)` leaves the request uncompressed. And a response compressed with an encoding your build lacks makes the request fail with `Error::UnsupportedContentEncoding`. If compression isn't working, check the macro definitions first. diff --git a/docs-src/pages/en/cookbook/c16-proxy.md b/docs-src/pages/en/cookbook/c16-proxy.md index 4625c4b..fc88217 100644 --- a/docs-src/pages/en/cookbook/c16-proxy.md +++ b/docs-src/pages/en/cookbook/c16-proxy.md @@ -55,7 +55,7 @@ You often want internal endpoints to skip the proxy. Configure a bypass list wit ```cpp cli.set_proxy("proxy.internal", 8080); -cli.set_no_proxy({"internal.corp", "10.0.0.0/8", "*.dev.local"}); +cli.set_no_proxy({"internal.corp", "10.0.0.0/8", ".dev.local"}); ``` Each entry is one of: diff --git a/docs-src/pages/en/cookbook/c17-error-codes.md b/docs-src/pages/en/cookbook/c17-error-codes.md index 78e1273..95becc7 100644 --- a/docs-src/pages/en/cookbook/c17-error-codes.md +++ b/docs-src/pages/en/cookbook/c17-error-codes.md @@ -29,8 +29,8 @@ Use `if (res)` to check success. On failure, `res.error()` returns a `httplib::E | --- | --- | | `Error::Connection` | Couldn't connect to the server | | `Error::ConnectionTimeout` | Connection timeout (`set_connection_timeout`) | -| `Error::Read` / `Error::Write` | Error during send or receive | -| `Error::Timeout` | Overall timeout set via `set_max_timeout` | +| `Error::Read` / `Error::Write` | Error during send or receive. A timeout from `set_read_timeout` or `set_max_timeout` is also reported as `Error::Read` | +| `Error::Timeout` | A body read timed out in `stream::Get()` or `SSEClient` | | `Error::ExceedRedirectCount` | Too many redirects | | `Error::SSLConnection` | TLS handshake failed | | `Error::SSLServerVerification` | Server certificate verification failed | diff --git a/docs-src/pages/en/cookbook/c18-ssl-errors.md b/docs-src/pages/en/cookbook/c18-ssl-errors.md index 7780e61..570df07 100644 --- a/docs-src/pages/en/cookbook/c18-ssl-errors.md +++ b/docs-src/pages/en/cookbook/c18-ssl-errors.md @@ -24,19 +24,24 @@ if (!res) { } ``` -`ssl_error()` returns the error code from the SSL library (e.g., OpenSSL's `SSL_get_error()`). `ssl_backend_error()` gives you the backend's more detailed error value — for OpenSSL, that's `ERR_get_error()`. +`ssl_error()` is a backend-independent TLS error category: an `httplib::tls::ErrorCode` cast to `int`. `ssl_backend_error()` gives you the backend's own error value. With OpenSSL that is `ERR_get_error()` when the handshake failed, and the verify result (`X509_V_ERR_*`) when certificate verification failed. ## Format OpenSSL errors as strings -When you have a value from `ssl_backend_error()`, pass it to OpenSSL's `ERR_error_string()` to get a readable message. +When you have a value from `ssl_backend_error()`, pass it to the OpenSSL function that matches the kind of failure to get a readable message. ```cpp #include +#include -if (res.ssl_backend_error() != 0) { +if (res.error() == httplib::Error::SSLConnection) { char buf[256]; ERR_error_string_n(res.ssl_backend_error(), buf, sizeof(buf)); std::cerr << "openssl: " << buf << std::endl; +} else if (res.error() == httplib::Error::SSLServerVerification || + res.error() == httplib::Error::SSLServerHostnameVerification) { + auto code = static_cast(res.ssl_backend_error()); + std::cerr << "openssl: " << X509_verify_cert_error_string(code) << std::endl; } ``` diff --git a/docs-src/pages/en/cookbook/e04-sse-client.md b/docs-src/pages/en/cookbook/e04-sse-client.md index 2199a5a..2d094e1 100644 --- a/docs-src/pages/en/cookbook/e04-sse-client.md +++ b/docs-src/pages/en/cookbook/e04-sse-client.md @@ -41,7 +41,7 @@ sse.on_event("leave", [](const auto &msg) { }); ``` -`on_message()` serves as a generic fallback for unnamed events (the default `message` type). +`on_message()` is the generic fallback: it receives every event that has no handler registered through `on_event()`. With `on_event("message", ...)` registered as above, `message` events go there instead. ## Connection lifecycle and errors @@ -55,7 +55,7 @@ sse.on_error([](httplib::Error err) { }); ``` -Hook into connection open and error events. Even when the error handler fires, `SSEClient` keeps trying to reconnect in the background. +Hook into connection open and error events. Even when the error handler fires, `SSEClient` keeps trying to reconnect in the background. The exceptions are a 204, 403, or 404 response, after which it stops. ## Run asynchronously diff --git a/docs-src/pages/en/cookbook/s04-static-files.md b/docs-src/pages/en/cookbook/s04-static-files.md index fd31499..4474f6a 100644 --- a/docs-src/pages/en/cookbook/s04-static-files.md +++ b/docs-src/pages/en/cookbook/s04-static-files.md @@ -30,7 +30,7 @@ You can even mount multiple directories at the same path — they're searched in ## Combine with API handlers -Static files and API handlers coexist happily. Handlers registered with `Get()` and friends take priority; the mount points are searched only when nothing matches. +Static files and API handlers coexist happily. For GET and HEAD, the mount points are searched first; handlers registered with `Get()` and friends run only when no file is found. ```cpp svr.Get("/api/users", [](const auto &req, auto &res) { diff --git a/docs-src/pages/en/cookbook/s05-stream-response.md b/docs-src/pages/en/cookbook/s05-stream-response.md index 0064411..0c5f5b8 100644 --- a/docs-src/pages/en/cookbook/s05-stream-response.md +++ b/docs-src/pages/en/cookbook/s05-stream-response.md @@ -15,14 +15,15 @@ svr.Get("/download", [](const httplib::Request &req, httplib::Response &res) { res.set_content_provider( total_size, "application/octet-stream", [](size_t offset, size_t length, httplib::DataSink &sink) { - auto data = read_range_from_file("large.bin", offset, length); + auto n = std::min(length, size_t(64 * 1024)); + auto data = read_range_from_file("large.bin", offset, n); sink.write(data.data(), data.size()); return true; }); }); ``` -The lambda is called repeatedly with `offset` and `length`. Read just that range and write it to `sink`. Only a small chunk sits in memory at any given time. +The lambda is called repeatedly with `offset`, the position sent so far, and `length`, the number of bytes still to send. Cap how much you read per call and write it to `sink`, and only a small chunk sits in memory at any given time. ## Just send a file diff --git a/docs-src/pages/en/cookbook/s07-multipart-reader.md b/docs-src/pages/en/cookbook/s07-multipart-reader.md index c862c8d..b004645 100644 --- a/docs-src/pages/en/cookbook/s07-multipart-reader.md +++ b/docs-src/pages/en/cookbook/s07-multipart-reader.md @@ -96,7 +96,7 @@ svr.Post("/upload", }); ``` -When `content_reader` returns `false`, set the response status yourself. The rest of the body is left unread and the connection is closed, so a client that is still sending sees the connection drop. +When `content_reader` returns `false`, the response status becomes 400 (413 if the body exceeded the size limit). Set it yourself if you want a different one. The rest of the body is left unread and the connection is closed, so a client that is still sending sees the connection drop. > **Warning:** When you use `HandlerWithContentReader`, `req.body` stays **empty**. Handle the body yourself inside the callbacks. diff --git a/docs-src/pages/en/cookbook/s08-compress-response.md b/docs-src/pages/en/cookbook/s08-compress-response.md index 9cc67a0..335d075 100644 --- a/docs-src/pages/en/cookbook/s08-compress-response.md +++ b/docs-src/pages/en/cookbook/s08-compress-response.md @@ -32,7 +32,7 @@ That's it. If the client sent `Accept-Encoding: gzip`, cpp-httplib compresses th ## Encoding priority -When the client accepts multiple encodings, cpp-httplib picks in this order (among those enabled at build time): Brotli → Zstd → gzip. Your code doesn't need to care — you always get the most efficient option available. +When the client accepts multiple encodings, cpp-httplib picks the one with the highest q-value in `Accept-Encoding`. On a tie the order is Brotli → gzip → Zstd (among those enabled at build time). ## Streaming responses are compressed too diff --git a/docs-src/pages/en/cookbook/s12-user-data.md b/docs-src/pages/en/cookbook/s12-user-data.md index 7bad980..76abb92 100644 --- a/docs-src/pages/en/cookbook/s12-user-data.md +++ b/docs-src/pages/en/cookbook/s12-user-data.md @@ -37,7 +37,7 @@ svr.Get("/me", [](const httplib::Request &req, httplib::Response &res) { ## Typical value types -Strings, numbers, structs, `std::shared_ptr` — anything copyable or movable works. +Strings, numbers, structs, `std::shared_ptr`: anything copyable works. ```cpp res.user_data.set("user_id", std::string{"42"}); diff --git a/docs-src/pages/en/cookbook/s15-server-logger.md b/docs-src/pages/en/cookbook/s15-server-logger.md index 2deb677..2f106e7 100644 --- a/docs-src/pages/en/cookbook/s15-server-logger.md +++ b/docs-src/pages/en/cookbook/s15-server-logger.md @@ -48,7 +48,7 @@ svr.set_pre_routing_handler([](const auto &req, auto &res) { return httplib::Server::HandlerResponse::Unhandled; }); -svr.set_logger([](const auto &req, const auto &res) { +svr.set_logger([](const httplib::Request &req, const httplib::Response &res) { auto *start = res.user_data.get("start"); auto elapsed = start ? std::chrono::duration_cast( diff --git a/docs-src/pages/en/cookbook/s19-graceful-shutdown.md b/docs-src/pages/en/cookbook/s19-graceful-shutdown.md index 65e5f31..9c4b603 100644 --- a/docs-src/pages/en/cookbook/s19-graceful-shutdown.md +++ b/docs-src/pages/en/cookbook/s19-graceful-shutdown.md @@ -52,6 +52,6 @@ int main() { ## What happens to in-flight requests -When you call `stop()`, new connections are refused, but requests already being processed are **allowed to finish**. Once all workers drain, `listen()` returns. That's what makes it graceful. +When you call `stop()`, new connections are refused, but handlers that are already running are **allowed to finish**. A response still being sent by a content provider (a streaming response, for example) is cut short, though. Once all workers drain, `listen()` returns. That's what makes it graceful. > **Warning:** There's a wait between calling `stop()` and `listen()` returning — it's the time in-flight requests take to finish. To enforce a timeout, you'll need to add your own shutdown timer in application code. diff --git a/docs-src/pages/en/cookbook/s22-unix-socket.md b/docs-src/pages/en/cookbook/s22-unix-socket.md index 6ca64b7..d30285e 100644 --- a/docs-src/pages/en/cookbook/s22-unix-socket.md +++ b/docs-src/pages/en/cookbook/s22-unix-socket.md @@ -19,7 +19,7 @@ svr.Get("/", [](const auto &, auto &res) { svr.listen("/tmp/httplib.sock", 80); ``` -Call `set_address_family(AF_UNIX)` first, then pass the socket file path as the first argument to `listen()`. The port number is unused but required by the signature — pass any value. +Call `set_address_family(AF_UNIX)` first, then pass the socket file path as the first argument to `listen()`. The port number is unused but required by the signature. Pass any value other than `0`, which makes `listen()` fail. ## Client side diff --git a/docs-src/pages/en/cookbook/t04-mtls.md b/docs-src/pages/en/cookbook/t04-mtls.md index 4224fb1..2cd50f9 100644 --- a/docs-src/pages/en/cookbook/t04-mtls.md +++ b/docs-src/pages/en/cookbook/t04-mtls.md @@ -55,7 +55,7 @@ httplib::SSLClient cli("api.example.com", 443, auto res = cli.Get("/"); ``` -Note you're using `SSLClient` directly, not `Client`. If the private key has a password, pass it as the fifth argument. +If the certificate and key paths are all you need, `Client` takes them too: `httplib::Client cli("https://api.example.com", "client-cert.pem", "client-key.pem")`. When the private key has a password, use `SSLClient` and pass it as the fifth argument. The client side has the same `PemMemory` struct too, letting you set the client certificate from PEM in memory. diff --git a/docs-src/pages/en/cookbook/w02-websocket-ping.md b/docs-src/pages/en/cookbook/w02-websocket-ping.md index 2a34bb1..ae85463 100644 --- a/docs-src/pages/en/cookbook/w02-websocket-ping.md +++ b/docs-src/pages/en/cookbook/w02-websocket-ping.md @@ -67,7 +67,7 @@ cli.set_websocket_max_missed_pongs(2); // close after 2 consecutive unacked ping The server side has the same `set_websocket_max_missed_pongs()`. -With a 30-second ping interval and `max_missed_pongs = 2`, a dead peer is detected within roughly 60 seconds and the connection is closed with `CloseStatus::GoingAway` and the reason `"pong timeout"`. +With a 30-second ping interval and `max_missed_pongs = 2`, a dead peer is detected 60 to 90 seconds after it stops answering, and the connection is closed with `CloseStatus::GoingAway` and the reason `"pong timeout"`. A `read()` waiting on the peer at that moment returns `Fail`. The counter is reset whenever `read()` consumes an incoming Pong frame, so this only works if your code is actively calling `read()` in a loop — which is what a normal WebSocket client does anyway. diff --git a/docs-src/pages/en/llm-app/ch02-rest-api.md b/docs-src/pages/en/llm-app/ch02-rest-api.md index 6204573..27e6659 100644 --- a/docs-src/pages/en/llm-app/ch02-rest-api.md +++ b/docs-src/pages/en/llm-app/ch02-rest-api.md @@ -18,10 +18,6 @@ Simply pass the path to a model file to `llamalib::Llama`, and model loading, co int main() { auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf"}; - // LLM inference takes time, so set a longer timeout (default is 5 seconds) - svr.set_read_timeout(300); - svr.set_write_timeout(300); - // ... Build and start the HTTP server ... } ``` @@ -78,7 +74,7 @@ svr.Post("/translate", }); ``` -`llm.chat()` can throw exceptions during inference (for example, when the context length is exceeded). By catching them with `try/catch` and returning the error as JSON, we prevent the server from crashing. +`llm.chat()` can throw exceptions during inference (for example, when the context length is exceeded). We catch them with `try/catch` and return the error as JSON. Left uncaught, cpp-httplib would still answer with a 500, but the client would not learn the cause. ## 2.3 Complete Code @@ -111,10 +107,6 @@ int main() { // Load the model downloaded in Chapter 1 auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf"}; - // LLM inference takes time, so set a longer timeout (default is 5 seconds) - svr.set_read_timeout(300); - svr.set_write_timeout(300); - // Log requests and responses svr.set_logger([](const auto &req, const auto &res) { std::cout << req.method << " " << req.path << " -> " << res.status diff --git a/docs-src/pages/en/llm-app/ch03-sse-streaming.md b/docs-src/pages/en/llm-app/ch03-sse-streaming.md index 656d9b4..11de7d8 100644 --- a/docs-src/pages/en/llm-app/ch03-sse-streaming.md +++ b/docs-src/pages/en/llm-app/ch03-sse-streaming.md @@ -73,7 +73,7 @@ A few key points: - After writing to `sink.os`, you can check whether the client is still connected with `sink.os.good()`. If the client has disconnected, it returns `false` to stop inference - Each token is escaped as a JSON string using `json(token).dump()` before sending. This is safe even for tokens containing newlines or quotes - The first three arguments of `dump(-1, ' ', false, ...)` are the defaults. What matters is the fourth argument, `json::error_handler_t::replace`. Since the LLM returns tokens at the subword level, multi-byte characters (such as Japanese) can be split mid-character across tokens. Passing an incomplete UTF-8 byte sequence directly to `dump()` would throw an exception, so `replace` safely substitutes them. The browser reassembles the bytes on its end, so everything displays correctly -- The entire lambda is wrapped in `try/catch`. `llm.chat()` can throw exceptions for reasons such as exceeding the context window. If an exception goes uncaught inside the lambda, the server will crash, so we return the error as an SSE event instead +- The entire lambda is wrapped in `try/catch`. `llm.chat()` can throw exceptions for reasons such as exceeding the context window. If an exception goes uncaught inside the lambda, cpp-httplib just drops the connection and the client never learns the cause, so we return the error as an SSE event instead - `data: [DONE]` follows the OpenAI API convention to signal the end of the stream to the client ## 3.4 Complete Code @@ -107,10 +107,6 @@ int main() { // Load the GGUF model auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf"}; - // LLM inference takes time, so set a longer timeout (default is 5 seconds) - svr.set_read_timeout(300); - svr.set_write_timeout(300); - // Log requests and responses svr.set_logger([](const auto &req, const auto &res) { std::cout << req.method << " " << req.path << " -> " << res.status diff --git a/docs-src/pages/en/llm-app/ch04-model-management.md b/docs-src/pages/en/llm-app/ch04-model-management.md index 0eb5ef6..8c10cb6 100644 --- a/docs-src/pages/en/llm-app/ch04-model-management.md +++ b/docs-src/pages/en/llm-app/ch04-model-management.md @@ -219,7 +219,6 @@ bool download_model(const ModelInfo &model, std::function progress_cb) { httplib::Client cli("https://huggingface.co"); cli.set_follow_location(true); - cli.set_read_timeout(std::chrono::hours(1)); auto url = "/" + model.repo + "/resolve/main/" + model.filename; auto path = get_models_dir() / model.filename; @@ -469,7 +468,6 @@ bool download_model(const ModelInfo &model, std::function progress_cb) { httplib::Client cli("https://huggingface.co"); cli.set_follow_location(true); // Hugging Face redirects to a CDN - cli.set_read_timeout(std::chrono::hours(1)); // Set a long timeout for large models auto url = "/" + model.repo + "/resolve/main/" + model.filename; auto path = get_models_dir() / model.filename; @@ -539,10 +537,6 @@ int main() { auto llm = llamalib::Llama{path}; std::mutex llm_mutex; // Protect access during model switching - // Set a long timeout since LLM inference takes time (default is 5 seconds) - svr.set_read_timeout(300); - svr.set_write_timeout(300); - svr.set_logger([](const auto &req, const auto &res) { std::cout << req.method << " " << req.path << " -> " << res.status << std::endl; diff --git a/docs-src/pages/en/llm-app/ch05-web-ui.md b/docs-src/pages/en/llm-app/ch05-web-ui.md index fb4e90d..0692972 100644 --- a/docs-src/pages/en/llm-app/ch05-web-ui.md +++ b/docs-src/pages/en/llm-app/ch05-web-ui.md @@ -939,7 +939,6 @@ bool download_model(const ModelInfo &model, std::function progress_cb) { httplib::Client cli("https://huggingface.co"); cli.set_follow_location(true); // Hugging Face redirects to CDN - cli.set_read_timeout(std::chrono::hours(1)); // Long timeout for large models auto url = "/" + model.repo + "/resolve/main/" + model.filename; auto path = get_models_dir() / model.filename; @@ -1008,10 +1007,6 @@ int main() { } auto llm = llamalib::Llama{path}; - // LLM inference takes time, so set a longer timeout (default is 5 seconds) - svr.set_read_timeout(300); - svr.set_write_timeout(300); - svr.set_logger([](const auto &req, const auto &res) { std::cout << req.method << " " << req.path << " -> " << res.status << std::endl; diff --git a/docs-src/pages/en/llm-app/ch06-desktop-app.md b/docs-src/pages/en/llm-app/ch06-desktop-app.md index 304382d..6960d6c 100644 --- a/docs-src/pages/en/llm-app/ch06-desktop-app.md +++ b/docs-src/pages/en/llm-app/ch06-desktop-app.md @@ -407,7 +407,6 @@ bool download_model(const ModelInfo &model, std::function progress_cb) { httplib::Client cli("https://huggingface.co"); cli.set_follow_location(true); // Hugging Face redirects to a CDN - cli.set_read_timeout(std::chrono::hours(1)); // Long timeout for large models auto url = "/" + model.repo + "/resolve/main/" + model.filename; auto path = get_models_dir() / model.filename; @@ -469,10 +468,6 @@ int main() { auto llm = llamalib::Llama{path}; std::mutex llm_mutex; // Protect access during model switching - // Set a long timeout since LLM inference takes time (default is 5 seconds) - svr.set_read_timeout(300); - svr.set_write_timeout(300); - svr.set_logger([](const auto &req, const auto &res) { std::cout << req.method << " " << req.path << " -> " << res.status << std::endl; diff --git a/docs-src/pages/en/llm-app/ch07-code-reading.md b/docs-src/pages/en/llm-app/ch07-code-reading.md index a28a723..e600525 100644 --- a/docs-src/pages/en/llm-app/ch07-code-reading.md +++ b/docs-src/pages/en/llm-app/ch07-code-reading.md @@ -11,13 +11,17 @@ Over the course of six chapters, we built a translation desktop app from scratch ## 7.1 Source Code Location ```ascii -llama.cpp/tools/server/ -├── server.cpp # Main server implementation -├── httplib.h # cpp-httplib (bundled version) -└── ... +llama.cpp/ +├── tools/server/ +│ ├── server.cpp # Entry point +│ ├── server-http.cpp # HTTP server (the layer that uses cpp-httplib) +│ ├── server-context.cpp # Inference and slot management +│ └── ... +└── vendor/cpp-httplib/ + └── httplib.h # cpp-httplib (bundled version) ``` -The code is contained in a single `server.cpp`. It runs to several thousand lines, but once you understand the structure, you can narrow down the parts worth reading. +The implementation is split across several files by role. It is a large code base, but once you understand the structure, you can narrow down the parts worth reading. ## 7.2 OpenAI-Compatible API diff --git a/docs-src/pages/en/tour/02-basic-client.md b/docs-src/pages/en/tour/02-basic-client.md index 9a909f7..31bce28 100644 --- a/docs-src/pages/en/tour/02-basic-client.md +++ b/docs-src/pages/en/tour/02-basic-client.md @@ -200,8 +200,8 @@ auto res = cli.Post("/submit", httplib::Params{ }); if (res) { std::cout << res->body << std::endl; - // age = 30 // name = Alice + // age = 30 } ``` @@ -241,7 +241,7 @@ auto res = cli.Get("/hi"); if (!res) { // Connection error std::cout << "Error: " << httplib::to_string(res.error()) << std::endl; - // Error: Connection + // Error: Could not establish connection return 1; } diff --git a/docs-src/pages/en/tour/04-static-file-server.md b/docs-src/pages/en/tour/04-static-file-server.md index 6760086..d62ab13 100644 --- a/docs-src/pages/en/tour/04-static-file-server.md +++ b/docs-src/pages/en/tour/04-static-file-server.md @@ -95,7 +95,7 @@ svr.set_mount_point("/", "./public"); svr.listen("0.0.0.0", 8080); ``` -Handlers take priority. The handler responds to `/api/hello`. For every other path, the server looks for a file in `./public`. +The server looks for a file in `./public` first and calls the handler when there is none. So the handler responds to `/api/hello` unless you put a file at `./public/api/hello`. ## Adding response headers diff --git a/docs-src/pages/en/tour/09-whats-next.md b/docs-src/pages/en/tour/09-whats-next.md index 815a2a7..b315e46 100644 --- a/docs-src/pages/en/tour/09-whats-next.md +++ b/docs-src/pages/en/tour/09-whats-next.md @@ -50,7 +50,7 @@ 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; // Return false to finish + return true; // Call sink.done() to finish }); }); ``` @@ -155,7 +155,7 @@ svr.set_pre_routing_handler([](const auto &req, auto &res) { }); svr.set_post_routing_handler([](const auto &req, auto &res) { - // Runs after the response is sent + // Runs just before the response is sent res.set_header("X-Server", "cpp-httplib"); }); ``` @@ -168,7 +168,7 @@ svr.set_pre_routing_handler([](const auto &req, auto &res) { return httplib::Server::HandlerResponse::Unhandled; }); -svr.Get("/me", [](const auto &req, auto &res) { +svr.Get("/me", [](const httplib::Request &req, httplib::Response &res) { auto *user = res.user_data.get("auth_user"); res.set_content("Hello, " + *user, "text/plain"); }); @@ -205,7 +205,7 @@ In addition to TCP, we support Unix Domain Sockets. You can use them for inter-p // Server httplib::Server svr; svr.set_address_family(AF_UNIX); -svr.listen("/tmp/httplib.sock", 0); +svr.listen("/tmp/httplib.sock", 80); // The port is unused, but must not be 0 ``` ```cpp diff --git a/docs-src/pages/ja/cookbook/c02-json.md b/docs-src/pages/ja/cookbook/c02-json.md index f35d01a..c42a0a8 100644 --- a/docs-src/pages/ja/cookbook/c02-json.md +++ b/docs-src/pages/ja/cookbook/c02-json.md @@ -17,7 +17,7 @@ auto res = cli.Post("/api/users", j.dump(), "application/json"); `Post()`の第2引数にJSON文字列、第3引数にContent-Typeを渡します。`Put()`や`Patch()`でも同じ形です。 -> **Warning:** 第3引数のContent-Typeを省略すると、サーバー側でJSONとして認識されないことがあります。`"application/json"`を必ず指定しましょう。 +> **Warning:** 第3引数のContent-Typeに`"application/json"`以外を渡すと、サーバー側でJSONとして認識されないことがあります。`"application/json"`を必ず指定しましょう。 ## JSONレスポンスを受け取る diff --git a/docs-src/pages/ja/cookbook/c11-progress-callback.md b/docs-src/pages/ja/cookbook/c11-progress-callback.md index 911d860..7e5bd67 100644 --- a/docs-src/pages/ja/cookbook/c11-progress-callback.md +++ b/docs-src/pages/ja/cookbook/c11-progress-callback.md @@ -21,7 +21,7 @@ auto res = cli.Get("/large-file", std::cout << std::endl; ``` -コールバックはデータを受信するたびに呼ばれます。`total`はContent-Lengthから取得した値で、サーバーが送ってこない場合は`0`になることがあります。その場合は進捗率が計算できないので、受信済みバイト数だけを表示するのが無難です。 +コールバックはデータを受信するたびに呼ばれます。`total`はContent-Lengthから取得した値です。Content-Lengthのないレスポンス(chunked転送など)では、進捗コールバックは呼ばれません。 ## アップロードの進捗 @@ -54,6 +54,8 @@ auto res = cli.Get("/large-file", }); ``` +> **Note:** Content-Lengthのないレスポンスでは進捗コールバックが呼ばれないので、この方法では中断できません。その場合は`ContentReceiver`から`false`を返します。 + > **Note:** `ContentReceiver`と進捗コールバックは同時に使えます。ファイルに書き出しながら進捗を表示したいときは、両方を渡しましょう。 > ファイル保存と組み合わせる具体例は[C01. レスポンスボディを取得する / ファイルに保存する](../c01-get-response-body)も参照してください。 diff --git a/docs-src/pages/ja/cookbook/c13-max-timeout.md b/docs-src/pages/ja/cookbook/c13-max-timeout.md index e8d42cc..685409d 100644 --- a/docs-src/pages/ja/cookbook/c13-max-timeout.md +++ b/docs-src/pages/ja/cookbook/c13-max-timeout.md @@ -16,7 +16,7 @@ cli.set_max_timeout(5000); // 5秒(ミリ秒単位) auto res = cli.Get("/slow-endpoint"); ``` -ミリ秒単位で指定します。接続、送信、受信をすべて含めて、リクエスト全体が指定時間を超えたら打ち切られます。 +ミリ秒単位で指定します。リクエスト開始からの経過時間がこの値を超えると、レスポンスの受信待ちが打ち切られます。接続と送信の待ち時間そのものは短縮されないので、そちらは`set_connection_timeout`と`set_write_timeout`で抑えます。 ## `std::chrono`で指定する diff --git a/docs-src/pages/ja/cookbook/c14-keep-alive.md b/docs-src/pages/ja/cookbook/c14-keep-alive.md index e182ea9..b949ada 100644 --- a/docs-src/pages/ja/cookbook/c14-keep-alive.md +++ b/docs-src/pages/ja/cookbook/c14-keep-alive.md @@ -4,30 +4,29 @@ order: 14 status: "draft" --- -`httplib::Client`は同じインスタンスで複数回リクエストを送ると、TCP接続を自動的に再利用します。HTTP/1.1のKeep-Aliveが有効に働くので、TCPハンドシェイクやTLSハンドシェイクのオーバーヘッドを毎回払わずに済みます。 +`httplib::Client`は、デフォルトではリクエストごとに接続を閉じます(`Connection: close`を送ります)。`set_keep_alive(true)`を呼ぶと、同じインスタンスで送る複数のリクエストが1本のTCP接続を使い回すようになり、TCPハンドシェイクやTLSハンドシェイクのオーバーヘッドを毎回払わずに済みます。 -## 接続は自動で使い回される +## Keep-Aliveを有効にする ```cpp httplib::Client cli("https://api.example.com"); +cli.set_keep_alive(true); auto res1 = cli.Get("/users/1"); auto res2 = cli.Get("/users/2"); // 同じ接続を再利用 auto res3 = cli.Get("/users/3"); // 同じ接続を再利用 ``` -特別な設定は要りません。`cli`を使い回すだけで、内部的には同じソケットで通信が続きます。とくにHTTPSでは、TLSハンドシェイクのコストが大きいので効果が顕著です。 +あとは`cli`を使い回すだけで、内部的には同じソケットで通信が続きます。とくにHTTPSでは、TLSハンドシェイクのコストが大きいので効果が顕著です。 -## Keep-Aliveを明示的にオフにする +## Keep-Aliveをオフに戻す -毎回新しい接続を張り直したい場合は、`set_keep_alive(false)`を呼びます。テスト目的などで使うことがあります。 +毎回新しい接続を張り直す動作に戻したい場合は、`set_keep_alive(false)`を呼びます。これがデフォルトの動作です。 ```cpp cli.set_keep_alive(false); ``` -ただし、普段はオン(デフォルト)のままで問題ありません。 - ## リクエストごとに`Client`を作らない 1回のリクエストのたびに`Client`をスコープから抜けて破棄すると、接続の再利用は効きません。ループの外でインスタンスを作り、中で使い回しましょう。 @@ -36,11 +35,13 @@ cli.set_keep_alive(false); // NG: 毎回接続が切れる for (auto id : ids) { httplib::Client cli("https://api.example.com"); + cli.set_keep_alive(true); cli.Get("/users/" + id); } // OK: 接続が再利用される httplib::Client cli("https://api.example.com"); +cli.set_keep_alive(true); for (auto id : ids) { cli.Get("/users/" + id); } @@ -50,4 +51,4 @@ for (auto id : ids) { 複数のスレッドから並行にリクエストを送りたいときは、スレッドごとに別々の`Client`インスタンスを持つのが無難です。1つの`Client`は1本のTCP接続を使い回すので、同じインスタンスに複数スレッドから同時にリクエストを投げると、結局どこかで直列化されます。 -> **Note:** サーバー側のKeep-Aliveタイムアウトを超えると、サーバーが接続を切ります。その場合cpp-httplibは自動で再接続して再試行するので、アプリケーションコードで気にする必要はありません。 +> **Note:** サーバー側のKeep-Aliveタイムアウトを超えると、サーバーが接続を切ります。cpp-httplibは次のリクエストを送る前にそれを検出して接続し直すので、アプリケーションコードで気にする必要はありません。 diff --git a/docs-src/pages/ja/cookbook/c15-compression.md b/docs-src/pages/ja/cookbook/c15-compression.md index cfc1a70..3753e66 100644 --- a/docs-src/pages/ja/cookbook/c15-compression.md +++ b/docs-src/pages/ja/cookbook/c15-compression.md @@ -28,7 +28,7 @@ std::string big_payload = build_payload(); auto res = cli.Post("/api/data", big_payload, "application/json"); ``` -`set_compress(true)`を呼んでおくと、POSTやPUTのリクエストボディがgzipで圧縮されて送信されます。サーバー側が対応している必要があります。 +`set_compress(true)`を呼んでおくと、POSTやPUTのリクエストボディが圧縮されて送信されます。方式は、ビルドで有効なものからBrotli、gzip、Zstdの順に選ばれます。サーバー側が対応している必要があります。 ## レスポンスを解凍する @@ -44,4 +44,4 @@ std::cout << res->body << std::endl; デフォルトで有効なので、通常は何もしなくても解凍されます。あえて生の圧縮データを触りたいときだけ`set_decompress(false)`にしましょう。 -> **Warning:** `CPPHTTPLIB_ZLIB_SUPPORT`を定義せずにビルドすると、`set_compress()`や`set_decompress()`を呼んでも何も起こりません。マクロの定義を忘れていないか、最初に確認しましょう。 +> **Warning:** 圧縮ライブラリを1つも有効にせずにビルドすると、`set_compress(true)`を呼んでもリクエストは圧縮されません。また、ビルドに含まれていない方式で圧縮されたレスポンスを受け取ると、リクエストは`Error::UnsupportedContentEncoding`で失敗します。マクロの定義を忘れていないか、最初に確認しましょう。 diff --git a/docs-src/pages/ja/cookbook/c16-proxy.md b/docs-src/pages/ja/cookbook/c16-proxy.md index afd1509..a064be0 100644 --- a/docs-src/pages/ja/cookbook/c16-proxy.md +++ b/docs-src/pages/ja/cookbook/c16-proxy.md @@ -55,7 +55,7 @@ cli.set_bearer_token_auth("api-token"); // エンドサーバー向け ```cpp cli.set_proxy("proxy.internal", 8080); -cli.set_no_proxy({"internal.corp", "10.0.0.0/8", "*.dev.local"}); +cli.set_no_proxy({"internal.corp", "10.0.0.0/8", ".dev.local"}); ``` エントリは次のいずれかです。 diff --git a/docs-src/pages/ja/cookbook/c17-error-codes.md b/docs-src/pages/ja/cookbook/c17-error-codes.md index 0bc5a9e..2bb8187 100644 --- a/docs-src/pages/ja/cookbook/c17-error-codes.md +++ b/docs-src/pages/ja/cookbook/c17-error-codes.md @@ -29,8 +29,8 @@ if (res) { | --- | --- | | `Error::Connection` | サーバーに接続できなかった | | `Error::ConnectionTimeout` | 接続タイムアウト(`set_connection_timeout`) | -| `Error::Read` / `Error::Write` | 送受信中のエラー | -| `Error::Timeout` | `set_max_timeout`で設定した全体タイムアウト | +| `Error::Read` / `Error::Write` | 送受信中のエラー。`set_read_timeout`や`set_max_timeout`によるタイムアウトも`Error::Read`になる | +| `Error::Timeout` | `stream::Get()`や`SSEClient`で、ボディの読み取りがタイムアウトした | | `Error::ExceedRedirectCount` | リダイレクト回数が上限を超えた | | `Error::SSLConnection` | TLSハンドシェイクに失敗 | | `Error::SSLServerVerification` | サーバー証明書の検証に失敗 | diff --git a/docs-src/pages/ja/cookbook/c18-ssl-errors.md b/docs-src/pages/ja/cookbook/c18-ssl-errors.md index b02b25e..8c3567b 100644 --- a/docs-src/pages/ja/cookbook/c18-ssl-errors.md +++ b/docs-src/pages/ja/cookbook/c18-ssl-errors.md @@ -24,19 +24,24 @@ if (!res) { } ``` -`ssl_error()`はSSLライブラリが返したエラーコード(OpenSSLの`SSL_get_error()`の値など)、`ssl_backend_error()`はバックエンドがさらに詳しく提供するエラー値です。OpenSSLなら`ERR_get_error()`の値が入ります。 +`ssl_error()`はバックエンドに依存しないTLSエラーの種別で、`httplib::tls::ErrorCode`を`int`にした値です。`ssl_backend_error()`にはバックエンド固有のエラー値が入ります。OpenSSLの場合、ハンドシェイクに失敗したときは`ERR_get_error()`の値、証明書の検証に失敗したときは検証結果のコード(`X509_V_ERR_*`)です。 ## OpenSSLのエラーを文字列化する -`ssl_backend_error()`で取得した値を、OpenSSLの`ERR_error_string()`で文字列にするとデバッグに便利です。 +`ssl_backend_error()`で取得した値は、失敗の種類に合ったOpenSSLの関数で文字列にするとデバッグに便利です。 ```cpp #include +#include -if (res.ssl_backend_error() != 0) { +if (res.error() == httplib::Error::SSLConnection) { char buf[256]; ERR_error_string_n(res.ssl_backend_error(), buf, sizeof(buf)); std::cerr << "openssl: " << buf << std::endl; +} else if (res.error() == httplib::Error::SSLServerVerification || + res.error() == httplib::Error::SSLServerHostnameVerification) { + auto code = static_cast(res.ssl_backend_error()); + std::cerr << "openssl: " << X509_verify_cert_error_string(code) << std::endl; } ``` diff --git a/docs-src/pages/ja/cookbook/e04-sse-client.md b/docs-src/pages/ja/cookbook/e04-sse-client.md index bf13f91..4399b8e 100644 --- a/docs-src/pages/ja/cookbook/e04-sse-client.md +++ b/docs-src/pages/ja/cookbook/e04-sse-client.md @@ -41,7 +41,7 @@ sse.on_event("leave", [](const auto &msg) { }); ``` -`on_message()`は、名前なし(デフォルトの`message`イベント)を受け取る汎用ハンドラとして使えます。 +`on_message()`は、`on_event()`でハンドラを登録していないイベントをすべて受け取る汎用ハンドラです。上の例のように`on_event("message", ...)`を登録すると、`message`イベントはそちらに届きます。 ## 接続イベントとエラーハンドリング @@ -55,7 +55,7 @@ sse.on_error([](httplib::Error err) { }); ``` -接続確立時やエラー発生時にもフックを挟めます。エラーハンドラが呼ばれても、`SSEClient`は内部で再接続を試みます。 +接続確立時やエラー発生時にもフックを挟めます。エラーハンドラが呼ばれても、`SSEClient`は内部で再接続を試みます。ただし、サーバーが204、403、404を返した場合は再接続しません。 ## 非同期で動かす diff --git a/docs-src/pages/ja/cookbook/s04-static-files.md b/docs-src/pages/ja/cookbook/s04-static-files.md index a7eb6d3..af76d93 100644 --- a/docs-src/pages/ja/cookbook/s04-static-files.md +++ b/docs-src/pages/ja/cookbook/s04-static-files.md @@ -30,7 +30,7 @@ svr.set_mount_point("/uploads", "./var/uploads"); ## APIハンドラと組み合わせる -静的ファイルとAPIハンドラは共存できます。`Get()`などで登録したハンドラが優先され、マッチしなかったときにマウントポイントが探されます。 +静的ファイルとAPIハンドラは共存できます。GETとHEADでは先にマウントポイントのファイルが探され、見つからなかったときに`Get()`などで登録したハンドラが呼ばれます。 ```cpp svr.Get("/api/users", [](const auto &req, auto &res) { diff --git a/docs-src/pages/ja/cookbook/s05-stream-response.md b/docs-src/pages/ja/cookbook/s05-stream-response.md index 4296545..c70b21b 100644 --- a/docs-src/pages/ja/cookbook/s05-stream-response.md +++ b/docs-src/pages/ja/cookbook/s05-stream-response.md @@ -15,14 +15,15 @@ svr.Get("/download", [](const httplib::Request &req, httplib::Response &res) { res.set_content_provider( total_size, "application/octet-stream", [](size_t offset, size_t length, httplib::DataSink &sink) { - auto data = read_range_from_file("large.bin", offset, length); + auto n = std::min(length, size_t(64 * 1024)); + auto data = read_range_from_file("large.bin", offset, n); sink.write(data.data(), data.size()); return true; }); }); ``` -ラムダが呼ばれるたびに`offset`と`length`が渡されるので、その範囲だけ読み込んで`sink.write()`で送ります。メモリには常に少量のチャンクしか載りません。 +ラムダは、送信済みの位置`offset`と残りのバイト数`length`を受け取って繰り返し呼ばれます。1回に読み込む量を自分で区切って`sink.write()`で送れば、メモリには常に少量のチャンクしか載りません。 ## ファイルをそのまま返す diff --git a/docs-src/pages/ja/cookbook/s07-multipart-reader.md b/docs-src/pages/ja/cookbook/s07-multipart-reader.md index bfa16b7..fb2adfe 100644 --- a/docs-src/pages/ja/cookbook/s07-multipart-reader.md +++ b/docs-src/pages/ja/cookbook/s07-multipart-reader.md @@ -96,7 +96,7 @@ svr.Post("/upload", }); ``` -`content_reader`が`false`を返したら、レスポンスのステータスは自分でセットしてください。ボディの残りは読まずに接続を閉じるので、送信中のクライアントには接続が切れたように見えます。 +`content_reader`が`false`を返すと、レスポンスのステータスは400(ボディが上限を超えた場合は413)になります。別のステータスを返したいときは自分でセットしてください。ボディの残りは読まずに接続を閉じるので、送信中のクライアントには接続が切れたように見えます。 > **Warning:** `HandlerWithContentReader`を使うと、`req.body`は**空のまま**です。ボディはコールバック内で自分で処理してください。 diff --git a/docs-src/pages/ja/cookbook/s08-compress-response.md b/docs-src/pages/ja/cookbook/s08-compress-response.md index cf56861..86183c9 100644 --- a/docs-src/pages/ja/cookbook/s08-compress-response.md +++ b/docs-src/pages/ja/cookbook/s08-compress-response.md @@ -32,7 +32,7 @@ svr.Get("/api/data", [](const httplib::Request &req, httplib::Response &res) { ## 圧縮の優先順位 -クライアントが複数の方式を受け入れる場合、Brotli → Zstd → gzipの順に選ばれます(ビルドで有効になっている中から)。クライアント側では気にせず、一番効率の良い方式で圧縮されます。 +クライアントが複数の方式を受け入れる場合、`Accept-Encoding`のq値が最も高い方式が選ばれます。q値が同じなら、Brotli → gzip → Zstdの順です(ビルドで有効になっている中から)。 ## ストリーミングレスポンスも圧縮される diff --git a/docs-src/pages/ja/cookbook/s12-user-data.md b/docs-src/pages/ja/cookbook/s12-user-data.md index 0318708..ef705f2 100644 --- a/docs-src/pages/ja/cookbook/s12-user-data.md +++ b/docs-src/pages/ja/cookbook/s12-user-data.md @@ -37,7 +37,7 @@ svr.Get("/me", [](const httplib::Request &req, httplib::Response &res) { ## よくある型 -`std::string`、数値、構造体、`std::shared_ptr`など、コピーかムーブできる値なら何でも入れられます。 +`std::string`、数値、構造体、`std::shared_ptr`など、コピーできる値なら何でも入れられます。 ```cpp res.user_data.set("user_id", std::string{"42"}); diff --git a/docs-src/pages/ja/cookbook/s15-server-logger.md b/docs-src/pages/ja/cookbook/s15-server-logger.md index 8d73273..d430b51 100644 --- a/docs-src/pages/ja/cookbook/s15-server-logger.md +++ b/docs-src/pages/ja/cookbook/s15-server-logger.md @@ -48,7 +48,7 @@ svr.set_pre_routing_handler([](const auto &req, auto &res) { return httplib::Server::HandlerResponse::Unhandled; }); -svr.set_logger([](const auto &req, const auto &res) { +svr.set_logger([](const httplib::Request &req, const httplib::Response &res) { auto *start = res.user_data.get("start"); auto elapsed = start ? std::chrono::duration_cast( diff --git a/docs-src/pages/ja/cookbook/s19-graceful-shutdown.md b/docs-src/pages/ja/cookbook/s19-graceful-shutdown.md index 25a26fd..ff92e37 100644 --- a/docs-src/pages/ja/cookbook/s19-graceful-shutdown.md +++ b/docs-src/pages/ja/cookbook/s19-graceful-shutdown.md @@ -52,6 +52,6 @@ int main() { ## 処理中のリクエストの扱い -`stop()`を呼ぶと、新しい接続は受け付けなくなりますが、すでに処理中のリクエストは**最後まで実行**されます。その後、スレッドプールのワーカーが順次終了し、`listen()`から戻ってきます。これがグレースフルシャットダウンと呼ばれる理由です。 +`stop()`を呼ぶと、新しい接続は受け付けなくなりますが、すでに実行中のハンドラは**最後まで実行**されます。ただし、コンテンツプロバイダで送信中のレスポンス(ストリーミングなど)はその場で打ち切られます。その後、スレッドプールのワーカーが順次終了し、`listen()`から戻ってきます。これがグレースフルシャットダウンと呼ばれる理由です。 > **Warning:** `stop()`を呼んでから`listen()`が戻るまでには、処理中のリクエストが終わるのを待つ時間がかかります。タイムアウトを強制したい場合は、シャットダウン用のタイマーを別途用意するなど、アプリケーション側の工夫が必要です。 diff --git a/docs-src/pages/ja/cookbook/s22-unix-socket.md b/docs-src/pages/ja/cookbook/s22-unix-socket.md index 998c1ed..cc6d0c8 100644 --- a/docs-src/pages/ja/cookbook/s22-unix-socket.md +++ b/docs-src/pages/ja/cookbook/s22-unix-socket.md @@ -19,7 +19,7 @@ svr.Get("/", [](const auto &, auto &res) { svr.listen("/tmp/httplib.sock", 80); ``` -`set_address_family(AF_UNIX)`を呼んでから、`listen()`の第1引数にソケットファイルのパスを渡します。第2引数のポート番号は使われませんが、シグネチャの都合で何か渡す必要があります。 +`set_address_family(AF_UNIX)`を呼んでから、`listen()`の第1引数にソケットファイルのパスを渡します。第2引数のポート番号は使われませんが、シグネチャの都合で`0`以外の値を渡す必要があります(`0`だと`listen()`が失敗します)。 ## クライアント側 diff --git a/docs-src/pages/ja/cookbook/t04-mtls.md b/docs-src/pages/ja/cookbook/t04-mtls.md index 2c2e5ae..e2b7ba9 100644 --- a/docs-src/pages/ja/cookbook/t04-mtls.md +++ b/docs-src/pages/ja/cookbook/t04-mtls.md @@ -55,7 +55,7 @@ httplib::SSLClient cli("api.example.com", 443, auto res = cli.Get("/"); ``` -`Client`ではなく`SSLClient`を直接使う点に注意してください。秘密鍵にパスワードがある場合は第5引数で渡せます。 +証明書と鍵のパスだけなら、`httplib::Client cli("https://api.example.com", "client-cert.pem", "client-key.pem")`のように`Client`にも渡せます。秘密鍵にパスワードがある場合は`SSLClient`を使い、第5引数で渡します。 クライアント側にも同じ`PemMemory`構造体があり、メモリ上のPEMからクライアント証明書を設定できます。 diff --git a/docs-src/pages/ja/cookbook/w02-websocket-ping.md b/docs-src/pages/ja/cookbook/w02-websocket-ping.md index c523f72..285f029 100644 --- a/docs-src/pages/ja/cookbook/w02-websocket-ping.md +++ b/docs-src/pages/ja/cookbook/w02-websocket-ping.md @@ -67,7 +67,7 @@ cli.set_websocket_max_missed_pongs(2); // 2回連続でPongが返ってこなけ サーバー側にも同じ`set_websocket_max_missed_pongs()`があります。 -たとえばPing間隔が30秒で`max_missed_pongs = 2`なら、無応答のピアは約60秒で検出され、`CloseStatus::GoingAway`(理由は`"pong timeout"`)で接続が閉じられます。 +たとえばPing間隔が30秒で`max_missed_pongs = 2`なら、無応答のピアは応答が止まってから60〜90秒で検出され、`CloseStatus::GoingAway`(理由は`"pong timeout"`)で接続が閉じられます。そのとき`read()`で待っていた呼び出しは`Fail`を返します。 この仕組みは`read()`を呼んでPongフレームを消費したタイミングでカウンタがリセットされます。つまり通常のWebSocketクライアントのように`read()`をループで回していれば、特に意識することなく動きます。 diff --git a/docs-src/pages/ja/llm-app/ch02-rest-api.md b/docs-src/pages/ja/llm-app/ch02-rest-api.md index b993fdf..351ec8f 100644 --- a/docs-src/pages/ja/llm-app/ch02-rest-api.md +++ b/docs-src/pages/ja/llm-app/ch02-rest-api.md @@ -18,10 +18,6 @@ llama.cppのAPIを直接扱うとコードが長くなるので、薄いラッ int main() { auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf"}; - // LLM推論は時間がかかるのでタイムアウトを長めに設定(デフォルトは5秒) - svr.set_read_timeout(300); - svr.set_write_timeout(300); - // ... HTTPサーバーの構築・起動 ... } ``` @@ -78,7 +74,7 @@ svr.Post("/translate", }); ``` -`llm.chat()`は推論中に例外を投げることがあります(コンテキスト長の超過など)。`try/catch`で捕捉してエラーをJSONで返すことで、サーバーがクラッシュするのを防ぎます。 +`llm.chat()`は推論中に例外を投げることがあります(コンテキスト長の超過など)。`try/catch`で捕捉して、エラーの内容をJSONで返します。捕捉しなくてもcpp-httplibが500を返しますが、原因はクライアントに伝わりません。 ## 2.3 全体のコード @@ -111,10 +107,6 @@ int main() { // 1章でダウンロードしたモデルをロード auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf"}; - // LLM推論は時間がかかるのでタイムアウトを長めに設定(デフォルトは5秒) - svr.set_read_timeout(300); - svr.set_write_timeout(300); - // リクエストとレスポンスをログに記録 svr.set_logger([](const auto &req, const auto &res) { std::cout << req.method << " " << req.path << " -> " << res.status diff --git a/docs-src/pages/ja/llm-app/ch03-sse-streaming.md b/docs-src/pages/ja/llm-app/ch03-sse-streaming.md index 37fec2a..85cf4f1 100644 --- a/docs-src/pages/ja/llm-app/ch03-sse-streaming.md +++ b/docs-src/pages/ja/llm-app/ch03-sse-streaming.md @@ -73,7 +73,7 @@ svr.Post("/translate/stream", - `sink.os`に書き込んだ後、`sink.os.good()`でクライアントがまだ接続しているかを確認できます。切断されていたら`false`を返して推論を止めます - 各トークンは`json(token).dump()`でJSON文字列としてエスケープしてから送ります。改行やクォートを含むトークンでも安全です - `dump(-1, ' ', false, ...)`の最初の3つの引数はデフォルトと同じです。重要なのは第4引数の`json::error_handler_t::replace`です。LLMはトークンをサブワード単位で返すため、マルチバイト文字(日本語など)の途中でトークンが切れることがあります。不完全なUTF-8バイト列をそのまま`dump()`に渡すと例外が飛ぶので、`replace`で安全に置換します。ブラウザ側で結合されるため、表示上の問題はありません -- `try/catch`でラムダ全体を囲んでいます。`llm.chat()`はコンテキストウィンドウの超過などで例外を投げることがあります。ラムダ内で例外が未捕捉だとサーバーがクラッシュするので、エラーをSSEイベントとして返します +- `try/catch`でラムダ全体を囲んでいます。`llm.chat()`はコンテキストウィンドウの超過などで例外を投げることがあります。ラムダ内で例外が未捕捉だと、cpp-httplibは接続を切るだけでエラーの内容がクライアントに伝わらないので、エラーをSSEイベントとして返します - `data: [DONE]`はOpenAI APIと同じ慣習で、ストリームの終了をクライアントに伝えます ## 3.4 全体のコード @@ -107,10 +107,6 @@ int main() { // GGUFモデルをロード auto llm = llamalib::Llama{"models/gemma-2-2b-it-Q4_K_M.gguf"}; - // LLM推論は時間がかかるのでタイムアウトを長めに設定(デフォルトは5秒) - svr.set_read_timeout(300); - svr.set_write_timeout(300); - // リクエストとレスポンスをログに記録 svr.set_logger([](const auto &req, const auto &res) { std::cout << req.method << " " << req.path << " -> " << res.status diff --git a/docs-src/pages/ja/llm-app/ch04-model-management.md b/docs-src/pages/ja/llm-app/ch04-model-management.md index 0436ecc..2a00ee8 100644 --- a/docs-src/pages/ja/llm-app/ch04-model-management.md +++ b/docs-src/pages/ja/llm-app/ch04-model-management.md @@ -219,7 +219,6 @@ bool download_model(const ModelInfo &model, std::function progress_cb) { httplib::Client cli("https://huggingface.co"); cli.set_follow_location(true); - cli.set_read_timeout(std::chrono::hours(1)); auto url = "/" + model.repo + "/resolve/main/" + model.filename; auto path = get_models_dir() / model.filename; @@ -469,7 +468,6 @@ bool download_model(const ModelInfo &model, std::function progress_cb) { httplib::Client cli("https://huggingface.co"); cli.set_follow_location(true); // Hugging FaceはCDNにリダイレクトする - cli.set_read_timeout(std::chrono::hours(1)); // 大きなモデルに備えて長めに auto url = "/" + model.repo + "/resolve/main/" + model.filename; auto path = get_models_dir() / model.filename; @@ -539,10 +537,6 @@ int main() { auto llm = llamalib::Llama{path}; std::mutex llm_mutex; // モデル切り替え中のアクセスを保護する - // LLM推論は時間がかかるのでタイムアウトを長めに設定(デフォルトは5秒) - svr.set_read_timeout(300); - svr.set_write_timeout(300); - svr.set_logger([](const auto &req, const auto &res) { std::cout << req.method << " " << req.path << " -> " << res.status << std::endl; diff --git a/docs-src/pages/ja/llm-app/ch05-web-ui.md b/docs-src/pages/ja/llm-app/ch05-web-ui.md index 1f6652f..66171d5 100644 --- a/docs-src/pages/ja/llm-app/ch05-web-ui.md +++ b/docs-src/pages/ja/llm-app/ch05-web-ui.md @@ -939,7 +939,6 @@ bool download_model(const ModelInfo &model, std::function progress_cb) { httplib::Client cli("https://huggingface.co"); cli.set_follow_location(true); // Hugging FaceはCDNにリダイレクトする - cli.set_read_timeout(std::chrono::hours(1)); // 大きなモデルに備えて長めに auto url = "/" + model.repo + "/resolve/main/" + model.filename; auto path = get_models_dir() / model.filename; @@ -1008,10 +1007,6 @@ int main() { } auto llm = llamalib::Llama{path}; - // LLM推論は時間がかかるのでタイムアウトを長めに設定(デフォルトは5秒) - svr.set_read_timeout(300); - svr.set_write_timeout(300); - svr.set_logger([](const auto &req, const auto &res) { std::cout << req.method << " " << req.path << " -> " << res.status << std::endl; diff --git a/docs-src/pages/ja/llm-app/ch06-desktop-app.md b/docs-src/pages/ja/llm-app/ch06-desktop-app.md index ebdedd6..f97fb9f 100644 --- a/docs-src/pages/ja/llm-app/ch06-desktop-app.md +++ b/docs-src/pages/ja/llm-app/ch06-desktop-app.md @@ -407,7 +407,6 @@ bool download_model(const ModelInfo &model, std::function progress_cb) { httplib::Client cli("https://huggingface.co"); cli.set_follow_location(true); // Hugging FaceはCDNにリダイレクトする - cli.set_read_timeout(std::chrono::hours(1)); // 大きなモデルに備えて長めに auto url = "/" + model.repo + "/resolve/main/" + model.filename; auto path = get_models_dir() / model.filename; @@ -469,10 +468,6 @@ int main() { auto llm = llamalib::Llama{path}; std::mutex llm_mutex; // モデル切り替え中のアクセスを保護する - // LLM推論は時間がかかるのでタイムアウトを長めに設定(デフォルトは5秒) - svr.set_read_timeout(300); - svr.set_write_timeout(300); - svr.set_logger([](const auto &req, const auto &res) { std::cout << req.method << " " << req.path << " -> " << res.status << std::endl; diff --git a/docs-src/pages/ja/llm-app/ch07-code-reading.md b/docs-src/pages/ja/llm-app/ch07-code-reading.md index 57e0e14..e9ce3ac 100644 --- a/docs-src/pages/ja/llm-app/ch07-code-reading.md +++ b/docs-src/pages/ja/llm-app/ch07-code-reading.md @@ -11,13 +11,17 @@ order: 7 ## 7.1 ソースコードの場所 ```ascii -llama.cpp/tools/server/ -├── server.cpp # メインのサーバー実装 -├── httplib.h # cpp-httplib(同梱版) -└── ... +llama.cpp/ +├── tools/server/ +│ ├── server.cpp # エントリポイント +│ ├── server-http.cpp # HTTPサーバー(cpp-httplibを使う層) +│ ├── server-context.cpp # 推論とスロットの管理 +│ └── ... +└── vendor/cpp-httplib/ + └── httplib.h # cpp-httplib(同梱版) ``` -ファイルは1つの`server.cpp`にまとまっています。数千行ありますが、構造を知っていれば読むべき箇所は絞れます。 +実装は役割ごとに複数のファイルに分かれています。全体の規模は大きいですが、構造を知っていれば読むべき箇所は絞れます。 ## 7.2 OpenAI互換API diff --git a/docs-src/pages/ja/tour/02-basic-client.md b/docs-src/pages/ja/tour/02-basic-client.md index f262b08..434b3af 100644 --- a/docs-src/pages/ja/tour/02-basic-client.md +++ b/docs-src/pages/ja/tour/02-basic-client.md @@ -200,8 +200,8 @@ auto res = cli.Post("/submit", httplib::Params{ }); if (res) { std::cout << res->body << std::endl; - // age = 30 // name = Alice + // age = 30 } ``` @@ -241,7 +241,7 @@ auto res = cli.Get("/hi"); if (!res) { // 接続エラー std::cout << "Error: " << httplib::to_string(res.error()) << std::endl; - // Error: Connection + // Error: Could not establish connection return 1; } diff --git a/docs-src/pages/ja/tour/04-static-file-server.md b/docs-src/pages/ja/tour/04-static-file-server.md index 95ac897..bf63476 100644 --- a/docs-src/pages/ja/tour/04-static-file-server.md +++ b/docs-src/pages/ja/tour/04-static-file-server.md @@ -95,7 +95,7 @@ svr.set_mount_point("/", "./public"); svr.listen("0.0.0.0", 8080); ``` -ハンドラーが先に評価されます。`/api/hello` にはハンドラーが応答し、それ以外のパスは `./public` ディレクトリからファイルを探します。 +先に`./public`ディレクトリのファイルが探され、見つからなければハンドラーが呼ばれます。`./public/api/hello`というファイルを置かない限り、`/api/hello`にはハンドラーが応答します。 ## レスポンスヘッダーの追加 diff --git a/docs-src/pages/ja/tour/09-whats-next.md b/docs-src/pages/ja/tour/09-whats-next.md index 4554cd8..0c24452 100644 --- a/docs-src/pages/ja/tour/09-whats-next.md +++ b/docs-src/pages/ja/tour/09-whats-next.md @@ -50,7 +50,7 @@ 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を返すと終了 + return true; // 終了するときはsink.done()を呼ぶ }); }); ``` @@ -155,7 +155,7 @@ svr.set_pre_routing_handler([](const auto &req, auto &res) { }); svr.set_post_routing_handler([](const auto &req, auto &res) { - // レスポンスが返された後に実行される + // レスポンスを送信する直前に実行される res.set_header("X-Server", "cpp-httplib"); }); ``` @@ -168,7 +168,7 @@ svr.set_pre_routing_handler([](const auto &req, auto &res) { return httplib::Server::HandlerResponse::Unhandled; }); -svr.Get("/me", [](const auto &req, auto &res) { +svr.Get("/me", [](const httplib::Request &req, httplib::Response &res) { auto *user = res.user_data.get("auth_user"); res.set_content("Hello, " + *user, "text/plain"); }); @@ -205,7 +205,7 @@ TCP以外に、Unix Domain Socketでの通信にも対応しています。同 // サーバー httplib::Server svr; svr.set_address_family(AF_UNIX); -svr.listen("/tmp/httplib.sock", 0); +svr.listen("/tmp/httplib.sock", 80); // ポート番号は使われない(0以外を渡す) ``` ```cpp