LIBWEB_FORM(7)

NAME
libweb_form - libweb www-form decoding

SYNOPSIS

#include <libweb/form.h>

DESCRIPTION
This component allows library users to decode payloads with Content-Type application/x-www-form-urlencoded. Contents are stored into an opaque abstraction, namely struct form, and can be retrieved with the functions described below. libweb_form(7) provides the following functions:

•   form_alloc(3): Parses a human-readable string of application/x-www-form-urlencoded-encoded data and, if valid, allocates a struct form instance that can be used to interact with other functions from this section.

•   form_value(3): Returns a value for a given key from a struct form instance previously allocated by form_alloc(3).

•   form_foreach(3): Executes a user-defined function for each key inside a struct form instance previously allocated by form_alloc(3).

•   form_free(3): Deallocates a struct form instance and all the data associated to it.

EXAMPLE
The following source code shows how to parse application/x-www-form-urlencoded-encoded data using the interface described by this section:

    #include <libweb/form.h>
    #include <stdlib.h>
    #include <stdio.h>
    static int print(const char *const key, const char *const value,
        void *const user)
    {
        unsigned *const cnt = user;
        printf("key=%s, value=%s, cnt=%u\n", key, value, ++(*cnt));
        return 0;
    }
    int main(int argc, char *argv[])
    {
        int ret = EXIT_FAILURE;
        const char *payload;
        struct form *f = NULL;
        unsigned cnt = 0;
        int n;
        if (argc != 2)
        {
            fprintf(stderr, "Usage: %s <payload>\n", *argv);
            goto end;
        }
        else if ((n = form_alloc(payload = argv[1], &f)) < 0)
        {
            fprintf(stderr, "%s: form_alloc failed\n", __func__);
            goto end;
        }
        else if (n)
        {
            fprintf(stderr, "%s: invalid user input: %s\n", __func__, payload);
            goto end;
        }
        else if (form_foreach(f, print, &cnt))
        {
            fprintf(stderr, "%s: form_foreach failed\n", __func__);
            goto end;
        }
        ret = EXIT_SUCCESS;
    end:
        form_free(f);
        return ret;
    }

SEE ALSO
form_alloc(3), form_value(3), form_foreach(3), form_free(3).

COPYRIGHT
Copyright (C) 2023-2025 libweb contributors

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.