aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/daemontest_postform.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcurl/daemontest_postform.c')
-rw-r--r--src/testcurl/daemontest_postform.c404
1 files changed, 404 insertions, 0 deletions
diff --git a/src/testcurl/daemontest_postform.c b/src/testcurl/daemontest_postform.c
new file mode 100644
index 00000000..4142b138
--- /dev/null
+++ b/src/testcurl/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}