diff options
Diffstat (limited to 'src/examples/minimal_example_comet.c')
-rw-r--r-- | src/examples/minimal_example_comet.c | 88 |
1 files changed, 88 insertions, 0 deletions
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 | |||
28 | static int | ||
29 | data_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 | |||
39 | static int | ||
40 | ahc_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 | |||
70 | int | ||
71 | main (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 | } | ||