C Collections

chttpclient · Section 3

chttpcli_resp_header_at

chttpcli_resp_header_at - get one occurrence of a response header

C Collections (libccollections, -lccollections)

#include <ccollections/chttpclient.h>
 
const char *chttpcli_resp_header_at(const chttpcli_response *resp,
                                    const char *name, size_t index);

This function returns the value of one line of the response header
block that carries the given name, compared without regard to case.
index counts the lines with that name from 0, in the order the server
sent them. The value is always the value of that one line exactly as
the server sent it, never a combined value.
 
chttpcli_resp_header_count tells you how many lines carry a name,
and chttpcli_resp_header returns the combined value of a repeated
name, or the first value of a repeated Set-Cookie.
 
The returned pointer stays valid until chttpcli_resp_free is
called.

resp   The response to query. It can be NULL, in which case the
       function returns NULL.
name   The header name. It can be NULL, in which case the function
       returns NULL.
index  The occurrence to return, from 0 to
       chttpcli_resp_header_count(resp, name) - 1.

A pointer to the value string of that occurrence, or NULL when index
is not below the number of lines with that name.

Only the response's header block counts. A trailer field of a chunked
body is not a response header, and this function never returns one.

size_t n = chttpcli_resp_header_count(resp, "Set-Cookie");
for (size_t i = 0; i < n; i++)
    printf("cookie: %s\n",
           chttpcli_resp_header_at(resp, "Set-Cookie", i));

chttpcli_resp_header_count(3), chttpcli_resp_header(3), chttpcli_resp_free(3)