libmicrohttpd

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

test_large_put.c (28488B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007, 2008 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_large_put.c
     24  * @brief  Testcase for libmicrohttpd PUT operations
     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 #include "mhd_has_param.h"
     44 
     45 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     46    test into a marked, classifiable test error (TESTING.md, P5). */
     47 #include "mhd_panic_tripwire.h"
     48 
     49 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2
     50 #undef MHD_CPU_COUNT
     51 #endif
     52 #if ! defined(MHD_CPU_COUNT)
     53 #define MHD_CPU_COUNT 2
     54 #endif
     55 
     56 
     57 #if defined(HAVE___FUNC__)
     58 #define externalErrorExit(ignore) \
     59     _externalErrorExit_func(NULL, __func__, __LINE__)
     60 #define externalErrorExitDesc(errDesc) \
     61     _externalErrorExit_func(errDesc, __func__, __LINE__)
     62 #define libcurlErrorExit(ignore) \
     63     _libcurlErrorExit_func(NULL, __func__, __LINE__)
     64 #define libcurlErrorExitDesc(errDesc) \
     65     _libcurlErrorExit_func(errDesc, __func__, __LINE__)
     66 #define mhdErrorExit(ignore) \
     67     _mhdErrorExit_func(NULL, __func__, __LINE__)
     68 #define mhdErrorExitDesc(errDesc) \
     69     _mhdErrorExit_func(errDesc, __func__, __LINE__)
     70 #elif defined(HAVE___FUNCTION__)
     71 #define externalErrorExit(ignore) \
     72     _externalErrorExit_func(NULL, __FUNCTION__, __LINE__)
     73 #define externalErrorExitDesc(errDesc) \
     74     _externalErrorExit_func(errDesc, __FUNCTION__, __LINE__)
     75 #define libcurlErrorExit(ignore) \
     76     _libcurlErrorExit_func(NULL, __FUNCTION__, __LINE__)
     77 #define libcurlErrorExitDesc(errDesc) \
     78     _libcurlErrorExit_func(errDesc, __FUNCTION__, __LINE__)
     79 #define mhdErrorExit(ignore) \
     80     _mhdErrorExit_func(NULL, __FUNCTION__, __LINE__)
     81 #define mhdErrorExitDesc(errDesc) \
     82     _mhdErrorExit_func(errDesc, __FUNCTION__, __LINE__)
     83 #else
     84 #define externalErrorExit(ignore) _externalErrorExit_func(NULL, NULL, __LINE__)
     85 #define externalErrorExitDesc(errDesc) \
     86   _externalErrorExit_func(errDesc, NULL, __LINE__)
     87 #define libcurlErrorExit(ignore) _libcurlErrorExit_func(NULL, NULL, __LINE__)
     88 #define libcurlErrorExitDesc(errDesc) \
     89   _libcurlErrorExit_func(errDesc, NULL, __LINE__)
     90 #define mhdErrorExit(ignore) _mhdErrorExit_func(NULL, NULL, __LINE__)
     91 #define mhdErrorExitDesc(errDesc) _mhdErrorExit_func(errDesc, NULL, __LINE__)
     92 #endif
     93 
     94 
     95 _MHD_NORETURN static void
     96 _externalErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
     97 {
     98   if ((NULL != errDesc) && (0 != errDesc[0]))
     99     fprintf (stderr, "%s", errDesc);
    100   else
    101     fprintf (stderr, "System or external library call failed");
    102   if ((NULL != funcName) && (0 != funcName[0]))
    103     fprintf (stderr, " in %s", funcName);
    104   if (0 < lineNum)
    105     fprintf (stderr, " at line %d", lineNum);
    106 
    107   fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
    108            strerror (errno));
    109 #ifdef MHD_WINSOCK_SOCKETS
    110   fprintf (stderr, "WSAGetLastError() value: %d\n", (int) WSAGetLastError ());
    111 #endif /* MHD_WINSOCK_SOCKETS */
    112   fflush (stderr);
    113   exit (99);
    114 }
    115 
    116 
    117 static char libcurl_errbuf[CURL_ERROR_SIZE] = "";
    118 
    119 _MHD_NORETURN static void
    120 _libcurlErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
    121 {
    122   if ((NULL != errDesc) && (0 != errDesc[0]))
    123     fprintf (stderr, "%s", errDesc);
    124   else
    125     fprintf (stderr, "CURL library call failed");
    126   if ((NULL != funcName) && (0 != funcName[0]))
    127     fprintf (stderr, " in %s", funcName);
    128   if (0 < lineNum)
    129     fprintf (stderr, " at line %d", lineNum);
    130 
    131   fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
    132            strerror (errno));
    133   if (0 != libcurl_errbuf[0])
    134     fprintf (stderr, "Last libcurl error details: %s\n", libcurl_errbuf);
    135 
    136   fflush (stderr);
    137   exit (99);
    138 }
    139 
    140 
    141 _MHD_NORETURN static void
    142 _mhdErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
    143 {
    144   if ((NULL != errDesc) && (0 != errDesc[0]))
    145     fprintf (stderr, "%s", errDesc);
    146   else
    147     fprintf (stderr, "MHD unexpected error");
    148   if ((NULL != funcName) && (0 != funcName[0]))
    149     fprintf (stderr, " in %s", funcName);
    150   if (0 < lineNum)
    151     fprintf (stderr, " at line %d", lineNum);
    152 
    153   fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
    154            strerror (errno));
    155 
    156   fflush (stderr);
    157   exit (8);
    158 }
    159 
    160 
    161 static int oneone;
    162 static int incr_read; /* Use incremental read */
    163 static int verbose; /* Be verbose */
    164 
    165 #define PUT_SIZE (256 * 1024)
    166 
    167 static char *put_buffer;
    168 
    169 struct CBC
    170 {
    171   char *buf;
    172   size_t pos;
    173   size_t size;
    174 };
    175 
    176 static char *
    177 alloc_init (size_t buf_size)
    178 {
    179   static const char template[] =
    180     "ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz";
    181   static const size_t templ_size = sizeof(template) / sizeof(char) - 1;
    182   char *buf;
    183   char *fill_ptr;
    184   size_t to_fill;
    185 
    186   buf = malloc (buf_size);
    187   if (NULL == buf)
    188     externalErrorExit ();
    189 
    190   fill_ptr = buf;
    191   to_fill = buf_size;
    192   while (to_fill > 0)
    193   {
    194     const size_t to_copy = to_fill > templ_size ? templ_size : to_fill;
    195     memcpy (fill_ptr, template, to_copy);
    196     fill_ptr += to_copy;
    197     to_fill -= to_copy;
    198   }
    199   return buf;
    200 }
    201 
    202 
    203 static size_t
    204 putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
    205 {
    206   size_t *pos = (size_t *) ptr;
    207   size_t wrt;
    208 
    209   wrt = size * nmemb;
    210   /* Check for overflow. */
    211   if (wrt / size != nmemb)
    212     libcurlErrorExitDesc ("Too large buffer size");
    213   if (wrt > PUT_SIZE - (*pos))
    214     wrt = PUT_SIZE - (*pos);
    215   memcpy (stream, &put_buffer[*pos], wrt);
    216   (*pos) += wrt;
    217   return wrt;
    218 }
    219 
    220 
    221 static size_t
    222 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
    223 {
    224   struct CBC *cbc = ctx;
    225 
    226   if (cbc->pos + size * nmemb > cbc->size)
    227     libcurlErrorExitDesc ("Too large buffer size");
    228   memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
    229   cbc->pos += size * nmemb;
    230   return size * nmemb;
    231 }
    232 
    233 
    234 static enum MHD_Result
    235 ahc_echo (void *cls,
    236           struct MHD_Connection *connection,
    237           const char *url,
    238           const char *method,
    239           const char *version,
    240           const char *upload_data, size_t *upload_data_size,
    241           void **req_cls)
    242 {
    243   int *done = cls;
    244   struct MHD_Response *response;
    245   enum MHD_Result ret;
    246   static size_t processed;
    247 
    248   if (NULL == cls)
    249     mhdErrorExitDesc ("cls parameter is NULL");
    250 
    251   if (0 != strcmp (version, oneone ?
    252                    MHD_HTTP_VERSION_1_1 : MHD_HTTP_VERSION_1_0))
    253     mhdErrorExitDesc ("Unexpected HTTP version");
    254 
    255   if (NULL == url)
    256     mhdErrorExitDesc ("url parameter is NULL");
    257 
    258   if (NULL == upload_data_size)
    259     mhdErrorExitDesc ("'upload_data_size' pointer is NULL");
    260 
    261   if (0 != strcmp ("PUT", method))
    262     mhdErrorExitDesc ("Unexpected request method");   /* unexpected method */
    263 
    264   if ((*done) == 0)
    265   {
    266     size_t *pproc;
    267     if (NULL == *req_cls)
    268     {
    269       processed = 0;
    270       /* Safe as long as only one parallel request served. */
    271       *req_cls = &processed;
    272     }
    273     pproc = (size_t *) *req_cls;
    274 
    275     if (0 == *upload_data_size)
    276       return MHD_YES;   /* No data to process. */
    277 
    278     if (*pproc + *upload_data_size > PUT_SIZE)
    279       mhdErrorExitDesc ("Incoming data larger than expected");
    280 
    281     if ( (! incr_read) && (*upload_data_size != PUT_SIZE) )
    282       return MHD_YES;   /* Wait until whole request is received. */
    283 
    284     if (0 != memcmp (upload_data, put_buffer + (*pproc), *upload_data_size))
    285       mhdErrorExitDesc ("Incoming data does not match sent data");
    286 
    287     *pproc += *upload_data_size;
    288     *upload_data_size = 0;   /* Current block of data is fully processed. */
    289 
    290     if (PUT_SIZE == *pproc)
    291       *done = 1;   /* Whole request is processed. */
    292     return MHD_YES;
    293   }
    294   response =
    295     MHD_create_response_from_buffer_copy (strlen (url),
    296                                           (const void *) url);
    297   if (NULL == response)
    298     mhdErrorExitDesc ("Failed to create response");
    299   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
    300   MHD_destroy_response (response);
    301   return ret;
    302 }
    303 
    304 
    305 static unsigned int
    306 testPutInternalThread (unsigned int add_flag)
    307 {
    308   struct MHD_Daemon *d;
    309   CURL *c;
    310   struct CBC cbc;
    311   size_t pos = 0;
    312   int done_flag = 0;
    313   CURLcode errornum;
    314   char buf[2048];
    315   uint16_t port;
    316 
    317   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    318     port = 0;
    319   else
    320   {
    321     port = 1270;
    322     if (oneone)
    323       port += 10;
    324     if (incr_read)
    325       port += 20;
    326   }
    327 
    328   cbc.buf = buf;
    329   cbc.size = 2048;
    330   cbc.pos = 0;
    331   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    332                         | add_flag,
    333                         port,
    334                         NULL, NULL, &ahc_echo, &done_flag,
    335                         MHD_OPTION_CONNECTION_MEMORY_LIMIT,
    336                         (size_t) (incr_read ? 1024 : (PUT_SIZE * 4 / 3)),
    337                         MHD_OPTION_END);
    338   if (d == NULL)
    339     mhdErrorExit ();
    340   if (0 == port)
    341   {
    342     const union MHD_DaemonInfo *dinfo;
    343     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    344     if ((NULL == dinfo) || (0 == dinfo->port) )
    345       mhdErrorExit ();
    346     port = dinfo->port;
    347   }
    348 
    349   c = curl_easy_init ();
    350   if (NULL == c)
    351   {
    352     fprintf (stderr, "curl_easy_init() failed.\n");
    353     externalErrorExit ();
    354   }
    355   if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L)) ||
    356       (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROXY, "")) ||
    357       (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL,
    358                                      "http://127.0.0.1/hello_world")) ||
    359       (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, (long) port)) ||
    360       (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEFUNCTION,
    361                                      &copyBuffer)) ||
    362       (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc)) ||
    363       (CURLE_OK != curl_easy_setopt (c, CURLOPT_READFUNCTION,
    364                                      &putBuffer)) ||
    365       (CURLE_OK != curl_easy_setopt (c, CURLOPT_READDATA, &pos)) ||
    366       (CURLE_OK != curl_easy_setopt (c, CURLOPT_UPLOAD, 1L)) ||
    367       (CURLE_OK != curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE)) ||
    368       (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) ||
    369       (CURLE_OK != curl_easy_setopt (c, CURLOPT_ERRORBUFFER, libcurl_errbuf)) ||
    370       (CURLE_OK != curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT,
    371                                      (long) 150)) ||
    372       (CURLE_OK != curl_easy_setopt (c, CURLOPT_TIMEOUT,
    373                                      (long) 150)) ||
    374       (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
    375                                      (oneone) ?
    376                                      CURL_HTTP_VERSION_1_1 :
    377                                      CURL_HTTP_VERSION_1_0)))
    378 
    379   {
    380     fprintf (stderr, "curl_easy_setopt() failed.\n");
    381     externalErrorExit ();
    382   }
    383 
    384   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    385   {
    386     fprintf (stderr,
    387              "curl_easy_perform failed: `%s'\n",
    388              curl_easy_strerror (errornum));
    389     curl_easy_cleanup (c);
    390     MHD_stop_daemon (d);
    391     return 2;
    392   }
    393   curl_easy_cleanup (c);
    394   MHD_stop_daemon (d);
    395   if (cbc.pos != strlen ("/hello_world"))
    396   {
    397     fprintf (stderr, "Got %u bytes ('%.*s'), expected %u bytes. ",
    398              (unsigned) cbc.pos, (int) cbc.pos, cbc.buf,
    399              (unsigned) strlen ("/hello_world"));
    400     mhdErrorExitDesc ("Wrong returned data length");
    401   }
    402   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    403   {
    404     fprintf (stderr, "Got invalid response '%.*s'. ", (int) cbc.pos, cbc.buf);
    405     mhdErrorExitDesc ("Wrong returned data length");
    406   }
    407   return 0;
    408 }
    409 
    410 
    411 static unsigned int
    412 testPutThreadPerConn (unsigned int add_flag)
    413 {
    414   struct MHD_Daemon *d;
    415   CURL *c;
    416   struct CBC cbc;
    417   size_t pos = 0;
    418   int done_flag = 0;
    419   CURLcode errornum;
    420   char buf[2048];
    421   uint16_t port;
    422 
    423   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    424     port = 0;
    425   else
    426   {
    427     port = 1271;
    428     if (oneone)
    429       port += 10;
    430     if (incr_read)
    431       port += 20;
    432   }
    433 
    434   cbc.buf = buf;
    435   cbc.size = 2048;
    436   cbc.pos = 0;
    437   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    438                         | MHD_USE_INTERNAL_POLLING_THREAD
    439                         | MHD_USE_ERROR_LOG | add_flag,
    440                         port,
    441                         NULL, NULL, &ahc_echo, &done_flag,
    442                         MHD_OPTION_CONNECTION_MEMORY_LIMIT,
    443                         (size_t) (incr_read ? 1024 : (PUT_SIZE * 4)),
    444                         MHD_OPTION_END);
    445   if (d == NULL)
    446     mhdErrorExit ();
    447   if (0 == port)
    448   {
    449     const union MHD_DaemonInfo *dinfo;
    450     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    451     if ((NULL == dinfo) || (0 == dinfo->port) )
    452       mhdErrorExit ();
    453     port = dinfo->port;
    454   }
    455 
    456   c = curl_easy_init ();
    457   if (NULL == c)
    458   {
    459     fprintf (stderr, "curl_easy_init() failed.\n");
    460     externalErrorExit ();
    461   }
    462   if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L)) ||
    463       (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROXY, "")) ||
    464       (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL,
    465                                      "http://127.0.0.1/hello_world")) ||
    466       (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, (long) port)) ||
    467       (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEFUNCTION,
    468                                      &copyBuffer)) ||
    469       (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc)) ||
    470       (CURLE_OK != curl_easy_setopt (c, CURLOPT_READFUNCTION,
    471                                      &putBuffer)) ||
    472       (CURLE_OK != curl_easy_setopt (c, CURLOPT_READDATA, &pos)) ||
    473       (CURLE_OK != curl_easy_setopt (c, CURLOPT_UPLOAD, 1L)) ||
    474       (CURLE_OK != curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE)) ||
    475       (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) ||
    476       (CURLE_OK != curl_easy_setopt (c, CURLOPT_ERRORBUFFER, libcurl_errbuf)) ||
    477       (CURLE_OK != curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT,
    478                                      (long) 150)) ||
    479       (CURLE_OK != curl_easy_setopt (c, CURLOPT_TIMEOUT,
    480                                      (long) 150)) ||
    481       (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
    482                                      (oneone) ?
    483                                      CURL_HTTP_VERSION_1_1 :
    484                                      CURL_HTTP_VERSION_1_0)))
    485   {
    486     fprintf (stderr, "curl_easy_setopt() failed.\n");
    487     externalErrorExit ();
    488   }
    489 
    490   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    491   {
    492     fprintf (stderr,
    493              "curl_easy_perform failed: `%s'\n",
    494              curl_easy_strerror (errornum));
    495     curl_easy_cleanup (c);
    496     MHD_stop_daemon (d);
    497     return 32;
    498   }
    499   curl_easy_cleanup (c);
    500   MHD_stop_daemon (d);
    501   if (cbc.pos != strlen ("/hello_world"))
    502   {
    503     fprintf (stderr, "Got %u bytes ('%.*s'), expected %u bytes. ",
    504              (unsigned) cbc.pos, (int) cbc.pos, cbc.buf,
    505              (unsigned) strlen ("/hello_world"));
    506     mhdErrorExitDesc ("Wrong returned data length");
    507   }
    508   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    509   {
    510     fprintf (stderr, "Got invalid response '%.*s'. ", (int) cbc.pos, cbc.buf);
    511     mhdErrorExitDesc ("Wrong returned data length");
    512   }
    513   return 0;
    514 }
    515 
    516 
    517 static unsigned int
    518 testPutThreadPool (unsigned int add_flag)
    519 {
    520   struct MHD_Daemon *d;
    521   CURL *c;
    522   struct CBC cbc;
    523   size_t pos = 0;
    524   int done_flag = 0;
    525   CURLcode errornum;
    526   char buf[2048];
    527   uint16_t port;
    528 
    529   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    530     port = 0;
    531   else
    532   {
    533     port = 1272;
    534     if (oneone)
    535       port += 10;
    536     if (incr_read)
    537       port += 20;
    538   }
    539 
    540   cbc.buf = buf;
    541   cbc.size = 2048;
    542   cbc.pos = 0;
    543   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    544                         | add_flag,
    545                         port,
    546                         NULL, NULL, &ahc_echo, &done_flag,
    547                         MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT,
    548                         MHD_OPTION_CONNECTION_MEMORY_LIMIT,
    549                         (size_t) (incr_read ? 1024 : (PUT_SIZE * 4)),
    550                         MHD_OPTION_END);
    551   if (d == NULL)
    552     mhdErrorExit ();
    553   if (0 == port)
    554   {
    555     const union MHD_DaemonInfo *dinfo;
    556     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    557     if ((NULL == dinfo) || (0 == dinfo->port) )
    558       mhdErrorExit ();
    559     port = dinfo->port;
    560   }
    561 
    562   c = curl_easy_init ();
    563   if (NULL == c)
    564   {
    565     fprintf (stderr, "curl_easy_init() failed.\n");
    566     externalErrorExit ();
    567   }
    568   if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L)) ||
    569       (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROXY, "")) ||
    570       (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL,
    571                                      "http://127.0.0.1/hello_world")) ||
    572       (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, (long) port)) ||
    573       (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEFUNCTION,
    574                                      &copyBuffer)) ||
    575       (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc)) ||
    576       (CURLE_OK != curl_easy_setopt (c, CURLOPT_READFUNCTION,
    577                                      &putBuffer)) ||
    578       (CURLE_OK != curl_easy_setopt (c, CURLOPT_READDATA, &pos)) ||
    579       (CURLE_OK != curl_easy_setopt (c, CURLOPT_UPLOAD, 1L)) ||
    580       (CURLE_OK != curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE)) ||
    581       (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) ||
    582       (CURLE_OK != curl_easy_setopt (c, CURLOPT_ERRORBUFFER, libcurl_errbuf)) ||
    583       (CURLE_OK != curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT,
    584                                      (long) 150)) ||
    585       (CURLE_OK != curl_easy_setopt (c, CURLOPT_TIMEOUT,
    586                                      (long) 150)) ||
    587       (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
    588                                      (oneone) ?
    589                                      CURL_HTTP_VERSION_1_1 :
    590                                      CURL_HTTP_VERSION_1_0)))
    591   {
    592     fprintf (stderr, "curl_easy_setopt() failed.\n");
    593     externalErrorExit ();
    594   }
    595   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    596   {
    597     fprintf (stderr,
    598              "curl_easy_perform failed: `%s'\n",
    599              curl_easy_strerror (errornum));
    600     curl_easy_cleanup (c);
    601     MHD_stop_daemon (d);
    602     return 32;
    603   }
    604   curl_easy_cleanup (c);
    605   MHD_stop_daemon (d);
    606   if (cbc.pos != strlen ("/hello_world"))
    607   {
    608     fprintf (stderr, "Got %u bytes ('%.*s'), expected %u bytes. ",
    609              (unsigned) cbc.pos, (int) cbc.pos, cbc.buf,
    610              (unsigned) strlen ("/hello_world"));
    611     mhdErrorExitDesc ("Wrong returned data length");
    612   }
    613   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    614   {
    615     fprintf (stderr, "Got invalid response '%.*s'. ", (int) cbc.pos, cbc.buf);
    616     mhdErrorExitDesc ("Wrong returned data length");
    617   }
    618   return 0;
    619 }
    620 
    621 
    622 static unsigned int
    623 testPutExternal (void)
    624 {
    625   struct MHD_Daemon *d;
    626   CURL *c;
    627   struct CBC cbc;
    628   CURLM *multi;
    629   CURLMcode mret;
    630   fd_set rs;
    631   fd_set ws;
    632   fd_set es;
    633   int running;
    634   struct CURLMsg *msg;
    635   time_t start;
    636   struct timeval tv;
    637   size_t pos = 0;
    638   int done_flag = 0;
    639   char buf[2048];
    640   uint16_t port;
    641 
    642   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    643     port = 0;
    644   else
    645   {
    646     port = 1273;
    647     if (oneone)
    648       port += 10;
    649     if (incr_read)
    650       port += 20;
    651   }
    652 
    653   cbc.buf = buf;
    654   cbc.size = 2048;
    655   cbc.pos = 0;
    656   multi = NULL;
    657   d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY,
    658                         port,
    659                         NULL, NULL, &ahc_echo, &done_flag,
    660                         MHD_OPTION_CONNECTION_MEMORY_LIMIT,
    661                         (size_t) (incr_read ? 1024 : (PUT_SIZE * 4)),
    662                         MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE,
    663                         MHD_OPTION_END);
    664   if (d == NULL)
    665     mhdErrorExit ();
    666   if (0 == port)
    667   {
    668     const union MHD_DaemonInfo *dinfo;
    669     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    670     if ((NULL == dinfo) || (0 == dinfo->port) )
    671       mhdErrorExit ();
    672     port = dinfo->port;
    673   }
    674 
    675   c = curl_easy_init ();
    676   if (NULL == c)
    677   {
    678     fprintf (stderr, "curl_easy_init() failed.\n");
    679     externalErrorExit ();
    680   }
    681   if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L)) ||
    682       (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROXY, "")) ||
    683       (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL,
    684                                      "http://127.0.0.1/hello_world")) ||
    685       (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, (long) port)) ||
    686       (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEFUNCTION,
    687                                      &copyBuffer)) ||
    688       (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc)) ||
    689       (CURLE_OK != curl_easy_setopt (c, CURLOPT_READFUNCTION,
    690                                      &putBuffer)) ||
    691       (CURLE_OK != curl_easy_setopt (c, CURLOPT_READDATA, &pos)) ||
    692       (CURLE_OK != curl_easy_setopt (c, CURLOPT_UPLOAD, 1L)) ||
    693       (CURLE_OK != curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE)) ||
    694       (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) ||
    695       (CURLE_OK != curl_easy_setopt (c, CURLOPT_ERRORBUFFER, libcurl_errbuf)) ||
    696       (CURLE_OK != curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT,
    697                                      (long) 150)) ||
    698       (CURLE_OK != curl_easy_setopt (c, CURLOPT_TIMEOUT,
    699                                      (long) 150)) ||
    700       (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
    701                                      (oneone) ?
    702                                      CURL_HTTP_VERSION_1_1 :
    703                                      CURL_HTTP_VERSION_1_0)))
    704   {
    705     fprintf (stderr, "curl_easy_setopt() failed.\n");
    706     externalErrorExit ();
    707   }
    708 
    709   multi = curl_multi_init ();
    710   if (multi == NULL)
    711     libcurlErrorExit ();
    712   mret = curl_multi_add_handle (multi, c);
    713   if (mret != CURLM_OK)
    714     libcurlErrorExit ();
    715 
    716   start = time (NULL);
    717   while ((time (NULL) - start < 45) && (multi != NULL))
    718   {
    719     MHD_socket maxMHDsock;
    720     int maxcurlsock;
    721     maxMHDsock = MHD_INVALID_SOCKET;
    722     maxcurlsock = -1;
    723     FD_ZERO (&rs);
    724     FD_ZERO (&ws);
    725     FD_ZERO (&es);
    726     mret = curl_multi_perform (multi, &running);
    727     if ((CURLM_OK != mret) && (CURLM_CALL_MULTI_PERFORM != mret))
    728     {
    729       fprintf (stderr, "curl_multi_perform() failed. Error: '%s'. ",
    730                curl_multi_strerror (mret));
    731       libcurlErrorExit ();
    732     }
    733     if (CURLM_OK != curl_multi_fdset (multi, &rs, &ws, &es, &maxcurlsock))
    734       libcurlErrorExitDesc ("curl_multi_fdset() failed");
    735     if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxMHDsock))
    736       mhdErrorExit ();
    737 
    738     tv.tv_sec = 0;
    739     tv.tv_usec = 1000;
    740 #ifndef MHD_WINSOCK_SOCKETS
    741     if (maxMHDsock > maxcurlsock)
    742       maxcurlsock = maxMHDsock;
    743 #endif /* MHD_WINSOCK_SOCKETS */
    744     if (-1 == select (maxcurlsock + 1, &rs, &ws, &es, &tv))
    745     {
    746 #ifdef MHD_POSIX_SOCKETS
    747       if (EINTR != errno)
    748         externalErrorExitDesc ("Unexpected select() error");
    749 #else
    750       if ((WSAEINVAL != WSAGetLastError ()) ||
    751           (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
    752         externalErrorExitDesc ("Unexpected select() error");
    753       Sleep ((DWORD) (tv.tv_sec * 1000 + tv.tv_usec / 1000));
    754 #endif
    755     }
    756 
    757     mret = curl_multi_perform (multi, &running);
    758     if ((CURLM_OK != mret) && (CURLM_CALL_MULTI_PERFORM != mret))
    759     {
    760       fprintf (stderr, "curl_multi_perform() failed. Error: '%s'. ",
    761                curl_multi_strerror (mret));
    762       libcurlErrorExit ();
    763     }
    764     if (0 == running)
    765     {
    766       int pending;
    767       int curl_fine = 0;
    768       while (NULL != (msg = curl_multi_info_read (multi, &pending)))
    769       {
    770         if (msg->msg == CURLMSG_DONE)
    771         {
    772           if (msg->data.result == CURLE_OK)
    773             curl_fine = 1;
    774           else
    775           {
    776             fprintf (stderr,
    777                      "curl_multi_perform() failed: '%s' ",
    778                      curl_easy_strerror (msg->data.result));
    779             libcurlErrorExit ();
    780           }
    781         }
    782       }
    783       if (! curl_fine)
    784       {
    785         fprintf (stderr, "libcurl haven't returned OK code ");
    786         mhdErrorExit ();
    787       }
    788       curl_multi_remove_handle (multi, c);
    789       curl_multi_cleanup (multi);
    790       curl_easy_cleanup (c);
    791       c = NULL;
    792       multi = NULL;
    793     }
    794     MHD_run (d);
    795   }
    796   if (multi != NULL)
    797     mhdErrorExitDesc ("Request has been aborted by timeout");
    798 
    799   MHD_stop_daemon (d);
    800   if (cbc.pos != strlen ("/hello_world"))
    801   {
    802     fprintf (stderr, "Got %u bytes ('%.*s'), expected %u bytes. ",
    803              (unsigned) cbc.pos, (int) cbc.pos, cbc.buf,
    804              (unsigned) strlen ("/hello_world"));
    805     mhdErrorExitDesc ("Wrong returned data length");
    806   }
    807   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    808   {
    809     fprintf (stderr, "Got invalid response '%.*s'. ", (int) cbc.pos, cbc.buf);
    810     mhdErrorExitDesc ("Wrong returned data length");
    811   }
    812   return 0;
    813 }
    814 
    815 
    816 int
    817 main (int argc, char *const *argv)
    818 {
    819   unsigned int errorCount = 0;
    820   unsigned int lastErr;
    821 
    822   oneone = has_in_name (argv[0], "11");
    823   incr_read = has_in_name (argv[0], "_inc");
    824   verbose = has_param (argc, argv, "-v");
    825   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
    826     return 99;
    827   put_buffer = alloc_init (PUT_SIZE);
    828   if (NULL == put_buffer)
    829     return 99;
    830   lastErr = testPutExternal ();
    831   if (verbose && (0 != lastErr))
    832     fprintf (stderr, "Error during testing with external select().\n");
    833   errorCount += lastErr;
    834   if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS))
    835   {
    836     lastErr = testPutInternalThread (0);
    837     if (verbose && (0 != lastErr) )
    838       fprintf (stderr,
    839                "Error during testing with internal thread with select().\n");
    840     errorCount += lastErr;
    841     lastErr = testPutThreadPerConn (0);
    842     if (verbose && (0 != lastErr) )
    843       fprintf (stderr,
    844                "Error during testing with internal thread per connection with select().\n");
    845     errorCount += lastErr;
    846     lastErr = testPutThreadPool (0);
    847     if (verbose && (0 != lastErr) )
    848       fprintf (stderr,
    849                "Error during testing with thread pool per connection with select().\n");
    850     errorCount += lastErr;
    851     if (MHD_is_feature_supported (MHD_FEATURE_POLL))
    852     {
    853       lastErr = testPutInternalThread (MHD_USE_POLL);
    854       if (verbose && (0 != lastErr) )
    855         fprintf (stderr,
    856                  "Error during testing with internal thread with poll().\n");
    857       errorCount += lastErr;
    858       lastErr = testPutThreadPerConn (MHD_USE_POLL);
    859       if (verbose && (0 != lastErr) )
    860         fprintf (stderr,
    861                  "Error during testing with internal thread per connection with poll().\n");
    862       errorCount += lastErr;
    863       lastErr = testPutThreadPool (MHD_USE_POLL);
    864       if (verbose && (0 != lastErr) )
    865         fprintf (stderr,
    866                  "Error during testing with thread pool per connection with poll().\n");
    867       errorCount += lastErr;
    868     }
    869     if (MHD_is_feature_supported (MHD_FEATURE_EPOLL))
    870     {
    871       lastErr = testPutInternalThread (MHD_USE_EPOLL);
    872       if (verbose && (0 != lastErr) )
    873         fprintf (stderr,
    874                  "Error during testing with internal thread with epoll.\n");
    875       errorCount += lastErr;
    876       lastErr = testPutThreadPool (MHD_USE_EPOLL);
    877       if (verbose && (0 != lastErr) )
    878         fprintf (stderr,
    879                  "Error during testing with thread pool per connection with epoll.\n");
    880       errorCount += lastErr;
    881     }
    882   }
    883   free (put_buffer);
    884   if (errorCount != 0)
    885     fprintf (stderr, "Error (code: %u)\n", errorCount);
    886   else if (verbose)
    887     printf ("All checks passed successfully.\n");
    888   curl_global_cleanup ();
    889   return (errorCount == 0) ? 0 : 1;
    890 }