libmicrohttpd

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

test_put_chunked.c (16321B)


      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 daemontest_put_chunked.c
     24  * @brief Testcase for libmicrohttpd PUT operations with chunked encoding
     25  *        for the upload data
     26  * @author Christian Grothoff
     27  * @author Karlson2k (Evgeny Grin)
     28  */
     29 
     30 #include "MHD_config.h"
     31 #include "platform.h"
     32 #include <curl/curl.h>
     33 #include <microhttpd.h>
     34 #include <stdlib.h>
     35 #include <string.h>
     36 #include <time.h>
     37 #include <errno.h>
     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 #ifndef WINDOWS
     44 #include <unistd.h>
     45 #endif
     46 
     47 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2
     48 #undef MHD_CPU_COUNT
     49 #endif
     50 #if ! defined(MHD_CPU_COUNT)
     51 #define MHD_CPU_COUNT 2
     52 #endif
     53 
     54 struct CBC
     55 {
     56   char *buf;
     57   size_t pos;
     58   size_t size;
     59 };
     60 
     61 static size_t
     62 putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
     63 {
     64   size_t *pos = ptr;
     65   size_t wrt;
     66 
     67   wrt = size * nmemb;
     68   if (wrt > 8 - (*pos))
     69     wrt = 8 - (*pos);
     70   if (wrt > 4)
     71     wrt = 4;                    /* only send half at first => force multiple chunks! */
     72   memcpy (stream, &("Hello123"[*pos]), wrt);
     73   (*pos) += wrt;
     74   return wrt;
     75 }
     76 
     77 
     78 static size_t
     79 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
     80 {
     81   struct CBC *cbc = ctx;
     82 
     83   if (cbc->pos + size * nmemb > cbc->size)
     84     return 0;                   /* overflow */
     85   memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
     86   cbc->pos += size * nmemb;
     87   return size * nmemb;
     88 }
     89 
     90 
     91 static enum MHD_Result
     92 ahc_echo (void *cls,
     93           struct MHD_Connection *connection,
     94           const char *url,
     95           const char *method,
     96           const char *version,
     97           const char *upload_data, size_t *upload_data_size,
     98           void **req_cls)
     99 {
    100   size_t *done = cls;
    101   struct MHD_Response *response;
    102   enum MHD_Result ret;
    103   size_t have;
    104   (void) version; (void) req_cls;   /* Unused. Silent compiler warning. */
    105 
    106   if (0 != strcmp ("PUT", method))
    107     return MHD_NO;              /* unexpected method */
    108   if ((*done) < 8)
    109   {
    110     have = *upload_data_size;
    111     if (have + *done > 8)
    112     {
    113       printf ("Invalid upload data `%8s'!\n", upload_data);
    114       return MHD_NO;
    115     }
    116     if (0 == have)
    117       return MHD_YES;
    118     if (0 == memcmp (upload_data, &"Hello123"[*done], have))
    119     {
    120       *done += have;
    121       *upload_data_size = 0;
    122     }
    123     else
    124     {
    125       printf ("Invalid upload data `%8s'!\n", upload_data);
    126       return MHD_NO;
    127     }
    128 #if 0
    129     fprintf (stderr, "Not ready for response: %u/%u\n", *done, 8);
    130 #endif
    131     return MHD_YES;
    132   }
    133   response = MHD_create_response_from_buffer_copy (strlen (url),
    134                                                    (const void *) url);
    135   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
    136   MHD_destroy_response (response);
    137   return ret;
    138 }
    139 
    140 
    141 static unsigned int
    142 testInternalPut (void)
    143 {
    144   struct MHD_Daemon *d;
    145   CURL *c;
    146   char buf[2048];
    147   struct CBC cbc;
    148   size_t pos = 0;
    149   size_t done_flag = 0;
    150   CURLcode errornum;
    151   uint16_t port;
    152 
    153   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    154     port = 0;
    155   else
    156     port = 1440;
    157 
    158   cbc.buf = buf;
    159   cbc.size = 2048;
    160   cbc.pos = 0;
    161   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    162                         port,
    163                         NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
    164   if (d == NULL)
    165     return 1;
    166   if (0 == port)
    167   {
    168     const union MHD_DaemonInfo *dinfo;
    169     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    170     if ((NULL == dinfo) || (0 == dinfo->port) )
    171     {
    172       MHD_stop_daemon (d); return 32;
    173     }
    174     port = dinfo->port;
    175   }
    176   c = curl_easy_init ();
    177   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    178   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    179   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    180   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    181   curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
    182   curl_easy_setopt (c, CURLOPT_READDATA, &pos);
    183   curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
    184   /* by not giving the file size, we force chunking! */
    185   /*
    186      curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
    187    */
    188   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    189   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    190   curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    191   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    192   /* NOTE: use of CONNECTTIMEOUT without also
    193    *   setting NOSIGNAL results in really weird
    194    *   crashes on my system! */
    195   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    196   curl_easy_setopt (c, CURLOPT_PROXY, "");
    197   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    198   {
    199     fprintf (stderr,
    200              "curl_easy_perform failed: `%s'\n",
    201              curl_easy_strerror (errornum));
    202     curl_easy_cleanup (c);
    203     MHD_stop_daemon (d);
    204     return 2;
    205   }
    206   curl_easy_cleanup (c);
    207   MHD_stop_daemon (d);
    208   if (cbc.pos != strlen ("/hello_world"))
    209     return 4;
    210   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    211     return 8;
    212   return 0;
    213 }
    214 
    215 
    216 static unsigned int
    217 testMultithreadedPut (void)
    218 {
    219   struct MHD_Daemon *d;
    220   CURL *c;
    221   char buf[2048];
    222   struct CBC cbc;
    223   size_t pos = 0;
    224   size_t done_flag = 0;
    225   CURLcode errornum;
    226   uint16_t port;
    227 
    228   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    229     port = 0;
    230   else
    231     port = 1441;
    232 
    233   cbc.buf = buf;
    234   cbc.size = 2048;
    235   cbc.pos = 0;
    236   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    237                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    238                         port,
    239                         NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
    240   if (d == NULL)
    241     return 16;
    242   if (0 == port)
    243   {
    244     const union MHD_DaemonInfo *dinfo;
    245     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    246     if ((NULL == dinfo) || (0 == dinfo->port) )
    247     {
    248       MHD_stop_daemon (d); return 32;
    249     }
    250     port = dinfo->port;
    251   }
    252   c = curl_easy_init ();
    253   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    254   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    255   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    256   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    257   curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
    258   curl_easy_setopt (c, CURLOPT_READDATA, &pos);
    259   curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
    260   /* by not giving the file size, we force chunking! */
    261   /*
    262      curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
    263    */
    264   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    265   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    266   curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    267   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    268   /* NOTE: use of CONNECTTIMEOUT without also
    269    *   setting NOSIGNAL results in really weird
    270    *   crashes on my system! */
    271   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    272   curl_easy_setopt (c, CURLOPT_PROXY, "");
    273   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    274   {
    275     fprintf (stderr,
    276              "curl_easy_perform failed: `%s'\n",
    277              curl_easy_strerror (errornum));
    278     curl_easy_cleanup (c);
    279     MHD_stop_daemon (d);
    280     return 32;
    281   }
    282   curl_easy_cleanup (c);
    283   MHD_stop_daemon (d);
    284   if (cbc.pos != strlen ("/hello_world"))
    285     return 64;
    286   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    287     return 128;
    288 
    289   return 0;
    290 }
    291 
    292 
    293 static unsigned int
    294 testMultithreadedPoolPut (void)
    295 {
    296   struct MHD_Daemon *d;
    297   CURL *c;
    298   char buf[2048];
    299   struct CBC cbc;
    300   size_t pos = 0;
    301   size_t done_flag = 0;
    302   CURLcode errornum;
    303   uint16_t port;
    304 
    305   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    306     port = 0;
    307   else
    308     port = 1442;
    309 
    310   cbc.buf = buf;
    311   cbc.size = 2048;
    312   cbc.pos = 0;
    313   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    314                         port,
    315                         NULL, NULL, &ahc_echo, &done_flag,
    316                         MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT,
    317                         MHD_OPTION_END);
    318   if (d == NULL)
    319     return 16;
    320   if (0 == port)
    321   {
    322     const union MHD_DaemonInfo *dinfo;
    323     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    324     if ((NULL == dinfo) || (0 == dinfo->port) )
    325     {
    326       MHD_stop_daemon (d); return 32;
    327     }
    328     port = dinfo->port;
    329   }
    330   c = curl_easy_init ();
    331   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    332   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    333   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    334   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    335   curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
    336   curl_easy_setopt (c, CURLOPT_READDATA, &pos);
    337   curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
    338   /* by not giving the file size, we force chunking! */
    339   /*
    340      curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
    341    */
    342   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    343   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    344   curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    345   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    346   /* NOTE: use of CONNECTTIMEOUT without also
    347    *   setting NOSIGNAL results in really weird
    348    *   crashes on my system! */
    349   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    350   curl_easy_setopt (c, CURLOPT_PROXY, "");
    351   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    352   {
    353     fprintf (stderr,
    354              "curl_easy_perform failed: `%s'\n",
    355              curl_easy_strerror (errornum));
    356     curl_easy_cleanup (c);
    357     MHD_stop_daemon (d);
    358     return 32;
    359   }
    360   curl_easy_cleanup (c);
    361   MHD_stop_daemon (d);
    362   if (cbc.pos != strlen ("/hello_world"))
    363     return 64;
    364   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    365     return 128;
    366 
    367   return 0;
    368 }
    369 
    370 
    371 static unsigned int
    372 testExternalPut (void)
    373 {
    374   struct MHD_Daemon *d;
    375   CURL *c;
    376   char buf[2048];
    377   struct CBC cbc;
    378   CURLM *multi;
    379   CURLMcode mret;
    380   fd_set rs;
    381   fd_set ws;
    382   fd_set es;
    383   MHD_socket maxsock;
    384 #ifdef MHD_WINSOCK_SOCKETS
    385   int maxposixs; /* Max socket number unused on W32 */
    386 #else  /* MHD_POSIX_SOCKETS */
    387 #define maxposixs maxsock
    388 #endif /* MHD_POSIX_SOCKETS */
    389   int running;
    390   struct CURLMsg *msg;
    391   time_t start;
    392   struct timeval tv;
    393   size_t pos = 0;
    394   size_t done_flag = 0;
    395   uint16_t port;
    396 
    397   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    398     port = 0;
    399   else
    400     port = 1443;
    401 
    402   multi = NULL;
    403   cbc.buf = buf;
    404   cbc.size = 2048;
    405   cbc.pos = 0;
    406   d = MHD_start_daemon (MHD_USE_ERROR_LOG,
    407                         port,
    408                         NULL, NULL, &ahc_echo, &done_flag,
    409                         MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE,
    410                         MHD_OPTION_END);
    411   if (d == NULL)
    412     return 256;
    413   if (0 == port)
    414   {
    415     const union MHD_DaemonInfo *dinfo;
    416     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    417     if ((NULL == dinfo) || (0 == dinfo->port) )
    418     {
    419       MHD_stop_daemon (d); return 32;
    420     }
    421     port = dinfo->port;
    422   }
    423   c = curl_easy_init ();
    424   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    425   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    426   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    427   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    428   curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
    429   curl_easy_setopt (c, CURLOPT_READDATA, &pos);
    430   curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
    431   /* by not giving the file size, we force chunking! */
    432   /*
    433      curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
    434    */
    435   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    436   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    437   curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    438   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    439   /* NOTE: use of CONNECTTIMEOUT without also
    440    *   setting NOSIGNAL results in really weird
    441    *   crashes on my system! */
    442   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    443   curl_easy_setopt (c, CURLOPT_PROXY, "");
    444 
    445 
    446   multi = curl_multi_init ();
    447   if (multi == NULL)
    448   {
    449     curl_easy_cleanup (c);
    450     MHD_stop_daemon (d);
    451     return 512;
    452   }
    453   mret = curl_multi_add_handle (multi, c);
    454   if (mret != CURLM_OK)
    455   {
    456     curl_multi_cleanup (multi);
    457     curl_easy_cleanup (c);
    458     MHD_stop_daemon (d);
    459     return 1024;
    460   }
    461   start = time (NULL);
    462   while ((time (NULL) - start < 5) && (multi != NULL))
    463   {
    464     maxsock = MHD_INVALID_SOCKET;
    465     maxposixs = -1;
    466     FD_ZERO (&rs);
    467     FD_ZERO (&ws);
    468     FD_ZERO (&es);
    469     curl_multi_perform (multi, &running);
    470     mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
    471     if (mret != CURLM_OK)
    472     {
    473       curl_multi_remove_handle (multi, c);
    474       curl_multi_cleanup (multi);
    475       curl_easy_cleanup (c);
    476       MHD_stop_daemon (d);
    477       return 2048;
    478     }
    479     if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock))
    480     {
    481       curl_multi_remove_handle (multi, c);
    482       curl_multi_cleanup (multi);
    483       curl_easy_cleanup (c);
    484       MHD_stop_daemon (d);
    485       return 4096;
    486     }
    487     tv.tv_sec = 0;
    488     tv.tv_usec = 1000;
    489     if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
    490     {
    491 #ifdef MHD_POSIX_SOCKETS
    492       if (EINTR != errno)
    493       {
    494         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    495                  (int) errno, __LINE__);
    496         fflush (stderr);
    497         exit (99);
    498       }
    499 #else
    500       if ((WSAEINVAL != WSAGetLastError ()) ||
    501           (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
    502       {
    503         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    504                  (int) WSAGetLastError (), __LINE__);
    505         fflush (stderr);
    506         exit (99);
    507       }
    508       Sleep (1);
    509 #endif
    510     }
    511     curl_multi_perform (multi, &running);
    512     if (0 == running)
    513     {
    514       int pending;
    515       int curl_fine = 0;
    516       while (NULL != (msg = curl_multi_info_read (multi, &pending)))
    517       {
    518         if (msg->msg == CURLMSG_DONE)
    519         {
    520           if (msg->data.result == CURLE_OK)
    521             curl_fine = 1;
    522           else
    523           {
    524             fprintf (stderr,
    525                      "%s failed at %s:%d: `%s'\n",
    526                      "curl_multi_perform",
    527                      __FILE__,
    528                      __LINE__, curl_easy_strerror (msg->data.result));
    529             abort ();
    530           }
    531         }
    532       }
    533       if (! curl_fine)
    534       {
    535         fprintf (stderr, "libcurl haven't returned OK code\n");
    536         abort ();
    537       }
    538       curl_multi_remove_handle (multi, c);
    539       curl_multi_cleanup (multi);
    540       curl_easy_cleanup (c);
    541       c = NULL;
    542       multi = NULL;
    543     }
    544     MHD_run (d);
    545   }
    546   if (multi != NULL)
    547   {
    548     curl_multi_remove_handle (multi, c);
    549     curl_easy_cleanup (c);
    550     curl_multi_cleanup (multi);
    551   }
    552   MHD_stop_daemon (d);
    553   if (cbc.pos != strlen ("/hello_world"))
    554     return 8192;
    555   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    556     return 16384;
    557   return 0;
    558 }
    559 
    560 
    561 int
    562 main (int argc, char *const *argv)
    563 {
    564   unsigned int errorCount = 0;
    565   (void) argc; (void) argv; /* Unused. Silent compiler warning. */
    566 
    567   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
    568     return 2;
    569   if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS))
    570   {
    571     errorCount += testInternalPut ();
    572     errorCount += testMultithreadedPut ();
    573     errorCount += testMultithreadedPoolPut ();
    574   }
    575   errorCount += testExternalPut ();
    576   if (errorCount != 0)
    577     fprintf (stderr, "Error (code: %u)\n", errorCount);
    578   curl_global_cleanup ();
    579   return (0 == errorCount) ? 0 : 1;       /* 0 == pass */
    580 }