KHTTP_URLABS(3)
π KHTTP_URLABS(3) β like, the URL-formatting one, bestie
okay so real quick before we dive in: this is a page from the Library Functions Manual for libkcgi, and it's literally just about gluing together absolute URLs. it's giving "small function, big responsibility" energy. let's go π
πΈ NAME
khttp_urlabs, khttp_vurlabs β URL formatting for kcgi π
π LIBRARY
library βlibkcgiβ β that's the one, that's our girl π«Ά
β¨ SYNOPSIS
first you gotta invite the headers in, no cap:
#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
then the two functions themselves, looking all cute and symmetrical:
char *
khttp_urlabs(enum kscheme scheme, const char *host, uint16_t port,
const char *path, ...);
char *
khttp_vurlabs(enum kscheme scheme, const char *host, uint16_t port,
const char *path, va_list ap);
π« DESCRIPTION
so what does it do? it formats an absolute (full) URL. that's it, that's the whole vibe. π₯
fun fact time: in some standards, the host is called the "authority" β like, very demure, very mysterious, but it's just the host ok π
the shapes it can produce are:
scheme://host:port/path?key=val
scheme://host:port/path
scheme:path
here's where it gets interesting, bestie, pay attention π:
- if you pass a
NULLor empty host, the whole//+ host + port chunk gets omitted. gone. not invited to the party. - if host is non-empty and not
NULL, and path is also non-empty and notNULL, then path gets a mandatory slash prepended. like, the slash is not optional, it's the main character. - if port is zero, it's omitted. periodt. and port only gets used at all if host is non-empty and not
NULLβ otherwise it's just sitting there, irrelevant. - the variable arguments come in pairs, terminated by a single
NULL. first of each pair is the query string key, second is the value. both arechar *. and if the query string value isNULL? it gets rendered as an empty string. no drama, no crash. π§
β οΈ important caveat energy: only the query string pairs get URL-encoded. so you, yes you, the caller, have to make sure the characters in host and path are actually valid. this function is not your babysitter, she's your bestie. π
also, the deprecated form of these functions, kutil_urlabs(), should no longer be used. she's retired, let her rest π
π¦ RETURN VALUES
returns newly-allocated strings that must be freed with free(3) β or NULL if allocation fails. that's it. free your memory, bestie, it's the responsible thing to do π
π EXAMPLES
okay this is the fun part, examples!! π
to print a full URL:
khttp_urlabs(KSCHEME_HTTPS, foo.com, 80, b, c, d, NULL);
this assigns https://foo.com:80/b?c=d β slay, look at her go β¨
setting the port to zero eliminates the port:
khttp_urlabs(KSCHEME_HTTPS, foo.com, 0, b, c, d, NULL);
this assigns https://foo.com/b?c=d β see, no port, clean, iconic π
an empty path component ends with the domain (or query string):
khttp_urlabs(KSCHEME_HTTPS, foo.com, 0, , c, d, NULL);
this assigns https://foo.com?c=d β the path just dipped, honestly relatable π«Ά
to use a host-less scheme, specify a NULL or empty host:
khttp_urlabs(KSCHEME_MAILTO, NULL, 0, k@b.com, NULL);
this assigns mailto:k@b.com β no host needed, she's independent π
π AUTHORS
Written by Kristaps Dzonsons <kristaps@bsd.lv>. king behavior, honestly. π
π CAVEATS
so, the tea β: these functions can currently be used to create semantically invalid URLs like https:?foo=bar by omitting components required for the scheme. yes, really. she'll let you do it. she won't stop you. π
semantically-correct URLs are the responsibility of the caller β that's you, bestie, no cap.
this behaviour may be verified for correctness in later versions of the software. so like, watch this space, iykyk π
and that's the whole page, mwah π β this little number came from Debian, stamped $Mdocdate$, and it's filed under KHTTP_URLABS(3). go forth and format your URLs correctly, i'm obsessed with you π§ββοΈπ
π¦ 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