]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/examples/post/post.c
5 static const char *html_form
=
6 "<html><body>POST example."
7 "<form method=\"POST\" action=\"/handle_post_request\">"
8 "Input 1: <input type=\"text\" name=\"input_1\" /> <br/>"
9 "Input 2: <input type=\"text\" name=\"input_2\" /> <br/>"
10 "<input type=\"submit\" />"
11 "</form></body></html>";
13 static int begin_request_handler(struct mg_connection
*conn
)
15 const struct mg_request_info
*ri
= mg_get_request_info(conn
);
16 char post_data
[1024], input1
[sizeof(post_data
)], input2
[sizeof(post_data
)];
19 if (!strcmp(ri
->uri
, "/handle_post_request")) {
20 // User has submitted a form, show submitted data and a variable value
21 post_data_len
= mg_read(conn
, post_data
, sizeof(post_data
));
23 // Parse form data. input1 and input2 are guaranteed to be NUL-terminated
24 mg_get_var(post_data
, post_data_len
, "input_1", input1
, sizeof(input1
));
25 mg_get_var(post_data
, post_data_len
, "input_2", input2
, sizeof(input2
));
27 // Send reply to the client, showing submitted form values.
28 mg_printf(conn
, "HTTP/1.0 200 OK\r\n"
29 "Content-Type: text/plain\r\n\r\n"
30 "Submitted data: [%.*s]\n"
31 "Submitted data length: %d bytes\n"
34 post_data_len
, post_data
, post_data_len
, input1
, input2
);
37 mg_printf(conn
, "HTTP/1.0 200 OK\r\n"
38 "Content-Length: %d\r\n"
39 "Content-Type: text/html\r\n\r\n%s",
40 (int) strlen(html_form
), html_form
);
42 return 1; // Mark request as processed
47 struct mg_context
*ctx
;
48 const char *options
[] = {"listening_ports", "8080", NULL
};
49 struct mg_callbacks callbacks
;
51 memset(&callbacks
, 0, sizeof(callbacks
));
52 callbacks
.begin_request
= begin_request_handler
;
53 ctx
= mg_start(&callbacks
, NULL
, options
);
54 getchar(); // Wait until user hits "enter"