aboutsummaryrefslogtreecommitdiff
path: root/src/testzzuf/test_post_form.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testzzuf/test_post_form.c')
-rw-r--r--src/testzzuf/test_post_form.c387
1 files changed, 387 insertions, 0 deletions
diff --git a/src/testzzuf/test_post_form.c b/src/testzzuf/test_post_form.c
new file mode 100644
index 00000000..873c7fef
--- /dev/null
+++ b/src/testzzuf/test_post_form.c
@@ -0,0 +1,387 @@
1/*
2 This file is part of libmicrohttpd
3 (C) 2007, 2008 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 "MHD_config.h"
28#include "platform.h"
29#include <curl/curl.h>
30#include <microhttpd.h>
31#include <stdlib.h>
32#include <string.h>
33#include <time.h>
34
35#ifndef WINDOWS
36#include <unistd.h>
37#endif
38
39
40#include "socat.c"
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
63/**
64 * Note that this post_iterator is not perfect
65 * in that it fails to support incremental processing.
66 * (to be fixed in the future)
67 */
68static int
69post_iterator (void *cls,
70 enum MHD_ValueKind kind,
71 const char *key,
72 const char *filename,
73 const char *content_type,
74 const char *transfer_encoding,
75 const char *value, uint64_t off, size_t size)
76{
77 int *eok = cls;
78
79 if (key == NULL)
80 return MHD_YES;
81#if 0
82 fprintf (stderr, "PI sees %s-%.*s\n", key, size, value);
83#endif
84 if ((0 == strcmp (key, "name")) &&
85 (size == strlen ("daniel")) && (0 == strncmp (value, "daniel", size)))
86 (*eok) |= 1;
87 if ((0 == strcmp (key, "project")) &&
88 (size == strlen ("curl")) && (0 == strncmp (value, "curl", size)))
89 (*eok) |= 2;
90 return MHD_YES;
91}
92
93static int
94ahc_echo (void *cls,
95 struct MHD_Connection *connection,
96 const char *url,
97 const char *method,
98 const char *version,
99 const char *upload_data, size_t *upload_data_size,
100 void **unused)
101{
102 static int eok;
103 struct MHD_Response *response;
104 struct MHD_PostProcessor *pp;
105 int ret;
106
107 if (0 != strcmp ("POST", method))
108 {
109 return MHD_NO; /* unexpected method */
110 }
111 pp = *unused;
112 if (pp == NULL)
113 {
114 eok = 0;
115 pp = MHD_create_post_processor (connection, 1024, &post_iterator, &eok);
116 if (pp == NULL)
117 return MHD_NO;
118 *unused = pp;
119 }
120 MHD_post_process (pp, upload_data, *upload_data_size);
121 if ((eok == 3) && (0 == *upload_data_size))
122 {
123 response = MHD_create_response_from_buffer (strlen (url),
124 (void *) url,
125 MHD_RESPMEM_MUST_COPY);
126 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
127 MHD_destroy_response (response);
128 MHD_destroy_post_processor (pp);
129 *unused = NULL;
130 return ret;
131 }
132 *upload_data_size = 0;
133 return MHD_YES;
134}
135
136static struct curl_httppost *
137make_form ()
138{
139 struct curl_httppost *post = NULL;
140 struct curl_httppost *last = NULL;
141
142 curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
143 CURLFORM_COPYCONTENTS, "daniel", CURLFORM_END);
144 curl_formadd (&post, &last, CURLFORM_COPYNAME, "project",
145 CURLFORM_COPYCONTENTS, "curl", CURLFORM_END);
146 return post;
147}
148
149
150static int
151testInternalPost ()
152{
153 struct MHD_Daemon *d;
154 CURL *c;
155 char buf[2048];
156 struct CBC cbc;
157 int i;
158 struct curl_httppost *pd;
159
160 cbc.buf = buf;
161 cbc.size = 2048;
162 cbc.pos = 0;
163 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY /* | MHD_USE_DEBUG */ ,
164 11080, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
165 if (d == NULL)
166 return 1;
167 zzuf_socat_start ();
168 for (i = 0; i < LOOP_COUNT; i++)
169 {
170 fprintf (stderr, ".");
171 c = curl_easy_init ();
172 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11081/hello_world");
173 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
174 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
175 pd = make_form ();
176 curl_easy_setopt (c, CURLOPT_HTTPPOST, pd);
177 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
178 curl_easy_setopt (c, CURLOPT_TIMEOUT_MS, CURL_TIMEOUT);
179 if (oneone)
180 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
181 else
182 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
183 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT_MS, CURL_TIMEOUT);
184 // NOTE: use of CONNECTTIMEOUT without also
185 // setting NOSIGNAL results in really weird
186 // crashes on my system!
187 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
188 curl_easy_perform (c);
189 curl_easy_cleanup (c);
190 curl_formfree (pd);
191 }
192 fprintf (stderr, "\n");
193 zzuf_socat_stop ();
194 MHD_stop_daemon (d);
195 return 0;
196}
197
198static int
199testMultithreadedPost ()
200{
201 struct MHD_Daemon *d;
202 CURL *c;
203 char buf[2048];
204 struct CBC cbc;
205 int i;
206 struct curl_httppost *pd;
207
208 cbc.buf = buf;
209 cbc.size = 2048;
210 cbc.pos = 0;
211 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION /* | MHD_USE_DEBUG */ ,
212 11080, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
213 if (d == NULL)
214 return 16;
215 zzuf_socat_start ();
216 for (i = 0; i < LOOP_COUNT; i++)
217 {
218 fprintf (stderr, ".");
219 c = curl_easy_init ();
220 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11081/hello_world");
221 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
222 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
223 pd = make_form ();
224 curl_easy_setopt (c, CURLOPT_HTTPPOST, pd);
225 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
226 curl_easy_setopt (c, CURLOPT_TIMEOUT_MS, CURL_TIMEOUT);
227 if (oneone)
228 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
229 else
230 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
231 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT_MS, CURL_TIMEOUT);
232 // NOTE: use of CONNECTTIMEOUT without also
233 // setting NOSIGNAL results in really weird
234 // crashes on my system!
235 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
236 curl_easy_perform (c);
237 curl_easy_cleanup (c);
238 curl_formfree (pd);
239 }
240 fprintf (stderr, "\n");
241 zzuf_socat_stop ();
242 MHD_stop_daemon (d);
243 return 0;
244}
245
246
247static int
248testExternalPost ()
249{
250 struct MHD_Daemon *d;
251 CURL *c;
252 char buf[2048];
253 struct CBC cbc;
254 CURLM *multi;
255 CURLMcode mret;
256 fd_set rs;
257 fd_set ws;
258 fd_set es;
259 int max;
260 int running;
261 time_t start;
262 struct timeval tv;
263 struct curl_httppost *pd;
264 int i;
265
266 multi = NULL;
267 cbc.buf = buf;
268 cbc.size = 2048;
269 cbc.pos = 0;
270 d = MHD_start_daemon (MHD_NO_FLAG /* | MHD_USE_DEBUG */ ,
271 1082, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
272 if (d == NULL)
273 return 256;
274 multi = curl_multi_init ();
275 if (multi == NULL)
276 {
277 MHD_stop_daemon (d);
278 return 512;
279 }
280 zzuf_socat_start ();
281 for (i = 0; i < LOOP_COUNT; i++)
282 {
283 fprintf (stderr, ".");
284
285 c = curl_easy_init ();
286 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world");
287 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
288 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
289 pd = make_form ();
290 curl_easy_setopt (c, CURLOPT_HTTPPOST, pd);
291 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
292 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
293 if (oneone)
294 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
295 else
296 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
297 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
298 // NOTE: use of CONNECTTIMEOUT without also
299 // setting NOSIGNAL results in really weird
300 // crashes on my system!
301 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
302
303
304 mret = curl_multi_add_handle (multi, c);
305 if (mret != CURLM_OK)
306 {
307 curl_multi_cleanup (multi);
308 curl_formfree (pd);
309 curl_easy_cleanup (c);
310 zzuf_socat_stop ();
311 MHD_stop_daemon (d);
312 return 1024;
313 }
314 start = time (NULL);
315 while ((time (NULL) - start < 5) && (c != NULL))
316 {
317 max = 0;
318 FD_ZERO (&rs);
319 FD_ZERO (&ws);
320 FD_ZERO (&es);
321 curl_multi_perform (multi, &running);
322 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
323 if (mret != CURLM_OK)
324 {
325 curl_multi_remove_handle (multi, c);
326 curl_multi_cleanup (multi);
327 curl_easy_cleanup (c);
328 zzuf_socat_stop ();
329 MHD_stop_daemon (d);
330 curl_formfree (pd);
331 return 2048;
332 }
333 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
334 {
335 curl_multi_remove_handle (multi, c);
336 curl_multi_cleanup (multi);
337 curl_easy_cleanup (c);
338 curl_formfree (pd);
339 zzuf_socat_stop ();
340 MHD_stop_daemon (d);
341 return 4096;
342 }
343 tv.tv_sec = 0;
344 tv.tv_usec = 1000;
345 select (max + 1, &rs, &ws, &es, &tv);
346 curl_multi_perform (multi, &running);
347 if (running == 0)
348 {
349 curl_multi_info_read (multi, &running);
350 curl_multi_remove_handle (multi, c);
351 curl_easy_cleanup (c);
352 c = NULL;
353 }
354 MHD_run (d);
355 }
356 if (c != NULL)
357 {
358 curl_multi_remove_handle (multi, c);
359 curl_easy_cleanup (c);
360 }
361 curl_formfree (pd);
362 }
363 fprintf (stderr, "\n");
364 zzuf_socat_stop ();
365
366 MHD_stop_daemon (d);
367 return 0;
368}
369
370
371
372int
373main (int argc, char *const *argv)
374{
375 unsigned int errorCount = 0;
376
377 oneone = NULL != strstr (argv[0], "11");
378 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
379 return 2;
380 errorCount += testInternalPost ();
381 errorCount += testMultithreadedPost ();
382 errorCount += testExternalPost ();
383 if (errorCount != 0)
384 fprintf (stderr, "Error (code: %u)\n", errorCount);
385 curl_global_cleanup ();
386 return errorCount != 0; /* 0 == pass */
387}