libmicrohttpd

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

test_delete.c (16450B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007, 2016 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_delete.c
     24  * @brief  Testcase for libmicrohttpd DELETE 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 ("DELETE", 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 unsigned int
    132 testInternalDelete (void)
    133 {
    134   struct MHD_Daemon *d;
    135   CURL *c;
    136   char buf[2048];
    137   struct CBC cbc;
    138   size_t pos = 0;
    139   int done_flag = 0;
    140   CURLcode errornum;
    141   uint16_t port;
    142 
    143   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    144     port = 0;
    145   else
    146     port = 1152;
    147 
    148   cbc.buf = buf;
    149   cbc.size = 2048;
    150   cbc.pos = 0;
    151   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    152                         port,
    153                         NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
    154   if (d == NULL)
    155     return 1;
    156   if (0 == port)
    157   {
    158     const union MHD_DaemonInfo *dinfo;
    159     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    160     if ((NULL == dinfo) || (0 == dinfo->port) )
    161     {
    162       MHD_stop_daemon (d); return 32;
    163     }
    164     port = dinfo->port;
    165   }
    166   c = curl_easy_init ();
    167   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    168   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    169   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    170   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    171   curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
    172   curl_easy_setopt (c, CURLOPT_READDATA, &pos);
    173   curl_easy_setopt (c, CURLOPT_CUSTOMREQUEST, "DELETE");
    174   curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
    175   curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
    176   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    177   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    178   if (oneone)
    179     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    180   else
    181     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    182   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    183   /* NOTE: use of CONNECTTIMEOUT without also
    184    *   setting NOSIGNAL results in really weird
    185    *   crashes on my system! */
    186   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    187   curl_easy_setopt (c, CURLOPT_PROXY, "");
    188   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    189   {
    190     fprintf (stderr,
    191              "curl_easy_perform failed: `%s'\n",
    192              curl_easy_strerror (errornum));
    193     curl_easy_cleanup (c);
    194     MHD_stop_daemon (d);
    195     return 2;
    196   }
    197   curl_easy_cleanup (c);
    198   MHD_stop_daemon (d);
    199   if (cbc.pos != strlen ("/hello_world"))
    200     return 4;
    201   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    202     return 8;
    203   return 0;
    204 }
    205 
    206 
    207 static unsigned int
    208 testMultithreadedDelete (void)
    209 {
    210   struct MHD_Daemon *d;
    211   CURL *c;
    212   char buf[2048];
    213   struct CBC cbc;
    214   size_t pos = 0;
    215   int done_flag = 0;
    216   CURLcode errornum;
    217   uint16_t port;
    218 
    219   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    220     port = 0;
    221   else
    222     port = 1153;
    223 
    224   cbc.buf = buf;
    225   cbc.size = 2048;
    226   cbc.pos = 0;
    227   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    228                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    229                         port,
    230                         NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
    231   if (d == NULL)
    232     return 16;
    233   if (0 == port)
    234   {
    235     const union MHD_DaemonInfo *dinfo;
    236     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    237     if ((NULL == dinfo) || (0 == dinfo->port) )
    238     {
    239       MHD_stop_daemon (d); return 32;
    240     }
    241     port = dinfo->port;
    242   }
    243   c = curl_easy_init ();
    244   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    245   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    246   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    247   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    248   curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
    249   curl_easy_setopt (c, CURLOPT_READDATA, &pos);
    250   curl_easy_setopt (c, CURLOPT_CUSTOMREQUEST, "DELETE");
    251   curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
    252   curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
    253   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    254   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    255   if (oneone)
    256     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    257   else
    258     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    259   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    260   /* NOTE: use of CONNECTTIMEOUT without also
    261    *   setting NOSIGNAL results in really weird
    262    *   crashes on my system! */
    263   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    264   curl_easy_setopt (c, CURLOPT_PROXY, "");
    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 testMultithreadedPoolDelete (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     port = 1154;
    301 
    302   cbc.buf = buf;
    303   cbc.size = 2048;
    304   cbc.pos = 0;
    305   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    306                         port,
    307                         NULL, NULL, &ahc_echo, &done_flag,
    308                         MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT,
    309                         MHD_OPTION_END);
    310   if (d == NULL)
    311     return 16;
    312   if (0 == port)
    313   {
    314     const union MHD_DaemonInfo *dinfo;
    315     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    316     if ((NULL == dinfo) || (0 == dinfo->port) )
    317     {
    318       MHD_stop_daemon (d); return 32;
    319     }
    320     port = dinfo->port;
    321   }
    322   c = curl_easy_init ();
    323   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    324   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    325   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    326   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    327   curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
    328   curl_easy_setopt (c, CURLOPT_READDATA, &pos);
    329   curl_easy_setopt (c, CURLOPT_CUSTOMREQUEST, "DELETE");
    330   curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
    331   curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
    332   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    333   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    334   if (oneone)
    335     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    336   else
    337     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    338   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    339   /* NOTE: use of CONNECTTIMEOUT without also
    340    *   setting NOSIGNAL results in really weird
    341    *   crashes on my system! */
    342   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    343   curl_easy_setopt (c, CURLOPT_PROXY, "");
    344   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    345   {
    346     fprintf (stderr,
    347              "curl_easy_perform failed: `%s'\n",
    348              curl_easy_strerror (errornum));
    349     curl_easy_cleanup (c);
    350     MHD_stop_daemon (d);
    351     return 32;
    352   }
    353   curl_easy_cleanup (c);
    354   MHD_stop_daemon (d);
    355   if (cbc.pos != strlen ("/hello_world"))
    356     return 64;
    357   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    358     return 128;
    359 
    360   return 0;
    361 }
    362 
    363 
    364 static unsigned int
    365 testExternalDelete (void)
    366 {
    367   struct MHD_Daemon *d;
    368   CURL *c;
    369   char buf[2048];
    370   struct CBC cbc;
    371   CURLM *multi;
    372   CURLMcode mret;
    373   fd_set rs;
    374   fd_set ws;
    375   fd_set es;
    376   MHD_socket maxsock;
    377 #ifdef MHD_WINSOCK_SOCKETS
    378   int maxposixs; /* Max socket number unused on W32 */
    379 #else  /* MHD_POSIX_SOCKETS */
    380 #define maxposixs maxsock
    381 #endif /* MHD_POSIX_SOCKETS */
    382   int running;
    383   struct CURLMsg *msg;
    384   time_t start;
    385   struct timeval tv;
    386   size_t pos = 0;
    387   int done_flag = 0;
    388   uint16_t port;
    389 
    390   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    391     port = 0;
    392   else
    393     port = 1154;
    394 
    395   multi = NULL;
    396   cbc.buf = buf;
    397   cbc.size = 2048;
    398   cbc.pos = 0;
    399   d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY,
    400                         port,
    401                         NULL, NULL, &ahc_echo, &done_flag,
    402                         MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE,
    403                         MHD_OPTION_END);
    404   if (d == NULL)
    405     return 256;
    406   if (0 == port)
    407   {
    408     const union MHD_DaemonInfo *dinfo;
    409     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    410     if ((NULL == dinfo) || (0 == dinfo->port) )
    411     {
    412       MHD_stop_daemon (d); return 32;
    413     }
    414     port = dinfo->port;
    415   }
    416   c = curl_easy_init ();
    417   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    418   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    419   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    420   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    421   curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
    422   curl_easy_setopt (c, CURLOPT_READDATA, &pos);
    423   curl_easy_setopt (c, CURLOPT_CUSTOMREQUEST, "DELETE");
    424   curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
    425   curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
    426   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    427   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    428   if (oneone)
    429     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    430   else
    431     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    432   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    433   /* NOTE: use of CONNECTTIMEOUT without also
    434    *   setting NOSIGNAL results in really weird
    435    *   crashes on my system! */
    436   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    437   curl_easy_setopt (c, CURLOPT_PROXY, "");
    438 
    439 
    440   multi = curl_multi_init ();
    441   if (multi == NULL)
    442   {
    443     curl_easy_cleanup (c);
    444     MHD_stop_daemon (d);
    445     return 512;
    446   }
    447   mret = curl_multi_add_handle (multi, c);
    448   if (mret != CURLM_OK)
    449   {
    450     curl_multi_cleanup (multi);
    451     curl_easy_cleanup (c);
    452     MHD_stop_daemon (d);
    453     return 1024;
    454   }
    455   start = time (NULL);
    456   while ((time (NULL) - start < 5) && (multi != NULL))
    457   {
    458     maxsock = MHD_INVALID_SOCKET;
    459     maxposixs = -1;
    460     FD_ZERO (&rs);
    461     FD_ZERO (&ws);
    462     FD_ZERO (&es);
    463     curl_multi_perform (multi, &running);
    464     mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
    465     if (mret != CURLM_OK)
    466     {
    467       curl_multi_remove_handle (multi, c);
    468       curl_multi_cleanup (multi);
    469       curl_easy_cleanup (c);
    470       MHD_stop_daemon (d);
    471       return 2048;
    472     }
    473     if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock))
    474     {
    475       curl_multi_remove_handle (multi, c);
    476       curl_multi_cleanup (multi);
    477       curl_easy_cleanup (c);
    478       MHD_stop_daemon (d);
    479       return 4096;
    480     }
    481     tv.tv_sec = 0;
    482     tv.tv_usec = 1000;
    483     if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
    484     {
    485 #ifdef MHD_POSIX_SOCKETS
    486       if (EINTR != errno)
    487       {
    488         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    489                  (int) errno, __LINE__);
    490         fflush (stderr);
    491         exit (99);
    492       }
    493 #else
    494       if ((WSAEINVAL != WSAGetLastError ()) ||
    495           (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
    496       {
    497         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    498                  (int) WSAGetLastError (), __LINE__);
    499         fflush (stderr);
    500         exit (99);
    501       }
    502       Sleep (1);
    503 #endif
    504     }
    505     curl_multi_perform (multi, &running);
    506     if (0 == running)
    507     {
    508       int pending;
    509       int curl_fine = 0;
    510       while (NULL != (msg = curl_multi_info_read (multi, &pending)))
    511       {
    512         if (msg->msg == CURLMSG_DONE)
    513         {
    514           if (msg->data.result == CURLE_OK)
    515             curl_fine = 1;
    516           else
    517           {
    518             fprintf (stderr,
    519                      "%s failed at %s:%d: `%s'\n",
    520                      "curl_multi_perform",
    521                      __FILE__,
    522                      __LINE__, curl_easy_strerror (msg->data.result));
    523             abort ();
    524           }
    525         }
    526       }
    527       if (! curl_fine)
    528       {
    529         fprintf (stderr, "libcurl haven't returned OK code\n");
    530         abort ();
    531       }
    532       curl_multi_remove_handle (multi, c);
    533       curl_multi_cleanup (multi);
    534       curl_easy_cleanup (c);
    535       c = NULL;
    536       multi = NULL;
    537     }
    538     MHD_run (d);
    539   }
    540   if (multi != NULL)
    541   {
    542     curl_multi_remove_handle (multi, c);
    543     curl_easy_cleanup (c);
    544     curl_multi_cleanup (multi);
    545   }
    546   MHD_stop_daemon (d);
    547   if (cbc.pos != strlen ("/hello_world"))
    548     return 8192;
    549   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    550     return 16384;
    551   return 0;
    552 }
    553 
    554 
    555 int
    556 main (int argc, char *const *argv)
    557 {
    558   unsigned int errorCount = 0;
    559   (void) argc;   /* Unused. Silent compiler warning. */
    560 
    561   if ((NULL == argv) || (0 == argv[0]))
    562     return 99;
    563   oneone = has_in_name (argv[0], "11");
    564   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
    565     return 2;
    566   if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS))
    567   {
    568     errorCount += testInternalDelete ();
    569     errorCount += testMultithreadedDelete ();
    570     errorCount += testMultithreadedPoolDelete ();
    571   }
    572   errorCount += testExternalDelete ();
    573   if (errorCount != 0)
    574     fprintf (stderr, "Error (code: %u)\n", errorCount);
    575   curl_global_cleanup ();
    576   return (0 == errorCount) ? 0 : 1;       /* 0 == pass */
    577 }