libmicrohttpd

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

test_postform.c (21089B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007 Christian Grothoff
      4      Copyright (C) 2014-2023 Evgeny Grin (Karlson2k)
      5 
      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
      8      by the Free Software Foundation; either version 2, or (at your
      9      option) any later version.
     10 
     11      libmicrohttpd is distributed in the hope that it will be useful, but
     12      WITHOUT ANY WARRANTY; without even the implied warranty of
     13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14      General Public License for more details.
     15 
     16      You should have received a copy of the GNU General Public License
     17      along with libmicrohttpd; see the file COPYING.  If not, write to the
     18      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19      Boston, MA 02110-1301, USA.
     20 */
     21 
     22 /**
     23  * @file test_postform.c
     24  * @brief  Testcase for libmicrohttpd POST operations using multipart/postform data
     25  * @author Christian Grothoff
     26  * @author Karlson2k (Evgeny Grin)
     27  */
     28 
     29 #include "MHD_config.h"
     30 #include "platform.h"
     31 #include <curl/curl.h>
     32 #include <microhttpd.h>
     33 #include <stdlib.h>
     34 #include <string.h>
     35 #include <time.h>
     36 #ifdef MHD_HTTPS_REQUIRE_GCRYPT
     37 #ifdef HAVE_GCRYPT_H
     38 #include <gcrypt.h>
     39 #endif
     40 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */
     41 #include <errno.h>
     42 
     43 #ifndef WINDOWS
     44 #include <unistd.h>
     45 #endif
     46 
     47 #ifndef CURL_VERSION_BITS
     48 #define CURL_VERSION_BITS(x,y,z) ((x) << 16 | (y) << 8 | (z))
     49 #endif /* ! CURL_VERSION_BITS */
     50 #ifndef CURL_AT_LEAST_VERSION
     51 #define CURL_AT_LEAST_VERSION(x,y,z) \
     52   (LIBCURL_VERSION_NUM >= CURL_VERSION_BITS (x, y, z))
     53 #endif /* ! CURL_AT_LEAST_VERSION */
     54 
     55 #if CURL_AT_LEAST_VERSION (7,56,0)
     56 #define HAS_CURL_MIME 1
     57 #endif /* CURL_AT_LEAST_VERSION(7,56,0) */
     58 
     59 #include "mhd_has_in_name.h"
     60 
     61 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     62    test into a marked, classifiable test error (TESTING.md, P5). */
     63 #include "mhd_panic_tripwire.h"
     64 
     65 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2
     66 #undef MHD_CPU_COUNT
     67 #endif
     68 #if ! defined(MHD_CPU_COUNT)
     69 #define MHD_CPU_COUNT 2
     70 #endif
     71 
     72 static int oneone;
     73 
     74 struct CBC
     75 {
     76   char *buf;
     77   size_t pos;
     78   size_t size;
     79 };
     80 
     81 
     82 static void
     83 completed_cb (void *cls,
     84               struct MHD_Connection *connection,
     85               void **req_cls,
     86               enum MHD_RequestTerminationCode toe)
     87 {
     88   struct MHD_PostProcessor *pp = *req_cls;
     89   (void) cls; (void) connection; (void) toe;            /* Unused. Silent compiler warning. */
     90 
     91   if (NULL != pp)
     92     MHD_destroy_post_processor (pp);
     93   *req_cls = NULL;
     94 }
     95 
     96 
     97 static size_t
     98 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
     99 {
    100   struct CBC *cbc = ctx;
    101 
    102   if (cbc->pos + size * nmemb > cbc->size)
    103     return 0;                   /* overflow */
    104   memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
    105   cbc->pos += size * nmemb;
    106   return size * nmemb;
    107 }
    108 
    109 
    110 /**
    111  * Note that this post_iterator is not perfect
    112  * in that it fails to support incremental processing.
    113  * (to be fixed in the future)
    114  */
    115 static enum MHD_Result
    116 post_iterator (void *cls,
    117                enum MHD_ValueKind kind,
    118                const char *key,
    119                const char *filename,
    120                const char *content_type,
    121                const char *transfer_encoding,
    122                const char *value, uint64_t off, size_t size)
    123 {
    124   int *eok = cls;
    125   (void) kind; (void) filename; (void) content_type; /* Unused. Silent compiler warning. */
    126   (void) transfer_encoding; (void) off;            /* Unused. Silent compiler warning. */
    127 
    128 #if 0
    129   fprintf (stderr, "PI sees %s-%.*s\n", key, size, value);
    130 #endif
    131   if ((0 == strcmp (key, "name")) &&
    132       (size == strlen ("daniel")) && (0 == strncmp (value, "daniel", size)))
    133     (*eok) |= 1;
    134   if ((0 == strcmp (key, "project")) &&
    135       (size == strlen ("curl")) && (0 == strncmp (value, "curl", size)))
    136     (*eok) |= 2;
    137   return MHD_YES;
    138 }
    139 
    140 
    141 static enum MHD_Result
    142 ahc_echo (void *cls,
    143           struct MHD_Connection *connection,
    144           const char *url,
    145           const char *method,
    146           const char *version,
    147           const char *upload_data, size_t *upload_data_size,
    148           void **req_cls)
    149 {
    150   static int eok;
    151   struct MHD_Response *response;
    152   struct MHD_PostProcessor *pp;
    153   enum MHD_Result ret;
    154   (void) cls; (void) version;      /* Unused. Silent compiler warning. */
    155 
    156   if (0 != strcmp ("POST", method))
    157   {
    158     printf ("METHOD: %s\n", method);
    159     return MHD_NO;              /* unexpected method */
    160   }
    161   pp = *req_cls;
    162   if (pp == NULL)
    163   {
    164     eok = 0;
    165     pp = MHD_create_post_processor (connection,
    166                                     1024,
    167                                     &post_iterator,
    168                                     &eok);
    169     if (NULL == pp)
    170       abort ();
    171     *req_cls = pp;
    172   }
    173   if (MHD_YES !=
    174       MHD_post_process (pp,
    175                         upload_data,
    176                         *upload_data_size))
    177     abort ();
    178   if ((eok == 3) && (0 == *upload_data_size))
    179   {
    180     response = MHD_create_response_from_buffer_copy (strlen (url),
    181                                                      (const void *) url);
    182     ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
    183     MHD_destroy_response (response);
    184     MHD_destroy_post_processor (pp);
    185     *req_cls = NULL;
    186     return ret;
    187   }
    188   *upload_data_size = 0;
    189   return MHD_YES;
    190 }
    191 
    192 
    193 struct mhd_test_postdata
    194 {
    195 #if defined(HAS_CURL_MIME)
    196   curl_mime *mime;
    197 #else  /* ! HAS_CURL_MIME */
    198   struct curl_httppost *post;
    199 #endif /* ! HAS_CURL_MIME */
    200 };
    201 
    202 /* Return non-zero if succeed */
    203 static int
    204 add_test_form (CURL *handle, struct mhd_test_postdata *postdata)
    205 {
    206 #if defined(HAS_CURL_MIME)
    207   postdata->mime = curl_mime_init (handle);
    208   if (NULL == postdata->mime)
    209     return 0;
    210   else
    211   {
    212     curl_mimepart *part;
    213     part = curl_mime_addpart (postdata->mime);
    214     if (NULL != part)
    215     {
    216       if ( (CURLE_OK == curl_mime_data (part, "daniel",
    217                                         CURL_ZERO_TERMINATED)) &&
    218            (CURLE_OK == curl_mime_name (part, "name")) )
    219       {
    220         part = curl_mime_addpart (postdata->mime);
    221         if (NULL != part)
    222         {
    223           if ( (CURLE_OK == curl_mime_data (part, "curl",
    224                                             CURL_ZERO_TERMINATED)) &&
    225                (CURLE_OK == curl_mime_name (part, "project")) )
    226           {
    227             if (CURLE_OK == curl_easy_setopt (handle,
    228                                               CURLOPT_MIMEPOST, postdata->mime))
    229             {
    230               return ! 0;
    231             }
    232           }
    233         }
    234       }
    235     }
    236   }
    237   curl_mime_free (postdata->mime);
    238   postdata->mime = NULL;
    239   return 0;
    240 #else  /* ! HAS_CURL_MIME */
    241   postdata->post = NULL;
    242   struct curl_httppost *last = NULL;
    243 
    244   if (0 == curl_formadd (&postdata->post, &last,
    245                          CURLFORM_COPYNAME, "name",
    246                          CURLFORM_COPYCONTENTS, "daniel", CURLFORM_END))
    247   {
    248     if (0 == curl_formadd (&postdata->post, &last,
    249                            CURLFORM_COPYNAME, "project",
    250                            CURLFORM_COPYCONTENTS, "curl", CURLFORM_END))
    251     {
    252       if (CURLE_OK == curl_easy_setopt (handle,
    253                                         CURLOPT_HTTPPOST, postdata->post))
    254       {
    255         return ! 0;
    256       }
    257     }
    258   }
    259   curl_formfree (postdata->post);
    260   return 0;
    261 #endif /* ! HAS_CURL_MIME */
    262 }
    263 
    264 
    265 static void
    266 free_test_form (struct mhd_test_postdata *postdata)
    267 {
    268 #if defined(HAS_CURL_MIME)
    269   if (NULL != postdata->mime)
    270     curl_mime_free (postdata->mime);
    271 #else  /* ! HAS_CURL_MIME */
    272   if (NULL != postdata->post)
    273     curl_formfree (postdata->post);
    274 #endif /* ! HAS_CURL_MIME */
    275 }
    276 
    277 
    278 static unsigned int
    279 testInternalPost (void)
    280 {
    281   struct MHD_Daemon *d;
    282   CURL *c;
    283   char buf[2048];
    284   struct CBC cbc;
    285   CURLcode errornum;
    286   struct mhd_test_postdata form;
    287   uint16_t port;
    288 
    289   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    290     port = 0;
    291   else
    292   {
    293     port = 1390;
    294     if (oneone)
    295       port += 10;
    296   }
    297 
    298   cbc.buf = buf;
    299   cbc.size = 2048;
    300   cbc.pos = 0;
    301   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    302                         port, NULL, NULL, &ahc_echo, NULL,
    303                         MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL,
    304                         MHD_OPTION_END);
    305   if (d == NULL)
    306     return 1;
    307   if (0 == port)
    308   {
    309     const union MHD_DaemonInfo *dinfo;
    310     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    311     if ((NULL == dinfo) || (0 == dinfo->port) )
    312     {
    313       MHD_stop_daemon (d); return 32;
    314     }
    315     port = dinfo->port;
    316   }
    317   c = curl_easy_init ();
    318   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    319   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    320   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    321   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    322   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    323   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    324   if (oneone)
    325     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    326   else
    327     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    328   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    329   /* NOTE: use of CONNECTTIMEOUT without also
    330    *   setting NOSIGNAL results in really weird
    331    *   crashes on my system! */
    332   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    333   curl_easy_setopt (c, CURLOPT_PROXY, "");
    334   if (! add_test_form (c, &form))
    335   {
    336     fprintf (stderr, "libcurl form initialisation error.\n");
    337     curl_easy_cleanup (c);
    338     return 2;
    339   }
    340   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    341   {
    342     fprintf (stderr,
    343              "curl_easy_perform failed: `%s'\n",
    344              curl_easy_strerror (errornum));
    345     curl_easy_cleanup (c);
    346     free_test_form (&form);
    347     MHD_stop_daemon (d);
    348     return 2;
    349   }
    350   curl_easy_cleanup (c);
    351   free_test_form (&form);
    352   MHD_stop_daemon (d);
    353   if (cbc.pos != strlen ("/hello_world"))
    354     return 4;
    355   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    356     return 8;
    357   return 0;
    358 }
    359 
    360 
    361 static unsigned int
    362 testMultithreadedPost (void)
    363 {
    364   struct MHD_Daemon *d;
    365   CURL *c;
    366   char buf[2048];
    367   struct CBC cbc;
    368   CURLcode errornum;
    369   struct mhd_test_postdata form;
    370   uint16_t port;
    371 
    372   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    373     port = 0;
    374   else
    375   {
    376     port = 1390;
    377     if (oneone)
    378       port += 10;
    379   }
    380 
    381   cbc.buf = buf;
    382   cbc.size = 2048;
    383   cbc.pos = 0;
    384   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    385                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    386                         port, NULL, NULL, &ahc_echo, NULL,
    387                         MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL,
    388                         MHD_OPTION_END);
    389   if (d == NULL)
    390     return 16;
    391   if (0 == port)
    392   {
    393     const union MHD_DaemonInfo *dinfo;
    394     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    395     if ((NULL == dinfo) || (0 == dinfo->port) )
    396     {
    397       MHD_stop_daemon (d); return 32;
    398     }
    399     port = dinfo->port;
    400   }
    401   c = curl_easy_init ();
    402   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    403   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    404   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    405   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    406   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    407   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    408   if (oneone)
    409     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    410   else
    411     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    412   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L);
    413   /* NOTE: use of CONNECTTIMEOUT without also
    414    *   setting NOSIGNAL results in really weird
    415    *   crashes on my system! */
    416   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    417   curl_easy_setopt (c, CURLOPT_PROXY, "");
    418   if (! add_test_form (c, &form))
    419   {
    420     fprintf (stderr, "libcurl form initialisation error.\n");
    421     curl_easy_cleanup (c);
    422     return 2;
    423   }
    424   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    425   {
    426     fprintf (stderr,
    427              "curl_easy_perform failed: `%s'\n",
    428              curl_easy_strerror (errornum));
    429     curl_easy_cleanup (c);
    430     free_test_form (&form);
    431     MHD_stop_daemon (d);
    432     return 32;
    433   }
    434   curl_easy_cleanup (c);
    435   free_test_form (&form);
    436   MHD_stop_daemon (d);
    437   if (cbc.pos != strlen ("/hello_world"))
    438     return 64;
    439   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    440     return 128;
    441   return 0;
    442 }
    443 
    444 
    445 static unsigned int
    446 testMultithreadedPoolPost (void)
    447 {
    448   struct MHD_Daemon *d;
    449   CURL *c;
    450   char buf[2048];
    451   struct CBC cbc;
    452   CURLcode errornum;
    453   struct mhd_test_postdata form;
    454   uint16_t port;
    455 
    456   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    457     port = 0;
    458   else
    459   {
    460     port = 1391;
    461     if (oneone)
    462       port += 10;
    463   }
    464 
    465   cbc.buf = buf;
    466   cbc.size = 2048;
    467   cbc.pos = 0;
    468   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    469                         port, NULL, NULL, &ahc_echo, NULL,
    470                         MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT,
    471                         MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL,
    472                         MHD_OPTION_END);
    473   if (d == NULL)
    474     return 16;
    475   if (0 == port)
    476   {
    477     const union MHD_DaemonInfo *dinfo;
    478     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    479     if ((NULL == dinfo) || (0 == dinfo->port) )
    480     {
    481       MHD_stop_daemon (d); return 32;
    482     }
    483     port = dinfo->port;
    484   }
    485   c = curl_easy_init ();
    486   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    487   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    488   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    489   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    490   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    491   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    492   if (oneone)
    493     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    494   else
    495     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    496   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L);
    497   /* NOTE: use of CONNECTTIMEOUT without also
    498    *   setting NOSIGNAL results in really weird
    499    *   crashes on my system! */
    500   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    501   curl_easy_setopt (c, CURLOPT_PROXY, "");
    502   if (! add_test_form (c, &form))
    503   {
    504     fprintf (stderr, "libcurl form initialisation error.\n");
    505     curl_easy_cleanup (c);
    506     return 2;
    507   }
    508   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    509   {
    510     fprintf (stderr,
    511              "curl_easy_perform failed: `%s'\n",
    512              curl_easy_strerror (errornum));
    513     curl_easy_cleanup (c);
    514     free_test_form (&form);
    515     MHD_stop_daemon (d);
    516     return 32;
    517   }
    518   curl_easy_cleanup (c);
    519   free_test_form (&form);
    520   MHD_stop_daemon (d);
    521   if (cbc.pos != strlen ("/hello_world"))
    522     return 64;
    523   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    524     return 128;
    525   return 0;
    526 }
    527 
    528 
    529 static unsigned int
    530 testExternalPost (void)
    531 {
    532   struct MHD_Daemon *d;
    533   CURL *c;
    534   char buf[2048];
    535   struct CBC cbc;
    536   CURLM *multi;
    537   CURLMcode mret;
    538   fd_set rs;
    539   fd_set ws;
    540   fd_set es;
    541   MHD_socket maxsock;
    542 #ifdef MHD_WINSOCK_SOCKETS
    543   int maxposixs; /* Max socket number unused on W32 */
    544 #else  /* MHD_POSIX_SOCKETS */
    545 #define maxposixs maxsock
    546 #endif /* MHD_POSIX_SOCKETS */
    547   int running;
    548   struct CURLMsg *msg;
    549   time_t start;
    550   struct timeval tv;
    551   struct mhd_test_postdata form;
    552   uint16_t port;
    553 
    554   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    555     port = 0;
    556   else
    557   {
    558     port = 1392;
    559     if (oneone)
    560       port += 10;
    561   }
    562 
    563   multi = NULL;
    564   cbc.buf = buf;
    565   cbc.size = 2048;
    566   cbc.pos = 0;
    567   d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY,
    568                         port, NULL, NULL, &ahc_echo, NULL,
    569                         MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL,
    570                         MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE,
    571                         MHD_OPTION_END);
    572   if (d == NULL)
    573     return 256;
    574   if (0 == port)
    575   {
    576     const union MHD_DaemonInfo *dinfo;
    577     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    578     if ((NULL == dinfo) || (0 == dinfo->port) )
    579     {
    580       MHD_stop_daemon (d); return 32;
    581     }
    582     port = dinfo->port;
    583   }
    584   c = curl_easy_init ();
    585   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    586   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    587   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    588   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    589   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    590   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    591   if (oneone)
    592     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    593   else
    594     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    595   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    596   /* NOTE: use of CONNECTTIMEOUT without also
    597    *   setting NOSIGNAL results in really weird
    598    *   crashes on my system! */
    599   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    600   curl_easy_setopt (c, CURLOPT_PROXY, "");
    601   if (! add_test_form (c, &form))
    602   {
    603     fprintf (stderr, "libcurl form initialisation error.\n");
    604     curl_easy_cleanup (c);
    605     return 2;
    606   }
    607 
    608   multi = curl_multi_init ();
    609   if (multi == NULL)
    610   {
    611     curl_easy_cleanup (c);
    612     free_test_form (&form);
    613     MHD_stop_daemon (d);
    614     return 512;
    615   }
    616   mret = curl_multi_add_handle (multi, c);
    617   if (mret != CURLM_OK)
    618   {
    619     curl_multi_cleanup (multi);
    620     free_test_form (&form);
    621     curl_easy_cleanup (c);
    622     MHD_stop_daemon (d);
    623     return 1024;
    624   }
    625   start = time (NULL);
    626   while ((time (NULL) - start < 5) && (multi != NULL))
    627   {
    628     maxsock = MHD_INVALID_SOCKET;
    629     maxposixs = -1;
    630     FD_ZERO (&rs);
    631     FD_ZERO (&ws);
    632     FD_ZERO (&es);
    633     curl_multi_perform (multi, &running);
    634     mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
    635     if (mret != CURLM_OK)
    636     {
    637       curl_multi_remove_handle (multi, c);
    638       curl_multi_cleanup (multi);
    639       curl_easy_cleanup (c);
    640       MHD_stop_daemon (d);
    641       free_test_form (&form);
    642       return 2048;
    643     }
    644     if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock))
    645     {
    646       curl_multi_remove_handle (multi, c);
    647       curl_multi_cleanup (multi);
    648       curl_easy_cleanup (c);
    649       free_test_form (&form);
    650       MHD_stop_daemon (d);
    651       return 4096;
    652     }
    653     tv.tv_sec = 0;
    654     tv.tv_usec = 1000;
    655     if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
    656     {
    657 #ifdef MHD_POSIX_SOCKETS
    658       if (EINTR != errno)
    659       {
    660         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    661                  (int) errno, __LINE__);
    662         fflush (stderr);
    663         exit (99);
    664       }
    665 #else
    666       if ((WSAEINVAL != WSAGetLastError ()) ||
    667           (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
    668       {
    669         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    670                  (int) WSAGetLastError (), __LINE__);
    671         fflush (stderr);
    672         exit (99);
    673       }
    674       Sleep (1);
    675 #endif
    676     }
    677     curl_multi_perform (multi, &running);
    678     if (0 == running)
    679     {
    680       int pending;
    681       int curl_fine = 0;
    682       while (NULL != (msg = curl_multi_info_read (multi, &pending)))
    683       {
    684         if (msg->msg == CURLMSG_DONE)
    685         {
    686           if (msg->data.result == CURLE_OK)
    687             curl_fine = 1;
    688           else
    689           {
    690             fprintf (stderr,
    691                      "%s failed at %s:%d: `%s'\n",
    692                      "curl_multi_perform",
    693                      __FILE__,
    694                      __LINE__, curl_easy_strerror (msg->data.result));
    695             abort ();
    696           }
    697         }
    698       }
    699       if (! curl_fine)
    700       {
    701         fprintf (stderr, "libcurl haven't returned OK code\n");
    702         abort ();
    703       }
    704       curl_multi_remove_handle (multi, c);
    705       curl_multi_cleanup (multi);
    706       curl_easy_cleanup (c);
    707       c = NULL;
    708       multi = NULL;
    709     }
    710     MHD_run (d);
    711   }
    712   if (multi != NULL)
    713   {
    714     curl_multi_remove_handle (multi, c);
    715     curl_easy_cleanup (c);
    716     curl_multi_cleanup (multi);
    717   }
    718   free_test_form (&form);
    719   MHD_stop_daemon (d);
    720   if (cbc.pos != strlen ("/hello_world"))
    721     return 8192;
    722   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    723     return 16384;
    724   return 0;
    725 }
    726 
    727 
    728 int
    729 main (int argc, char *const *argv)
    730 {
    731   unsigned int errorCount = 0;
    732   (void) argc;   /* Unused. Silent compiler warning. */
    733 
    734 #ifdef MHD_HTTPS_REQUIRE_GCRYPT
    735 #ifdef HAVE_GCRYPT_H
    736   gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
    737 #ifdef GCRYCTL_INITIALIZATION_FINISHED
    738   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
    739 #endif
    740 #endif
    741 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */
    742   if ((NULL == argv) || (0 == argv[0]))
    743     return 99;
    744   oneone = has_in_name (argv[0], "11");
    745   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
    746     return 2;
    747   if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS))
    748   {
    749     errorCount += testInternalPost ();
    750     errorCount += testMultithreadedPost ();
    751     errorCount += testMultithreadedPoolPost ();
    752   }
    753   errorCount += testExternalPost ();
    754   if (errorCount != 0)
    755     fprintf (stderr, "Error (code: %u)\n", errorCount);
    756   curl_global_cleanup ();
    757   return (0 == errorCount) ? 0 : 1;       /* 0 == pass */
    758 }