chttpclient · Section 3
chttp_request_create_mp
NAME
chttp_request_create_mp - allocate and initialize an HTTP request with a custom allocator
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/chttpclient.h>
chttp_request *chttp_request_create_mp(chttp_method method,
const char *url,
const chttp_request_body *body,
ccol_memmgmt_procs *mprocs,
char **err_str);
DESCRIPTION
This function allocates a new chttp_request. It copies the URL into a buffer that the request owns, and does the same with the body data and the content_type string when they are not NULL, so the caller can free its own originals as soon as this call returns. The request's method, expect_continue and prevent_tls_downgrade_on_redirect fields are public to read and to write. The url and body fields are public to read only, because the request owns the copies they point to: This function allocates them with mprocs and chttp_request_free frees them with mprocs. Do not assign to url or to any member of body after construction; build another request when you need another URL or another body. Pass NULL for body, or use the CHTTP_NO_BODY macro, when the request has no body, as with GET and DELETE. body->data can be NULL only when body->len == 0. A NULL body->data with a body->len that is not zero is rejected and the function returns NULL, instead of taking the pair as a request with no body. Only CHTTP_POST, CHTTP_PUT and CHTTP_PATCH send a body. With any other method the client never sends the body, and sends no Content-Length and no Content-Type either. A POST, PUT or PATCH without a body goes out with "Content-Length: 0". The expect_continue field of a new request is false, since the library zero-fills the struct. If you set it to true after construction (for example req->expect_continue = true), the client sends "Expect: 100-continue" and waits for the server's interim response before it sends the body; chttpcli_do(3) describes the full protocol. The field has no effect on a request without a body, or on a request that already has an explicit "Expect" header from chttp_request_set_header. Every tier honors it the same way: chttpcli_do and chttpcli_do_streaming, chttpcli_do_async and chttpcli_do_async_streaming, and chttpcli_do_pooled and chttpcli_do_pooled_streaming.
PARAMETERS
method The HTTP method (CHTTP_GET, CHTTP_POST and so on). The
function refuses CHTTP_ANY, which is a server route
wildcard, and every value outside the enum.
url The target URL. It must not be NULL. The function copies it
into the request.
body A pointer to a chttp_request_body, or NULL for no body.
mprocs A custom allocator, or NULL for malloc/free.
err_str Optional. It gets a static error string if the call fails.
RETURN VALUE
A pointer to the new request, or NULL if the call fails. The call fails for a NULL url, for a method that names no HTTP method (CHTTP_ANY included), for a NULL body->data with a body->len that is not zero, and when an allocation fails.
NOTES
Every allocation goes through mprocs when mprocs is not NULL. The request must stay alive until every chttpcli_do call that uses it has returned, because a synchronous call like that reads the request's method, body and headers directly. body->content_type is copied here as it is, without a check, because at this point the function cannot know whether that value will ever reach the wire: An explicit "content-type" header that you set later with chttp_request_set_header takes its place. If body->content_type holds a CR or an LF byte when the request is serialized, or any other byte below 0x20 except a horizontal tab, or 0x7F, the chttpcli_do, chttp_perform or other call that uses this request fails with ccol_invalid_args, and the value is never written to the wire. chttp_request_set_header(3) explains why.
SEE ALSO
chttp_request_create(3), chttp_request_set_header(3), chttp_request_free(3), chttpcli_do(3)