KHTTP_BODY(3)
β¨ NAME β¨
Okay bestie, this is KHTTP_BODY(3) from the Library Functions Manual, and it's all about khttp_body and khttp_body_compress β the iconic little duo that closes the HTTP header sequence for kcgi. Periodt. π
π«Ά LIBRARY π«Ά
The library is βlibkcgiβ. That's the vibe. π
π SYNOPSIS π
#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
enum kcgi_err
khttp_body(struct kreq *req);
enum kcgi_err
khttp_body_compress(struct kreq *req, int compress);
π DESCRIPTION π
So khttp_body() and khttp_body_compress() are the finishers: they terminate the zero or more khttp_head(3) calls for a kcgi(3) context named req. Like, header party over, bestie. π
β οΈ After these functions, if you invokekhttp_head(3)again, or invokekhttp_body()orkhttp_body_compress()again, that's undefined behaviour. Not a cute look. Do not do it. π
khttp_body() is the one that checks whether compression should be enabled by examining request headers. If it applies, it emits the appropriate content encoding header and enables compressed output for subsequent khttp_write(3) calls. Automatic. Slay. β¨
khttp_body_compress() takes the compress argument. If compress is zero, it disables compressed output for subsequent khttp_write(3) calls. That's the move for caller-managed compression, like when you're sending pre-compressed files. If compress is non-zero, compressed output is enabled. In either case, the content encoding headers must be managed by the caller β that part is on you, bestie. π«Ά
π RETURN VALUES π
Both khttp_body() and khttp_body_compress() return an enum kcgi_err that tells you the error state. Here's the whole mood ring:
KCGI_OKβ success (not an error). We love a clean run. βKCGI_ENOMEMβ internal memory allocation failure. Sad little malloc moment. πKCGI_SYSTEMβ internal system error writing to the output stream. The output stream said no. πKCGI_HUPβ the output connection has been terminated. For FastCGI connections, the current connection should be released withkhttp_free(3)and the parse loop reentered. Bye connection, re-enter the loop. πKCGI_FORMβ returned bykhttp_body_compress()if compression was requested but is not provided by the operating system. The OS did not come through. π
π§ EXAMPLES π§
First one: write an HTTP header sequence and text content with automatic compression detection. Assume that r is a pointer to a struct kreq successfully initialised by khttp_parse(3). Very main-character setup. β¨
khttp_head(r, kresps[KRESP_STATUS],
"%s", khttps[KHTTP_200]);
khttp_head(r, kresps[KRESP_CONTENT_TYPE],
"%s", kmimetypes[KMIME_TEXT_PLAIN]);
khttp_body(r);
khttp_puts(r, "Hello, world!\n");
To explicitly disable compression, do this:
khttp_head(r, kresps[KRESP_STATUS],
"%s", khttps[KHTTP_200]);
khttp_head(r, kresps[KRESP_CONTENT_TYPE],
"%s", kmimetypes[KMIME_TEXT_PLAIN]);
khttp_body_compress(r, 0);
khttp_puts(r, "Hello, world!\n");
To disable compression but emit a compressed file β yes, that combo β do this:
khttp_head(r, kresps[KRESP_STATUS],
"%s", khttps[KHTTP_200]);
khttp_head(r, kresps[KRESP_CONTENT_TYPE],
"%s", kmimetypes[KMIME_TEXT_PLAIN]);
khttp_head(r, kresps[KRESP_CONTENT_ENCODING],
"%s", "gzip");
khttp_body_compress(r, 0);
khttp_template(r, NULL, "compressed.txt.gz");
π₯ SEE ALSO π₯
π AUTHORS π
The khttp_body() function was written by Kristaps Dzonsons <kristaps@bsd.lv>. Iconic. π
Source/version: Debian, date: $Mdocdate$, manual: KHTTP_BODY(3) π
π¦ 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