Add streamed response callback with chunked transfer support#1151
Add streamed response callback with chunked transfer support#1151Kirpatik wants to merge 1 commit into
Conversation
|
Sounds great. :) |
|
The current implementation would block one working thread per running client request, right? |
Yes. do_write_streamed() is called synchronously from complete_request() and performs blocking asio::write calls in a loop, so one worker thread stays occupied by that request until the stream finishes. |
|
We have a concrete use case for this feature in a C++ real-time imaging application. Our HTTP server exposes a long-lived MJPEG endpoint using: Each connected client waits for a newly produced JPEG frame and then sends the multipart header and image bytes. We currently use cpp-httplib because its chunked content provider supports this model. We evaluated Crow 1.3.3, but response::write() buffers data until response::end(), so it cannot implement this endpoint. The unknown-length set_streamed_body() API proposed here appears to provide exactly the missing functionality. For our expected deployment, the blocking implementation may be acceptable because the number of simultaneous MJPEG viewers is small. However, cancellation when a client disconnects and clean server shutdown are important to us. Is this API still being considered for Crow? We would also be interested in testing the branch with our MJPEG workload and reporting the results. |
Summary
This PR adds explicit streamed response sending via callback, including support for unknown total size responses.
Added API
set_streamed_body(std::function<size_t(void*, size_t)> reader, size_t content_length, std::string content_type = "", size_t chunk_size = 16 * 1024)set_streamed_body(std::function<size_t(void*, size_t)> reader, std::string content_type = "", size_t chunk_size = 16 * 1024)Callback contract:
(buffer, max_size)max_sizebytes0to indicate end-of-streamBehavior
Transfer-Encoding: chunked.chunk_sizeis configurable per response; default remains 16KB.HEADwith known-length streamed responses sends headers only and preservesContent-Length.Implementation Notes
Connection::do_write_streamed().Tests
Added/updated tests:
streamed_responsestreamed_response_unknown_size_chunkedstreamed_response_unknown_size_http10_fallbackstreamed_response_head_known_length_no_body