]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/examples/post/post.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / civetweb / examples / post / post.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "civetweb.h"
4
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>";
12
13 static int begin_request_handler(struct mg_connection *conn)
14 {
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)];
17 int post_data_len;
18
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));
22
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));
26
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"
32 "input_1: [%s]\n"
33 "input_2: [%s]\n",
34 post_data_len, post_data, post_data_len, input1, input2);
35 } else {
36 // Show HTML form.
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);
41 }
42 return 1; // Mark request as processed
43 }
44
45 int main(void)
46 {
47 struct mg_context *ctx;
48 const char *options[] = {"listening_ports", "8080", NULL};
49 struct mg_callbacks callbacks;
50
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"
55 mg_stop(ctx);
56
57 return 0;
58 }