aboutsummaryrefslogtreecommitdiff
path: root/src/testzzuf/daemontest_post_loop.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testzzuf/daemontest_post_loop.c')
-rw-r--r--src/testzzuf/daemontest_post_loop.c386
1 files changed, 0 insertions, 386 deletions
diff --git a/src/testzzuf/daemontest_post_loop.c b/src/testzzuf/daemontest_post_loop.c
deleted file mode 100644
index 8630304e..00000000
--- a/src/testzzuf/daemontest_post_loop.c
+++ /dev/null
@@ -1,386 +0,0 @@
1/*
2 This file is part of libmicrohttpd
3 (C) 2007 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_post_loop.c
23 * @brief Testcase for libmicrohttpd POST operations using URL-encoding
24 * @author Christian Grothoff (inspired by bug report #1296)
25 */
26
27#include "config.h"
28#include <curl/curl.h>
29#include <microhttpd.h>
30#include <stdlib.h>
31#include <string.h>
32#include <time.h>
33
34#ifndef WINDOWS
35#include <unistd.h>
36#endif
37
38#define POST_DATA "<?xml version='1.0' ?>\n<xml>\n<data-id>1</data-id>\n</xml>\n"
39
40#define LOOPCOUNT 10
41
42static int oneone;
43
44struct CBC
45{
46 char *buf;
47 size_t pos;
48 size_t size;
49};
50
51static size_t
52copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
53{
54 struct CBC *cbc = ctx;
55
56 if (cbc->pos + size * nmemb > cbc->size)
57 return 0; /* overflow */
58 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
59 cbc->pos += size * nmemb;
60 return size * nmemb;
61}
62
63static int
64ahc_echo (void *cls,
65 struct MHD_Connection *connection,
66 const char *url,
67 const char *method,
68 const char *version,
69 const char *upload_data, unsigned int *upload_data_size,
70 void **mptr)
71{
72 static int marker;
73 struct MHD_Response *response;
74 int ret;
75
76 if (0 != strcmp ("POST", method))
77 {
78 printf ("METHOD: %s\n", method);
79 return MHD_NO; /* unexpected method */
80 }
81 if ((*mptr != NULL) && (0 == *upload_data_size))
82 {
83 if (*mptr != &marker)
84 abort ();
85 response = MHD_create_response_from_data (2, "OK", MHD_NO, MHD_NO);
86 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
87 MHD_destroy_response (response);
88 *mptr = NULL;
89 return ret;
90 }
91 if (strlen (POST_DATA) != *upload_data_size)
92 return MHD_YES;
93 *upload_data_size = 0;
94 *mptr = &marker;
95 return MHD_YES;
96}
97
98
99static int
100testInternalPost ()
101{
102 struct MHD_Daemon *d;
103 CURL *c;
104 char buf[2048];
105 struct CBC cbc;
106 CURLcode errornum;
107 int i;
108 char url[1024];
109
110 cbc.buf = buf;
111 cbc.size = 2048;
112 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
113 1080, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
114 if (d == NULL)
115 return 1;
116 for (i = 0; i < LOOPCOUNT; i++)
117 {
118 if (99 == i % 100)
119 fprintf (stderr, ".");
120 c = curl_easy_init ();
121 cbc.pos = 0;
122 buf[0] = '\0';
123 sprintf (url, "http://localhost:1080/hw%d", i);
124 curl_easy_setopt (c, CURLOPT_URL, url);
125 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
126 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
127 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
128 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
129 curl_easy_setopt (c, CURLOPT_POST, 1L);
130 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
131 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
132 if (oneone)
133 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
134 else
135 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
136 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
137 // NOTE: use of CONNECTTIMEOUT without also
138 // setting NOSIGNAL results in really weird
139 // crashes on my system!
140 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
141 if (CURLE_OK != (errornum = curl_easy_perform (c)))
142 {
143 fprintf (stderr,
144 "curl_easy_perform failed: `%s'\n",
145 curl_easy_strerror (errornum));
146 curl_easy_cleanup (c);
147 MHD_stop_daemon (d);
148 return 2;
149 }
150 curl_easy_cleanup (c);
151 if ((buf[0] != 'O') || (buf[1] != 'K'))
152 {
153 MHD_stop_daemon (d);
154 return 4;
155 }
156 }
157 MHD_stop_daemon (d);
158 if (LOOPCOUNT >= 99)
159 fprintf (stderr, "\n");
160 return 0;
161}
162
163static int
164testMultithreadedPost ()
165{
166 struct MHD_Daemon *d;
167 CURL *c;
168 char buf[2048];
169 struct CBC cbc;
170 CURLcode errornum;
171 int i;
172 char url[1024];
173
174 cbc.buf = buf;
175 cbc.size = 2048;
176 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
177 1081, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
178 if (d == NULL)
179 return 16;
180 for (i = 0; i < LOOPCOUNT; i++)
181 {
182 if (99 == i % 100)
183 fprintf (stderr, ".");
184 c = curl_easy_init ();
185 cbc.pos = 0;
186 buf[0] = '\0';
187 sprintf (url, "http://localhost:1081/hw%d", i);
188 curl_easy_setopt (c, CURLOPT_URL, url);
189 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
190 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
191 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
192 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
193 curl_easy_setopt (c, CURLOPT_POST, 1L);
194 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
195 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
196 if (oneone)
197 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
198 else
199 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
200 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
201 // NOTE: use of CONNECTTIMEOUT without also
202 // setting NOSIGNAL results in really weird
203 // crashes on my system!
204 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
205 if (CURLE_OK != (errornum = curl_easy_perform (c)))
206 {
207 fprintf (stderr,
208 "curl_easy_perform failed: `%s'\n",
209 curl_easy_strerror (errornum));
210 curl_easy_cleanup (c);
211 MHD_stop_daemon (d);
212 return 32;
213 }
214 curl_easy_cleanup (c);
215 if ((buf[0] != 'O') || (buf[1] != 'K'))
216 {
217 MHD_stop_daemon (d);
218 return 64;
219 }
220 }
221 MHD_stop_daemon (d);
222 if (LOOPCOUNT >= 99)
223 fprintf (stderr, "\n");
224 return 0;
225}
226
227
228static int
229testExternalPost ()
230{
231 struct MHD_Daemon *d;
232 CURL *c;
233 char buf[2048];
234 struct CBC cbc;
235 CURLM *multi;
236 CURLMcode mret;
237 fd_set rs;
238 fd_set ws;
239 fd_set es;
240 int max;
241 int running;
242 struct CURLMsg *msg;
243 time_t start;
244 struct timeval tv;
245 int i;
246 unsigned long long timeout;
247 long ctimeout;
248 char url[1024];
249
250 multi = NULL;
251 cbc.buf = buf;
252 cbc.size = 2048;
253 cbc.pos = 0;
254 d = MHD_start_daemon (MHD_USE_DEBUG,
255 1082, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
256 if (d == NULL)
257 return 256;
258 multi = curl_multi_init ();
259 if (multi == NULL)
260 {
261 MHD_stop_daemon (d);
262 return 512;
263 }
264 for (i = 0; i < LOOPCOUNT; i++)
265 {
266 fprintf (stderr, ".");
267 c = curl_easy_init ();
268 cbc.pos = 0;
269 buf[0] = '\0';
270 sprintf (url, "http://localhost:1082/hw%d", i);
271 curl_easy_setopt (c, CURLOPT_URL, url);
272 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
273 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
274 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
275 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
276 curl_easy_setopt (c, CURLOPT_POST, 1L);
277 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
278 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
279 if (oneone)
280 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
281 else
282 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
283 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
284 // NOTE: use of CONNECTTIMEOUT without also
285 // setting NOSIGNAL results in really weird
286 // crashes on my system!
287 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
288 mret = curl_multi_add_handle (multi, c);
289 if (mret != CURLM_OK)
290 {
291 curl_multi_cleanup (multi);
292 curl_easy_cleanup (c);
293 MHD_stop_daemon (d);
294 return 1024;
295 }
296 start = time (NULL);
297 while ((time (NULL) - start < 5) && (multi != NULL))
298 {
299 max = 0;
300 FD_ZERO (&rs);
301 FD_ZERO (&ws);
302 FD_ZERO (&es);
303 while (CURLM_CALL_MULTI_PERFORM ==
304 curl_multi_perform (multi, &running));
305 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
306 if (mret != CURLM_OK)
307 {
308 curl_multi_remove_handle (multi, c);
309 curl_multi_cleanup (multi);
310 curl_easy_cleanup (c);
311 MHD_stop_daemon (d);
312 return 2048;
313 }
314 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
315 {
316 curl_multi_remove_handle (multi, c);
317 curl_multi_cleanup (multi);
318 curl_easy_cleanup (c);
319 MHD_stop_daemon (d);
320 return 4096;
321 }
322 if (MHD_NO == MHD_get_timeout (d, &timeout))
323 timeout = 100; /* 100ms == INFTY -- CURL bug... */
324 if ((CURLM_OK == curl_multi_timeout (multi, &ctimeout)) &&
325 (ctimeout < timeout) && (ctimeout >= 0))
326 timeout = ctimeout;
327 tv.tv_sec = timeout / 1000;
328 tv.tv_usec = (timeout % 1000) * 1000;
329 select (max + 1, &rs, &ws, &es, &tv);
330 while (CURLM_CALL_MULTI_PERFORM ==
331 curl_multi_perform (multi, &running));
332 if (running == 0)
333 {
334 msg = curl_multi_info_read (multi, &running);
335 if (msg == NULL)
336 break;
337 if (msg->msg == CURLMSG_DONE)
338 {
339 if (msg->data.result != CURLE_OK)
340 printf ("%s failed at %s:%d: `%s'\n",
341 "curl_multi_perform",
342 __FILE__,
343 __LINE__, curl_easy_strerror (msg->data.result));
344 curl_multi_remove_handle (multi, c);
345 curl_easy_cleanup (c);
346 c = NULL;
347 }
348 }
349 MHD_run (d);
350 }
351 if (c != NULL)
352 {
353 curl_multi_remove_handle (multi, c);
354 curl_easy_cleanup (c);
355 }
356 if ((buf[0] != 'O') || (buf[1] != 'K'))
357 {
358 curl_multi_cleanup (multi);
359 MHD_stop_daemon (d);
360 return 8192;
361 }
362 }
363 curl_multi_cleanup (multi);
364 MHD_stop_daemon (d);
365 fprintf (stderr, "\n");
366 return 0;
367}
368
369
370
371int
372main (int argc, char *const *argv)
373{
374 unsigned int errorCount = 0;
375
376 oneone = NULL != strstr (argv[0], "11");
377 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
378 return 2;
379 errorCount += testInternalPost ();
380 errorCount += testMultithreadedPost ();
381 errorCount += testExternalPost ();
382 if (errorCount != 0)
383 fprintf (stderr, "Error (code: %u)\n", errorCount);
384 curl_global_cleanup ();
385 return errorCount != 0; /* 0 == pass */
386}