aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/test_termination.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcurl/test_termination.c')
-rw-r--r--src/testcurl/test_termination.c118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/testcurl/test_termination.c b/src/testcurl/test_termination.c
new file mode 100644
index 00000000..36e81fdc
--- /dev/null
+++ b/src/testcurl/test_termination.c
@@ -0,0 +1,118 @@
1/*
2 This file is part of libmicrohttpd
3 (C) 2009 Christian Grothoff
4
5 libmicrohttpd is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
9
10 libmicrohttpd is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with libmicrohttpd; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file daemontest_termination.c
23 * @brief Testcase for libmicrohttpd tolerating client not closing immediately
24 * @author hollosig
25 */
26#define PORT 12345
27
28#include "platform.h"
29#include <stdio.h>
30#include <string.h>
31#include <stdint.h>
32#include <stdarg.h>
33#include <stdlib.h>
34#include <sys/types.h>
35#include <microhttpd.h>
36#include <unistd.h>
37#include <curl/curl.h>
38
39#ifndef __MINGW32__
40#include <sys/select.h>
41#include <sys/socket.h>
42#endif
43
44static int
45connection_handler (void *cls,
46 struct MHD_Connection *connection,
47 const char *url,
48 const char *method,
49 const char *version,
50 const char *upload_data, size_t * upload_data_size,
51 void **ptr)
52{
53 static int i;
54
55 if (*ptr == NULL)
56 {
57 *ptr = &i;
58 return MHD_YES;
59 }
60
61 if (*upload_data_size != 0)
62 {
63 (*upload_data_size) = 0;
64 return MHD_YES;
65 }
66
67 struct MHD_Response *response =
68 MHD_create_response_from_buffer (strlen ("Response"), "Response",
69 MHD_RESPMEM_PERSISTENT);
70 int ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
71 MHD_destroy_response (response);
72
73 return ret;
74}
75
76static size_t
77write_data (void *ptr, size_t size, size_t nmemb, void *stream)
78{
79 return size * nmemb;
80}
81
82int
83main ()
84{
85 struct MHD_Daemon *daemon;
86
87 daemon = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
88 PORT,
89 NULL,
90 NULL, connection_handler, NULL, MHD_OPTION_END);
91
92 if (daemon == NULL)
93 {
94 fprintf (stderr, "Daemon cannot be started!");
95 exit (1);
96 }
97
98 CURL *curl = curl_easy_init ();
99 //curl_easy_setopt(curl, CURLOPT_POST, 1L);
100 char url[255];
101 sprintf (url, "http://127.0.0.1:%d", PORT);
102 curl_easy_setopt (curl, CURLOPT_URL, url);
103 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_data);
104
105 CURLcode success = curl_easy_perform (curl);
106 if (success != 0)
107 {
108 fprintf (stderr, "CURL Error");
109 exit (1);
110 }
111 /* CPU used to go crazy here */
112 sleep (1);
113
114 curl_easy_cleanup (curl);
115 MHD_stop_daemon (daemon);
116
117 return 0;
118}