C Collections

chttpserver · Section 3

chttpsvr_req_read

chttpsvr_req_read - read body bytes as they arrive (streaming handlers)

C Collections (libccollections, -lccollections)

#include <ccollections/chttpserver.h>
 
ssize_t chttpsvr_req_read(chttpsvr_req *req, void *buf, size_t buflen);

Reads up to buflen bytes of the request body into buf, batch by
batch, as they arrive on the connection.  The call reads the socket
itself on the streaming thread that runs the handler, and the reactor
thread never does this read.  It blocks that thread until at least
one byte is available, the body ends, an error occurs,
chttpsvr_config.stream_read_timeout_us expires with no new data,
chttpsvr_config.max_body_read_duration_us expires (when that field
is set), or the average rate of the body since the first call falls
below chttpsvr_config.min_transfer_rate_bps after
chttpsvr_config.min_transfer_rate_grace_us has passed.
 
Return values:
  > 0   The number of bytes copied into buf.
    0   The body has ended, or buflen is 0.  A buflen of 0 is valid,
        and the call then does nothing.
   -1   One of seven things happened: req is NULL; buf is NULL while
        buflen is above 0; the handler was registered as a buffered
        handler with chttpsvr_register_handler and not with
        chttpsvr_register_streaming_handler; a middleware called it
        for a request that the library itself rejects (404, 405,
        or a 413 found at the headers) and that matched no route; the
        connection dropped or was reset; stream_read_timeout_us or
        max_body_read_duration_us expired, or the rate floor ended
        the read; or the body went past max_body_size in the middle
        of the stream.  The last three are real transfer errors that
        depend on the data, and chttpsvr_req_stream_error, called
        right after this call, tells them apart.  For a NULL buf and
        for the wrong kind of handler that function reports
        ccol_success, which you can't tell apart from "no error";
        both are plain programming errors that happen on every call
        of the faulty code.
 
With a buflen of 0 the function returns 0 at once, whatever buf
holds, so a NULL buf with a buflen of 0 is valid: The call returns 0
and does nothing, as POSIX read(2) does.  buf must be non-NULL only
when buflen is above 0.
 
A call returns the body bytes that the connection already has, up to
buflen, and waits only when there are none.  It reads the socket at
most once for that, and one read brings at most 8 KiB of body, so a
call can return far fewer bytes than buflen while more of the body is
still on the way.  As with read(2), only a return of 0 means that the
body has ended.
 
This function is for a streaming handler registered with
chttpsvr_register_streaming_handler.  A buffered handler uses
chttpsvr_req_body.
 
The first call of this function for a request writes the interim
"100 Continue" response when the request carried
"Expect: 100-continue" and really has a body, which means a
Content-Length or a chunked Transfer-Encoding.  That interim response tells the client
to go ahead and upload the body, and it goes out before the function
tries to read anything.  A streaming handler can instead refuse the
request (for bad authentication or an unacceptable Content-Type, for
example) by writing a final response and never calling this function.
The server then sends no interim response at all, so the client gets
the real rejection directly, exactly as if it had not sent
"Expect: 100-continue", and it is never asked to upload a body that
the server wasn't going to read.  A request with "Expect: 100-continue" and no
body at all gets no interim response either, since there is nothing
to upload.
 
The trailer fields of a chunked request body (RFC 7230 4.1.2) are
parsed and checked on the way to the end of the body and then thrown
away.  They are not request headers, and chttpsvr_req_header never
returns one; that page explains why.

chttpsvr_req_body(3), chttpsvr_req_header(3), chttpsvr_req_stream_error(3), chttpsvr_register_streaming_handler(3)