gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] 94/156: test_postform: updated to support the new libcur


From: gnunet
Subject: [libmicrohttpd] 94/156: test_postform: updated to support the new libcurl API
Date: Sun, 28 May 2023 17:52:27 +0200

This is an automated email from the git hooks/post-receive script.

karlson2k pushed a commit to tag v0.9.77
in repository libmicrohttpd.

commit e530d7ba47828e372d4c497a620c734fc229b7bd
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
AuthorDate: Mon May 15 17:50:30 2023 +0300

    test_postform: updated to support the new libcurl API
    
    Muted libcurl deprecations warnings.
---
 src/testcurl/test_postform.c | 180 +++++++++++++++++++++++++++++++++----------
 1 file changed, 139 insertions(+), 41 deletions(-)

diff --git a/src/testcurl/test_postform.c b/src/testcurl/test_postform.c
index 78f6650f..8aea85f5 100644
--- 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-2021 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
@@ -167,17 +179,88 @@ ahc_echo (void *cls,
 }
 
 
-static struct curl_httppost *
-make_form ()
+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 */
 }
 
 
@@ -189,8 +272,8 @@ testInternalPost ()
   char buf[2048];
   struct CBC cbc;
   CURLcode errornum;
-  struct curl_httppost *pd;
-  int port;
+  struct mhd_test_postdata form;
+  uint16_t port;
 
   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
     port = 0;
@@ -218,15 +301,13 @@ testInternalPost ()
     {
       MHD_stop_daemon (d); return 32;
     }
-    port = (int) dinfo->port;
+    port = dinfo->port;
   }
   c = curl_easy_init ();
   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world";);
   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)
@@ -238,18 +319,24 @@ testInternalPost ()
    *   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;
@@ -267,8 +354,8 @@ testMultithreadedPost ()
   char buf[2048];
   struct CBC cbc;
   CURLcode errornum;
-  struct curl_httppost *pd;
-  int port;
+  struct mhd_test_postdata form;
+  uint16_t port;
 
   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
     port = 0;
@@ -297,15 +384,13 @@ testMultithreadedPost ()
     {
       MHD_stop_daemon (d); return 32;
     }
-    port = (int) dinfo->port;
+    port = dinfo->port;
   }
   c = curl_easy_init ();
   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world";);
   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)
@@ -317,18 +402,24 @@ testMultithreadedPost ()
    *   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;
@@ -346,8 +437,8 @@ testMultithreadedPoolPost ()
   char buf[2048];
   struct CBC cbc;
   CURLcode errornum;
-  struct curl_httppost *pd;
-  int port;
+  struct mhd_test_postdata form;
+  uint16_t port;
 
   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
     port = 0;
@@ -376,15 +467,13 @@ testMultithreadedPoolPost ()
     {
       MHD_stop_daemon (d); return 32;
     }
-    port = (int) dinfo->port;
+    port = dinfo->port;
   }
   c = curl_easy_init ();
   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world";);
   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)
@@ -396,18 +485,24 @@ testMultithreadedPoolPost ()
    *   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;
@@ -439,8 +534,8 @@ testExternalPost ()
   struct CURLMsg *msg;
   time_t start;
   struct timeval tv;
-  struct curl_httppost *pd;
-  int port;
+  struct mhd_test_postdata form;
+  uint16_t port;
 
   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
     port = 0;
@@ -469,15 +564,13 @@ testExternalPost ()
     {
       MHD_stop_daemon (d); return 32;
     }
-    port = (int) dinfo->port;
+    port = dinfo->port;
   }
   c = curl_easy_init ();
   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world";);
   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)
@@ -489,13 +582,18 @@ testExternalPost ()
    *   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;
   }
@@ -503,7 +601,7 @@ testExternalPost ()
   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;
@@ -524,7 +622,7 @@ testExternalPost ()
       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))
@@ -532,7 +630,7 @@ testExternalPost ()
       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;
     }
@@ -601,7 +699,7 @@ testExternalPost ()
     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;

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]