aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/testcurl/Makefile.am12
-rw-r--r--src/testcurl/daemontest_post.c (renamed from src/daemon/daemontest_post.c)0
-rw-r--r--src/testcurl/daemontest_post_loop.c (renamed from src/daemon/daemontest_post_loop.c)0
-rw-r--r--src/testcurl/daemontest_postform.c (renamed from src/daemon/daemontest_postform.c)0
-rw-r--r--src/testzzuf/Makefile.am12
-rw-r--r--src/testzzuf/daemontest_post.c376
-rw-r--r--src/testzzuf/daemontest_post_loop.c386
-rw-r--r--src/testzzuf/daemontest_postform.c404
8 files changed, 1166 insertions, 24 deletions
diff --git a/src/testcurl/Makefile.am b/src/testcurl/Makefile.am
index d220ef08..608c004f 100644
--- a/src/testcurl/Makefile.am
+++ b/src/testcurl/Makefile.am
@@ -3,8 +3,6 @@ SUBDIRS = .
3INCLUDES = -I$(top_srcdir)/src/include 3INCLUDES = -I$(top_srcdir)/src/include
4 4
5check_PROGRAMS = \ 5check_PROGRAMS = \
6 postprocessor_test \
7 daemontest \
8 daemontest_get \ 6 daemontest_get \
9 daemontest_post \ 7 daemontest_post \
10 daemontest_postform \ 8 daemontest_postform \
@@ -23,16 +21,6 @@ check_PROGRAMS = \
23 21
24TESTS = $(check_PROGRAMS) 22TESTS = $(check_PROGRAMS)
25 23
26daemontest_SOURCES = \
27 daemontest.c
28daemontest_LDADD = \
29 $(top_builddir)/src/daemon/libmicrohttpd.la
30
31postprocessor_test_SOURCES = \
32 postprocessor_test.c
33postprocessor_test_LDADD = \
34 $(top_builddir)/src/daemon/libmicrohttpd.la
35
36daemontest_get_SOURCES = \ 24daemontest_get_SOURCES = \
37 daemontest_get.c 25 daemontest_get.c
38daemontest_get_LDADD = \ 26daemontest_get_LDADD = \
diff --git a/src/daemon/daemontest_post.c b/src/testcurl/daemontest_post.c
index 335377e6..335377e6 100644
--- a/src/daemon/daemontest_post.c
+++ b/src/testcurl/daemontest_post.c
diff --git a/src/daemon/daemontest_post_loop.c b/src/testcurl/daemontest_post_loop.c
index 8630304e..8630304e 100644
--- a/src/daemon/daemontest_post_loop.c
+++ b/src/testcurl/daemontest_post_loop.c
diff --git a/src/daemon/daemontest_postform.c b/src/testcurl/daemontest_postform.c
index 4142b138..4142b138 100644
--- a/src/daemon/daemontest_postform.c
+++ b/src/testcurl/daemontest_postform.c
diff --git a/src/testzzuf/Makefile.am b/src/testzzuf/Makefile.am
index b48b75be..0ebaf893 100644
--- a/src/testzzuf/Makefile.am
+++ b/src/testzzuf/Makefile.am
@@ -3,8 +3,6 @@ SUBDIRS = .
3INCLUDES = -I$(top_srcdir)/src/include 3INCLUDES = -I$(top_srcdir)/src/include
4 4
5check_PROGRAMS = \ 5check_PROGRAMS = \
6 postprocessor_test \
7 daemontest \
8 daemontest_get \ 6 daemontest_get \
9 daemontest_post \ 7 daemontest_post \
10 daemontest_postform \ 8 daemontest_postform \
@@ -23,16 +21,6 @@ check_PROGRAMS = \
23 21
24TESTS = $(check_PROGRAMS) 22TESTS = $(check_PROGRAMS)
25 23
26daemontest_SOURCES = \
27 daemontest.c
28daemontest_LDADD = \
29 $(top_builddir)/src/daemon/libmicrohttpd.la
30
31postprocessor_test_SOURCES = \
32 postprocessor_test.c
33postprocessor_test_LDADD = \
34 $(top_builddir)/src/daemon/libmicrohttpd.la
35
36daemontest_get_SOURCES = \ 24daemontest_get_SOURCES = \
37 daemontest_get.c 25 daemontest_get.c
38daemontest_get_LDADD = \ 26daemontest_get_LDADD = \
diff --git a/src/testzzuf/daemontest_post.c b/src/testzzuf/daemontest_post.c
new file mode 100644
index 00000000..335377e6
--- /dev/null
+++ b/src/testzzuf/daemontest_post.c
@@ -0,0 +1,376 @@
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.c
23 * @brief Testcase for libmicrohttpd POST operations using URL-encoding
24 * @author Christian Grothoff
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 "name=daniel&project=curl"
39
40static int oneone;
41
42struct CBC
43{
44 char *buf;
45 size_t pos;
46 size_t size;
47};
48
49static size_t
50copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
51{
52 struct CBC *cbc = ctx;
53
54 if (cbc->pos + size * nmemb > cbc->size)
55 return 0; /* overflow */
56 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
57 cbc->pos += size * nmemb;
58 return size * nmemb;
59}
60
61/**
62 * Note that this post_iterator is not perfect
63 * in that it fails to support incremental processing.
64 * (to be fixed in the future)
65 */
66static int
67post_iterator (void *cls,
68 enum MHD_ValueKind kind,
69 const char *key,
70 const char *filename,
71 const char *content_type,
72 const char *transfer_encoding,
73 const char *value, size_t off, size_t size)
74{
75 int *eok = cls;
76
77 if ((0 == strcmp (key, "name")) &&
78 (size == strlen ("daniel")) && (0 == strncmp (value, "daniel", size)))
79 (*eok) |= 1;
80 if ((0 == strcmp (key, "project")) &&
81 (size == strlen ("curl")) && (0 == strncmp (value, "curl", size)))
82 (*eok) |= 2;
83 return MHD_YES;
84}
85
86static int
87ahc_echo (void *cls,
88 struct MHD_Connection *connection,
89 const char *url,
90 const char *method,
91 const char *version,
92 const char *upload_data, unsigned int *upload_data_size,
93 void **unused)
94{
95 static int eok;
96 struct MHD_Response *response;
97 struct MHD_PostProcessor *pp;
98 int ret;
99
100 if (0 != strcmp ("POST", method))
101 {
102 printf ("METHOD: %s\n", method);
103 return MHD_NO; /* unexpected method */
104 }
105 pp = *unused;
106 if (pp == NULL)
107 {
108 eok = 0;
109 pp = MHD_create_post_processor (connection, 1024, &post_iterator, &eok);
110 *unused = pp;
111 }
112 MHD_post_process (pp, upload_data, *upload_data_size);
113 if ((eok == 3) && (0 == *upload_data_size))
114 {
115 response = MHD_create_response_from_data (strlen (url),
116 (void *) url,
117 MHD_NO, MHD_YES);
118 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
119 MHD_destroy_response (response);
120 MHD_destroy_post_processor (pp);
121 *unused = NULL;
122 return ret;
123 }
124 *upload_data_size = 0;
125 return MHD_YES;
126}
127
128
129static int
130testInternalPost ()
131{
132 struct MHD_Daemon *d;
133 CURL *c;
134 char buf[2048];
135 struct CBC cbc;
136 CURLcode errornum;
137
138 cbc.buf = buf;
139 cbc.size = 2048;
140 cbc.pos = 0;
141 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
142 1080, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
143 if (d == NULL)
144 return 1;
145 c = curl_easy_init ();
146 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world");
147 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
148 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
149 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
150 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
151 curl_easy_setopt (c, CURLOPT_POST, 1L);
152 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
153 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
154 if (oneone)
155 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
156 else
157 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
158 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
159 // NOTE: use of CONNECTTIMEOUT without also
160 // setting NOSIGNAL results in really weird
161 // crashes on my system!
162 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
163 if (CURLE_OK != (errornum = curl_easy_perform (c)))
164 {
165 fprintf (stderr,
166 "curl_easy_perform failed: `%s'\n",
167 curl_easy_strerror (errornum));
168 curl_easy_cleanup (c);
169 MHD_stop_daemon (d);
170 return 2;
171 }
172 curl_easy_cleanup (c);
173 MHD_stop_daemon (d);
174 if (cbc.pos != strlen ("/hello_world"))
175 return 4;
176 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
177 return 8;
178 return 0;
179}
180
181static int
182testMultithreadedPost ()
183{
184 struct MHD_Daemon *d;
185 CURL *c;
186 char buf[2048];
187 struct CBC cbc;
188 CURLcode errornum;
189
190 cbc.buf = buf;
191 cbc.size = 2048;
192 cbc.pos = 0;
193 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
194 1081, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
195 if (d == NULL)
196 return 16;
197 c = curl_easy_init ();
198 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world");
199 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
200 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
201 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
202 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
203 curl_easy_setopt (c, CURLOPT_POST, 1L);
204 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
205 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
206 if (oneone)
207 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
208 else
209 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
210 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
211 // NOTE: use of CONNECTTIMEOUT without also
212 // setting NOSIGNAL results in really weird
213 // crashes on my system!
214 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
215 if (CURLE_OK != (errornum = curl_easy_perform (c)))
216 {
217 fprintf (stderr,
218 "curl_easy_perform failed: `%s'\n",
219 curl_easy_strerror (errornum));
220 curl_easy_cleanup (c);
221 MHD_stop_daemon (d);
222 return 32;
223 }
224 curl_easy_cleanup (c);
225 MHD_stop_daemon (d);
226 if (cbc.pos != strlen ("/hello_world"))
227 return 64;
228 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
229 return 128;
230 return 0;
231}
232
233
234static int
235testExternalPost ()
236{
237 struct MHD_Daemon *d;
238 CURL *c;
239 char buf[2048];
240 struct CBC cbc;
241 CURLM *multi;
242 CURLMcode mret;
243 fd_set rs;
244 fd_set ws;
245 fd_set es;
246 int max;
247 int running;
248 struct CURLMsg *msg;
249 time_t start;
250 struct timeval tv;
251
252 multi = NULL;
253 cbc.buf = buf;
254 cbc.size = 2048;
255 cbc.pos = 0;
256 d = MHD_start_daemon (MHD_USE_DEBUG,
257 1082, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
258 if (d == NULL)
259 return 256;
260 c = curl_easy_init ();
261 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world");
262 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
263 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
264 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
265 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
266 curl_easy_setopt (c, CURLOPT_POST, 1L);
267 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
268 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
269 if (oneone)
270 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
271 else
272 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
273 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
274 // NOTE: use of CONNECTTIMEOUT without also
275 // setting NOSIGNAL results in really weird
276 // crashes on my system!
277 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
278
279
280 multi = curl_multi_init ();
281 if (multi == NULL)
282 {
283 curl_easy_cleanup (c);
284 MHD_stop_daemon (d);
285 return 512;
286 }
287 mret = curl_multi_add_handle (multi, c);
288 if (mret != CURLM_OK)
289 {
290 curl_multi_cleanup (multi);
291 curl_easy_cleanup (c);
292 MHD_stop_daemon (d);
293 return 1024;
294 }
295 start = time (NULL);
296 while ((time (NULL) - start < 5) && (multi != NULL))
297 {
298 max = 0;
299 FD_ZERO (&rs);
300 FD_ZERO (&ws);
301 FD_ZERO (&es);
302 curl_multi_perform (multi, &running);
303 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
304 if (mret != CURLM_OK)
305 {
306 curl_multi_remove_handle (multi, c);
307 curl_multi_cleanup (multi);
308 curl_easy_cleanup (c);
309 MHD_stop_daemon (d);
310 return 2048;
311 }
312 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
313 {
314 curl_multi_remove_handle (multi, c);
315 curl_multi_cleanup (multi);
316 curl_easy_cleanup (c);
317 MHD_stop_daemon (d);
318 return 4096;
319 }
320 tv.tv_sec = 0;
321 tv.tv_usec = 1000;
322 select (max + 1, &rs, &ws, &es, &tv);
323 curl_multi_perform (multi, &running);
324 if (running == 0)
325 {
326 msg = curl_multi_info_read (multi, &running);
327 if (msg == NULL)
328 break;
329 if (msg->msg == CURLMSG_DONE)
330 {
331 if (msg->data.result != CURLE_OK)
332 printf ("%s failed at %s:%d: `%s'\n",
333 "curl_multi_perform",
334 __FILE__,
335 __LINE__, curl_easy_strerror (msg->data.result));
336 curl_multi_remove_handle (multi, c);
337 curl_multi_cleanup (multi);
338 curl_easy_cleanup (c);
339 c = NULL;
340 multi = NULL;
341 }
342 }
343 MHD_run (d);
344 }
345 if (multi != NULL)
346 {
347 curl_multi_remove_handle (multi, c);
348 curl_easy_cleanup (c);
349 curl_multi_cleanup (multi);
350 }
351 MHD_stop_daemon (d);
352 if (cbc.pos != strlen ("/hello_world"))
353 return 8192;
354 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
355 return 16384;
356 return 0;
357}
358
359
360
361int
362main (int argc, char *const *argv)
363{
364 unsigned int errorCount = 0;
365
366 oneone = NULL != strstr (argv[0], "11");
367 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
368 return 2;
369 errorCount += testInternalPost ();
370 errorCount += testMultithreadedPost ();
371 errorCount += testExternalPost ();
372 if (errorCount != 0)
373 fprintf (stderr, "Error (code: %u)\n", errorCount);
374 curl_global_cleanup ();
375 return errorCount != 0; /* 0 == pass */
376}
diff --git a/src/testzzuf/daemontest_post_loop.c b/src/testzzuf/daemontest_post_loop.c
new file mode 100644
index 00000000..8630304e
--- /dev/null
+++ b/src/testzzuf/daemontest_post_loop.c
@@ -0,0 +1,386 @@
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}
diff --git a/src/testzzuf/daemontest_postform.c b/src/testzzuf/daemontest_postform.c
new file mode 100644
index 00000000..4142b138
--- /dev/null
+++ b/src/testzzuf/daemontest_postform.c
@@ -0,0 +1,404 @@
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.c
23 * @brief Testcase for libmicrohttpd POST operations using multipart/postform data
24 * @author Christian Grothoff
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
38static int oneone;
39
40struct CBC
41{
42 char *buf;
43 size_t pos;
44 size_t size;
45};
46
47static size_t
48copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
49{
50 struct CBC *cbc = ctx;
51
52 if (cbc->pos + size * nmemb > cbc->size)
53 return 0; /* overflow */
54 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
55 cbc->pos += size * nmemb;
56 return size * nmemb;
57}
58
59/**
60 * Note that this post_iterator is not perfect
61 * in that it fails to support incremental processing.
62 * (to be fixed in the future)
63 */
64static int
65post_iterator (void *cls,
66 enum MHD_ValueKind kind,
67 const char *key,
68 const char *filename,
69 const char *content_type,
70 const char *transfer_encoding,
71 const char *value, size_t off, size_t size)
72{
73 int *eok = cls;
74
75#if 0
76 fprintf (stderr, "PI sees %s-%.*s\n", key, size, value);
77#endif
78 if ((0 == strcmp (key, "name")) &&
79 (size == strlen ("daniel")) && (0 == strncmp (value, "daniel", size)))
80 (*eok) |= 1;
81 if ((0 == strcmp (key, "project")) &&
82 (size == strlen ("curl")) && (0 == strncmp (value, "curl", size)))
83 (*eok) |= 2;
84 return MHD_YES;
85}
86
87static int
88ahc_echo (void *cls,
89 struct MHD_Connection *connection,
90 const char *url,
91 const char *method,
92 const char *version,
93 const char *upload_data, unsigned int *upload_data_size,
94 void **unused)
95{
96 static int eok;
97 struct MHD_Response *response;
98 struct MHD_PostProcessor *pp;
99 int ret;
100
101 if (0 != strcmp ("POST", method))
102 {
103 printf ("METHOD: %s\n", method);
104 return MHD_NO; /* unexpected method */
105 }
106 pp = *unused;
107 if (pp == NULL)
108 {
109 eok = 0;
110 pp = MHD_create_post_processor (connection, 1024, &post_iterator, &eok);
111 if (pp == NULL)
112 abort ();
113 *unused = pp;
114 }
115 MHD_post_process (pp, upload_data, *upload_data_size);
116 if ((eok == 3) && (0 == *upload_data_size))
117 {
118 response = MHD_create_response_from_data (strlen (url),
119 (void *) url,
120 MHD_NO, MHD_YES);
121 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
122 MHD_destroy_response (response);
123 MHD_destroy_post_processor (pp);
124 *unused = NULL;
125 return ret;
126 }
127 *upload_data_size = 0;
128 return MHD_YES;
129}
130
131static struct curl_httppost *
132make_form ()
133{
134 struct curl_httppost *post = NULL;
135 struct curl_httppost *last = NULL;
136
137 curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
138 CURLFORM_COPYCONTENTS, "daniel", CURLFORM_END);
139 curl_formadd (&post, &last, CURLFORM_COPYNAME, "project",
140 CURLFORM_COPYCONTENTS, "curl", CURLFORM_END);
141 return post;
142}
143
144
145static int
146testInternalPost ()
147{
148 struct MHD_Daemon *d;
149 CURL *c;
150 char buf[2048];
151 struct CBC cbc;
152 CURLcode errornum;
153 struct curl_httppost *pd;
154
155 cbc.buf = buf;
156 cbc.size = 2048;
157 cbc.pos = 0;
158 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
159 1080, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
160 if (d == NULL)
161 return 1;
162 c = curl_easy_init ();
163 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world");
164 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
165 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
166 pd = make_form ();
167 curl_easy_setopt (c, CURLOPT_HTTPPOST, pd);
168 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
169 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
170 if (oneone)
171 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
172 else
173 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
174 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
175 // NOTE: use of CONNECTTIMEOUT without also
176 // setting NOSIGNAL results in really weird
177 // crashes on my system!
178 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
179 if (CURLE_OK != (errornum = curl_easy_perform (c)))
180 {
181 fprintf (stderr,
182 "curl_easy_perform failed: `%s'\n",
183 curl_easy_strerror (errornum));
184 curl_easy_cleanup (c);
185 curl_formfree (pd);
186 MHD_stop_daemon (d);
187 return 2;
188 }
189 curl_easy_cleanup (c);
190 curl_formfree (pd);
191 MHD_stop_daemon (d);
192 if (cbc.pos != strlen ("/hello_world"))
193 return 4;
194 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
195 return 8;
196 return 0;
197}
198
199static int
200testMultithreadedPost ()
201{
202 struct MHD_Daemon *d;
203 CURL *c;
204 char buf[2048];
205 struct CBC cbc;
206 CURLcode errornum;
207 struct curl_httppost *pd;
208
209 cbc.buf = buf;
210 cbc.size = 2048;
211 cbc.pos = 0;
212 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
213 1081, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
214 if (d == NULL)
215 return 16;
216 c = curl_easy_init ();
217 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world");
218 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
219 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
220 pd = make_form ();
221 curl_easy_setopt (c, CURLOPT_HTTPPOST, pd);
222 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
223 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
224 if (oneone)
225 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
226 else
227 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
228 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L);
229 // NOTE: use of CONNECTTIMEOUT without also
230 // setting NOSIGNAL results in really weird
231 // crashes on my system!
232 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
233 if (CURLE_OK != (errornum = curl_easy_perform (c)))
234 {
235 fprintf (stderr,
236 "curl_easy_perform failed: `%s'\n",
237 curl_easy_strerror (errornum));
238 curl_easy_cleanup (c);
239 curl_formfree (pd);
240 MHD_stop_daemon (d);
241 return 32;
242 }
243 curl_easy_cleanup (c);
244 curl_formfree (pd);
245 MHD_stop_daemon (d);
246 if (cbc.pos != strlen ("/hello_world"))
247 return 64;
248 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
249 return 128;
250 return 0;
251}
252
253
254static int
255testExternalPost ()
256{
257 struct MHD_Daemon *d;
258 CURL *c;
259 char buf[2048];
260 struct CBC cbc;
261 CURLM *multi;
262 CURLMcode mret;
263 fd_set rs;
264 fd_set ws;
265 fd_set es;
266 int max;
267 int running;
268 struct CURLMsg *msg;
269 time_t start;
270 struct timeval tv;
271 struct curl_httppost *pd;
272
273 multi = NULL;
274 cbc.buf = buf;
275 cbc.size = 2048;
276 cbc.pos = 0;
277 d = MHD_start_daemon (MHD_USE_DEBUG,
278 1082, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
279 if (d == NULL)
280 return 256;
281 c = curl_easy_init ();
282 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world");
283 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
284 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
285 pd = make_form ();
286 curl_easy_setopt (c, CURLOPT_HTTPPOST, pd);
287 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
288 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
289 if (oneone)
290 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
291 else
292 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
293 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
294 // NOTE: use of CONNECTTIMEOUT without also
295 // setting NOSIGNAL results in really weird
296 // crashes on my system!
297 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
298
299
300 multi = curl_multi_init ();
301 if (multi == NULL)
302 {
303 curl_easy_cleanup (c);
304 curl_formfree (pd);
305 MHD_stop_daemon (d);
306 return 512;
307 }
308 mret = curl_multi_add_handle (multi, c);
309 if (mret != CURLM_OK)
310 {
311 curl_multi_cleanup (multi);
312 curl_formfree (pd);
313 curl_easy_cleanup (c);
314 MHD_stop_daemon (d);
315 return 1024;
316 }
317 start = time (NULL);
318 while ((time (NULL) - start < 5) && (multi != NULL))
319 {
320 max = 0;
321 FD_ZERO (&rs);
322 FD_ZERO (&ws);
323 FD_ZERO (&es);
324 curl_multi_perform (multi, &running);
325 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
326 if (mret != CURLM_OK)
327 {
328 curl_multi_remove_handle (multi, c);
329 curl_multi_cleanup (multi);
330 curl_easy_cleanup (c);
331 MHD_stop_daemon (d);
332 curl_formfree (pd);
333 return 2048;
334 }
335 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
336 {
337 curl_multi_remove_handle (multi, c);
338 curl_multi_cleanup (multi);
339 curl_easy_cleanup (c);
340 curl_formfree (pd);
341 MHD_stop_daemon (d);
342 return 4096;
343 }
344 tv.tv_sec = 0;
345 tv.tv_usec = 1000;
346 select (max + 1, &rs, &ws, &es, &tv);
347 curl_multi_perform (multi, &running);
348 if (running == 0)
349 {
350 msg = curl_multi_info_read (multi, &running);
351 if (msg == NULL)
352 break;
353 if (msg->msg == CURLMSG_DONE)
354 {
355 if (msg->data.result != CURLE_OK)
356 printf ("%s failed at %s:%d: `%s'\n",
357 "curl_multi_perform",
358 __FILE__,
359 __LINE__, curl_easy_strerror (msg->data.result));
360 curl_multi_remove_handle (multi, c);
361 curl_multi_cleanup (multi);
362 curl_easy_cleanup (c);
363 c = NULL;
364 multi = NULL;
365 }
366 }
367 MHD_run (d);
368 }
369 if (multi != NULL)
370 {
371 curl_multi_remove_handle (multi, c);
372 curl_easy_cleanup (c);
373 curl_multi_cleanup (multi);
374 }
375 curl_formfree (pd);
376 MHD_stop_daemon (d);
377 if (cbc.pos != strlen ("/hello_world"))
378 return 8192;
379 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
380 return 16384;
381 return 0;
382}
383
384
385
386int
387main (int argc, char *const *argv)
388{
389 unsigned int errorCount = 0;
390
391 oneone = NULL != strstr (argv[0], "11");
392 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
393 return 2;
394 errorCount += testInternalPost ();
395 if (0)
396 {
397 errorCount += testMultithreadedPost ();
398 errorCount += testExternalPost ();
399 }
400 if (errorCount != 0)
401 fprintf (stderr, "Error (code: %u)\n", errorCount);
402 curl_global_cleanup ();
403 return errorCount != 0; /* 0 == pass */
404}