]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/examples/_obsolete/hello/hello.c
buildsys: switch source download to quincy
[ceph.git] / ceph / src / civetweb / examples / _obsolete / hello / hello.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "civetweb.h"
4
5 // This function will be called by civetweb on every new request.
6 static int begin_request_handler(struct mg_connection *conn)
7 {
8 const struct mg_request_info *request_info = mg_get_request_info(conn);
9 char content[100];
10
11 // Prepare the message we're going to send
12 int content_length = snprintf(content, sizeof(content),
13 "Hello from civetweb! Remote port: %d",
14 request_info->remote_port);
15
16 // Send HTTP reply to the client
17 mg_printf(conn,
18 "HTTP/1.1 200 OK\r\n"
19 "Content-Type: text/plain\r\n"
20 "Content-Length: %d\r\n" // Always set Content-Length
21 "\r\n"
22 "%s",
23 content_length, content);
24
25 // Returning non-zero tells civetweb that our function has replied to
26 // the client, and civetweb should not send client any more data.
27 return 1;
28 }
29
30 int main(void)
31 {
32 struct mg_context *ctx;
33 struct mg_callbacks callbacks;
34
35 // List of options. Last element must be NULL.
36 const char *options[] = {"listening_ports", "8080", NULL};
37
38 // Prepare callbacks structure. We have only one callback, the rest are NULL.
39 memset(&callbacks, 0, sizeof(callbacks));
40 callbacks.begin_request = begin_request_handler;
41
42 // Start the web server.
43 ctx = mg_start(&callbacks, NULL, options);
44
45 // Wait until user hits "enter". Server is running in separate thread.
46 // Navigating to http://localhost:8080 will invoke begin_request_handler().
47 getchar();
48
49 // Stop the server.
50 mg_stop(ctx);
51
52 return 0;
53 }