khttp_head
— emit
one HTTP header for kcgi
#include
<sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
enum kcgi_err
khttp_head
(struct kreq *req,
const char *key, const char
*fmt, ...);
extern const char *const
kresps[KRESP__MAX];
The
khttp_head
()
function takes a
kcgi(3)
context req that was previously initialised by
khttp_parse(3)
and emits one HTTP header with the given HTTP response
key. The fmt string and variable
arguments, which follow
printf(3)
syntax, form the corresponding value in the HTTP response. This function may
only be invoked prior to
khttp_body(3);
otherwise, its behaviour is undefined.
The kresps global array, indexed with the
KRESP_*
symbolic constants defined in
<kcgi.h>
, provides standard
HTTP key names. Use it for the key argument to avoid
typos.
See
khttp_body(3)
for a discussion on the "Content-Encoding" header: do not specify
this header before doing so!
The khttp_head
() function returns an
enum kcgi_err indicating the error state.
KCGI_OK
- Success (not an error).
KCGI_ENOMEM
- Internal memory allocation failure.
KCGI_SYSTEM
- Internal system error writing to the output stream.
KCGI_HUP
- The output connection has been terminated. For FastCGI connections, the
current connection should be released with
khttp_free(3)
and parse loop reentered.
To emit a session cookie (one-year expiration date) with key
ckey and value cval for the
global path, one may invoke the following. Assume that
r is a pointer to a struct kreq
successfully initialised by
khttp_parse(3).
const char *ckey = "foo", *cval = "bar";
char buf[64];
khttp_epoch2str(time(NULL) + 31536000, buf, 64);
khttp_head(r, kresps[KRESP_SET_COOKIE],
"%s=%s; path=/; expires=%s", ckey, cval, buf);
RFC 2616, section 14: “Header Field
Definitions”.
The example references RFC 6265, section 4.1:
“Set-Cookie”.
The khttp_head
() function was written by
Kristaps Dzonsons
<kristaps@bsd.lv>.