diff options
| author | xavi <xavi@noreply.localhost> | 2023-11-20 16:47:53 +0100 |
|---|---|---|
| committer | xavi <xavi@noreply.localhost> | 2023-11-20 16:47:53 +0100 |
| commit | b94f76033f72d4eba629a6473a2b301dd7988f97 (patch) | |
| tree | adac8adce7dc9ce6c0f880c0439cd9f3b39527ab /examples/put/main.c | |
| parent | 8f1ad3124e9e063ca0ee7c2548c747b20de3529d (diff) | |
| parent | dc8b14d99028b9235aa7d7633906a979aa08e4f9 (diff) | |
| download | libweb-b94f76033f72d4eba629a6473a2b301dd7988f97.tar.gz | |
Merge pull request 'Add support for HTTP `PUT`' (#3) from midokura-xavi/libweb:put into master
Reviewed-on: https://gitea.privatedns.org/xavi/libweb/pulls/3
Diffstat (limited to 'examples/put/main.c')
| -rw-r--r-- | examples/put/main.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/examples/put/main.c b/examples/put/main.c new file mode 100644 index 0000000..cf38248 --- /dev/null +++ b/examples/put/main.c @@ -0,0 +1,90 @@ +#include <dynstr.h> +#include <libweb/handler.h> +#include <libweb/html.h> +#include <libweb/http.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> + +static int on_put(const struct http_payload *const pl, + struct http_response *const r, void *const user) +{ + if (pl->expect_continue) + { + *r = (const struct http_response) + { + .status = HTTP_STATUS_CONTINUE + }; + + return 0; + } + + printf("File uploaded to %s\n", pl->u.put.tmpname); + + *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_OK + }; + + return 0; +} + +int main(int argc, char *argv[]) +{ + int ret = EXIT_FAILURE; + const struct handler_cfg cfg = + { + .tmpdir = "/tmp", + .length = on_length + }; + + struct handler *const h = handler_alloc(&cfg); + static const char *const urls[] = {"/*"}; + + 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_PUT, on_put, NULL)) + { + fprintf(stderr, "%s: handler_add failed\n", __func__); + goto end; + } + + unsigned short outport; + + if (handler_listen(h, 0, &outport)) + { + fprintf(stderr, "%s: handler_listen failed\n", __func__); + goto end; + } + + printf("Listening on port %hu\n", outport); + + if (handler_loop(h)) + { + fprintf(stderr, "%s: handler_loop failed\n", __func__); + goto end; + } + + ret = EXIT_SUCCESS; + +end: + handler_free(h); + return ret; +} |
