aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-24 12:46:27 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-24 12:46:27 +0200
commit82e6ba21d28c6852658edcb03d935eb65aae36ef (patch)
tree49f5577d0643d40fe1463e8f29631ff291895e51
parent4ce0a69094d3865970e050db8df41f4f4d34486e (diff)
downloadlibmicrohttpd-82e6ba21d28c6852658edcb03d935eb65aae36ef.tar.gz
libmicrohttpd-82e6ba21d28c6852658edcb03d935eb65aae36ef.zip
add minimal example for empty body
-rw-r--r--src/examples/Makefile.am8
-rw-r--r--src/examples/minimal_example_empty.c94
2 files changed, 101 insertions, 1 deletions
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am
index f934d6ad..7ffddff5 100644
--- a/src/examples/Makefile.am
+++ b/src/examples/Makefile.am
@@ -20,6 +20,7 @@ noinst_PROGRAMS = \
20 benchmark_https \ 20 benchmark_https \
21 chunked_example \ 21 chunked_example \
22 minimal_example \ 22 minimal_example \
23 minimal_example_empty \
23 dual_stack_example \ 24 dual_stack_example \
24 minimal_example_comet \ 25 minimal_example_comet \
25 querystring_example \ 26 querystring_example \
@@ -83,6 +84,11 @@ minimal_example_SOURCES = \
83minimal_example_LDADD = \ 84minimal_example_LDADD = \
84 $(top_builddir)/src/microhttpd/libmicrohttpd.la 85 $(top_builddir)/src/microhttpd/libmicrohttpd.la
85 86
87minimal_example_empty_SOURCES = \
88 minimal_example_empty.c
89minimal_example_empty_LDADD = \
90 $(top_builddir)/src/microhttpd/libmicrohttpd.la
91
86upgrade_example_SOURCES = \ 92upgrade_example_SOURCES = \
87 upgrade_example.c 93 upgrade_example.c
88upgrade_example_CFLAGS = \ 94upgrade_example_CFLAGS = \
@@ -216,4 +222,4 @@ http_chunked_compression_LDADD = \
216if HAVE_ZLIB 222if HAVE_ZLIB
217 http_compression_LDADD += -lz 223 http_compression_LDADD += -lz
218 http_chunked_compression_LDADD += -lz 224 http_chunked_compression_LDADD += -lz
219endif \ No newline at end of file 225endif
diff --git a/src/examples/minimal_example_empty.c b/src/examples/minimal_example_empty.c
new file mode 100644
index 00000000..2de1cc8b
--- /dev/null
+++ b/src/examples/minimal_example_empty.c
@@ -0,0 +1,94 @@
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 minimal_example.c
21 * @brief minimal example for how to use libmicrohttpd
22 * @author Christian Grothoff
23 */
24
25#include "platform.h"
26#include <microhttpd.h>
27
28
29static int
30ahc_echo (void *cls,
31 struct MHD_Connection *connection,
32 const char *url,
33 const char *method,
34 const char *version,
35 const char *upload_data,
36 size_t *upload_data_size,
37 void **ptr)
38{
39 static int aptr;
40 struct MHD_Response *response;
41 int ret;
42
43 (void) url; /* Unused. Silent compiler warning. */
44 (void) version; /* Unused. Silent compiler warning. */
45 (void) upload_data; /* Unused. Silent compiler warning. */
46 (void) upload_data_size; /* Unused. Silent compiler warning. */
47
48 if (0 != strcmp (method, "GET"))
49 return MHD_NO; /* unexpected method */
50 if (&aptr != *ptr)
51 {
52 /* do never respond on first call */
53 *ptr = &aptr;
54 return MHD_YES;
55 }
56 *ptr = NULL; /* reset when done */
57 response = MHD_create_response_from_buffer (0,
58 NULL,
59 MHD_RESPMEM_PERSISTENT);
60 ret = MHD_queue_response (connection,
61 MHD_HTTP_NO_CONTENT,
62 response);
63 MHD_destroy_response (response);
64 return ret;
65}
66
67
68int
69main (int argc,
70 char *const *argv)
71{
72 struct MHD_Daemon *d;
73
74 if (argc != 2)
75 {
76 printf ("%s PORT\n", argv[0]);
77 return 1;
78 }
79 d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
80 MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
81 /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
82 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
83 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
84 atoi (argv[1]),
85 NULL, NULL, &ahc_echo, NULL,
86 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
87 MHD_OPTION_STRICT_FOR_CLIENT, (int) 1,
88 MHD_OPTION_END);
89 if (d == NULL)
90 return 1;
91 (void) getc (stdin);
92 MHD_stop_daemon (d);
93 return 0;
94}