chttpclient · Section 3
chttp_perform
NAME
chttp_perform - run one request with explicit headers, with the default client
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/chttpclient.h>
ccol_retval chttp_perform(chttp_method method, const char *url,
const chttp_request_body *body,
chmap headers,
chttpcli_response **resp_out);
DESCRIPTION
This is a convenience wrapper that builds a chttp_request internally, attaches headers to it without taking ownership, calls chttp_do_default and then frees the request it built. Of the convenience functions for the default client, this is the only one that takes a header set of your choice in a single call. With chttp_get, chttp_post and the others you have to build a chttp_request yourself, with chttp_request_create followed by one chttp_request_set_header call per header. The caller keeps full ownership of headers. The function only borrows the map for the duration of the call, and never frees or changes it. The keys in headers can be in any case, such as "Host", "Content-Type" or "Accept". The module adds its own default value for some keys (host, accept, user-agent, content-length, content-type, authorization and expect) and looks each of them up in headers without regard to case, so one of your headers in its natural case replaces the module's default for it, and the two never both go onto the wire. A chmap compares keys by their exact bytes, so headers can hold two keys that differ as bytes but name the same header under HTTP's case-insensitive rules, such as "Host" and "host" together. chttp_request_set_header lowercases every key before storing it, so it can never create that situation, but a headers map you build yourself gives no such guarantee. Only one key of such a pair reaches the wire, never both: The key that went into headers last wins, the same as an update of one exact key. The client drops the other one instead of sending it as a second header line. Because you pass headers directly, they skip the validation of chttp_request_set_header. Every key must be a token, and no value may hold a byte below 0x20 other than a horizontal tab (CR and LF among them), or 0x7F. Those are the rules of chttp_request_set_header, for the same reason: Data with a CRLF in it could otherwise inject extra header lines or split the request in two. For such a headers map the function returns ccol_invalid_args before it opens any connection. headers must not hold a "Transfer-Encoding" key either, in any case, for the reason chttp_request_set_header rejects that name: This client never applies a transfer coding to a request body, so it cannot honor such a header, and on the wire it would sit next to the module's automatic Content-Length over a body with no transfer coding, which is ambiguous framing. The function DOES accept a "Content-Length" key in headers, but checks it on a request that carries a body (POST, PUT or PATCH). A valid value is a plain unsigned string of decimal digits, with no leading '+' or '-' and no whitespace in it, and it must also match the real length of the body that the client sends. A bad value gives ccol_invalid_args once the request reaches the wire. chttpcli_do(3) and chttp_request_set_header(3) describe the same check, which exists for the same reason: The declared framing and the wire must not disagree.
PARAMETERS
method The HTTP method: One of CHTTP_GET to CHTTP_OPTIONS.
CHTTP_ANY and values outside the enum are refused.
url The target URL. It must not be NULL.
body The request body, or NULL for a request with no body.
headers A chmap(char* -> char*) of the request headers, or NULL for
no headers. The function borrows it for the duration of
the call and neither consumes nor frees it. Every key must
be a token, no value may hold a byte below 0x20 other than
a horizontal tab, or 0x7F, and no key may be
"Transfer-Encoding".
resp_out It must not be NULL. The function sets it to NULL first,
and stores the pointer to the response there if the call
succeeds.
RETURN VALUE
The same codes as chttpcli_do. It also returns ccol_not_enough_memory when the internal request object cannot be allocated, and ccol_invalid_args when url or resp_out is NULL, when method names no HTTP method (CHTTP_ANY included), when body has a NULL data with a len that is not zero, when headers holds a key that is not a token or a value with a control byte other than a horizontal tab (CR and LF among them) or 0x7F, or when headers holds a "Transfer-Encoding" key.
SEE ALSO
chttp_do_default(3), chttp_get(3), chttp_post(3), chttp_request_create(3), chttp_request_set_header(3), chttpcli_do(3)