C Collections

chttp · Section 3

chttp_basic_auth_mp

chttp_basic_auth_mp - build an HTTP Basic auth header value with a custom allocator

C Collections (libccollections, -lccollections)

#include <ccollections/chttp.h>
 
char *chttp_basic_auth_mp(ccol_memmgmt_procs *mp,
                          const char *username, const char *password);

This function builds a "Basic <base64(username:password)>" header
value as RFC 7617 describes it.  It makes only the header VALUE and
not the "Authorization: " part, so you can pass the result straight
to chttp_request_set_header(req, "authorization", ...) or to the
matching function of chttpsvr.
 
RFC 7617 SS2 makes the colon the only delimiter of the encoded
"user-id:password" string, and the recipient splits that string at
its FIRST colon.  A username with a colon in it cannot be sent
without ambiguity, so the function rejects it.  If it did not, the
username "a:b" with the password "c" would encode to the same bytes
as the username "a" with the password "b:c", and a caller who asks
for one identity would send another.  A colon in the password needs
no escape, and the function builds the string as you ask.
 
RFC 7617 SS2 also forbids a control character (a byte from 0x00 to
0x1F, or 0x7F) in both the user-id and the password, so the function
refuses either one if it holds such a byte.  Every other byte, UTF-8
included, is encoded as it is.

mp        A custom allocator, or NULL for malloc/free.
username  The username. It must not be NULL and must not contain a
          colon (':') or a control character, but it can be empty.
password  The password. It must not be NULL and must not contain a
          control character, but it can be empty and can contain
          colons.

A new "Basic <base64>" string on success.
NULL if the allocation fails, if username or password is NULL, if
username contains a colon, or if username or password contains a
control character.

chttp_basic_auth(3), chttp_base64_encode_mp(3), chttp_request_set_header(3)