libmicrohttpd

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

test_get_empty.c (28245B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007, 2009, 2011, 2019 Christian Grothoff
      4      Copyright (C) 2014-2022 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  * @file test_get_empty.c
     23  * @brief  Testcase for libmicrohttpd GET operations returning an empty body
     24  * @author Christian Grothoff
     25  * @author Karlson2k (Evgeny Grin)
     26  */
     27 #include "MHD_config.h"
     28 #include "platform.h"
     29 #include <curl/curl.h>
     30 #include <microhttpd.h>
     31 #include <stdlib.h>
     32 #include <string.h>
     33 #include <time.h>
     34 #include <errno.h>
     35 #include "mhd_has_in_name.h"
     36 #include "mhd_has_param.h"
     37 #include "mhd_sockets.h" /* only macros used */
     38 
     39 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     40    test into a marked, classifiable test error (TESTING.md, P5). */
     41 #include "mhd_panic_tripwire.h"
     42 
     43 
     44 #define EXPECTED_URI_PATH "/hello_world?a=%26&b=c"
     45 
     46 #ifdef _WIN32
     47 #ifndef WIN32_LEAN_AND_MEAN
     48 #define WIN32_LEAN_AND_MEAN 1
     49 #endif /* !WIN32_LEAN_AND_MEAN */
     50 #include <windows.h>
     51 #endif
     52 
     53 #ifndef WINDOWS
     54 #include <unistd.h>
     55 #include <sys/socket.h>
     56 #endif
     57 
     58 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2
     59 #undef MHD_CPU_COUNT
     60 #endif
     61 #if ! defined(MHD_CPU_COUNT)
     62 #define MHD_CPU_COUNT 2
     63 #endif
     64 
     65 static int oneone;
     66 static uint16_t global_port;
     67 
     68 struct CBC
     69 {
     70   char *buf;
     71   size_t pos;
     72   size_t size;
     73 };
     74 
     75 
     76 static size_t
     77 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
     78 {
     79   struct CBC *cbc = ctx;
     80 
     81   if (cbc->pos + size * nmemb > cbc->size)
     82     return 0;                   /* overflow */
     83   memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
     84   cbc->pos += size * nmemb;
     85   return size * nmemb;
     86 }
     87 
     88 
     89 static void *
     90 log_cb (void *cls,
     91         const char *uri,
     92         struct MHD_Connection *con)
     93 {
     94   (void) cls;
     95   (void) con;
     96   if (0 != strcmp (uri,
     97                    EXPECTED_URI_PATH))
     98   {
     99     fprintf (stderr,
    100              "Wrong URI: `%s'\n",
    101              uri);
    102     _exit (22);
    103   }
    104   return NULL;
    105 }
    106 
    107 
    108 static int
    109 ahc_echo (void *cls,
    110           struct MHD_Connection *connection,
    111           const char *url,
    112           const char *method,
    113           const char *version,
    114           const char *upload_data, size_t *upload_data_size,
    115           void **req_cls)
    116 {
    117   static int ptr;
    118   const char *me = cls;
    119   struct MHD_Response *response;
    120   int ret;
    121   (void) version;
    122   (void) upload_data;
    123   (void) upload_data_size;       /* Unused. Silence compiler warning. */
    124 
    125   if (0 != strcmp (me, method))
    126     return MHD_NO;              /* unexpected method */
    127   if (&ptr != *req_cls)
    128   {
    129     *req_cls = &ptr;
    130     return MHD_YES;
    131   }
    132   *req_cls = NULL;
    133   response = MHD_create_response_empty (MHD_RF_NONE);
    134   ret = MHD_queue_response (connection,
    135                             MHD_HTTP_NO_CONTENT,
    136                             response);
    137   MHD_destroy_response (response);
    138   if (ret == MHD_NO)
    139   {
    140     fprintf (stderr, "Failed to queue response.\n");
    141     _exit (19);
    142   }
    143   return ret;
    144 }
    145 
    146 
    147 static unsigned int
    148 testInternalGet (uint32_t poll_flag)
    149 {
    150   struct MHD_Daemon *d;
    151   CURL *c;
    152   char buf[2048];
    153   struct CBC cbc;
    154   CURLcode errornum;
    155 
    156   if ( (0 == global_port) &&
    157        (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) )
    158   {
    159     global_port = 1220;
    160     if (oneone)
    161       global_port += 20;
    162   }
    163 
    164   cbc.buf = buf;
    165   cbc.size = 2048;
    166   cbc.pos = 0;
    167   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    168                         | (enum MHD_FLAG) poll_flag,
    169                         global_port, NULL, NULL,
    170                         &ahc_echo, "GET",
    171                         MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL,
    172                         MHD_OPTION_END);
    173   if (d == NULL)
    174     return 1;
    175   if (0 == global_port)
    176   {
    177     const union MHD_DaemonInfo *dinfo;
    178     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    179     if ((NULL == dinfo) || (0 == dinfo->port) )
    180     {
    181       MHD_stop_daemon (d); return 32;
    182     }
    183     global_port = dinfo->port;
    184   }
    185   c = curl_easy_init ();
    186   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1" EXPECTED_URI_PATH);
    187   curl_easy_setopt (c, CURLOPT_PORT, (long) global_port);
    188   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    189   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    190   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    191   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    192   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    193   if (oneone)
    194     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    195   else
    196     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    197   /* NOTE: use of CONNECTTIMEOUT without also
    198      setting NOSIGNAL results in really weird
    199      crashes on my system!*/
    200   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    201   curl_easy_setopt (c, CURLOPT_PROXY, "");
    202   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    203   {
    204     fprintf (stderr,
    205              "curl_easy_perform failed: `%s'\n",
    206              curl_easy_strerror (errornum));
    207     curl_easy_cleanup (c);
    208     MHD_stop_daemon (d);
    209     return 2;
    210   }
    211   curl_easy_cleanup (c);
    212   MHD_stop_daemon (d);
    213   if (cbc.pos != 0)
    214     return 4;
    215   return 0;
    216 }
    217 
    218 
    219 static unsigned int
    220 testMultithreadedGet (uint32_t poll_flag)
    221 {
    222   struct MHD_Daemon *d;
    223   CURL *c;
    224   char buf[2048];
    225   struct CBC cbc;
    226   CURLcode errornum;
    227 
    228   if ( (0 == global_port) &&
    229        (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) )
    230   {
    231     global_port = 1221;
    232     if (oneone)
    233       global_port += 20;
    234   }
    235 
    236   cbc.buf = buf;
    237   cbc.size = 2048;
    238   cbc.pos = 0;
    239   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    240                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    241                         | (enum MHD_FLAG) poll_flag,
    242                         global_port, NULL, NULL,
    243                         &ahc_echo, "GET",
    244                         MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL,
    245                         MHD_OPTION_END);
    246   if (d == NULL)
    247     return 16;
    248   if (0 == global_port)
    249   {
    250     const union MHD_DaemonInfo *dinfo;
    251     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    252     if ((NULL == dinfo) || (0 == dinfo->port) )
    253     {
    254       MHD_stop_daemon (d); return 32;
    255     }
    256     global_port = dinfo->port;
    257   }
    258   c = curl_easy_init ();
    259   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1" EXPECTED_URI_PATH);
    260   curl_easy_setopt (c, CURLOPT_PORT, (long) global_port);
    261   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    262   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    263   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    264   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    265   if (oneone)
    266     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    267   else
    268     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    269   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    270   /* NOTE: use of CONNECTTIMEOUT without also
    271      setting NOSIGNAL results in really weird
    272      crashes on my system! */
    273   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    274   curl_easy_setopt (c, CURLOPT_PROXY, "");
    275   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    276   {
    277     fprintf (stderr,
    278              "curl_easy_perform failed: `%s'\n",
    279              curl_easy_strerror (errornum));
    280     curl_easy_cleanup (c);
    281     MHD_stop_daemon (d);
    282     return 32;
    283   }
    284   curl_easy_cleanup (c);
    285   MHD_stop_daemon (d);
    286   if (cbc.pos != 0)
    287     return 64;
    288   return 0;
    289 }
    290 
    291 
    292 static unsigned int
    293 testMultithreadedPoolGet (uint32_t poll_flag)
    294 {
    295   struct MHD_Daemon *d;
    296   CURL *c;
    297   char buf[2048];
    298   struct CBC cbc;
    299   CURLcode errornum;
    300 
    301   if ( (0 == global_port) &&
    302        (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) )
    303   {
    304     global_port = 1222;
    305     if (oneone)
    306       global_port += 20;
    307   }
    308 
    309   cbc.buf = buf;
    310   cbc.size = 2048;
    311   cbc.pos = 0;
    312   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    313                         | (enum MHD_FLAG) poll_flag,
    314                         global_port, NULL, NULL,
    315                         &ahc_echo, "GET",
    316                         MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT,
    317                         MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL,
    318                         MHD_OPTION_END);
    319   if (d == NULL)
    320     return 16;
    321   if (0 == global_port)
    322   {
    323     const union MHD_DaemonInfo *dinfo;
    324     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    325     if ((NULL == dinfo) || (0 == dinfo->port) )
    326     {
    327       MHD_stop_daemon (d); return 32;
    328     }
    329     global_port = dinfo->port;
    330   }
    331   c = curl_easy_init ();
    332   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1" EXPECTED_URI_PATH);
    333   curl_easy_setopt (c, CURLOPT_PORT, (long) global_port);
    334   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    335   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    336   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    337   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    338   if (oneone)
    339     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    340   else
    341     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    342   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    343   /* NOTE: use of CONNECTTIMEOUT without also
    344      setting NOSIGNAL results in really weird
    345      crashes on my system!*/
    346   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    347   curl_easy_setopt (c, CURLOPT_PROXY, "");
    348   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    349   {
    350     fprintf (stderr,
    351              "curl_easy_perform failed: `%s'\n",
    352              curl_easy_strerror (errornum));
    353     curl_easy_cleanup (c);
    354     MHD_stop_daemon (d);
    355     return 32;
    356   }
    357   curl_easy_cleanup (c);
    358   MHD_stop_daemon (d);
    359   if (cbc.pos != 0)
    360     return 64;
    361   return 0;
    362 }
    363 
    364 
    365 static unsigned int
    366 testExternalGet ()
    367 {
    368   struct MHD_Daemon *d;
    369   CURL *c;
    370   char buf[2048];
    371   struct CBC cbc;
    372   CURLM *multi;
    373   CURLMcode mret;
    374   fd_set rs;
    375   fd_set ws;
    376   fd_set es;
    377   MHD_socket maxsock;
    378   int maxposixs;
    379   int running;
    380   struct CURLMsg *msg;
    381   time_t start;
    382   struct timeval tv;
    383 
    384   if ( (0 == global_port) &&
    385        (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) )
    386   {
    387     global_port = 1223;
    388     if (oneone)
    389       global_port += 20;
    390   }
    391 
    392   multi = NULL;
    393   cbc.buf = buf;
    394   cbc.size = 2048;
    395   cbc.pos = 0;
    396   d = MHD_start_daemon (MHD_USE_ERROR_LOG,
    397                         global_port, NULL, NULL,
    398                         &ahc_echo, "GET",
    399                         MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL,
    400                         MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE,
    401                         MHD_OPTION_END);
    402   if (d == NULL)
    403     return 256;
    404   if (0 == global_port)
    405   {
    406     const union MHD_DaemonInfo *dinfo;
    407     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    408     if ((NULL == dinfo) || (0 == dinfo->port) )
    409     {
    410       MHD_stop_daemon (d); return 32;
    411     }
    412     global_port = dinfo->port;
    413   }
    414   c = curl_easy_init ();
    415   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1" EXPECTED_URI_PATH);
    416   curl_easy_setopt (c, CURLOPT_PORT, (long) global_port);
    417   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    418   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    419   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    420   if (oneone)
    421     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    422   else
    423     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    424   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    425   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    426   /* NOTE: use of CONNECTTIMEOUT without also
    427      setting NOSIGNAL results in really weird
    428      crashes on my system! */
    429   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    430   curl_easy_setopt (c, CURLOPT_PROXY, "");
    431 
    432 
    433   multi = curl_multi_init ();
    434   if (multi == NULL)
    435   {
    436     curl_easy_cleanup (c);
    437     MHD_stop_daemon (d);
    438     return 512;
    439   }
    440   mret = curl_multi_add_handle (multi, c);
    441   if (mret != CURLM_OK)
    442   {
    443     curl_multi_cleanup (multi);
    444     curl_easy_cleanup (c);
    445     MHD_stop_daemon (d);
    446     return 1024;
    447   }
    448   start = time (NULL);
    449   while ((time (NULL) - start < 5) && (multi != NULL))
    450   {
    451     maxsock = MHD_INVALID_SOCKET;
    452     maxposixs = -1;
    453     FD_ZERO (&rs);
    454     FD_ZERO (&ws);
    455     FD_ZERO (&es);
    456     curl_multi_perform (multi, &running);
    457     mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
    458     if (mret != CURLM_OK)
    459     {
    460       curl_multi_remove_handle (multi, c);
    461       curl_multi_cleanup (multi);
    462       curl_easy_cleanup (c);
    463       MHD_stop_daemon (d);
    464       return 2048;
    465     }
    466     if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock))
    467     {
    468       curl_multi_remove_handle (multi, c);
    469       curl_multi_cleanup (multi);
    470       curl_easy_cleanup (c);
    471       MHD_stop_daemon (d);
    472       return 4096;
    473     }
    474     tv.tv_sec = 0;
    475     tv.tv_usec = 1000;
    476 #ifdef MHD_POSIX_SOCKETS
    477     if (maxsock > maxposixs)
    478       maxposixs = maxsock;
    479 #endif /* MHD_POSIX_SOCKETS */
    480     if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
    481     {
    482 #ifdef MHD_POSIX_SOCKETS
    483       if (EINTR != errno)
    484       {
    485         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    486                  (int) errno, __LINE__);
    487         fflush (stderr);
    488         exit (99);
    489       }
    490 #else
    491       if ((WSAEINVAL != WSAGetLastError ()) ||
    492           (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
    493       {
    494         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    495                  (int) WSAGetLastError (), __LINE__);
    496         fflush (stderr);
    497         exit (99);
    498       }
    499       Sleep (1);
    500 #endif
    501     }
    502     curl_multi_perform (multi, &running);
    503     if (0 == running)
    504     {
    505       int pending;
    506       int curl_fine = 0;
    507       while (NULL != (msg = curl_multi_info_read (multi, &pending)))
    508       {
    509         if (msg->msg == CURLMSG_DONE)
    510         {
    511           if (msg->data.result == CURLE_OK)
    512             curl_fine = 1;
    513           else
    514           {
    515             fprintf (stderr,
    516                      "%s failed at %s:%d: `%s'\n",
    517                      "curl_multi_perform",
    518                      __FILE__,
    519                      __LINE__, curl_easy_strerror (msg->data.result));
    520             abort ();
    521           }
    522         }
    523       }
    524       if (! curl_fine)
    525       {
    526         fprintf (stderr, "libcurl haven't returned OK code\n");
    527         abort ();
    528       }
    529       curl_multi_remove_handle (multi, c);
    530       curl_multi_cleanup (multi);
    531       curl_easy_cleanup (c);
    532       c = NULL;
    533       multi = NULL;
    534     }
    535     MHD_run (d);
    536   }
    537   if (multi != NULL)
    538   {
    539     curl_multi_remove_handle (multi, c);
    540     curl_easy_cleanup (c);
    541     curl_multi_cleanup (multi);
    542   }
    543   MHD_stop_daemon (d);
    544   if (cbc.pos != 0)
    545     return 8192;
    546   return 0;
    547 }
    548 
    549 
    550 static unsigned int
    551 testUnknownPortGet (uint32_t poll_flag)
    552 {
    553   struct MHD_Daemon *d;
    554   const union MHD_DaemonInfo *di;
    555   CURL *c;
    556   char buf[2048];
    557   struct CBC cbc;
    558   CURLcode errornum;
    559   uint16_t port;
    560 
    561   struct sockaddr_in addr;
    562   socklen_t addr_len = sizeof(addr);
    563   memset (&addr, 0, sizeof(addr));
    564   addr.sin_family = AF_INET;
    565   addr.sin_port = 0;
    566   addr.sin_addr.s_addr = INADDR_ANY;
    567 
    568   cbc.buf = buf;
    569   cbc.size = 2048;
    570   cbc.pos = 0;
    571   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    572                         | (enum MHD_FLAG) poll_flag,
    573                         0, NULL, NULL, &ahc_echo, "GET",
    574                         MHD_OPTION_SOCK_ADDR, &addr,
    575                         MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL,
    576                         MHD_OPTION_END);
    577   if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    578   {
    579     di = MHD_get_daemon_info (d, MHD_DAEMON_INFO_LISTEN_FD);
    580     if (di == NULL)
    581       return 65536;
    582 
    583     if (0 != getsockname (di->listen_fd, (struct sockaddr *) &addr, &addr_len))
    584       return 131072;
    585 
    586     if (addr.sin_family != AF_INET)
    587       return 26214;
    588     port = ntohs (addr.sin_port);
    589   }
    590   else
    591   {
    592     const union MHD_DaemonInfo *dinfo;
    593     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    594     if ((NULL == dinfo) || (0 == dinfo->port) )
    595     {
    596       MHD_stop_daemon (d); return 32;
    597     }
    598     port = dinfo->port;
    599   }
    600 
    601   snprintf (buf,
    602             sizeof(buf),
    603             "http://127.0.0.1:%u%s",
    604             (unsigned int) port,
    605             EXPECTED_URI_PATH);
    606 
    607   c = curl_easy_init ();
    608   curl_easy_setopt (c, CURLOPT_URL, buf);
    609   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    610   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    611   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    612   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    613   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    614   if (oneone)
    615     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    616   else
    617     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    618   /* NOTE: use of CONNECTTIMEOUT without also
    619      setting NOSIGNAL results in really weird
    620      crashes on my system! */
    621   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    622   curl_easy_setopt (c, CURLOPT_PROXY, "");
    623   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    624   {
    625     fprintf (stderr,
    626              "curl_easy_perform failed: `%s'\n",
    627              curl_easy_strerror (errornum));
    628     curl_easy_cleanup (c);
    629     MHD_stop_daemon (d);
    630     return 524288;
    631   }
    632   curl_easy_cleanup (c);
    633   MHD_stop_daemon (d);
    634   if (cbc.pos != 0)
    635     return 1048576;
    636   return 0;
    637 }
    638 
    639 
    640 static unsigned int
    641 testStopRace (uint32_t poll_flag)
    642 {
    643   struct sockaddr_in sin;
    644   MHD_socket fd;
    645   struct MHD_Daemon *d;
    646 
    647   if ( (0 == global_port) &&
    648        (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) )
    649   {
    650     global_port = 1224;
    651     if (oneone)
    652       global_port += 20;
    653   }
    654 
    655   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    656                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    657                         | (enum MHD_FLAG) poll_flag,
    658                         global_port, NULL, NULL,
    659                         &ahc_echo, "GET",
    660                         MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL,
    661                         MHD_OPTION_END);
    662   if (d == NULL)
    663     return 16;
    664   if (0 == global_port)
    665   {
    666     const union MHD_DaemonInfo *dinfo;
    667     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    668     if ((NULL == dinfo) || (0 == dinfo->port) )
    669     {
    670       MHD_stop_daemon (d); return 32;
    671     }
    672     global_port = dinfo->port;
    673   }
    674 
    675   fd = socket (PF_INET, SOCK_STREAM, 0);
    676   if (fd == MHD_INVALID_SOCKET)
    677   {
    678     fprintf (stderr, "socket error\n");
    679     return 256;
    680   }
    681 
    682   memset (&sin, 0, sizeof(sin));
    683   sin.sin_family = AF_INET;
    684   sin.sin_port = htons (global_port);
    685   sin.sin_addr.s_addr = htonl (0x7f000001);
    686 
    687   if (connect (fd, (struct sockaddr *) (&sin), sizeof(sin)) < 0)
    688   {
    689     fprintf (stderr, "connect error\n");
    690     MHD_socket_close_chk_ (fd);
    691     return 512;
    692   }
    693 
    694   /*  printf("Waiting\n"); */
    695   /* Let the thread get going. */
    696   usleep (500000);
    697 
    698   /* printf("Stopping daemon\n"); */
    699   MHD_stop_daemon (d);
    700 
    701   MHD_socket_close_chk_ (fd);
    702 
    703   /* printf("good\n"); */
    704   return 0;
    705 }
    706 
    707 
    708 static int
    709 ahc_empty (void *cls,
    710            struct MHD_Connection *connection,
    711            const char *url,
    712            const char *method,
    713            const char *version,
    714            const char *upload_data, size_t *upload_data_size,
    715            void **req_cls)
    716 {
    717   static int ptr;
    718   struct MHD_Response *response;
    719   int ret;
    720   (void) cls;
    721   (void) url;
    722   (void) url;
    723   (void) version;          /* Unused. Silence compiler warning. */
    724   (void) upload_data;
    725   (void) upload_data_size; /* Unused. Silent compiler warning. */
    726 
    727   if (0 != strcmp ("GET", method))
    728     return MHD_NO;              /* unexpected method */
    729   if (&ptr != *req_cls)
    730   {
    731     *req_cls = &ptr;
    732     return MHD_YES;
    733   }
    734   *req_cls = NULL;
    735   response = MHD_create_response_empty (MHD_RF_NONE);
    736   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
    737   MHD_destroy_response (response);
    738   if (ret == MHD_NO)
    739   {
    740     fprintf (stderr, "Failed to queue response.\n");
    741     _exit (20);
    742   }
    743   return ret;
    744 }
    745 
    746 
    747 static int
    748 curlExcessFound (CURL *c, curl_infotype type, char *data, size_t size,
    749                  void *cls)
    750 {
    751   static const char *excess_found = "Excess found";
    752   const size_t str_size = strlen (excess_found);
    753   (void) c;      /* Unused. Silent compiler warning. */
    754 
    755   if ((CURLINFO_TEXT == type)
    756       && (size >= str_size)
    757       && (0 == strncmp (excess_found, data, str_size)))
    758     *(int *) cls = 1;
    759   return 0;
    760 }
    761 
    762 
    763 static unsigned int
    764 testEmptyGet (uint32_t poll_flag)
    765 {
    766   struct MHD_Daemon *d;
    767   CURL *c;
    768   char buf[2048];
    769   struct CBC cbc;
    770   CURLcode errornum;
    771   int excess_found = 0;
    772 
    773   if ( (0 == global_port) &&
    774        (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) )
    775   {
    776     global_port = 1225;
    777     if (oneone)
    778       global_port += 20;
    779   }
    780 
    781   cbc.buf = buf;
    782   cbc.size = 2048;
    783   cbc.pos = 0;
    784   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    785                         | (enum MHD_FLAG) poll_flag,
    786                         global_port, NULL, NULL,
    787                         &ahc_empty, NULL,
    788                         MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL,
    789                         MHD_OPTION_END);
    790   if (d == NULL)
    791     return 4194304;
    792   if (0 == global_port)
    793   {
    794     const union MHD_DaemonInfo *dinfo;
    795     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    796     if ((NULL == dinfo) || (0 == dinfo->port) )
    797     {
    798       MHD_stop_daemon (d); return 32;
    799     }
    800     global_port = dinfo->port;
    801   }
    802   c = curl_easy_init ();
    803   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1" EXPECTED_URI_PATH);
    804   curl_easy_setopt (c, CURLOPT_PORT, (long) global_port);
    805   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    806   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    807   curl_easy_setopt (c, CURLOPT_DEBUGFUNCTION, &curlExcessFound);
    808   curl_easy_setopt (c, CURLOPT_DEBUGDATA, &excess_found);
    809   curl_easy_setopt (c, CURLOPT_VERBOSE, 1L);
    810   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    811   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    812   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    813   if (oneone)
    814     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    815   else
    816     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    817   /* NOTE: use of CONNECTTIMEOUT without also
    818      setting NOSIGNAL results in really weird
    819      crashes on my system!*/
    820   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    821   curl_easy_setopt (c, CURLOPT_PROXY, "");
    822   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    823   {
    824     fprintf (stderr,
    825              "curl_easy_perform failed: `%s'\n",
    826              curl_easy_strerror (errornum));
    827     curl_easy_cleanup (c);
    828     MHD_stop_daemon (d);
    829     return 8388608;
    830   }
    831   curl_easy_cleanup (c);
    832   MHD_stop_daemon (d);
    833   if (cbc.pos != 0)
    834     return 16777216;
    835   if (excess_found)
    836     return 33554432;
    837   return 0;
    838 }
    839 
    840 
    841 int
    842 main (int argc, char *const *argv)
    843 {
    844   unsigned int errorCount = 0;
    845   unsigned int test_result = 0;
    846   int verbose = 0;
    847 
    848   if ((NULL == argv) || (0 == argv[0]))
    849     return 99;
    850   oneone = has_in_name (argv[0], "11");
    851   verbose = has_param (argc, argv, "-v") || has_param (argc, argv, "--verbose");
    852   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
    853     return 2;
    854   global_port = 0;
    855   test_result = testExternalGet ();
    856   if (test_result)
    857     fprintf (stderr, "FAILED: testExternalGet () - %u.\n", test_result);
    858   else if (verbose)
    859     printf ("PASSED: testExternalGet ().\n");
    860   errorCount += test_result;
    861   if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS))
    862   {
    863     test_result += testInternalGet (0);
    864     if (test_result)
    865       fprintf (stderr, "FAILED: testInternalGet (0) - %u.\n", test_result);
    866     else if (verbose)
    867       printf ("PASSED: testInternalGet (0).\n");
    868     errorCount += test_result;
    869     test_result += testMultithreadedGet (0);
    870     if (test_result)
    871       fprintf (stderr, "FAILED: testMultithreadedGet (0) - %u.\n", test_result);
    872     else if (verbose)
    873       printf ("PASSED: testMultithreadedGet (0).\n");
    874     errorCount += test_result;
    875     test_result += testMultithreadedPoolGet (0);
    876     if (test_result)
    877       fprintf (stderr, "FAILED: testMultithreadedPoolGet (0) - %u.\n",
    878                test_result);
    879     else if (verbose)
    880       printf ("PASSED: testMultithreadedPoolGet (0).\n");
    881     errorCount += test_result;
    882     test_result += testUnknownPortGet (0);
    883     if (test_result)
    884       fprintf (stderr, "FAILED: testUnknownPortGet (0) - %u.\n", test_result);
    885     else if (verbose)
    886       printf ("PASSED: testUnknownPortGet (0).\n");
    887     errorCount += test_result;
    888     test_result += testEmptyGet (0);
    889     if (test_result)
    890       fprintf (stderr, "FAILED: testEmptyGet (0) - %u.\n", test_result);
    891     else if (verbose)
    892       printf ("PASSED: testEmptyGet (0).\n");
    893     errorCount += test_result;
    894     if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_POLL))
    895     {
    896       test_result += testInternalGet (MHD_USE_POLL);
    897       if (test_result)
    898         fprintf (stderr, "FAILED: testInternalGet (MHD_USE_POLL) - %u.\n",
    899                  test_result);
    900       else if (verbose)
    901         printf ("PASSED: testInternalGet (MHD_USE_POLL).\n");
    902       errorCount += test_result;
    903       test_result += testMultithreadedGet (MHD_USE_POLL);
    904       if (test_result)
    905         fprintf (stderr, "FAILED: testMultithreadedGet (MHD_USE_POLL) - %u.\n",
    906                  test_result);
    907       else if (verbose)
    908         printf ("PASSED: testMultithreadedGet (MHD_USE_POLL).\n");
    909       errorCount += test_result;
    910       test_result += testMultithreadedPoolGet (MHD_USE_POLL);
    911       if (test_result)
    912         fprintf (stderr,
    913                  "FAILED: testMultithreadedPoolGet (MHD_USE_POLL) - %u.\n",
    914                  test_result);
    915       else if (verbose)
    916         printf ("PASSED: testMultithreadedPoolGet (MHD_USE_POLL).\n");
    917       errorCount += test_result;
    918       test_result += testUnknownPortGet (MHD_USE_POLL);
    919       if (test_result)
    920         fprintf (stderr, "FAILED: testUnknownPortGet (MHD_USE_POLL) - %u.\n",
    921                  test_result);
    922       else if (verbose)
    923         printf ("PASSED: testUnknownPortGet (MHD_USE_POLL).\n");
    924       errorCount += test_result;
    925       test_result += testEmptyGet (MHD_USE_POLL);
    926       if (test_result)
    927         fprintf (stderr, "FAILED: testEmptyGet (MHD_USE_POLL) - %u.\n",
    928                  test_result);
    929       else if (verbose)
    930         printf ("PASSED: testEmptyGet (MHD_USE_POLL).\n");
    931       errorCount += test_result;
    932     }
    933     if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_EPOLL))
    934     {
    935       test_result += testInternalGet (MHD_USE_EPOLL);
    936       if (test_result)
    937         fprintf (stderr, "FAILED: testInternalGet (MHD_USE_EPOLL) - %u.\n",
    938                  test_result);
    939       else if (verbose)
    940         printf ("PASSED: testInternalGet (MHD_USE_EPOLL).\n");
    941       errorCount += test_result;
    942       test_result += testMultithreadedPoolGet (MHD_USE_EPOLL);
    943       if (test_result)
    944         fprintf (stderr,
    945                  "FAILED: testMultithreadedPoolGet (MHD_USE_EPOLL) - %u.\n",
    946                  test_result);
    947       else if (verbose)
    948         printf ("PASSED: testMultithreadedPoolGet (MHD_USE_EPOLL).\n");
    949       errorCount += test_result;
    950       test_result += testUnknownPortGet (MHD_USE_EPOLL);
    951       if (test_result)
    952         fprintf (stderr, "FAILED: testUnknownPortGet (MHD_USE_EPOLL) - %u.\n",
    953                  test_result);
    954       else if (verbose)
    955         printf ("PASSED: testUnknownPortGet (MHD_USE_EPOLL).\n");
    956       errorCount += test_result;
    957       test_result += testEmptyGet (MHD_USE_EPOLL);
    958       if (test_result)
    959         fprintf (stderr, "FAILED: testEmptyGet (MHD_USE_EPOLL) - %u.\n",
    960                  test_result);
    961       else if (verbose)
    962         printf ("PASSED: testEmptyGet (MHD_USE_EPOLL).\n");
    963       errorCount += test_result;
    964     }
    965   }
    966   if (0 != errorCount)
    967     fprintf (stderr,
    968              "Error (code: %u)\n",
    969              errorCount);
    970   else if (verbose)
    971     printf ("All tests passed.\n");
    972   curl_global_cleanup ();
    973   return (0 == errorCount) ? 0 : 1;       /* 0 == pass */
    974 }