aboutsummaryrefslogtreecommitdiff
path: root/examples/form/main.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-09-30 23:50:33 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2025-10-02 15:52:14 +0200
commita0f5f7509bb9040752fa61fe0fdb447608e22b1c (patch)
tree53337bce28ed75f26953c5969af6bc76d8841f2b /examples/form/main.c
parentbba0b62f4e9e17927b9a2cda51dd5a4aa1b1f14e (diff)
downloadlibweb-a0f5f7509bb9040752fa61fe0fdb447608e22b1c.tar.gz
Implement form interface
This new interface allows library users to parse application/x-www-form-urlencoded data conveniently.
Diffstat (limited to 'examples/form/main.c')
-rw-r--r--examples/form/main.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/examples/form/main.c b/examples/form/main.c
new file mode 100644
index 0000000..afc1954
--- /dev/null
+++ b/examples/form/main.c
@@ -0,0 +1,48 @@
+#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;
+}