libmicrohttpd

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

test_get_iovec.c (21258B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007-2021 Christian Grothoff
      4      Copyright (C) 2014-2022 Evgeny Grin
      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_get_iovec.c
     24  * @brief  Testcase for libmicrohttpd response from scatter/gather array
     25  * @author Christian Grothoff
     26  * @author Karlson2k (Evgeny Grin)
     27  * @author Lawrence Sebald
     28  */
     29 
     30 /*
     31  * This test is largely derived from the test_get_sendfile.c file, with the
     32  * daemon using MHD_create_response_from_iovec instead of working from an fd.
     33  */
     34 
     35 #include "mhd_options.h"
     36 #include "platform.h"
     37 #include <curl/curl.h>
     38 #include <microhttpd.h>
     39 #include <stdlib.h>
     40 #include <string.h>
     41 #include <time.h>
     42 #include <sys/types.h>
     43 #include <fcntl.h>
     44 #ifdef HAVE_STDBOOL_H
     45 #include <stdbool.h>
     46 #endif
     47 #include <errno.h>
     48 #include "mhd_sockets.h"
     49 #include "mhd_has_in_name.h"
     50 
     51 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     52    test into a marked, classifiable test error (TESTING.md, P5). */
     53 #include "mhd_panic_tripwire.h"
     54 
     55 #ifndef WINDOWS
     56 #include <sys/socket.h>
     57 #include <unistd.h>
     58 #endif
     59 
     60 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2
     61 #undef MHD_CPU_COUNT
     62 #endif
     63 #if ! defined(MHD_CPU_COUNT)
     64 #define MHD_CPU_COUNT 2
     65 #endif
     66 
     67 #define TESTSTR_IOVLEN 20480
     68 #define TESTSTR_IOVCNT 20
     69 #define TESTSTR_SIZE   (TESTSTR_IOVCNT * TESTSTR_IOVLEN)
     70 
     71 static int oneone;
     72 
     73 static int readbuf[TESTSTR_SIZE * 2 / sizeof(int)];
     74 
     75 struct CBC
     76 {
     77   char *buf;
     78   size_t pos;
     79   size_t size;
     80 };
     81 
     82 
     83 static size_t
     84 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
     85 {
     86   struct CBC *cbc = ctx;
     87 
     88   if (cbc->pos + size * nmemb > cbc->size)
     89     _exit (7);                   /* overflow */
     90   memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
     91   cbc->pos += size * nmemb;
     92   return size * nmemb;
     93 }
     94 
     95 
     96 static void
     97 iov_free_callback (void *cls)
     98 {
     99   free (cls);
    100 }
    101 
    102 
    103 struct iovncont_data
    104 {
    105   void *ptrs[TESTSTR_IOVCNT];
    106 };
    107 
    108 static void
    109 iovncont_free_callback (void *cls)
    110 {
    111   struct iovncont_data *data = (struct iovncont_data *) cls;
    112   unsigned int i;
    113 
    114   for (i = 0; i < TESTSTR_IOVCNT; ++i)
    115     free (data->ptrs[i]);
    116   free (data);
    117 }
    118 
    119 
    120 static int
    121 check_read_data (const void *ptr, size_t len)
    122 {
    123   const int *buf;
    124   size_t i;
    125 
    126   if (len % sizeof(int))
    127     return -1;
    128 
    129   buf = (const int *) ptr;
    130 
    131   for (i = 0; i < len / sizeof(int); ++i)
    132   {
    133     if (buf[i] != (int) i)
    134       return -1;
    135   }
    136 
    137   return 0;
    138 }
    139 
    140 
    141 static enum MHD_Result
    142 ahc_cont (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 ptr;
    151   struct MHD_Response *response;
    152   enum MHD_Result ret;
    153   int *data;
    154   struct MHD_IoVec iov[TESTSTR_IOVCNT];
    155   int i;
    156   (void) cls;
    157   (void) url; (void) version;                      /* Unused. Silent compiler warning. */
    158   (void) upload_data; (void) upload_data_size;     /* Unused. Silent compiler warning. */
    159 
    160   if (0 != strcmp (MHD_HTTP_METHOD_GET, method))
    161     return MHD_NO;              /* unexpected method */
    162   if (&ptr != *req_cls)
    163   {
    164     *req_cls = &ptr;
    165     return MHD_YES;
    166   }
    167   *req_cls = NULL;
    168 
    169   /* Create some test data. */
    170   if (NULL == (data = malloc (TESTSTR_SIZE)))
    171     return MHD_NO;
    172 
    173   for (i = 0; i < (int) (TESTSTR_SIZE / sizeof(int)); ++i)
    174   {
    175     data[i] = i;
    176   }
    177 
    178   for (i = 0; i < TESTSTR_IOVCNT; ++i)
    179   {
    180     iov[i].iov_base = data + (((size_t) i)
    181                               * (TESTSTR_SIZE / TESTSTR_IOVCNT / sizeof(int)));
    182     iov[i].iov_len = TESTSTR_SIZE / TESTSTR_IOVCNT;
    183   }
    184 
    185   response = MHD_create_response_from_iovec (iov,
    186                                              TESTSTR_IOVCNT,
    187                                              &iov_free_callback,
    188                                              data);
    189   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
    190   MHD_destroy_response (response);
    191   if (ret == MHD_NO)
    192     abort ();
    193   return ret;
    194 }
    195 
    196 
    197 static enum MHD_Result
    198 ahc_ncont (void *cls,
    199            struct MHD_Connection *connection,
    200            const char *url,
    201            const char *method,
    202            const char *version,
    203            const char *upload_data, size_t *upload_data_size,
    204            void **req_cls)
    205 {
    206   static int ptr;
    207   struct MHD_Response *response;
    208   enum MHD_Result ret;
    209   struct MHD_IoVec iov[TESTSTR_IOVCNT];
    210   struct iovncont_data *clear_cls;
    211   int i, j;
    212   (void) cls;
    213   (void) url; (void) version;                      /* Unused. Silent compiler warning. */
    214   (void) upload_data; (void) upload_data_size;     /* Unused. Silent compiler warning. */
    215 
    216   if (0 != strcmp (MHD_HTTP_METHOD_GET, method))
    217     return MHD_NO;              /* unexpected method */
    218   if (&ptr != *req_cls)
    219   {
    220     *req_cls = &ptr;
    221     return MHD_YES;
    222   }
    223   *req_cls = NULL;
    224 
    225   clear_cls = malloc (sizeof(struct iovncont_data));
    226   if (NULL == clear_cls)
    227     abort ();
    228   memset (iov, 0, sizeof(struct MHD_IoVec) * TESTSTR_IOVCNT);
    229 
    230   /* Create some test data. */
    231   for (j = TESTSTR_IOVCNT - 1; j >= 0; --j)
    232   {
    233     int *data;
    234     data = malloc (TESTSTR_IOVLEN);
    235     if (NULL == data)
    236       abort ();
    237     clear_cls->ptrs[j] = (void *) data;
    238 
    239     for (i = 0; i < (int) (TESTSTR_IOVLEN / sizeof(int)); ++i)
    240     {
    241       data[i] = i + (j * (int) (TESTSTR_IOVLEN / sizeof(int)));
    242     }
    243     iov[j].iov_base = (const void *) data;
    244     iov[j].iov_len = TESTSTR_IOVLEN;
    245 
    246   }
    247 
    248   response = MHD_create_response_from_iovec (iov,
    249                                              TESTSTR_IOVCNT,
    250                                              &iovncont_free_callback,
    251                                              clear_cls);
    252   ret = MHD_queue_response (connection,
    253                             MHD_HTTP_OK,
    254                             response);
    255   MHD_destroy_response (response);
    256   if (ret == MHD_NO)
    257     abort ();
    258   return ret;
    259 }
    260 
    261 
    262 static unsigned int
    263 testInternalGet (bool contiguous)
    264 {
    265   struct MHD_Daemon *d;
    266   CURL *c;
    267   struct CBC cbc;
    268   CURLcode errornum;
    269   uint16_t port;
    270 
    271   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    272     port = 0;
    273   else
    274   {
    275     port = 1200;
    276     if (oneone)
    277       port += 10;
    278   }
    279 
    280   cbc.buf = (char *) readbuf;
    281   cbc.size = sizeof(readbuf);
    282   cbc.pos = 0;
    283 
    284   if (contiguous)
    285   {
    286     d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    287                           port, NULL, NULL, &ahc_cont, NULL, MHD_OPTION_END);
    288   }
    289   else
    290   {
    291     d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    292                           port, NULL, NULL, &ahc_ncont, NULL, MHD_OPTION_END);
    293   }
    294 
    295   if (d == NULL)
    296     return 1;
    297   if (0 == port)
    298   {
    299     const union MHD_DaemonInfo *dinfo;
    300     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    301     if ((NULL == dinfo) || (0 == dinfo->port) )
    302     {
    303       MHD_stop_daemon (d); return 32;
    304     }
    305     port = dinfo->port;
    306   }
    307   c = curl_easy_init ();
    308   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/");
    309   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    310   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    311   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    312   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    313   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    314   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    315   if (oneone)
    316     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    317   else
    318     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    319   /* NOTE: use of CONNECTTIMEOUT without also
    320      setting NOSIGNAL results in really weird
    321      crashes on my system!*/
    322   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    323   curl_easy_setopt (c, CURLOPT_PROXY, "");
    324   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    325   {
    326     fprintf (stderr,
    327              "curl_easy_perform failed: `%s'\n",
    328              curl_easy_strerror (errornum));
    329     curl_easy_cleanup (c);
    330     MHD_stop_daemon (d);
    331     return 2;
    332   }
    333   curl_easy_cleanup (c);
    334   MHD_stop_daemon (d);
    335   if (cbc.pos != TESTSTR_SIZE)
    336     return 4;
    337   if (0 != check_read_data (cbc.buf, cbc.pos))
    338     return 8;
    339   return 0;
    340 }
    341 
    342 
    343 static unsigned int
    344 testMultithreadedGet (void)
    345 {
    346   struct MHD_Daemon *d;
    347   CURL *c;
    348   struct CBC cbc;
    349   CURLcode errornum;
    350   uint16_t port;
    351 
    352   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    353     port = 0;
    354   else
    355   {
    356     port = 1201;
    357     if (oneone)
    358       port += 10;
    359   }
    360 
    361   cbc.buf = (char *) readbuf;
    362   cbc.size = sizeof(readbuf);
    363   cbc.pos = 0;
    364   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    365                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    366                         | MHD_USE_AUTO,
    367                         port, NULL, NULL, &ahc_cont, NULL, MHD_OPTION_END);
    368   if (d == NULL)
    369     return 16;
    370   if (0 == port)
    371   {
    372     const union MHD_DaemonInfo *dinfo;
    373     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    374     if ((NULL == dinfo) || (0 == dinfo->port) )
    375     {
    376       MHD_stop_daemon (d); return 32;
    377     }
    378     port = dinfo->port;
    379   }
    380   c = curl_easy_init ();
    381   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/");
    382   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    383   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    384   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    385   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    386   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    387   if (oneone)
    388     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    389   else
    390     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    391   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    392   /* NOTE: use of CONNECTTIMEOUT without also
    393      setting NOSIGNAL results in really weird
    394      crashes on my system! */
    395   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    396   curl_easy_setopt (c, CURLOPT_PROXY, "");
    397   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    398   {
    399     fprintf (stderr,
    400              "curl_easy_perform failed: `%s'\n",
    401              curl_easy_strerror (errornum));
    402     curl_easy_cleanup (c);
    403     MHD_stop_daemon (d);
    404     return 32;
    405   }
    406   curl_easy_cleanup (c);
    407   MHD_stop_daemon (d);
    408   if (cbc.pos != TESTSTR_SIZE)
    409     return 64;
    410   if (0 != check_read_data (cbc.buf, cbc.pos))
    411     return 128;
    412   return 0;
    413 }
    414 
    415 
    416 static unsigned int
    417 testMultithreadedPoolGet (void)
    418 {
    419   struct MHD_Daemon *d;
    420   CURL *c;
    421   struct CBC cbc;
    422   CURLcode errornum;
    423   uint16_t port;
    424 
    425   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    426     port = 0;
    427   else
    428   {
    429     port = 1202;
    430     if (oneone)
    431       port += 10;
    432   }
    433 
    434   cbc.buf = (char *) readbuf;
    435   cbc.size = sizeof(readbuf);
    436   cbc.pos = 0;
    437   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    438                         | MHD_USE_AUTO,
    439                         port, NULL, NULL, &ahc_cont, NULL,
    440                         MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT,
    441                         MHD_OPTION_END);
    442   if (d == NULL)
    443     return 16;
    444   if (0 == port)
    445   {
    446     const union MHD_DaemonInfo *dinfo;
    447     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    448     if ((NULL == dinfo) || (0 == dinfo->port) )
    449     {
    450       MHD_stop_daemon (d); return 32;
    451     }
    452     port = dinfo->port;
    453   }
    454   c = curl_easy_init ();
    455   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/");
    456   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    457   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    458   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    459   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    460   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    461   if (oneone)
    462     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    463   else
    464     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    465   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    466   /* NOTE: use of CONNECTTIMEOUT without also
    467      setting NOSIGNAL results in really weird
    468      crashes on my system!*/
    469   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    470   curl_easy_setopt (c, CURLOPT_PROXY, "");
    471   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    472   {
    473     fprintf (stderr,
    474              "curl_easy_perform failed: `%s'\n",
    475              curl_easy_strerror (errornum));
    476     curl_easy_cleanup (c);
    477     MHD_stop_daemon (d);
    478     return 32;
    479   }
    480   curl_easy_cleanup (c);
    481   MHD_stop_daemon (d);
    482   if (cbc.pos != TESTSTR_SIZE)
    483     return 64;
    484   if (0 != check_read_data (cbc.buf, cbc.pos))
    485     return 128;
    486   return 0;
    487 }
    488 
    489 
    490 static unsigned int
    491 testExternalGet (int thread_unsafe)
    492 {
    493   struct MHD_Daemon *d;
    494   CURL *c;
    495   struct CBC cbc;
    496   CURLM *multi;
    497   CURLMcode mret;
    498   fd_set rs;
    499   fd_set ws;
    500   fd_set es;
    501   MHD_socket maxsock;
    502 #ifdef MHD_WINSOCK_SOCKETS
    503   int maxposixs; /* Max socket number unused on W32 */
    504 #else  /* MHD_POSIX_SOCKETS */
    505 #define maxposixs maxsock
    506 #endif /* MHD_POSIX_SOCKETS */
    507   int running;
    508   struct CURLMsg *msg;
    509   time_t start;
    510   struct timeval tv;
    511   uint16_t port;
    512 
    513   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    514     port = 0;
    515   else
    516   {
    517     port = 1203;
    518     if (oneone)
    519       port += 10;
    520   }
    521 
    522   multi = NULL;
    523   cbc.buf = (char *) readbuf;
    524   cbc.size = sizeof(readbuf);
    525   cbc.pos = 0;
    526   d = MHD_start_daemon (MHD_USE_ERROR_LOG
    527                         | (thread_unsafe ? MHD_USE_NO_THREAD_SAFETY : 0),
    528                         port, NULL, NULL, &ahc_cont, NULL,
    529                         MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE,
    530                         MHD_OPTION_END);
    531   if (d == NULL)
    532     return 256;
    533   if (0 == port)
    534   {
    535     const union MHD_DaemonInfo *dinfo;
    536     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    537     if ((NULL == dinfo) || (0 == dinfo->port) )
    538     {
    539       MHD_stop_daemon (d); return 32;
    540     }
    541     port = dinfo->port;
    542   }
    543   c = curl_easy_init ();
    544   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/");
    545   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    546   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    547   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    548   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    549   if (oneone)
    550     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    551   else
    552     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    553   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    554   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    555   /* NOTE: use of CONNECTTIMEOUT without also
    556      setting NOSIGNAL results in really weird
    557      crashes on my system! */
    558   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    559   curl_easy_setopt (c, CURLOPT_PROXY, "");
    560 
    561 
    562   multi = curl_multi_init ();
    563   if (multi == NULL)
    564   {
    565     curl_easy_cleanup (c);
    566     MHD_stop_daemon (d);
    567     return 512;
    568   }
    569   mret = curl_multi_add_handle (multi, c);
    570   if (mret != CURLM_OK)
    571   {
    572     curl_multi_cleanup (multi);
    573     curl_easy_cleanup (c);
    574     MHD_stop_daemon (d);
    575     return 1024;
    576   }
    577   start = time (NULL);
    578   while ((time (NULL) - start < 5) && (multi != NULL))
    579   {
    580     maxsock = MHD_INVALID_SOCKET;
    581     maxposixs = -1;
    582     FD_ZERO (&rs);
    583     FD_ZERO (&ws);
    584     FD_ZERO (&es);
    585     curl_multi_perform (multi, &running);
    586     mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
    587     if (mret != CURLM_OK)
    588     {
    589       curl_multi_remove_handle (multi, c);
    590       curl_multi_cleanup (multi);
    591       curl_easy_cleanup (c);
    592       MHD_stop_daemon (d);
    593       return 2048;
    594     }
    595     if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock))
    596     {
    597       curl_multi_remove_handle (multi, c);
    598       curl_multi_cleanup (multi);
    599       curl_easy_cleanup (c);
    600       MHD_stop_daemon (d);
    601       return 4096;
    602     }
    603     tv.tv_sec = 0;
    604     tv.tv_usec = 1000;
    605     if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
    606     {
    607 #ifdef MHD_POSIX_SOCKETS
    608       if (EINTR != errno)
    609       {
    610         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    611                  (int) errno, __LINE__);
    612         fflush (stderr);
    613         exit (99);
    614       }
    615 #else
    616       if ((WSAEINVAL != WSAGetLastError ()) ||
    617           (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
    618       {
    619         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    620                  (int) WSAGetLastError (), __LINE__);
    621         fflush (stderr);
    622         exit (99);
    623       }
    624       Sleep (1);
    625 #endif
    626     }
    627     curl_multi_perform (multi, &running);
    628     if (0 == running)
    629     {
    630       int pending;
    631       int curl_fine = 0;
    632       while (NULL != (msg = curl_multi_info_read (multi, &pending)))
    633       {
    634         if (msg->msg == CURLMSG_DONE)
    635         {
    636           if (msg->data.result == CURLE_OK)
    637             curl_fine = 1;
    638           else
    639           {
    640             fprintf (stderr,
    641                      "%s failed at %s:%d: `%s'\n",
    642                      "curl_multi_perform",
    643                      __FILE__,
    644                      __LINE__, curl_easy_strerror (msg->data.result));
    645             abort ();
    646           }
    647         }
    648       }
    649       if (! curl_fine)
    650       {
    651         fprintf (stderr, "libcurl haven't returned OK code\n");
    652         abort ();
    653       }
    654       curl_multi_remove_handle (multi, c);
    655       curl_multi_cleanup (multi);
    656       curl_easy_cleanup (c);
    657       c = NULL;
    658       multi = NULL;
    659     }
    660     MHD_run (d);
    661   }
    662   if (multi != NULL)
    663   {
    664     curl_multi_remove_handle (multi, c);
    665     curl_easy_cleanup (c);
    666     curl_multi_cleanup (multi);
    667   }
    668   MHD_stop_daemon (d);
    669   if (cbc.pos != TESTSTR_SIZE)
    670     return 8192;
    671   if (0 != check_read_data (cbc.buf, cbc.pos))
    672     return 16384;
    673   return 0;
    674 }
    675 
    676 
    677 static unsigned int
    678 testUnknownPortGet (void)
    679 {
    680   struct MHD_Daemon *d;
    681   const union MHD_DaemonInfo *di;
    682   CURL *c;
    683   struct CBC cbc;
    684   CURLcode errornum;
    685   uint16_t port;
    686   char buf[2048];
    687 
    688   struct sockaddr_in addr;
    689   socklen_t addr_len = sizeof(addr);
    690   memset (&addr, 0, sizeof(addr));
    691   addr.sin_family = AF_INET;
    692   addr.sin_port = 0;
    693   addr.sin_addr.s_addr = INADDR_ANY;
    694 
    695   cbc.buf = (char *) readbuf;
    696   cbc.size = sizeof(readbuf);
    697   cbc.pos = 0;
    698   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    699                         0, NULL, NULL, &ahc_cont, NULL,
    700                         MHD_OPTION_SOCK_ADDR, &addr,
    701                         MHD_OPTION_END);
    702   if (d == NULL)
    703     return 32768;
    704 
    705   if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    706   {
    707     di = MHD_get_daemon_info (d, MHD_DAEMON_INFO_LISTEN_FD);
    708     if (di == NULL)
    709       return 65536;
    710 
    711     if (0 != getsockname (di->listen_fd, (struct sockaddr *) &addr, &addr_len))
    712       return 131072;
    713 
    714     if (addr.sin_family != AF_INET)
    715       return 26214;
    716     port = ntohs (addr.sin_port);
    717   }
    718   else
    719   {
    720     const union MHD_DaemonInfo *dinfo;
    721     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    722     if ((NULL == dinfo) || (0 == dinfo->port) )
    723     {
    724       MHD_stop_daemon (d); return 32;
    725     }
    726     port = dinfo->port;
    727   }
    728 
    729   snprintf (buf, sizeof(buf), "http://127.0.0.1:%u/",
    730             (unsigned int) port);
    731 
    732   c = curl_easy_init ();
    733   curl_easy_setopt (c, CURLOPT_URL, buf);
    734   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    735   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    736   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    737   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    738   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    739   if (oneone)
    740     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    741   else
    742     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    743   /* NOTE: use of CONNECTTIMEOUT without also
    744      setting NOSIGNAL results in really weird
    745      crashes on my system! */
    746   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    747   curl_easy_setopt (c, CURLOPT_PROXY, "");
    748   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    749   {
    750     fprintf (stderr,
    751              "curl_easy_perform failed: `%s'\n",
    752              curl_easy_strerror (errornum));
    753     curl_easy_cleanup (c);
    754     MHD_stop_daemon (d);
    755     return 524288;
    756   }
    757   curl_easy_cleanup (c);
    758   MHD_stop_daemon (d);
    759   if (cbc.pos != TESTSTR_SIZE)
    760     return 1048576;
    761   if (0 != check_read_data (cbc.buf, cbc.pos))
    762     return 2097152;
    763   return 0;
    764 }
    765 
    766 
    767 int
    768 main (int argc, char *const *argv)
    769 {
    770   unsigned int errorCount = 0;
    771   (void) argc;   /* Unused. Silent compiler warning. */
    772 
    773   if ((NULL == argv) || (0 == argv[0]))
    774     return 99;
    775   oneone = has_in_name (argv[0], "11");
    776 
    777   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
    778     return 2;
    779   if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS))
    780   {
    781     errorCount += testInternalGet (true);
    782     errorCount += testInternalGet (false);
    783     errorCount += testMultithreadedGet ();
    784     errorCount += testMultithreadedPoolGet ();
    785     errorCount += testUnknownPortGet ();
    786     errorCount += testExternalGet (0);
    787   }
    788   errorCount += testExternalGet (! 0);
    789   if (errorCount != 0)
    790     fprintf (stderr, "Error (code: %u)\n", errorCount);
    791   curl_global_cleanup ();
    792   return (0 == errorCount) ? 0 : 1;       /* 0 == pass */
    793 }