From 65031ca3502e0c27780be847fd97c112546741a9 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sat, 18 Nov 2023 00:56:04 +0100 Subject: Send HTTP headers to payload callback Even if libweb already parses some common headers, such as Content-Length, some users might find it interesting to inspect which headers were received from a request. Since HTTP/1.1 does not define a limit on the number of maximum headers a client can send, for security reasons a maximum value must be provided by the user. Any extra headers shall be then discarded by libweb. An example application showing this new feature is also provided. --- examples/headers/main.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 examples/headers/main.c (limited to 'examples/headers/main.c') diff --git a/examples/headers/main.c b/examples/headers/main.c new file mode 100644 index 0000000..b465aa1 --- /dev/null +++ b/examples/headers/main.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include + +static const size_t max_headers = 5; + +static int hello(const struct http_payload *const pl, + struct http_response *const r, void *const user) +{ + printf("Got %zu headers from the client (max %zu).\n", + pl->n_headers, max_headers); + + for (size_t i = 0; i < pl->n_headers; i++) + { + const struct http_header *const h = &pl->headers[i]; + + printf("%s: %s\n", h->header, h->value); + } + + *r = (const struct http_response) + { + .status = HTTP_STATUS_OK + }; + + return 0; +} + +static int on_length(const unsigned long long len, + const struct http_cookie *const c, struct http_response *const r, + void *const user) +{ + *r = (const struct http_response) + { + .status = HTTP_STATUS_FORBIDDEN + }; + + return 1; +} + +int main(int argc, char *argv[]) +{ + int ret = EXIT_FAILURE; + const short port = 8080; + const struct handler_cfg cfg = + { + .length = on_length, + .max_headers = max_headers + }; + + struct handler *const h = handler_alloc(&cfg); + static const char *const urls[] = {"/", "/index.html"}; + + if (!h) + { + fprintf(stderr, "%s: handler_alloc failed\n", __func__); + goto end; + } + + for (size_t i = 0; i < sizeof urls / sizeof *urls; i++) + if (handler_add(h, urls[i], HTTP_OP_GET, hello, NULL)) + { + fprintf(stderr, "%s: handler_add failed\n", __func__); + goto end; + } + + if (handler_listen(h, port)) + { + fprintf(stderr, "%s: handler_listen failed\n", __func__); + goto end; + } + + ret = EXIT_SUCCESS; + +end: + handler_free(h); + return ret; +} -- cgit v1.2.3