aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2008-11-02 21:34:35 +0000
committerChristian Grothoff <christian@grothoff.org>2008-11-02 21:34:35 +0000
commit31c0a70137d40b0d1a618ad12e37739fb5013770 (patch)
tree8a3e99aa14c46a50d99289315a388ed0a7c1b7f8
parent88d10158d9df1a92e83ffe3200006853221865b7 (diff)
downloadlibmicrohttpd-31c0a70137d40b0d1a618ad12e37739fb5013770.tar.gz
libmicrohttpd-31c0a70137d40b0d1a618ad12e37739fb5013770.zip
comet
-rw-r--r--src/examples/Makefile.am6
-rw-r--r--src/examples/minimal_example_comet.c88
2 files changed, 94 insertions, 0 deletions
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am
index a06f7e1f..f175915b 100644
--- a/src/examples/Makefile.am
+++ b/src/examples/Makefile.am
@@ -8,6 +8,7 @@ AM_CPPFLAGS = \
8noinst_PROGRAMS = \ 8noinst_PROGRAMS = \
9authorization_example \ 9authorization_example \
10minimal_example \ 10minimal_example \
11minimal_example_comet \
11querystring_example \ 12querystring_example \
12fileserver_example \ 13fileserver_example \
13fileserver_example_external_select \ 14fileserver_example_external_select \
@@ -22,6 +23,11 @@ minimal_example_SOURCES = \
22minimal_example_LDADD = \ 23minimal_example_LDADD = \
23 $(top_builddir)/src/daemon/libmicrohttpd.la 24 $(top_builddir)/src/daemon/libmicrohttpd.la
24 25
26minimal_example_comet_SOURCES = \
27 minimal_example_comet.c
28minimal_example_comet_LDADD = \
29 $(top_builddir)/src/daemon/libmicrohttpd.la
30
25authorization_example_SOURCES = \ 31authorization_example_SOURCES = \
26 authorization_example.c 32 authorization_example.c
27authorization_example_LDADD = \ 33authorization_example_LDADD = \
diff --git a/src/examples/minimal_example_comet.c b/src/examples/minimal_example_comet.c
new file mode 100644
index 00000000..3117df1e
--- /dev/null
+++ b/src/examples/minimal_example_comet.c
@@ -0,0 +1,88 @@
1/*
2 This file is part of libmicrohttpd
3 (C) 2007, 2008 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 generate an infinite stream with libmicrohttpd
22 * @author Christian Grothoff
23 */
24
25#include "platform.h"
26#include <microhttpd.h>
27
28static int
29data_generator(void * cls,
30 size_t pos,
31 char * buf,
32 int max)
33{
34 memset(buf, 'A', max-1);
35 buf[max-1] = '\n';
36 return max;
37}
38
39static int
40ahc_echo (void *cls,
41 struct MHD_Connection *connection,
42 const char *url,
43 const char *method,
44 const char *version,
45 const char *upload_data, unsigned int *upload_data_size, void **ptr)
46{
47 static int aptr;
48 struct MHD_Response *response;
49 int ret;
50
51 if (0 != strcmp (method, "GET"))
52 return MHD_NO; /* unexpected method */
53 if (&aptr != *ptr)
54 {
55 /* do never respond on first call */
56 *ptr = &aptr;
57 return MHD_YES;
58 }
59 *ptr = NULL; /* reset when done */
60 response = MHD_create_response_from_callback (-1,
61 80,
62 &data_generator,
63 NULL,
64 NULL);
65 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
66 MHD_destroy_response (response);
67 return ret;
68}
69
70int
71main (int argc, char *const *argv)
72{
73 struct MHD_Daemon *d;
74
75 if (argc != 3)
76 {
77 printf ("%s PORT SECONDS-TO-RUN\n", argv[0]);
78 return 1;
79 }
80 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
81 atoi (argv[1]),
82 NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
83 if (d == NULL)
84 return 1;
85 sleep (atoi (argv[2]));
86 MHD_stop_daemon (d);
87 return 0;
88}