aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2018-12-12 17:50:06 +0100
committerChristian Grothoff <christian@grothoff.org>2018-12-12 17:50:06 +0100
commit8a9b198d4ab3a6f47569b139343f4b52a2c9494e (patch)
treebb1d08b72e43be3bf71bc7052738f3872d681120
parent5d75a87ef6270e65c54f63b66f8d9c8052ef60e9 (diff)
downloadlibmicrohttpd-8a9b198d4ab3a6f47569b139343f4b52a2c9494e.tar.gz
libmicrohttpd-8a9b198d4ab3a6f47569b139343f4b52a2c9494e.zip
add conn. close example
-rw-r--r--src/examples/connection_close.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/examples/connection_close.c b/src/examples/connection_close.c
new file mode 100644
index 00000000..88222cbf
--- /dev/null
+++ b/src/examples/connection_close.c
@@ -0,0 +1,119 @@
1/*
2 This file is part of libmicrohttpd
3 Copyright (C) 2007 Christian Grothoff (and other contributing authors)
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19/**
20 * @file connection_close.c
21 * @brief minimal example for connection close notifications
22 * @author Christian Grothoff
23 */
24
25#include "platform.h"
26#include <microhttpd.h>
27
28#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
29
30static int
31ahc_echo (void *cls,
32 struct MHD_Connection *connection,
33 const char *url,
34 const char *method,
35 const char *version,
36 const char *upload_data, size_t *upload_data_size, void **ptr)
37{
38 static int aptr;
39 const char *me = cls;
40 struct MHD_Response *response;
41 int ret;
42 (void)url; /* Unused. Silent compiler warning. */
43 (void)version; /* Unused. Silent compiler warning. */
44 (void)upload_data; /* Unused. Silent compiler warning. */
45 (void)upload_data_size; /* Unused. Silent compiler warning. */
46
47 if (0 != strcmp (method, "GET"))
48 return MHD_NO; /* unexpected method */
49 if (&aptr != *ptr)
50 {
51 /* do never respond on first call */
52 *ptr = &aptr;
53 return MHD_YES;
54 }
55 *ptr = NULL; /* reset when done */
56 response = MHD_create_response_from_buffer (strlen (me),
57 (void *) me,
58 MHD_RESPMEM_PERSISTENT);
59 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
60 MHD_destroy_response (response);
61 return ret;
62}
63
64#include <x86intrin.h>
65
66static void
67request_completed (void *cls,
68 struct MHD_Connection *connection,
69 void **con_cls,
70 enum MHD_RequestTerminationCode toe)
71{
72 fprintf (stderr,
73 "%llu - RC: %d\n",
74 (unsigned long long) __rdtsc(),
75 toe);
76}
77
78
79static void
80connection_completed (void *cls,
81 struct MHD_Connection *connection,
82 void **socket_context,
83 enum MHD_ConnectionNotificationCode toe)
84{
85 fprintf (stderr,
86 "%llu - CC: %d\n",
87 (unsigned long long) __rdtsc(),
88 toe);
89}
90
91
92int
93main (int argc, char *const *argv)
94{
95 struct MHD_Daemon *d;
96
97 if (argc != 2)
98 {
99 printf ("%s PORT\n", argv[0]);
100 return 1;
101 }
102 d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
103 /* MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
104 /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
105 MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, // | MHD_USE_ITC,
106 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
107 atoi (argv[1]),
108 NULL, NULL, &ahc_echo, PAGE,
109 MHD_OPTION_NOTIFY_COMPLETED, &request_completed, NULL,
110 MHD_OPTION_NOTIFY_CONNECTION, &connection_completed, NULL,
111 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
112 MHD_OPTION_STRICT_FOR_CLIENT, (int) 1,
113 MHD_OPTION_END);
114 if (d == NULL)
115 return 1;
116 (void) getc (stdin);
117 MHD_stop_daemon (d);
118 return 0;
119}