You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README20.md
+36-14Lines changed: 36 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
# cpp-httplib C++20 Streaming API
2
2
3
-
This document describes the C++20 streaming extensions for cpp-httplib, providing a generator-like API for handling HTTP responses incrementally.
3
+
This document describes the C++20 streaming extensions for cpp-httplib, providing a generator-like API for handling HTTP responses incrementally with **true socket-level streaming**.
4
4
5
5
## Overview
6
6
7
-
The C++20 streaming API allows you to process HTTP response bodies chunk by chunk using C++20 coroutines, similar to Python's generators or C++23's `std::generator`. This is particularly useful for:
7
+
The C++20 streaming API allows you to process HTTP response bodies chunk by chunk using C++20 coroutines, similar to Python's generators or C++23's `std::generator`. Data is read directly from the network socket, enabling low-memory processing of large responses. This is particularly useful for:
The `StreamHandle` struct provides direct control over streaming responses.
45
+
The `StreamHandle` struct provides direct control over streaming responses. It takes ownership of the socket connection and reads data directly from the network.
46
+
47
+
> **Note:** When using `open_stream()`, the connection is dedicated to streaming and **Keep-Alive is not supported**. For Keep-Alive connections, use `client.Get()` instead.
46
48
47
49
```cpp
48
-
// Open a stream
50
+
// Open a stream (takes ownership of socket)
49
51
httplib::Client cli("http://localhost:8080");
50
52
auto handle = cli.open_stream("/path");
51
53
@@ -74,7 +76,8 @@ if (handle.is_valid()) {
74
76
| `response` | `std::unique_ptr<Response>` | HTTP response with headers |
| Best for | Small responses | Low-level control | Modern streaming |
268
+
| Best for | Small responses, Keep-Alive | Low-level streaming | Modern streaming |
269
+
270
+
## Features
271
+
272
+
-**True socket-level streaming**: Data is read directly from the network socket
273
+
-**Low memory footprint**: Only the current chunk is held in memory
274
+
-**Compression support**: Automatic decompression for gzip, brotli, and zstd
275
+
-**Chunked transfer**: Full support for chunked transfer encoding
276
+
-**SSL/TLS support**: Works with HTTPS connections
277
+
-**C++23 ready**: `Generator<T>` is compatible with `std::generator` interface
278
+
279
+
## Important Notes
264
280
265
-
##Current Limitations
281
+
### Keep-Alive Behavior
266
282
267
-
> **Note:**The current implementation loads the entire response body into memory before streaming. True socket-level streaming (reading directly from the network) is planned for a future release.
283
+
The streaming API (`GetStream()` / `open_stream()`) takes ownership of the socket connection for the duration of the stream. This means:
268
284
269
-
This means:
285
+
-**Keep-Alive is not supported** for streaming connections
286
+
- The socket is closed when `StreamHandle` is destroyed
287
+
- For Keep-Alive scenarios, use the standard `client.Get()` API instead
270
288
271
-
- Memory usage is similar to `Client::Get()` for now
272
-
- The API is ready for future optimization
273
-
- Useful for header-first processing and chunked iteration patterns
289
+
```cpp
290
+
// Use for streaming (no Keep-Alive)
291
+
auto stream = httplib::GetStream(cli, "/large-stream");
292
+
293
+
// Use for Keep-Alive connections
294
+
auto result = cli.Get("/api/data"); // Connection can be reused
0 commit comments