aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt1
-rw-r--r--examples/form/CMakeLists.txt4
-rw-r--r--examples/form/Makefile29
-rw-r--r--examples/form/README.md39
-rw-r--r--examples/form/main.c48
5 files changed, 121 insertions, 0 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 4c2a6a2..3370acb 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 3.13)
+add_subdirectory(form)
add_subdirectory(headers)
add_subdirectory(hello)
add_subdirectory(html)
diff --git a/examples/form/CMakeLists.txt b/examples/form/CMakeLists.txt
new file mode 100644
index 0000000..d855a89
--- /dev/null
+++ b/examples/form/CMakeLists.txt
@@ -0,0 +1,4 @@
+cmake_minimum_required(VERSION 3.13)
+project(form C)
+add_executable(${PROJECT_NAME} main.c)
+target_link_libraries(${PROJECT_NAME} PRIVATE web)
diff --git a/examples/form/Makefile b/examples/form/Makefile
new file mode 100644
index 0000000..a965d76
--- /dev/null
+++ b/examples/form/Makefile
@@ -0,0 +1,29 @@
+.POSIX:
+
+PROJECT = form
+DEPS = \
+ main.o
+LIBWEB = ../../libweb.a
+DYNSTR = ../../dynstr/libdynstr.a
+CFLAGS = -I ../../include -I ../../dynstr/include
+LIBWEB_FLAGS = -L ../../ -l web
+DYNSTR_FLAGS = -L ../../dynstr -l dynstr
+
+all: $(PROJECT)
+
+clean:
+ rm -f $(DEPS)
+
+distclean: clean
+ rm -f $(PROJECT)
+
+FORCE:
+
+$(PROJECT): $(DEPS) $(LIBWEB) $(DYNSTR)
+ $(CC) $(LDFLAGS) $(DEPS) $(LIBWEB_FLAGS) $(DYNSTR_FLAGS) -o $@
+
+$(LIBWEB): FORCE
+ +cd ../../ && $(MAKE)
+
+$(DYNSTR): FORCE
+ +cd ../../dynstr && $(MAKE)
diff --git a/examples/form/README.md b/examples/form/README.md
new file mode 100644
index 0000000..6be1969
--- /dev/null
+++ b/examples/form/README.md
@@ -0,0 +1,39 @@
+# HTML forms example
+
+This example shows how to parse `application/x-www-form-urlencoded`-encoded
+data using `libweb`s `form` API, using untrusted user input.
+
+## How to build
+
+If using `make(1)`, just run `make` from this directory.
+
+If using CMake, examples are built by default when configuring the project
+from [the top-level `CMakeLists.txt`](../../CMakeLists.txt).
+
+## How to run
+
+Run the executable with a `application/x-www-form-urlencoded`-encoded given
+as its only argument.
+
+For example:
+
+```sh
+$ ./form 'username=admin&password=mypassword'
+```
+
+Shall return:
+
+```
+key=username, value=admin, cnt=1
+key=password, value=mypassword, cnt=2
+```
+
+An invalid string shall be reported to `stderr`:
+
+```sh
+$ ./form 'username=admin&thisisawrongvalue'
+```
+
+```
+main: invalid user input: username=admin&thisisawrongvalue
+```
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;
+}