aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2023-05-15 17:50:30 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2023-05-15 17:50:59 +0300
commit9ee937b3a59d44cfc3f74f828515fc6c2d81e681 (patch)
treec3395aa036671a20b13a79e21c3ed0d6de48af13
parent84f3cde4527c56b1b0829c048500c94f8b665a75 (diff)
downloadlibmicrohttpd-9ee937b3a59d44cfc3f74f828515fc6c2d81e681.tar.gz
libmicrohttpd-9ee937b3a59d44cfc3f74f828515fc6c2d81e681.zip
test_postform: updated to support the new libcurl API
Muted libcurl deprecations warnings.
-rw-r--r--src/testcurl/test_postform.c164
1 files changed, 131 insertions, 33 deletions
diff --git a/src/testcurl/test_postform.c b/src/testcurl/test_postform.c
index ff510b6f..289a95de 100644
--- a/src/testcurl/test_postform.c
+++ b/src/testcurl/test_postform.c
@@ -1,7 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2007 Christian Grothoff 3 Copyright (C) 2007 Christian Grothoff
4 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) 4 Copyright (C) 2014-2023 Evgeny Grin (Karlson2k)
5 5
6 libmicrohttpd is free software; you can redistribute it and/or modify 6 libmicrohttpd is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published 7 it under the terms of the GNU General Public License as published
@@ -43,6 +43,18 @@
43#include <unistd.h> 43#include <unistd.h>
44#endif 44#endif
45 45
46#ifndef CURL_VERSION_BITS
47#define CURL_VERSION_BITS(x,y,z) ((x) << 16 | (y) << 8 | (z))
48#endif /* ! CURL_VERSION_BITS */
49#ifndef CURL_AT_LEAST_VERSION
50#define CURL_AT_LEAST_VERSION(x,y,z) \
51 (LIBCURL_VERSION_NUM >= CURL_VERSION_BITS (x, y, z))
52#endif /* ! CURL_AT_LEAST_VERSION */
53
54#if CURL_AT_LEAST_VERSION (7,56,0)
55#define HAS_CURL_MIME 1
56#endif /* CURL_AT_LEAST_VERSION(7,56,0) */
57
46#include "mhd_has_in_name.h" 58#include "mhd_has_in_name.h"
47 59
48#if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2 60#if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2
@@ -173,17 +185,88 @@ ahc_echo (void *cls,
173} 185}
174 186
175 187
176static struct curl_httppost * 188struct mhd_test_postdata
177make_form (void) 189{
190#if defined(HAS_CURL_MIME)
191 curl_mime *mime;
192#else /* ! HAS_CURL_MIME */
193 struct curl_httppost *post;
194#endif /* ! HAS_CURL_MIME */
195};
196
197/* Return non-zero if succeed */
198static int
199add_test_form (CURL *handle, struct mhd_test_postdata *postdata)
178{ 200{
179 struct curl_httppost *post = NULL; 201#if defined(HAS_CURL_MIME)
202 postdata->mime = curl_mime_init (handle);
203 if (NULL == postdata->mime)
204 return 0;
205 else
206 {
207 curl_mimepart *part;
208 part = curl_mime_addpart (postdata->mime);
209 if (NULL != part)
210 {
211 if ( (CURLE_OK == curl_mime_data (part, "daniel",
212 CURL_ZERO_TERMINATED)) &&
213 (CURLE_OK == curl_mime_name (part, "name")) )
214 {
215 part = curl_mime_addpart (postdata->mime);
216 if (NULL != part)
217 {
218 if ( (CURLE_OK == curl_mime_data (part, "curl",
219 CURL_ZERO_TERMINATED)) &&
220 (CURLE_OK == curl_mime_name (part, "project")) )
221 {
222 if (CURLE_OK == curl_easy_setopt (handle,
223 CURLOPT_MIMEPOST, postdata->mime))
224 {
225 return ! 0;
226 }
227 }
228 }
229 }
230 }
231 }
232 curl_mime_free (postdata->mime);
233 postdata->mime = NULL;
234 return 0;
235#else /* ! HAS_CURL_MIME */
236 postdata->post = NULL;
180 struct curl_httppost *last = NULL; 237 struct curl_httppost *last = NULL;
181 238
182 curl_formadd (&post, &last, CURLFORM_COPYNAME, "name", 239 if (0 == curl_formadd (&postdata->post, &last,
183 CURLFORM_COPYCONTENTS, "daniel", CURLFORM_END); 240 CURLFORM_COPYNAME, "name",
184 curl_formadd (&post, &last, CURLFORM_COPYNAME, "project", 241 CURLFORM_COPYCONTENTS, "daniel", CURLFORM_END))
185 CURLFORM_COPYCONTENTS, "curl", CURLFORM_END); 242 {
186 return post; 243 if (0 == curl_formadd (&postdata->post, &last,
244 CURLFORM_COPYNAME, "project",
245 CURLFORM_COPYCONTENTS, "curl", CURLFORM_END))
246 {
247 if (CURLE_OK == curl_easy_setopt (handle,
248 CURLOPT_HTTPPOST, postdata->post))
249 {
250 return ! 0;
251 }
252 }
253 }
254 curl_formfree (postdata->post);
255 return 0;
256#endif /* ! HAS_CURL_MIME */
257}
258
259
260static void
261free_test_form (struct mhd_test_postdata *postdata)
262{
263#if defined(HAS_CURL_MIME)
264 if (NULL != postdata->mime)
265 curl_mime_free (postdata->mime);
266#else /* ! HAS_CURL_MIME */
267 if (NULL != postdata->post)
268 curl_formfree (postdata->post);
269#endif /* ! HAS_CURL_MIME */
187} 270}
188 271
189 272
@@ -195,7 +278,7 @@ testInternalPost (void)
195 char buf[2048]; 278 char buf[2048];
196 struct CBC cbc; 279 struct CBC cbc;
197 CURLcode errornum; 280 CURLcode errornum;
198 struct curl_httppost *pd; 281 struct mhd_test_postdata form;
199 uint16_t port; 282 uint16_t port;
200 283
201 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 284 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
@@ -231,8 +314,6 @@ testInternalPost (void)
231 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 314 curl_easy_setopt (c, CURLOPT_PORT, (long) port);
232 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); 315 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
233 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 316 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
234 pd = make_form ();
235 curl_easy_setopt (c, CURLOPT_HTTPPOST, pd);
236 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 317 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
237 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 318 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
238 if (oneone) 319 if (oneone)
@@ -244,18 +325,24 @@ testInternalPost (void)
244 * setting NOSIGNAL results in really weird 325 * setting NOSIGNAL results in really weird
245 * crashes on my system! */ 326 * crashes on my system! */
246 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 327 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
328 if (! add_test_form (c, &form))
329 {
330 fprintf (stderr, "libcurl form initialisation error.\n");
331 curl_easy_cleanup (c);
332 return 2;
333 }
247 if (CURLE_OK != (errornum = curl_easy_perform (c))) 334 if (CURLE_OK != (errornum = curl_easy_perform (c)))
248 { 335 {
249 fprintf (stderr, 336 fprintf (stderr,
250 "curl_easy_perform failed: `%s'\n", 337 "curl_easy_perform failed: `%s'\n",
251 curl_easy_strerror (errornum)); 338 curl_easy_strerror (errornum));
252 curl_easy_cleanup (c); 339 curl_easy_cleanup (c);
253 curl_formfree (pd); 340 free_test_form (&form);
254 MHD_stop_daemon (d); 341 MHD_stop_daemon (d);
255 return 2; 342 return 2;
256 } 343 }
257 curl_easy_cleanup (c); 344 curl_easy_cleanup (c);
258 curl_formfree (pd); 345 free_test_form (&form);
259 MHD_stop_daemon (d); 346 MHD_stop_daemon (d);
260 if (cbc.pos != strlen ("/hello_world")) 347 if (cbc.pos != strlen ("/hello_world"))
261 return 4; 348 return 4;
@@ -273,7 +360,7 @@ testMultithreadedPost (void)
273 char buf[2048]; 360 char buf[2048];
274 struct CBC cbc; 361 struct CBC cbc;
275 CURLcode errornum; 362 CURLcode errornum;
276 struct curl_httppost *pd; 363 struct mhd_test_postdata form;
277 uint16_t port; 364 uint16_t port;
278 365
279 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 366 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
@@ -310,8 +397,6 @@ testMultithreadedPost (void)
310 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 397 curl_easy_setopt (c, CURLOPT_PORT, (long) port);
311 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); 398 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
312 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 399 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
313 pd = make_form ();
314 curl_easy_setopt (c, CURLOPT_HTTPPOST, pd);
315 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 400 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
316 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 401 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
317 if (oneone) 402 if (oneone)
@@ -323,18 +408,24 @@ testMultithreadedPost (void)
323 * setting NOSIGNAL results in really weird 408 * setting NOSIGNAL results in really weird
324 * crashes on my system! */ 409 * crashes on my system! */
325 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 410 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
411 if (! add_test_form (c, &form))
412 {
413 fprintf (stderr, "libcurl form initialisation error.\n");
414 curl_easy_cleanup (c);
415 return 2;
416 }
326 if (CURLE_OK != (errornum = curl_easy_perform (c))) 417 if (CURLE_OK != (errornum = curl_easy_perform (c)))
327 { 418 {
328 fprintf (stderr, 419 fprintf (stderr,
329 "curl_easy_perform failed: `%s'\n", 420 "curl_easy_perform failed: `%s'\n",
330 curl_easy_strerror (errornum)); 421 curl_easy_strerror (errornum));
331 curl_easy_cleanup (c); 422 curl_easy_cleanup (c);
332 curl_formfree (pd); 423 free_test_form (&form);
333 MHD_stop_daemon (d); 424 MHD_stop_daemon (d);
334 return 32; 425 return 32;
335 } 426 }
336 curl_easy_cleanup (c); 427 curl_easy_cleanup (c);
337 curl_formfree (pd); 428 free_test_form (&form);
338 MHD_stop_daemon (d); 429 MHD_stop_daemon (d);
339 if (cbc.pos != strlen ("/hello_world")) 430 if (cbc.pos != strlen ("/hello_world"))
340 return 64; 431 return 64;
@@ -352,7 +443,7 @@ testMultithreadedPoolPost (void)
352 char buf[2048]; 443 char buf[2048];
353 struct CBC cbc; 444 struct CBC cbc;
354 CURLcode errornum; 445 CURLcode errornum;
355 struct curl_httppost *pd; 446 struct mhd_test_postdata form;
356 uint16_t port; 447 uint16_t port;
357 448
358 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 449 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
@@ -389,8 +480,6 @@ testMultithreadedPoolPost (void)
389 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 480 curl_easy_setopt (c, CURLOPT_PORT, (long) port);
390 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); 481 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
391 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 482 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
392 pd = make_form ();
393 curl_easy_setopt (c, CURLOPT_HTTPPOST, pd);
394 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 483 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
395 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 484 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
396 if (oneone) 485 if (oneone)
@@ -402,18 +491,24 @@ testMultithreadedPoolPost (void)
402 * setting NOSIGNAL results in really weird 491 * setting NOSIGNAL results in really weird
403 * crashes on my system! */ 492 * crashes on my system! */
404 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 493 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
494 if (! add_test_form (c, &form))
495 {
496 fprintf (stderr, "libcurl form initialisation error.\n");
497 curl_easy_cleanup (c);
498 return 2;
499 }
405 if (CURLE_OK != (errornum = curl_easy_perform (c))) 500 if (CURLE_OK != (errornum = curl_easy_perform (c)))
406 { 501 {
407 fprintf (stderr, 502 fprintf (stderr,
408 "curl_easy_perform failed: `%s'\n", 503 "curl_easy_perform failed: `%s'\n",
409 curl_easy_strerror (errornum)); 504 curl_easy_strerror (errornum));
410 curl_easy_cleanup (c); 505 curl_easy_cleanup (c);
411 curl_formfree (pd); 506 free_test_form (&form);
412 MHD_stop_daemon (d); 507 MHD_stop_daemon (d);
413 return 32; 508 return 32;
414 } 509 }
415 curl_easy_cleanup (c); 510 curl_easy_cleanup (c);
416 curl_formfree (pd); 511 free_test_form (&form);
417 MHD_stop_daemon (d); 512 MHD_stop_daemon (d);
418 if (cbc.pos != strlen ("/hello_world")) 513 if (cbc.pos != strlen ("/hello_world"))
419 return 64; 514 return 64;
@@ -445,7 +540,7 @@ testExternalPost (void)
445 struct CURLMsg *msg; 540 struct CURLMsg *msg;
446 time_t start; 541 time_t start;
447 struct timeval tv; 542 struct timeval tv;
448 struct curl_httppost *pd; 543 struct mhd_test_postdata form;
449 uint16_t port; 544 uint16_t port;
450 545
451 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 546 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
@@ -482,8 +577,6 @@ testExternalPost (void)
482 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 577 curl_easy_setopt (c, CURLOPT_PORT, (long) port);
483 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); 578 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
484 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 579 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
485 pd = make_form ();
486 curl_easy_setopt (c, CURLOPT_HTTPPOST, pd);
487 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 580 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
488 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 581 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
489 if (oneone) 582 if (oneone)
@@ -495,13 +588,18 @@ testExternalPost (void)
495 * setting NOSIGNAL results in really weird 588 * setting NOSIGNAL results in really weird
496 * crashes on my system! */ 589 * crashes on my system! */
497 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 590 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
498 591 if (! add_test_form (c, &form))
592 {
593 fprintf (stderr, "libcurl form initialisation error.\n");
594 curl_easy_cleanup (c);
595 return 2;
596 }
499 597
500 multi = curl_multi_init (); 598 multi = curl_multi_init ();
501 if (multi == NULL) 599 if (multi == NULL)
502 { 600 {
503 curl_easy_cleanup (c); 601 curl_easy_cleanup (c);
504 curl_formfree (pd); 602 free_test_form (&form);
505 MHD_stop_daemon (d); 603 MHD_stop_daemon (d);
506 return 512; 604 return 512;
507 } 605 }
@@ -509,7 +607,7 @@ testExternalPost (void)
509 if (mret != CURLM_OK) 607 if (mret != CURLM_OK)
510 { 608 {
511 curl_multi_cleanup (multi); 609 curl_multi_cleanup (multi);
512 curl_formfree (pd); 610 free_test_form (&form);
513 curl_easy_cleanup (c); 611 curl_easy_cleanup (c);
514 MHD_stop_daemon (d); 612 MHD_stop_daemon (d);
515 return 1024; 613 return 1024;
@@ -530,7 +628,7 @@ testExternalPost (void)
530 curl_multi_cleanup (multi); 628 curl_multi_cleanup (multi);
531 curl_easy_cleanup (c); 629 curl_easy_cleanup (c);
532 MHD_stop_daemon (d); 630 MHD_stop_daemon (d);
533 curl_formfree (pd); 631 free_test_form (&form);
534 return 2048; 632 return 2048;
535 } 633 }
536 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock)) 634 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock))
@@ -538,7 +636,7 @@ testExternalPost (void)
538 curl_multi_remove_handle (multi, c); 636 curl_multi_remove_handle (multi, c);
539 curl_multi_cleanup (multi); 637 curl_multi_cleanup (multi);
540 curl_easy_cleanup (c); 638 curl_easy_cleanup (c);
541 curl_formfree (pd); 639 free_test_form (&form);
542 MHD_stop_daemon (d); 640 MHD_stop_daemon (d);
543 return 4096; 641 return 4096;
544 } 642 }
@@ -607,7 +705,7 @@ testExternalPost (void)
607 curl_easy_cleanup (c); 705 curl_easy_cleanup (c);
608 curl_multi_cleanup (multi); 706 curl_multi_cleanup (multi);
609 } 707 }
610 curl_formfree (pd); 708 free_test_form (&form);
611 MHD_stop_daemon (d); 709 MHD_stop_daemon (d);
612 if (cbc.pos != strlen ("/hello_world")) 710 if (cbc.pos != strlen ("/hello_world"))
613 return 8192; 711 return 8192;