C Collections

chttp · Section 3

chttp_basic_auth

chttp_basic_auth - build an HTTP Basic auth header value (default allocator)

C Collections (libccollections, -lccollections)

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

This is a static inline wrapper that calls chttp_basic_auth_mp() with
NULL memory management procedures, so it allocates with malloc.
 
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.

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_mp(3), chttp_base64_encode(3)