KUTIL_URLENCODE(3) Library Functions Manual KUTIL_URLENCODE(3)


kutil_urlabs, kutil_urlpart, kutil_urlpartx, kutil_urlencodeURL formatting for kcgi


library “libkcgi”


#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
char *
kutil_urlabs(enum kscheme scheme, const char *host, uint16_t port, const char *path);
char *
kutil_urlpart(struct kreq *req, const char *path, const char *suffix, const char *page, ...);
char *
kutil_urlpartx(struct kreq *req, const char *path, const char *suffix, const char *page, ...);
char *
kutil_urlencode(const char *cp);


These functions format various parts of a URL.
The kutil_urlpart() and kutil_urlpartx() functions format the path/page.suffix part of an HTTP URL, appending a variable number of query string key-value pairs. If page is NULL, these return NULL. If path is NULL, the URL will be relative to page; otherwise, path will be followed by a path separator. The last argument must be NULL. If the suffix is NULL, it is omitted from the URL. The page, the keys, and the values are URL-encoded, but the path and the suffix are not.
kutil_urlpart() accepts a variable number of pairs of char * arguments. In each pair, the first argument is the key and the second one the corresponding value.
kutil_urlpartx() accepts a variable number of groups of three arguments each. In each group, the first argument is a char * giving the key. The second argument is an enum kattrx specifying the type of the third argument: KATTRX_STRING followed by a char * string, KATTRX_INT followed by an int64_t signed integer, or KATTRX_DOUBLE followed by a double floating-point number. If all types are KATTRX_STRING, the invocation can be shortened to kutil_urlpart().
The kutil_urlencode() function encodes a string for embedding in a URL.
The kutil_urlabs() function assembles the URL schema://host:portpath. Unless the path is an empty string, it needs to begin with a slash. None of this is URL-encoded, so make sure to use kutil_urlpart(), kutil_urlpartx(), or just kutil_urlencode() for the sensitive parts.


These functions return newly-allocated strings that must be freed with free(3). They return NULL if allocation fails. The kutil_urlencode() function also returns NULL if cp is NULL.


These functions were written by Kristaps Dzonsons <kristaps@bsd.lv>.


The kutil_urlabs() function accepts scheme arguments that aren't valid in URLs.
The req argument is not used and should be removed.
January 18, 2018 OpenBSD 6.2