KHTTP_URLABS(3)

πŸ“¦ version 1.0.1, in alpine 3.24

πŸ’… 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 NULL or 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 not NULL, 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 are char *. and if the query string value is NULL? 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms