libmicrohttpd

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

test_get_chunked.c (22398B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007 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 /**
     23  * @file test_get_chunked.c
     24  * @brief  Testcase for libmicrohttpd GET operations with chunked content encoding
     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 #include <errno.h>
     37 
     38 #ifndef WINDOWS
     39 #include <unistd.h>
     40 #endif
     41 
     42 #include "mhd_has_in_name.h"
     43 
     44 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     45    test into a marked, classifiable test error (TESTING.md, P5). */
     46 #include "mhd_panic_tripwire.h"
     47 
     48 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2
     49 #undef MHD_CPU_COUNT
     50 #endif
     51 #if ! defined(MHD_CPU_COUNT)
     52 #define MHD_CPU_COUNT 2
     53 #endif
     54 
     55 #define HDR_CHUNKED_ENCODING MHD_HTTP_HEADER_TRANSFER_ENCODING ": chunked"
     56 #define RESP_FOOTER_NAME "Footer"
     57 #define RESP_FOOTER_VALUE "working"
     58 #define RESP_FOOTER RESP_FOOTER_NAME ": " RESP_FOOTER_VALUE
     59 
     60 #define RESP_BLOCK_SIZE 128
     61 #define RESP_BLOCK_QUANTIY 10
     62 #define RESP_SIZE (RESP_BLOCK_SIZE * RESP_BLOCK_QUANTIY)
     63 
     64 /**
     65  * Use "Connection: close" header?
     66  */
     67 static int conn_close;
     68 
     69 /**
     70  * Use static string response instead of callback-generated?
     71  */
     72 static int resp_string;
     73 
     74 /**
     75  * Use response with known size?
     76  */
     77 static int resp_sized;
     78 
     79 /**
     80  * Use empty (zero-sized) response?
     81  */
     82 static int resp_empty;
     83 
     84 /**
     85  * Force chunked response by response header?
     86  */
     87 static int chunked_forced;
     88 
     89 /**
     90  * MHD port used for testing
     91  */
     92 static uint16_t port_global;
     93 
     94 
     95 struct headers_check_result
     96 {
     97   int found_chunked;
     98   int found_footer;
     99 };
    100 
    101 static size_t
    102 lcurl_hdr_callback (char *buffer, size_t size, size_t nitems,
    103                     void *userdata)
    104 {
    105   const size_t data_size = size * nitems;
    106   struct headers_check_result *check_res =
    107     (struct headers_check_result *) userdata;
    108 
    109   if ((data_size == strlen (HDR_CHUNKED_ENCODING) + 2) &&
    110       (0 == memcmp (buffer, HDR_CHUNKED_ENCODING "\r\n", data_size)))
    111     check_res->found_chunked = 1;
    112   if ((data_size == strlen (RESP_FOOTER) + 2) &&
    113       (0 == memcmp (buffer, RESP_FOOTER "\r\n", data_size)))
    114     check_res->found_footer = 1;
    115 
    116   return data_size;
    117 }
    118 
    119 
    120 struct CBC
    121 {
    122   char *buf;
    123   size_t pos;
    124   size_t size;
    125 };
    126 
    127 
    128 static size_t
    129 copyBuffer (void *ptr,
    130             size_t size,
    131             size_t nmemb,
    132             void *ctx)
    133 {
    134   struct CBC *cbc = ctx;
    135 
    136   if (cbc->pos + size * nmemb > cbc->size)
    137     return 0;                   /* overflow */
    138   memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
    139   cbc->pos += size * nmemb;
    140   return size * nmemb;
    141 }
    142 
    143 
    144 /**
    145  * MHD content reader callback that returns data in chunks.
    146  */
    147 static ssize_t
    148 crc (void *cls,
    149      uint64_t pos,
    150      char *buf,
    151      size_t max)
    152 {
    153   struct MHD_Response **responseptr = cls;
    154 
    155   if (resp_empty || (pos == RESP_SIZE - RESP_BLOCK_SIZE))
    156   { /* Add footer with the last block */
    157     if (MHD_YES != MHD_add_response_footer (*responseptr,
    158                                             RESP_FOOTER_NAME,
    159                                             RESP_FOOTER_VALUE))
    160       abort ();
    161   }
    162   if (resp_empty || (pos == RESP_SIZE))
    163     return MHD_CONTENT_READER_END_OF_STREAM;
    164 
    165   if (max < RESP_BLOCK_SIZE)
    166     abort ();                   /* should not happen in this testcase... */
    167   memset (buf,
    168           'A' + (char) (unsigned char) (pos / RESP_BLOCK_SIZE),
    169           RESP_BLOCK_SIZE);
    170   return RESP_BLOCK_SIZE;
    171 }
    172 
    173 
    174 /**
    175  * Dummy function that frees the "responseptr".
    176  */
    177 static void
    178 crcf (void *ptr)
    179 {
    180   free (ptr);
    181 }
    182 
    183 
    184 static enum MHD_Result
    185 ahc_echo (void *cls,
    186           struct MHD_Connection *connection,
    187           const char *url,
    188           const char *method,
    189           const char *version,
    190           const char *upload_data, size_t *upload_data_size, void **req_cls)
    191 {
    192   static int aptr;
    193   struct MHD_Response *response;
    194   enum MHD_Result ret;
    195 
    196   (void) cls;
    197   (void) url;
    198   (void) version;              /* Unused. Silent compiler warning. */
    199   (void) upload_data;
    200   (void) upload_data_size;     /* Unused. Silent compiler warning. */
    201 
    202   if (0 != strcmp (MHD_HTTP_METHOD_GET, method))
    203     return MHD_NO;              /* unexpected method */
    204   if (&aptr != *req_cls)
    205   {
    206     /* do never respond on first call */
    207     *req_cls = &aptr;
    208     return MHD_YES;
    209   }
    210   if (! resp_string)
    211   {
    212     struct MHD_Response **responseptr;
    213     responseptr = malloc (sizeof (struct MHD_Response *));
    214     if (NULL == responseptr)
    215       _exit (99);
    216 
    217     response = MHD_create_response_from_callback (resp_sized ?
    218                                                   RESP_SIZE : MHD_SIZE_UNKNOWN,
    219                                                   1024,
    220                                                   &crc,
    221                                                   responseptr,
    222                                                   &crcf);
    223     *responseptr = response;
    224   }
    225   else
    226   {
    227     if (! resp_empty)
    228     {
    229       size_t pos;
    230       static const size_t resp_size = RESP_SIZE;
    231       char *buf = malloc (resp_size);
    232       if (NULL == buf)
    233         _exit (99);
    234       for (pos = 0; pos < resp_size; pos += RESP_BLOCK_SIZE)
    235         memset (buf + pos,
    236                 'A' + (char) (unsigned char) (pos / RESP_BLOCK_SIZE),
    237                 RESP_BLOCK_SIZE);
    238 
    239       response = MHD_create_response_from_buffer_copy (resp_size, buf);
    240       free (buf);
    241     }
    242     else
    243       response = MHD_create_response_empty (MHD_RF_NONE);
    244   }
    245   if (NULL == response)
    246     abort ();
    247   if (chunked_forced)
    248   {
    249     if (MHD_NO == MHD_add_response_header (response,
    250                                            MHD_HTTP_HEADER_TRANSFER_ENCODING,
    251                                            "chunked"))
    252       abort ();
    253   }
    254   if (MHD_NO == MHD_add_response_header (response,
    255                                          MHD_HTTP_HEADER_TRAILER,
    256                                          RESP_FOOTER_NAME))
    257     abort ();
    258 
    259   if (resp_string || (resp_sized && resp_empty))
    260   {
    261     /* There is no chance to add footer later */
    262     if (MHD_YES != MHD_add_response_footer (response,
    263                                             RESP_FOOTER_NAME,
    264                                             RESP_FOOTER_VALUE))
    265       abort ();
    266   }
    267 
    268   ret = MHD_queue_response (connection,
    269                             MHD_HTTP_OK,
    270                             response);
    271   MHD_destroy_response (response);
    272   return ret;
    273 }
    274 
    275 
    276 static unsigned int
    277 validate (struct CBC cbc, unsigned int ebase)
    278 {
    279   int i;
    280   char buf[RESP_BLOCK_SIZE];
    281 
    282   if (resp_empty)
    283   {
    284     if (0 != cbc.pos)
    285     {
    286       fprintf (stderr,
    287                "Got %u bytes instead of zero!\n",
    288                (unsigned int) cbc.pos);
    289       return 1;
    290     }
    291     return 0;
    292   }
    293 
    294   if (cbc.pos != RESP_SIZE)
    295   {
    296     fprintf (stderr,
    297              "Got %u bytes instead of 1280!\n",
    298              (unsigned int) cbc.pos);
    299     return ebase;
    300   }
    301 
    302   for (i = 0; i < RESP_BLOCK_QUANTIY; i++)
    303   {
    304     memset (buf, 'A' + i, RESP_BLOCK_SIZE);
    305     if (0 != memcmp (buf, &cbc.buf[i * RESP_BLOCK_SIZE], RESP_BLOCK_SIZE))
    306     {
    307       fprintf (stderr,
    308                "Got  `%.*s'\nWant `%.*s'\n",
    309                RESP_BLOCK_SIZE, &cbc.buf[i * RESP_BLOCK_SIZE],
    310                RESP_BLOCK_SIZE, buf);
    311       return ebase * 2;
    312     }
    313   }
    314   return 0;
    315 }
    316 
    317 
    318 static unsigned int
    319 testInternalGet (void)
    320 {
    321   struct MHD_Daemon *d;
    322   CURL *c;
    323   char buf[2048];
    324   struct CBC cbc;
    325   CURLcode errornum;
    326   uint16_t port;
    327   struct curl_slist *h_list = NULL;
    328   struct headers_check_result hdr_check;
    329 
    330   port = port_global;
    331   cbc.buf = buf;
    332   cbc.size = 2048;
    333   cbc.pos = 0;
    334   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    335                         port, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
    336   if (d == NULL)
    337     return 1;
    338   if (0 == port)
    339   {
    340     const union MHD_DaemonInfo *dinfo;
    341     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    342     if ((NULL == dinfo) || (0 == dinfo->port) )
    343     {
    344       MHD_stop_daemon (d); return 32;
    345     }
    346     port = dinfo->port;
    347     if (0 == port_global)
    348       port_global = port; /* Re-use the same port for all checks */
    349   }
    350   hdr_check.found_chunked = 0;
    351   hdr_check.found_footer = 0;
    352   c = curl_easy_init ();
    353   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    354   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    355   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    356   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    357   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    358   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    359   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    360   curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    361   curl_easy_setopt (c, CURLOPT_HEADERFUNCTION, lcurl_hdr_callback);
    362   curl_easy_setopt (c, CURLOPT_HEADERDATA, &hdr_check);
    363   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    364   curl_easy_setopt (c, CURLOPT_PROXY, "");
    365   if (conn_close)
    366   {
    367     h_list = curl_slist_append (h_list, "Connection: close");
    368     if (NULL == h_list)
    369       abort ();
    370     curl_easy_setopt (c, CURLOPT_HTTPHEADER, h_list);
    371   }
    372   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    373   {
    374     fprintf (stderr,
    375              "curl_easy_perform failed: `%s'\n",
    376              curl_easy_strerror (errornum));
    377     curl_easy_cleanup (c);
    378     curl_slist_free_all (h_list);
    379     MHD_stop_daemon (d);
    380     return 2;
    381   }
    382   curl_easy_cleanup (c);
    383   curl_slist_free_all (h_list);
    384   MHD_stop_daemon (d);
    385   if (1 != hdr_check.found_chunked)
    386   {
    387     fprintf (stderr,
    388              "Chunked encoding header was not found in the response\n");
    389     return 8;
    390   }
    391   if (1 != hdr_check.found_footer)
    392   {
    393     fprintf (stderr,
    394              "The specified footer was not found in the response\n");
    395     return 16;
    396   }
    397   return validate (cbc, 4);
    398 }
    399 
    400 
    401 static unsigned int
    402 testMultithreadedGet (void)
    403 {
    404   struct MHD_Daemon *d;
    405   CURL *c;
    406   char buf[2048];
    407   struct CBC cbc;
    408   CURLcode errornum;
    409   uint16_t port;
    410   struct curl_slist *h_list = NULL;
    411   struct headers_check_result hdr_check;
    412 
    413   port = port_global;
    414   cbc.buf = buf;
    415   cbc.size = 2048;
    416   cbc.pos = 0;
    417   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    418                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    419                         port, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
    420   if (d == NULL)
    421     return 16;
    422   if (0 == port)
    423   {
    424     const union MHD_DaemonInfo *dinfo;
    425     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    426     if ((NULL == dinfo) || (0 == dinfo->port) )
    427     {
    428       MHD_stop_daemon (d); return 32;
    429     }
    430     port = dinfo->port;
    431     if (0 == port_global)
    432       port_global = port; /* Re-use the same port for all checks */
    433   }
    434   c = curl_easy_init ();
    435   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    436   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    437   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    438   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    439   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    440   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    441   curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    442   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    443   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    444   curl_easy_setopt (c, CURLOPT_PROXY, "");
    445   hdr_check.found_chunked = 0;
    446   hdr_check.found_footer = 0;
    447   curl_easy_setopt (c, CURLOPT_HEADERFUNCTION, lcurl_hdr_callback);
    448   curl_easy_setopt (c, CURLOPT_HEADERDATA, &hdr_check);
    449   if (conn_close)
    450   {
    451     h_list = curl_slist_append (h_list, "Connection: close");
    452     if (NULL == h_list)
    453       abort ();
    454     curl_easy_setopt (c, CURLOPT_HTTPHEADER, h_list);
    455   }
    456   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    457   {
    458     fprintf (stderr,
    459              "curl_easy_perform failed: `%s'\n",
    460              curl_easy_strerror (errornum));
    461     curl_easy_cleanup (c);
    462     curl_slist_free_all (h_list);
    463     MHD_stop_daemon (d);
    464     return 32;
    465   }
    466   curl_easy_cleanup (c);
    467   curl_slist_free_all (h_list);
    468   MHD_stop_daemon (d);
    469   if (1 != hdr_check.found_chunked)
    470   {
    471     fprintf (stderr,
    472              "Chunked encoding header was not found in the response\n");
    473     return 8;
    474   }
    475   if (1 != hdr_check.found_footer)
    476   {
    477     fprintf (stderr,
    478              "The specified footer was not found in the response\n");
    479     return 16;
    480   }
    481   return validate (cbc, 64);
    482 }
    483 
    484 
    485 static unsigned int
    486 testMultithreadedPoolGet (void)
    487 {
    488   struct MHD_Daemon *d;
    489   CURL *c;
    490   char buf[2048];
    491   struct CBC cbc;
    492   CURLcode errornum;
    493   uint16_t port;
    494   struct curl_slist *h_list = NULL;
    495   struct headers_check_result hdr_check;
    496 
    497   port = port_global;
    498   cbc.buf = buf;
    499   cbc.size = 2048;
    500   cbc.pos = 0;
    501   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    502                         port, NULL, NULL, &ahc_echo, NULL,
    503                         MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT,
    504                         MHD_OPTION_END);
    505   if (d == NULL)
    506     return 16;
    507   if (0 == port)
    508   {
    509     const union MHD_DaemonInfo *dinfo;
    510     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    511     if ((NULL == dinfo) || (0 == dinfo->port) )
    512     {
    513       MHD_stop_daemon (d); return 32;
    514     }
    515     port = dinfo->port;
    516     if (0 == port_global)
    517       port_global = port; /* Re-use the same port for all checks */
    518   }
    519   c = curl_easy_init ();
    520   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    521   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    522   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    523   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    524   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    525   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    526   curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    527   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    528   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    529   curl_easy_setopt (c, CURLOPT_PROXY, "");
    530   hdr_check.found_chunked = 0;
    531   hdr_check.found_footer = 0;
    532   curl_easy_setopt (c, CURLOPT_HEADERFUNCTION, lcurl_hdr_callback);
    533   curl_easy_setopt (c, CURLOPT_HEADERDATA, &hdr_check);
    534   if (conn_close)
    535   {
    536     h_list = curl_slist_append (h_list, "Connection: close");
    537     if (NULL == h_list)
    538       abort ();
    539     curl_easy_setopt (c, CURLOPT_HTTPHEADER, h_list);
    540   }
    541   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    542   {
    543     fprintf (stderr,
    544              "curl_easy_perform failed: `%s'\n",
    545              curl_easy_strerror (errornum));
    546     curl_easy_cleanup (c);
    547     curl_slist_free_all (h_list);
    548     MHD_stop_daemon (d);
    549     return 32;
    550   }
    551   curl_easy_cleanup (c);
    552   curl_slist_free_all (h_list);
    553   MHD_stop_daemon (d);
    554   if (1 != hdr_check.found_chunked)
    555   {
    556     fprintf (stderr,
    557              "Chunked encoding header was not found in the response\n");
    558     return 8;
    559   }
    560   if (1 != hdr_check.found_footer)
    561   {
    562     fprintf (stderr,
    563              "The specified footer was not found in the response\n");
    564     return 16;
    565   }
    566   return validate (cbc, 64);
    567 }
    568 
    569 
    570 static unsigned int
    571 testExternalGet (void)
    572 {
    573   struct MHD_Daemon *d;
    574   CURL *c;
    575   char buf[2048];
    576   struct CBC cbc;
    577   CURLM *multi;
    578   CURLMcode mret;
    579   fd_set rs;
    580   fd_set ws;
    581   fd_set es;
    582   MHD_socket maxsock;
    583 #ifdef MHD_WINSOCK_SOCKETS
    584   int maxposixs; /* Max socket number unused on W32 */
    585 #else  /* MHD_POSIX_SOCKETS */
    586 #define maxposixs maxsock
    587 #endif /* MHD_POSIX_SOCKETS */
    588   int running;
    589   struct CURLMsg *msg;
    590   time_t start;
    591   struct timeval tv;
    592   uint16_t port;
    593   struct curl_slist *h_list = NULL;
    594   struct headers_check_result hdr_check;
    595 
    596   port = port_global;
    597   multi = NULL;
    598   cbc.buf = buf;
    599   cbc.size = 2048;
    600   cbc.pos = 0;
    601   d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY,
    602                         port, NULL, NULL, &ahc_echo, NULL,
    603                         MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE,
    604                         MHD_OPTION_END);
    605   if (d == NULL)
    606     return 256;
    607   if (0 == port)
    608   {
    609     const union MHD_DaemonInfo *dinfo;
    610     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    611     if ((NULL == dinfo) || (0 == dinfo->port) )
    612     {
    613       MHD_stop_daemon (d); return 32;
    614     }
    615     port = dinfo->port;
    616     if (0 == port_global)
    617       port_global = port; /* Re-use the same port for all checks */
    618   }
    619   c = curl_easy_init ();
    620   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    621   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    622   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    623   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    624   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    625   curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    626   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    627   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L);
    628   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    629   curl_easy_setopt (c, CURLOPT_PROXY, "");
    630   hdr_check.found_chunked = 0;
    631   hdr_check.found_footer = 0;
    632   curl_easy_setopt (c, CURLOPT_HEADERFUNCTION, lcurl_hdr_callback);
    633   curl_easy_setopt (c, CURLOPT_HEADERDATA, &hdr_check);
    634   if (conn_close)
    635   {
    636     h_list = curl_slist_append (h_list, "Connection: close");
    637     if (NULL == h_list)
    638       abort ();
    639     curl_easy_setopt (c, CURLOPT_HTTPHEADER, h_list);
    640   }
    641 
    642   multi = curl_multi_init ();
    643   if (multi == NULL)
    644   {
    645     curl_easy_cleanup (c);
    646     curl_slist_free_all (h_list);
    647     MHD_stop_daemon (d);
    648     return 512;
    649   }
    650   mret = curl_multi_add_handle (multi, c);
    651   if (mret != CURLM_OK)
    652   {
    653     curl_multi_cleanup (multi);
    654     curl_easy_cleanup (c);
    655     curl_slist_free_all (h_list);
    656     MHD_stop_daemon (d);
    657     return 1024;
    658   }
    659   start = time (NULL);
    660   while ((time (NULL) - start < 5) && (multi != NULL))
    661   {
    662     maxsock = MHD_INVALID_SOCKET;
    663     maxposixs = -1;
    664     FD_ZERO (&rs);
    665     FD_ZERO (&ws);
    666     FD_ZERO (&es);
    667     curl_multi_perform (multi, &running);
    668     mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
    669     if (mret != CURLM_OK)
    670     {
    671       curl_multi_remove_handle (multi, c);
    672       curl_multi_cleanup (multi);
    673       curl_easy_cleanup (c);
    674       curl_slist_free_all (h_list);
    675       MHD_stop_daemon (d);
    676       return 2048;
    677     }
    678     if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock))
    679     {
    680       curl_multi_remove_handle (multi, c);
    681       curl_multi_cleanup (multi);
    682       curl_easy_cleanup (c);
    683       curl_slist_free_all (h_list);
    684       MHD_stop_daemon (d);
    685       return 4096;
    686     }
    687     tv.tv_sec = 0;
    688     tv.tv_usec = 1000;
    689     if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
    690     {
    691 #ifdef MHD_POSIX_SOCKETS
    692       if (EINTR != errno)
    693       {
    694         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    695                  (int) errno, __LINE__);
    696         fflush (stderr);
    697         exit (99);
    698       }
    699 #else
    700       if ((WSAEINVAL != WSAGetLastError ()) ||
    701           (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
    702       {
    703         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    704                  (int) WSAGetLastError (), __LINE__);
    705         fflush (stderr);
    706         exit (99);
    707       }
    708       Sleep (1);
    709 #endif
    710     }
    711     curl_multi_perform (multi, &running);
    712     if (0 == running)
    713     {
    714       int pending;
    715       int curl_fine = 0;
    716       while (NULL != (msg = curl_multi_info_read (multi, &pending)))
    717       {
    718         if (msg->msg == CURLMSG_DONE)
    719         {
    720           if (msg->data.result == CURLE_OK)
    721             curl_fine = 1;
    722           else
    723           {
    724             fprintf (stderr,
    725                      "%s failed at %s:%d: `%s'\n",
    726                      "curl_multi_perform",
    727                      __FILE__,
    728                      __LINE__, curl_easy_strerror (msg->data.result));
    729             abort ();
    730           }
    731         }
    732       }
    733       if (! curl_fine)
    734       {
    735         fprintf (stderr, "libcurl haven't returned OK code\n");
    736         abort ();
    737       }
    738       curl_multi_remove_handle (multi, c);
    739       curl_multi_cleanup (multi);
    740       curl_easy_cleanup (c);
    741       curl_slist_free_all (h_list);
    742       h_list = NULL;
    743       c = NULL;
    744       multi = NULL;
    745     }
    746     MHD_run (d);
    747   }
    748   MHD_stop_daemon (d);
    749   if (multi != NULL)
    750   {
    751     curl_multi_remove_handle (multi, c);
    752     curl_easy_cleanup (c);
    753     curl_multi_cleanup (multi);
    754   }
    755   curl_slist_free_all (h_list);
    756   if (1 != hdr_check.found_chunked)
    757   {
    758     fprintf (stderr,
    759              "Chunked encoding header was not found in the response\n");
    760     return 8;
    761   }
    762   if (1 != hdr_check.found_footer)
    763   {
    764     fprintf (stderr,
    765              "The specified footer was not found in the response\n");
    766     return 16;
    767   }
    768   return validate (cbc, 8192);
    769 }
    770 
    771 
    772 int
    773 main (int argc, char *const *argv)
    774 {
    775   unsigned int errorCount = 0;
    776   (void) argc; (void) argv; /* Unused. Silent compiler warning. */
    777 
    778   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
    779     return 2;
    780   conn_close = has_in_name (argv[0], "_close");
    781   resp_string = has_in_name (argv[0], "_string");
    782   resp_sized = has_in_name (argv[0], "_sized");
    783   resp_empty = has_in_name (argv[0], "_empty");
    784   chunked_forced = has_in_name (argv[0], "_forced");
    785   if (resp_string)
    786     resp_sized = ! 0;
    787   if (resp_sized)
    788     chunked_forced = ! 0;
    789 
    790   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    791     port_global = 0;
    792   else
    793   {
    794     port_global = 4100;
    795     if (conn_close)
    796       port_global += 1 << 0;
    797     if (resp_string)
    798       port_global += 1 << 1;
    799     if (resp_sized)
    800       port_global += 1 << 2;
    801     if (resp_empty)
    802       port_global += 1 << 3;
    803     if (chunked_forced)
    804       port_global += 1 << 4;
    805   }
    806 
    807   if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS))
    808   {
    809     errorCount += testInternalGet ();
    810     errorCount += testMultithreadedGet ();
    811     errorCount += testMultithreadedPoolGet ();
    812   }
    813   errorCount += testExternalGet ();
    814   if (errorCount != 0)
    815     fprintf (stderr, "Error (code: %u)\n", errorCount);
    816   curl_global_cleanup ();
    817   return (0 == errorCount) ? 0 : 1;       /* 0 == pass */
    818 }