libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 9ee937b3a59d44cfc3f74f828515fc6c2d81e681
parent 84f3cde4527c56b1b0829c048500c94f8b665a75
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Mon, 15 May 2023 17:50:30 +0300

test_postform: updated to support the new libcurl API

Muted libcurl deprecations warnings.

Diffstat:
Msrc/testcurl/test_postform.c | 164+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 131 insertions(+), 33 deletions(-)

diff --git a/src/testcurl/test_postform.c b/src/testcurl/test_postform.c @@ -1,7 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2007 Christian Grothoff - Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) + Copyright (C) 2014-2023 Evgeny Grin (Karlson2k) libmicrohttpd is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -43,6 +43,18 @@ #include <unistd.h> #endif +#ifndef CURL_VERSION_BITS +#define CURL_VERSION_BITS(x,y,z) ((x) << 16 | (y) << 8 | (z)) +#endif /* ! CURL_VERSION_BITS */ +#ifndef CURL_AT_LEAST_VERSION +#define CURL_AT_LEAST_VERSION(x,y,z) \ + (LIBCURL_VERSION_NUM >= CURL_VERSION_BITS (x, y, z)) +#endif /* ! CURL_AT_LEAST_VERSION */ + +#if CURL_AT_LEAST_VERSION (7,56,0) +#define HAS_CURL_MIME 1 +#endif /* CURL_AT_LEAST_VERSION(7,56,0) */ + #include "mhd_has_in_name.h" #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2 @@ -173,17 +185,88 @@ ahc_echo (void *cls, } -static struct curl_httppost * -make_form (void) +struct mhd_test_postdata +{ +#if defined(HAS_CURL_MIME) + curl_mime *mime; +#else /* ! HAS_CURL_MIME */ + struct curl_httppost *post; +#endif /* ! HAS_CURL_MIME */ +}; + +/* Return non-zero if succeed */ +static int +add_test_form (CURL *handle, struct mhd_test_postdata *postdata) { - struct curl_httppost *post = NULL; +#if defined(HAS_CURL_MIME) + postdata->mime = curl_mime_init (handle); + if (NULL == postdata->mime) + return 0; + else + { + curl_mimepart *part; + part = curl_mime_addpart (postdata->mime); + if (NULL != part) + { + if ( (CURLE_OK == curl_mime_data (part, "daniel", + CURL_ZERO_TERMINATED)) && + (CURLE_OK == curl_mime_name (part, "name")) ) + { + part = curl_mime_addpart (postdata->mime); + if (NULL != part) + { + if ( (CURLE_OK == curl_mime_data (part, "curl", + CURL_ZERO_TERMINATED)) && + (CURLE_OK == curl_mime_name (part, "project")) ) + { + if (CURLE_OK == curl_easy_setopt (handle, + CURLOPT_MIMEPOST, postdata->mime)) + { + return ! 0; + } + } + } + } + } + } + curl_mime_free (postdata->mime); + postdata->mime = NULL; + return 0; +#else /* ! HAS_CURL_MIME */ + postdata->post = NULL; struct curl_httppost *last = NULL; - curl_formadd (&post, &last, CURLFORM_COPYNAME, "name", - CURLFORM_COPYCONTENTS, "daniel", CURLFORM_END); - curl_formadd (&post, &last, CURLFORM_COPYNAME, "project", - CURLFORM_COPYCONTENTS, "curl", CURLFORM_END); - return post; + if (0 == curl_formadd (&postdata->post, &last, + CURLFORM_COPYNAME, "name", + CURLFORM_COPYCONTENTS, "daniel", CURLFORM_END)) + { + if (0 == curl_formadd (&postdata->post, &last, + CURLFORM_COPYNAME, "project", + CURLFORM_COPYCONTENTS, "curl", CURLFORM_END)) + { + if (CURLE_OK == curl_easy_setopt (handle, + CURLOPT_HTTPPOST, postdata->post)) + { + return ! 0; + } + } + } + curl_formfree (postdata->post); + return 0; +#endif /* ! HAS_CURL_MIME */ +} + + +static void +free_test_form (struct mhd_test_postdata *postdata) +{ +#if defined(HAS_CURL_MIME) + if (NULL != postdata->mime) + curl_mime_free (postdata->mime); +#else /* ! HAS_CURL_MIME */ + if (NULL != postdata->post) + curl_formfree (postdata->post); +#endif /* ! HAS_CURL_MIME */ } @@ -195,7 +278,7 @@ testInternalPost (void) char buf[2048]; struct CBC cbc; CURLcode errornum; - struct curl_httppost *pd; + struct mhd_test_postdata form; uint16_t port; if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) @@ -231,8 +314,6 @@ testInternalPost (void) curl_easy_setopt (c, CURLOPT_PORT, (long) port); curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); - pd = make_form (); - curl_easy_setopt (c, CURLOPT_HTTPPOST, pd); curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); if (oneone) @@ -244,18 +325,24 @@ testInternalPost (void) * setting NOSIGNAL results in really weird * crashes on my system! */ curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); + if (! add_test_form (c, &form)) + { + fprintf (stderr, "libcurl form initialisation error.\n"); + curl_easy_cleanup (c); + return 2; + } if (CURLE_OK != (errornum = curl_easy_perform (c))) { fprintf (stderr, "curl_easy_perform failed: `%s'\n", curl_easy_strerror (errornum)); curl_easy_cleanup (c); - curl_formfree (pd); + free_test_form (&form); MHD_stop_daemon (d); return 2; } curl_easy_cleanup (c); - curl_formfree (pd); + free_test_form (&form); MHD_stop_daemon (d); if (cbc.pos != strlen ("/hello_world")) return 4; @@ -273,7 +360,7 @@ testMultithreadedPost (void) char buf[2048]; struct CBC cbc; CURLcode errornum; - struct curl_httppost *pd; + struct mhd_test_postdata form; uint16_t port; if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) @@ -310,8 +397,6 @@ testMultithreadedPost (void) curl_easy_setopt (c, CURLOPT_PORT, (long) port); curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); - pd = make_form (); - curl_easy_setopt (c, CURLOPT_HTTPPOST, pd); curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); if (oneone) @@ -323,18 +408,24 @@ testMultithreadedPost (void) * setting NOSIGNAL results in really weird * crashes on my system! */ curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); + if (! add_test_form (c, &form)) + { + fprintf (stderr, "libcurl form initialisation error.\n"); + curl_easy_cleanup (c); + return 2; + } if (CURLE_OK != (errornum = curl_easy_perform (c))) { fprintf (stderr, "curl_easy_perform failed: `%s'\n", curl_easy_strerror (errornum)); curl_easy_cleanup (c); - curl_formfree (pd); + free_test_form (&form); MHD_stop_daemon (d); return 32; } curl_easy_cleanup (c); - curl_formfree (pd); + free_test_form (&form); MHD_stop_daemon (d); if (cbc.pos != strlen ("/hello_world")) return 64; @@ -352,7 +443,7 @@ testMultithreadedPoolPost (void) char buf[2048]; struct CBC cbc; CURLcode errornum; - struct curl_httppost *pd; + struct mhd_test_postdata form; uint16_t port; if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) @@ -389,8 +480,6 @@ testMultithreadedPoolPost (void) curl_easy_setopt (c, CURLOPT_PORT, (long) port); curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); - pd = make_form (); - curl_easy_setopt (c, CURLOPT_HTTPPOST, pd); curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); if (oneone) @@ -402,18 +491,24 @@ testMultithreadedPoolPost (void) * setting NOSIGNAL results in really weird * crashes on my system! */ curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); + if (! add_test_form (c, &form)) + { + fprintf (stderr, "libcurl form initialisation error.\n"); + curl_easy_cleanup (c); + return 2; + } if (CURLE_OK != (errornum = curl_easy_perform (c))) { fprintf (stderr, "curl_easy_perform failed: `%s'\n", curl_easy_strerror (errornum)); curl_easy_cleanup (c); - curl_formfree (pd); + free_test_form (&form); MHD_stop_daemon (d); return 32; } curl_easy_cleanup (c); - curl_formfree (pd); + free_test_form (&form); MHD_stop_daemon (d); if (cbc.pos != strlen ("/hello_world")) return 64; @@ -445,7 +540,7 @@ testExternalPost (void) struct CURLMsg *msg; time_t start; struct timeval tv; - struct curl_httppost *pd; + struct mhd_test_postdata form; uint16_t port; if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) @@ -482,8 +577,6 @@ testExternalPost (void) curl_easy_setopt (c, CURLOPT_PORT, (long) port); curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); - pd = make_form (); - curl_easy_setopt (c, CURLOPT_HTTPPOST, pd); curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); if (oneone) @@ -495,13 +588,18 @@ testExternalPost (void) * setting NOSIGNAL results in really weird * crashes on my system! */ curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); - + if (! add_test_form (c, &form)) + { + fprintf (stderr, "libcurl form initialisation error.\n"); + curl_easy_cleanup (c); + return 2; + } multi = curl_multi_init (); if (multi == NULL) { curl_easy_cleanup (c); - curl_formfree (pd); + free_test_form (&form); MHD_stop_daemon (d); return 512; } @@ -509,7 +607,7 @@ testExternalPost (void) if (mret != CURLM_OK) { curl_multi_cleanup (multi); - curl_formfree (pd); + free_test_form (&form); curl_easy_cleanup (c); MHD_stop_daemon (d); return 1024; @@ -530,7 +628,7 @@ testExternalPost (void) curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); - curl_formfree (pd); + free_test_form (&form); return 2048; } if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock)) @@ -538,7 +636,7 @@ testExternalPost (void) curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); - curl_formfree (pd); + free_test_form (&form); MHD_stop_daemon (d); return 4096; } @@ -607,7 +705,7 @@ testExternalPost (void) curl_easy_cleanup (c); curl_multi_cleanup (multi); } - curl_formfree (pd); + free_test_form (&form); MHD_stop_daemon (d); if (cbc.pos != strlen ("/hello_world")) return 8192;