Commit 12d98a17 authored by Sergey Lyubka's avatar Sergey Lyubka

Restores post.c example

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