Skip to content
Snippets Groups Projects
Commit 12d98a17 authored by Sergey Lyubka's avatar Sergey Lyubka
Browse files

Restores post.c example

parent a6c2c2a1
No related branches found
No related tags found
No related merge requests found
......@@ -19,9 +19,9 @@ endif
all:
$(CC) hello.c ../mongoose.c -o hello $(CFLAGS)
$(CC) qcomm.c ../mongoose.c -o qcomm $(CFLAGS)
$(CC) post.c ../mongoose.c -o post $(CFLAGS)
# $(CC) upload.c ../mongoose.c -o upload $(CFLAGS)
# $(CC) post.c ../mongoose.c -o post $(CFLAGS)
# $(CC) -DUSE_WEBSOCKET websocket.c ../mongoose.c -o $@ $(CFLAGS)
# $(CC) chat.c ../mongoose.c -o chat $(CFLAGS)
# $(CC) lua_dll.c ../build/lua_5.2.1.c -o $@.so $(CFLAGS) $(DLL_FLAGS)
......
......@@ -10,49 +10,45 @@ static const char *html_form =
"<input type=\"submit\" />"
"</form></body></html>";
static int event_handler(struct mg_event *event) {
char post_data[1024], input1[sizeof(post_data)], input2[sizeof(post_data)];
int post_data_len;
if (event->type == MG_REQUEST_BEGIN) {
if (!strcmp(event->request_info->uri, "/handle_post_request")) {
// User has submitted a form, show submitted data and a variable value
post_data_len = mg_read(event->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(event->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(event->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 event as processed
static int handler(struct mg_connection *conn) {
char var1[500], var2[500], reply[2000];
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
snprintf(reply, sizeof(reply), "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",
conn->content_len, conn->content, conn->content_len, var1, var2);
mg_write(conn, reply, strlen(reply));
} else {
// Show HTML form.
snprintf(reply, sizeof(reply), "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);
mg_write(conn, reply, strlen(reply));
}
// All other events are left not processed
return 0;
return 1;
}
int main(void) {
struct mg_context *ctx;
const char *options[] = {"listening_ports", "8080", NULL};
ctx = mg_start(options, &event_handler, NULL);
getchar(); // Wait until user hits "enter"
mg_stop(ctx);
struct mg_server *server = mg_create_server(NULL);
mg_set_option(server, "listening_port", "8080");
mg_add_uri_handler(server, "/", handler);
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
for (;;) {
mg_poll_server(server, 1000);
}
mg_destroy_server(&server);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment