KHTTP_HEAD(3)
π
khttp_head(3) β like, the header-emitting queen of libkcgi
Okay bestie, sit down, we're doing a manual page for khttp_head(3) from the Library Functions Manual (yes the whole library is libkcgi and yes she's iconic). This one is short and sweet, and fr fr you're gonna get it in like two minutes.
β¨ NAME β¨
khttp_head β emit exactly one HTTP header for kcgi. One. Just one. That's her whole job and she does it slayfully π
π LIBRARY π
library βlibkcgiβ π«Ά
π SYNOPSIS π
#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];
That's it. That's the whole vibe check. Four includes, one function, one global array. Clean. Minimal. Main character energy π
π¦ DESCRIPTION π¦
So khttp_head() takes a kcgi(3) context req β like, one that was already initialised by khttp_parse(3), don't get ahead of yourself bestie β and it emits one HTTP header with the given HTTP response key. Just one. If you want twelve headers you call her twelve times, that's the deal π
The fmt string and the variable arguments that follow it use printf(3) syntax, and together they form the corresponding value in the HTTP response. So fmt is the template, the varargs fill it in, periodt.
β οΈ Okay this part matters so much: this function may only be invoked prior to khttp_body(3). If you call it after? The behaviour is undefined. Not "a little weird", not "we'll figure it out", undefined. Do not test her. π
The kresps global array is indexed with the KRESP_* symbolic constants that live in <kcgi.h>, and it gives you the standard HTTP key names. Use it for the key argument so you don't typo "Content-Tpye" and ruin your whole life. Like, just use it. It's right there. π§
And hmm, also: go read khttp_body(3) for the whole discussion about the "Content-Encoding" headerβ¦
π¨ Do not specify this header before doing so! Like, readkhttp_body(3)first and then talk to me about"Content-Encoding". I'm serious. π
π RETURN VALUES π
khttp_head() returns an enum kcgi_err telling you the error state. So, the cast of characters:
KCGI_OKβ success! Not an error! We love a win πKCGI_ENOMEMβ internal memory allocation failure. The memory was not giving πKCGI_SYSTEMβ internal system error writing to the output stream. Something went sideways on the way out πKCGI_HUPβ the output connection has been terminated. And like, for FastCGI connections specifically? You should release the current connection withkhttp_free(3)and then reenter the parse loop. That's the whole protocol, iykyk π₯
π EXAMPLES π
Say you wanna emit a session cookie β one-year expiration date, ckey as the key, cval as the value, for the global path. You'd invoke the following, bestie. Assume r is a pointer to a struct kreq that was 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);
Look at that. 31536000 seconds, one whole year, buf of 64 bytes, kresps[KRESP_SET_COOKIE] instead of a hand-typed string. That's the way. Ate and left no crumbs πͺ
π SEE ALSO π
kcgi(3), khttp_body(3), khttp_parse(3)
(the holy trinity, honestly πΈ)
π STANDARDS π
RFC 2616, section 14: βHeader Field Definitionsβ.- The example references
RFC 6265, section 4.1: βSet-Cookieβ.
Both cited, both exact, no cap π
π«Ά AUTHORS π«Ά
The khttp_head() function was written by Kristaps Dzonsons <kristaps@bsd.lv>. Thank you king, we are obsessed π
and that's the whole page!! This cutie came from the Debian build, dated $Mdocdate$, and it's KHTTP_HEAD(3) forever and always. Bye bestie, go emit some headers responsibly πβ¨ππ¦
π¦ where this page comes from
- alpine 3.24: kcgi-doc 1.0.1-r0 Β· ISC
- Copyright (c) 2014, 2017 Kristaps Dzonsons <kristaps@bsd.lv>
- Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms