commit 8b0e534ac401da8187e67568e392d7635bf0c747
parent c4448622ca947316434cc1e340a56aa411bc0ad4
Author: Christian Grothoff <christian@grothoff.org>
Date: Sat, 5 Dec 2015 16:30:51 +0000
add timeout example
Diffstat:
2 files changed, 57 insertions(+), 3 deletions(-)
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am
@@ -23,10 +23,11 @@ noinst_PROGRAMS = \
dual_stack_example \
minimal_example_comet \
querystring_example \
+ timeout \
fileserver_example \
fileserver_example_dirs \
fileserver_example_external_select \
- refuse_post_example
+ refuse_post_example
if ENABLE_HTTPS
@@ -61,6 +62,11 @@ minimal_example_SOURCES = \
minimal_example_LDADD = \
$(top_builddir)/src/microhttpd/libmicrohttpd.la
+timeout_SOURCES = \
+ timeout.c
+timeout_LDADD = \
+ $(top_builddir)/src/microhttpd/libmicrohttpd.la
+
chunked_example_SOURCES = \
chunked_example.c
chunked_example_LDADD = \
@@ -156,5 +162,3 @@ https_fileserver_example_CPPFLAGS = \
$(AM_CPPFLAGS) $(GNUTLS_CPPFLAGS)
https_fileserver_example_LDADD = \
$(top_builddir)/src/microhttpd/libmicrohttpd.la
-
-
diff --git a/src/examples/timeout.c b/src/examples/timeout.c
@@ -0,0 +1,50 @@
+#include <microhttpd.h>
+#include <stdio.h>
+#include <string.h>
+
+#define PORT 8080
+
+static int
+answer_to_connection(void *cls,
+ struct MHD_Connection *connection,
+ const char *url,
+ const char *method,
+ const char *version,
+ const char *upload_data,
+ size_t *upload_data_size,
+ void **con_cls)
+{
+ const char *page = "<html><body>Hello world!</body></html>";
+ struct MHD_Response *response;
+ int ret;
+
+ response = MHD_create_response_from_buffer (strlen(page),
+ (void *) page,
+ MHD_RESPMEM_PERSISTENT);
+ MHD_add_response_header(response,
+ MHD_HTTP_HEADER_CONTENT_TYPE,
+ "text/html");
+ ret = MHD_queue_response (connection,
+ MHD_HTTP_OK,
+ response);
+ MHD_destroy_response(response);
+ return ret;
+}
+
+
+int main()
+{
+ struct MHD_Daemon *daemon;
+
+ daemon = MHD_start_daemon(// MHD_USE_SELECT_INTERNALLY |
+ MHD_USE_THREAD_PER_CONNECTION, // if you comment this line, the problem dies
+ PORT, NULL, NULL,
+ &answer_to_connection, NULL,
+ MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 3,
+ MHD_OPTION_END);
+ if (NULL == daemon)
+ return 1;
+ getchar();
+ MHD_stop_daemon(daemon);
+ return 0;
+}