diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/civetweb/examples/_obsolete/post | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/civetweb/examples/_obsolete/post')
-rw-r--r-- | src/civetweb/examples/_obsolete/post/Makefile | 36 | ||||
-rw-r--r-- | src/civetweb/examples/_obsolete/post/post.c | 58 |
2 files changed, 94 insertions, 0 deletions
diff --git a/src/civetweb/examples/_obsolete/post/Makefile b/src/civetweb/examples/_obsolete/post/Makefile new file mode 100644 index 000000000..6e504f437 --- /dev/null +++ b/src/civetweb/examples/_obsolete/post/Makefile @@ -0,0 +1,36 @@ +# +# Copyright (c) 2013 No Face Press, LLC +# License http://opensource.org/licenses/mit-license.php MIT License +# + +#This makefile is used to test the other Makefiles + + +PROG = post +SRC = post.c + +TOP = ../.. +CIVETWEB_LIB = libcivetweb.a + +CFLAGS = -I$(TOP)/include $(COPT) +LIBS = -lpthread + +include $(TOP)/resources/Makefile.in-os + +ifeq ($(TARGET_OS),LINUX) + LIBS += -ldl +endif + +all: $(PROG) + +$(PROG): $(CIVETWEB_LIB) $(SRC) + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS) + +$(CIVETWEB_LIB): + $(MAKE) -C $(TOP) clean lib + cp $(TOP)/$(CIVETWEB_LIB) . + +clean: + rm -f $(CIVETWEB_LIB) $(PROG) + +.PHONY: all clean diff --git a/src/civetweb/examples/_obsolete/post/post.c b/src/civetweb/examples/_obsolete/post/post.c new file mode 100644 index 000000000..3c5c68c8d --- /dev/null +++ b/src/civetweb/examples/_obsolete/post/post.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <string.h> +#include "civetweb.h" + +static const char *html_form = + "<html><body>POST example." + "<form method=\"POST\" action=\"/handle_post_request\">" + "Input 1: <input type=\"text\" name=\"input_1\" /> <br/>" + "Input 2: <input type=\"text\" name=\"input_2\" /> <br/>" + "<input type=\"submit\" />" + "</form></body></html>"; + +static int begin_request_handler(struct mg_connection *conn) +{ + const struct mg_request_info *ri = mg_get_request_info(conn); + char post_data[1024], input1[sizeof(post_data)], input2[sizeof(post_data)]; + int post_data_len; + + if (!strcmp(ri->uri, "/handle_post_request")) { + // User has submitted a form, show submitted data and a variable value + post_data_len = mg_read(conn, post_data, sizeof(post_data)); + + // Parse form data. input1 and input2 are guaranteed to be NUL-terminated + mg_get_var(post_data, post_data_len, "input_1", input1, sizeof(input1)); + mg_get_var(post_data, post_data_len, "input_2", input2, sizeof(input2)); + + // Send reply to the client, showing submitted form values. + mg_printf(conn, "HTTP/1.0 200 OK\r\n" + "Content-Type: text/plain\r\n\r\n" + "Submitted data: [%.*s]\n" + "Submitted data length: %d bytes\n" + "input_1: [%s]\n" + "input_2: [%s]\n", + post_data_len, post_data, post_data_len, input1, input2); + } else { + // Show HTML form. + mg_printf(conn, "HTTP/1.0 200 OK\r\n" + "Content-Length: %d\r\n" + "Content-Type: text/html\r\n\r\n%s", + (int) strlen(html_form), html_form); + } + return 1; // Mark request as processed +} + +int main(void) +{ + struct mg_context *ctx; + const char *options[] = {"listening_ports", "8080", NULL}; + struct mg_callbacks callbacks; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.begin_request = begin_request_handler; + ctx = mg_start(&callbacks, NULL, options); + getchar(); // Wait until user hits "enter" + mg_stop(ctx); + + return 0; +} |