From 3e780d21fc0226f149f94e768e72c6d99f172994 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Fri, 10 Oct 2014 09:44:45 +0100 Subject: [PATCH] moved form.c example to separate dir --- examples/post.c | 62 ------------------- .../{mongoose_server => restful_api}/Makefile | 2 +- .../restful_api.c} | 0 3 files changed, 1 insertion(+), 63 deletions(-) delete mode 100644 examples/post.c rename examples/{mongoose_server => restful_api}/Makefile (92%) rename examples/{form_submit/form_submit.c => restful_api/restful_api.c} (100%) diff --git a/examples/post.c b/examples/post.c deleted file mode 100644 index f6893114..00000000 --- a/examples/post.c +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -#include "mongoose.h" - -static const char *html_form = - "POST example." - "
" - "Input 1:
" - "Input 2:
" - "" - "
"; - -static void send_reply(struct mg_connection *conn) { - char var1[500], var2[500]; - - if (strcmp(conn->uri, "/handle_post_request") == 0) { - // User has submitted a form, show submitted data and a variable value - // Parse form data. var1 and var2 are guaranteed to be NUL-terminated - mg_get_var(conn, "input_1", var1, sizeof(var1)); - mg_get_var(conn, "input_2", var2, sizeof(var2)); - - // Send reply to the client, showing submitted form values. - // POST data is in conn->content, data length is in conn->content_len - mg_send_header(conn, "Content-Type", "text/plain"); - mg_printf_data(conn, - "Submitted data: [%.*s]\n" - "Submitted data length: %d bytes\n" - "input_1: [%s]\n" - "input_2: [%s]\n", - conn->content_len, conn->content, - conn->content_len, var1, var2); - } else { - // Show HTML form. - mg_send_data(conn, html_form, strlen(html_form)); - } -} - -static int ev_handler(struct mg_connection *conn, enum mg_event ev) { - if (ev == MG_REQUEST) { - send_reply(conn); - return MG_TRUE; - } else if (ev == MG_AUTH) { - return MG_TRUE; - } else { - return MG_FALSE; - } -} - -int main(void) { - struct mg_server *server = mg_create_server(NULL, ev_handler); - - mg_set_option(server, "listening_port", "8080"); - - printf("Starting on port %s\n", mg_get_option(server, "listening_port")); - for (;;) { - mg_poll_server(server, 1000); - } - - mg_destroy_server(&server); - - return 0; -} diff --git a/examples/mongoose_server/Makefile b/examples/restful_api/Makefile similarity index 92% rename from examples/mongoose_server/Makefile rename to examples/restful_api/Makefile index 532d2ddb..e56fed32 100644 --- a/examples/mongoose_server/Makefile +++ b/examples/restful_api/Makefile @@ -1,7 +1,7 @@ # Copyright (c) 2014 Cesanta Software # All rights reserved -PROG = mongoose_server +PROG = restful_api CFLAGS = -W -Wall -I../.. -g -O0 $(CFLAGS_EXTRA) SOURCES = $(PROG).c ../../mongoose.c diff --git a/examples/form_submit/form_submit.c b/examples/restful_api/restful_api.c similarity index 100% rename from examples/form_submit/form_submit.c rename to examples/restful_api/restful_api.c