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