mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-28 20:58:31 +00:00
Restores post.c example
This commit is contained in:
1 parent
a6c2c2a10b
commit
12d98a1732
2 files changed
+34
-38
No files matched your search
+1
-1
@@ -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)
|
||||||
|
|||||||
+33
-37
@@ -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
|
// Parse form data. var1 and var2 are guaranteed to be NUL-terminated
|
||||||
post_data_len = mg_read(event->conn, post_data, sizeof(post_data));
|
mg_get_var(conn, "input_1", var1, sizeof(var1));
|
||||||
|
mg_get_var(conn, "input_2", var2, sizeof(var2));
|
||||||
|
|
||||||
// Parse form data. input1 and input2 are guaranteed to be NUL-terminated
|
// Send reply to the client, showing submitted form values.
|
||||||
mg_get_var(post_data, post_data_len, "input_1", input1, sizeof(input1));
|
// POST data is in conn->content, data length is in conn->content_len
|
||||||
mg_get_var(post_data, post_data_len, "input_2", input2, sizeof(input2));
|
snprintf(reply, sizeof(reply), "HTTP/1.0 200 OK\r\n"
|
||||||
|
"Content-Type: text/plain\r\n\r\n"
|
||||||
// Send reply to the client, showing submitted form values.
|
"Submitted data: [%.*s]\n"
|
||||||
mg_printf(event->conn, "HTTP/1.0 200 OK\r\n"
|
"Submitted data length: %d bytes\n"
|
||||||
"Content-Type: text/plain\r\n\r\n"
|
"input_1: [%s]\n"
|
||||||
"Submitted data: [%.*s]\n"
|
"input_2: [%s]\n",
|
||||||
"Submitted data length: %d bytes\n"
|
conn->content_len, conn->content, conn->content_len, var1, var2);
|
||||||
"input_1: [%s]\n"
|
mg_write(conn, reply, strlen(reply));
|
||||||
"input_2: [%s]\n",
|
} else {
|
||||||
post_data_len, post_data, post_data_len, input1, input2);
|
// Show HTML form.
|
||||||
} else {
|
snprintf(reply, sizeof(reply), "HTTP/1.0 200 OK\r\n"
|
||||||
// Show HTML form.
|
"Content-Length: %d\r\n"
|
||||||
mg_printf(event->conn, "HTTP/1.0 200 OK\r\n"
|
"Content-Type: text/html\r\n\r\n%s",
|
||||||
"Content-Length: %d\r\n"
|
(int) strlen(html_form), html_form);
|
||||||
"Content-Type: text/html\r\n\r\n%s",
|
mg_write(conn, reply, strlen(reply));
|
||||||
(int) strlen(html_form), html_form);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1; // Mark event as processed
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// All other events are left not processed
|
return 1;
|
||||||
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;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user