libmicrohttpd

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

test_patch.c (13674B)


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