C Collections

chttpclient · Section 3

chttpcli_set_max_response_body_size

chttpcli_set_max_response_body_size - cap the size of a response body in the buffer

C Collections (libccollections, -lccollections)

#include <ccollections/chttpclient.h>
 
ccol_retval chttpcli_set_max_response_body_size(chttpcli cli,
                                                      size_t max_bytes);

This function caps the size of a buffered response body. The cap works
in all three tiers, so it covers chttpcli_do, chttpcli_do_async
and the pooled-sync wrappers built on chttpcli_do_async, such as
chttpcli_do_pooled, chttp_get, chttp_post, chttp_put, chttp_delete,
chttp_patch and chttp_perform. Pass 0, the default, to buffer with no
limit again.
 
The cap has no effect on chttpcli_do_streaming,
chttpcli_do_async_streaming or chttpcli_do_pooled_streaming. A
streaming caller controls its own memory through the value that its
chttpcli_write_fn returns (a value less than len aborts the transfer),
so on that path there is nothing for this cap to bound.
 
The client enforces the cap in two ways. A response whose
Content-Length header alone declares more than max_bytes fails at
once, before a single byte of the body is read off the wire. A chunked
body, or a body that ends when the connection closes, declares no
length up front, so the client counts it as it arrives and fails the
request as soon as the total so far goes past max_bytes. Both cases
give ccol_msg_too_large: chttpcli_do returns it, the rv of the async
result holds it, and chttpcli_do_pooled returns it. The client never
reuses that connection afterwards and treats it like the connection of
any other malformed response.
 
The cap does not apply to a redirect hop, whatever the declared or real
size of that hop's body, because the client always throws the body of
an intermediate hop away and never buffers it. Only the body of the
final response that the caller receives counts against max_bytes.
 
The up-front check of the declared length only applies to a message
that actually delivers its body to the caller. The declared
Content-Length of a 1xx informational response is exempt: RFC 7230
3.3.2 says that a conforming server never sends one on a 1xx response,
but a misbehaving server or an attacker can, and the client always
drops such a response without passing it to the caller. The declared
Content-Length of a response to HEAD is exempt too, because it
describes what a GET would return (RFC 7231 4.3.2) and no body bytes
follow it.
 
You can call this function before or after the first request. It takes
effect at once for every later request and does not change a request
that is already in flight.

cli       The client handle. It must not be CHTTPCLI_NULL.
max_bytes The maximum size in bytes of a buffered response body.
          0 means no limit.

ccol_success      The client has the new cap.
ccol_invalid_args cli is CHTTPCLI_NULL, or a stale handle of a
                  client that has been destroyed.

chttpcli_do(3), chttpcli_do_streaming(3), chttpcli_do_async(3), chttpcli_do_pooled(3), chttpcli_set_request_timeout(3)