aboutsummaryrefslogtreecommitdiff
path: root/doc/man7/libweb_form.7
blob: 8b218106e026492bc39cb5c4c5fd93e604c6d78b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
.TH LIBWEB_FORM 7 2025-10-02 0.5.0 "libweb Library Reference"

.SH NAME
libweb_form \- libweb www-form decoding

.SH SYNOPSIS
.LP
.nf
#include <libweb/form.h>
.fi

.SH DESCRIPTION
This component allows library users to decode payloads with
.IR "Content-Type application/x-www-form-urlencoded" .
Contents are stored into an opaque abstraction, namely
.IR "struct form" ,
and can be retrieved with the functions described below.

.IR libweb_form (7)
provides the following functions:

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

.IP \(bu 2
.IR form_value (3):
Returns a value for a given key from a
.I struct form
instance previously allocated by
.IR form_alloc (3).

.IP \(bu 2
.IR form_foreach (3):
Executes a user-defined function for each key inside a
.I struct form
instance previously allocated by
.IR form_alloc (3).

.IP \(bu 2
.IR form_free (3):
Deallocates a
.I struct form
instance and all the data associated to it.

.SH EXAMPLE

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

.PP
.in +4n
.EX
#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\en", 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>\en", *argv);
        goto end;
    }
    else if ((n = form_alloc(payload = argv[1], &f)) < 0)
    {
        fprintf(stderr, "%s: form_alloc failed\en", __func__);
        goto end;
    }
    else if (n)
    {
        fprintf(stderr, "%s: invalid user input: %s\en", __func__, payload);
        goto end;
    }
    else if (form_foreach(f, print, &cnt))
    {
        fprintf(stderr, "%s: form_foreach failed\en", __func__);
        goto end;
    }

    ret = EXIT_SUCCESS;

end:
    form_free(f);
    return ret;
}
.EE
.in
.PP

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

.SH COPYRIGHT
Copyright (C) 2023-2025 libweb contributors
.P
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.