chttpserver · Section 3
chttpsvr_register_streaming_handler
NAME
chttpsvr_register_streaming_handler - register a streaming handler on the root router
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/chttpserver.h>
ccol_retval chttpsvr_register_streaming_handler(chttpsvr srv,
chttp_method method,
const char *pattern,
chttpsvr_handler_fn fn, void *ctx);
DESCRIPTION
Like chttpsvr_register_handler, but the route is marked as a streaming route, which makes chttpsvr_req_read available to the handler so that it can read the body one piece at a time. On a route that is not a streaming route chttpsvr_req_read returns -1. A streaming handler reads its body at the pace of the client and can keep its thread for as long as the client takes. So it runs on a pool of its own with chttpsvr_config.streaming_thread_count threads, and never on the reactor thread or on the worker pool of the buffered routes, which means slow streaming clients can take up at most that pool. The server creates the pool when the first streaming request arrives. Its threads count as workers of the server, so a call that a handler must not make on its own server, such as chttpsvr_destroy() or a restart, is refused or fatal there exactly as in a buffered handler. If chttpsvr_config.streaming_thread_count is CHTTPSVR_STREAMING_POOL_OFF, streaming handlers run on the worker pool instead; chttpsvr_start(3) describes what that changes. CHTTP_ANY as method matches every HTTP method on the pattern; see chttpsvr_register_handler for the full rules of CHTTP_ANY. The server routes the request as soon as the headers are parsed, before it reads a single body byte, and the worker then reads the body by itself, batch by batch, straight from the socket. The reactor thread never reads it. chttpsvr_req_read hands each batch to the handler as it arrives. Once the body has ended it returns 0, and every later call returns 0 as well. It returns -1 for a dropped connection, for a body above max_body_size and for a client that stalled. chttpsvr_config.stream_read_timeout_us bounds each gap and chttpsvr_config.max_body_read_duration_us, when it is set, bounds the whole read; chttpsvr_req_stream_error tells these causes apart. chttpsvr_config.min_transfer_rate_bps also ends the read, with ccol_timed_out, of a client whose average rate falls below that floor. A streaming request that finds every streaming thread busy waits in a queue of chttpsvr_config.streaming_queue_capacity entries, and it holds no thread while it waits. When that queue is full, or when a request has waited there longer than chttpsvr_config.streaming_queue_timeout_us, the server answers at once with 503 Service Unavailable and "Retry-After: 5", and the handler never runs for that request. None of this blocks the reactor thread. The pattern rules of chttpsvr_register_handler apply here too: pattern must start with '/', a pattern with two consecutive slashes or a trailing slash is rejected, a {name} segment must contain only [A-Za-z0-9_], and the same {name} must not appear more than once in one pattern. chttpsvr_register_handler has the full details of the pattern checks and of how a path segment is decoded.
RETURN VALUE
ccol_success on success.
ccol_invalid_args when srv is CHTTPSVR_NULL or a stale handle that
was destroyed; when method is neither one of the
seven methods nor CHTTP_ANY; when pattern or fn is
NULL; when pattern does not start with '/', has two
consecutive slashes or has a trailing slash; when a
segment of the form {name} contains a character
outside [A-Za-z0-9_]; or when the same {name}
appears more than once.
ccol_not_enough_memory when an allocation fails.
SEE ALSO
chttpsvr_register_handler(3), chttpsvr_req_read(3), chttpsvr_req_context(3), CHTTPSVR_CONFIG_DEFAULT(3)