C Collections

chttpserver · Section 3

chttpsvr_resp_write

chttpsvr_resp_write - append bytes to the response body

C Collections (libccollections, -lccollections)

#include <ccollections/chttpserver.h>
 
ccol_retval chttpsvr_resp_write(chttpsvr_resp *resp,
                                   const void *data, size_t len);

Appends len bytes of data to the internal buffer of the response
body.  The buffer grows as needed, and each reallocation at least
doubles the capacity, with a guard that keeps the size computation
from wrapping around a size_t.  When len plus the current body length
would overflow a size_t, or when the allocator returns NULL, the
function returns ccol_not_enough_memory.

ccol_success on success.
ccol_invalid_args when resp is NULL, or when data is NULL and len is
                  above 0.
ccol_not_enough_memory when the body length would overflow a size_t,
                       or when the allocator cannot serve the request.

For a CHTTP_HEAD request where the handler wrote a body, the server
puts the real collected body length in the Content-Length response
header, which is the value a GET would report, as RFC 7231 4.3.2
requires.  If a HEAD handler writes no body, the response carries the
Content-Length that the handler set with chttpsvr_resp_set_header(3),
or none.  The body bytes themselves never go on the wire for such a
request, so a handler that CHTTP_HEAD can reach, explicitly or through
a CHTTP_ANY registration, doesn't have to special-case the method
before it calls this function.
 
The server drops the body in the same way, for any request method,
when the final status code of the response is 204, 205 or 304, since
RFC 9110 6.4.1, 15.3.6 and 15.4.5 forbid a body on all three.  The
bytes you write with this function for those codes are held back from
the wire as well, without any error.  A 205 carries
"content-length:0", a 204 carries no Content-Length, and a 304
carries the Content-Length that the handler set with
chttpsvr_resp_set_header(3), or none.  See chttpsvr_resp_set_status(3).

chttpsvr_resp_write_str(3), chttpsvr_resp_printf(3), chttpsvr_resp_write_json(3)