libmicrohttpd

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

test_process_headers.c (16601B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007 Christian Grothoff
      4      Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
      5 
      6      libmicrohttpd is free software; you can redistribute it and/or modify
      7      it under the terms of the GNU General Public License as published
      8      by the Free Software Foundation; either version 2, or (at your
      9      option) any later version.
     10 
     11      libmicrohttpd is distributed in the hope that it will be useful, but
     12      WITHOUT ANY WARRANTY; without even the implied warranty of
     13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14      General Public License for more details.
     15 
     16      You should have received a copy of the GNU General Public License
     17      along with libmicrohttpd; see the file COPYING.  If not, write to the
     18      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19      Boston, MA 02110-1301, USA.
     20 */
     21 
     22 /**
     23  * @file test_process_headers.c
     24  * @brief  Testcase for HTTP header access
     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 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
     65 {
     66   struct CBC *cbc = ctx;
     67 
     68   if (cbc->pos + size * nmemb > cbc->size)
     69     return 0;                   /* overflow */
     70   memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
     71   cbc->pos += size * nmemb;
     72   return size * nmemb;
     73 }
     74 
     75 
     76 static enum MHD_Result
     77 kv_cb (void *cls, enum MHD_ValueKind kind, const char *key, const char *value)
     78 {
     79   if ((0 == strcmp (key, MHD_HTTP_HEADER_HOST)) &&
     80       (0 == strncmp (value, "127.0.0.1", strlen ("127.0.0.1"))) && (kind ==
     81                                                                     MHD_HEADER_KIND))
     82   {
     83     *((int *) cls) = 1;
     84     return MHD_NO;
     85   }
     86   return MHD_YES;
     87 }
     88 
     89 
     90 static enum MHD_Result
     91 ahc_echo (void *cls,
     92           struct MHD_Connection *connection,
     93           const char *url,
     94           const char *method,
     95           const char *version,
     96           const char *upload_data, size_t *upload_data_size,
     97           void **req_cls)
     98 {
     99   static int ptr;
    100   struct MHD_Response *response;
    101   enum MHD_Result ret;
    102   const char *hdr;
    103   (void) cls;
    104   (void) version; (void) upload_data; (void) upload_data_size;       /* Unused. Silent compiler warning. */
    105 
    106   if (0 != strcmp (MHD_HTTP_METHOD_GET, method))
    107     return MHD_NO;              /* unexpected method */
    108   if (&ptr != *req_cls)
    109   {
    110     *req_cls = &ptr;
    111     return MHD_YES;
    112   }
    113   *req_cls = NULL;
    114   ret = 0;
    115   MHD_get_connection_values (connection, MHD_HEADER_KIND, &kv_cb, &ret);
    116   if (ret != 1)
    117     abort ();
    118   hdr = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, "NotFound");
    119   if (hdr != NULL)
    120     abort ();
    121   hdr = MHD_lookup_connection_value (connection,
    122                                      MHD_HEADER_KIND, MHD_HTTP_HEADER_ACCEPT);
    123   if ((hdr == NULL) || (0 != strcmp (hdr, "*/*")))
    124     abort ();
    125   hdr = MHD_lookup_connection_value (connection,
    126                                      MHD_HEADER_KIND, MHD_HTTP_HEADER_HOST);
    127   if ((hdr == NULL) || (0 != strncmp (hdr, "127.0.0.1", strlen ("127.0.0.1"))))
    128     abort ();
    129   MHD_set_connection_value (connection,
    130                             MHD_HEADER_KIND, "FakeHeader", "NowPresent");
    131   hdr = MHD_lookup_connection_value (connection,
    132                                      MHD_HEADER_KIND, "FakeHeader");
    133   if ((hdr == NULL) || (0 != strcmp (hdr, "NowPresent")))
    134     abort ();
    135 
    136   response = MHD_create_response_from_buffer_copy (strlen (url),
    137                                                    (const void *) url);
    138   if (NULL == response)
    139     abort ();
    140   MHD_add_response_header (response, "MyHeader", "MyValue");
    141   hdr = MHD_get_response_header (response, "MyHeader");
    142   if (0 != strcmp ("MyValue", hdr))
    143     abort ();
    144   MHD_add_response_header (response, "MyHeader", "MyValueToo");
    145   if (MHD_YES != MHD_del_response_header (response, "MyHeader", "MyValue"))
    146     abort ();
    147   hdr = MHD_get_response_header (response, "MyHeader");
    148   if (0 != strcmp ("MyValueToo", hdr))
    149     abort ();
    150   if (1 != MHD_get_response_headers (response, NULL, NULL))
    151     abort ();
    152   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
    153   MHD_destroy_response (response);
    154   if (ret == MHD_NO)
    155     abort ();
    156   return ret;
    157 }
    158 
    159 
    160 static unsigned int
    161 testInternalGet (void)
    162 {
    163   struct MHD_Daemon *d;
    164   CURL *c;
    165   char buf[2048];
    166   struct CBC cbc;
    167   CURLcode errornum;
    168   uint16_t port;
    169 
    170   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    171     port = 0;
    172   else
    173   {
    174     port = 1420;
    175     if (oneone)
    176       port += 10;
    177   }
    178 
    179   cbc.buf = buf;
    180   cbc.size = 2048;
    181   cbc.pos = 0;
    182   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    183                         port, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
    184   if (d == NULL)
    185     return 1;
    186   if (0 == port)
    187   {
    188     const union MHD_DaemonInfo *dinfo;
    189     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    190     if ((NULL == dinfo) || (0 == dinfo->port) )
    191     {
    192       MHD_stop_daemon (d); return 32;
    193     }
    194     port = dinfo->port;
    195   }
    196   c = curl_easy_init ();
    197   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    198   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    199   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    200   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    201   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    202   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    203   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    204   if (oneone)
    205     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    206   else
    207     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    208   /* NOTE: use of CONNECTTIMEOUT without also
    209      setting NOSIGNAL results in really weird
    210      crashes on my system! */
    211   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    212   curl_easy_setopt (c, CURLOPT_PROXY, "");
    213   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    214   {
    215     fprintf (stderr,
    216              "curl_easy_perform failed: `%s'\n",
    217              curl_easy_strerror (errornum));
    218     curl_easy_cleanup (c);
    219     MHD_stop_daemon (d);
    220     return 2;
    221   }
    222   curl_easy_cleanup (c);
    223   MHD_stop_daemon (d);
    224   if (cbc.pos != strlen ("/hello_world"))
    225     return 4;
    226   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    227     return 8;
    228   return 0;
    229 }
    230 
    231 
    232 static unsigned int
    233 testMultithreadedGet (void)
    234 {
    235   struct MHD_Daemon *d;
    236   CURL *c;
    237   char buf[2048];
    238   struct CBC cbc;
    239   CURLcode errornum;
    240   uint16_t port;
    241 
    242   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    243     port = 0;
    244   else
    245   {
    246     port = 1421;
    247     if (oneone)
    248       port += 10;
    249   }
    250 
    251   cbc.buf = buf;
    252   cbc.size = 2048;
    253   cbc.pos = 0;
    254   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    255                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    256                         port, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
    257   if (d == NULL)
    258     return 16;
    259   if (0 == port)
    260   {
    261     const union MHD_DaemonInfo *dinfo;
    262     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    263     if ((NULL == dinfo) || (0 == dinfo->port) )
    264     {
    265       MHD_stop_daemon (d); return 32;
    266     }
    267     port = dinfo->port;
    268   }
    269   c = curl_easy_init ();
    270   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    271   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    272   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    273   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    274   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    275   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    276   if (oneone)
    277     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    278   else
    279     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    280   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    281   /* NOTE: use of CONNECTTIMEOUT without also
    282      setting NOSIGNAL results in really weird
    283      crashes on my system! */
    284   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    285   curl_easy_setopt (c, CURLOPT_PROXY, "");
    286   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    287   {
    288     fprintf (stderr,
    289              "curl_easy_perform failed: `%s'\n",
    290              curl_easy_strerror (errornum));
    291     curl_easy_cleanup (c);
    292     MHD_stop_daemon (d);
    293     return 32;
    294   }
    295   curl_easy_cleanup (c);
    296   MHD_stop_daemon (d);
    297   if (cbc.pos != strlen ("/hello_world"))
    298     return 64;
    299   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    300     return 128;
    301   return 0;
    302 }
    303 
    304 
    305 static unsigned int
    306 testMultithreadedPoolGet (void)
    307 {
    308   struct MHD_Daemon *d;
    309   CURL *c;
    310   char buf[2048];
    311   struct CBC cbc;
    312   CURLcode errornum;
    313   uint16_t port;
    314 
    315   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    316     port = 0;
    317   else
    318   {
    319     port = 1422;
    320     if (oneone)
    321       port += 10;
    322   }
    323 
    324   cbc.buf = buf;
    325   cbc.size = 2048;
    326   cbc.pos = 0;
    327   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
    328                         port, NULL, NULL, &ahc_echo, NULL,
    329                         MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT,
    330                         MHD_OPTION_END);
    331   if (d == NULL)
    332     return 16;
    333   if (0 == port)
    334   {
    335     const union MHD_DaemonInfo *dinfo;
    336     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    337     if ((NULL == dinfo) || (0 == dinfo->port) )
    338     {
    339       MHD_stop_daemon (d); return 32;
    340     }
    341     port = dinfo->port;
    342   }
    343   c = curl_easy_init ();
    344   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    345   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    346   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    347   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    348   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    349   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    350   if (oneone)
    351     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    352   else
    353     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    354   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    355   /* NOTE: use of CONNECTTIMEOUT without also
    356      setting NOSIGNAL results in really weird
    357      crashes on my system! */
    358   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    359   curl_easy_setopt (c, CURLOPT_PROXY, "");
    360   if (CURLE_OK != (errornum = curl_easy_perform (c)))
    361   {
    362     fprintf (stderr,
    363              "curl_easy_perform failed: `%s'\n",
    364              curl_easy_strerror (errornum));
    365     curl_easy_cleanup (c);
    366     MHD_stop_daemon (d);
    367     return 32;
    368   }
    369   curl_easy_cleanup (c);
    370   MHD_stop_daemon (d);
    371   if (cbc.pos != strlen ("/hello_world"))
    372     return 64;
    373   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    374     return 128;
    375   return 0;
    376 }
    377 
    378 
    379 static unsigned int
    380 testExternalGet (void)
    381 {
    382   struct MHD_Daemon *d;
    383   CURL *c;
    384   char buf[2048];
    385   struct CBC cbc;
    386   CURLM *multi;
    387   CURLMcode mret;
    388   fd_set rs;
    389   fd_set ws;
    390   fd_set es;
    391   MHD_socket maxsock;
    392 #ifdef MHD_WINSOCK_SOCKETS
    393   int maxposixs; /* Max socket number unused on W32 */
    394 #else  /* MHD_POSIX_SOCKETS */
    395 #define maxposixs maxsock
    396 #endif /* MHD_POSIX_SOCKETS */
    397   int running;
    398   struct CURLMsg *msg;
    399   time_t start;
    400   struct timeval tv;
    401   uint16_t port;
    402 
    403   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    404     port = 0;
    405   else
    406   {
    407     port = 1423;
    408     if (oneone)
    409       port += 10;
    410   }
    411 
    412   multi = NULL;
    413   cbc.buf = buf;
    414   cbc.size = 2048;
    415   cbc.pos = 0;
    416   d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY,
    417                         port, NULL, NULL, &ahc_echo, NULL,
    418                         MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE,
    419                         MHD_OPTION_END);
    420   if (d == NULL)
    421     return 256;
    422   if (0 == port)
    423   {
    424     const union MHD_DaemonInfo *dinfo;
    425     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    426     if ((NULL == dinfo) || (0 == dinfo->port) )
    427     {
    428       MHD_stop_daemon (d); return 32;
    429     }
    430     port = dinfo->port;
    431   }
    432   c = curl_easy_init ();
    433   curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
    434   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    435   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    436   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    437   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    438   if (oneone)
    439     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    440   else
    441     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    442   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    443   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    444   /* NOTE: use of CONNECTTIMEOUT without also
    445      setting NOSIGNAL results in really weird
    446      crashes on my system! */
    447   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    448   curl_easy_setopt (c, CURLOPT_PROXY, "");
    449 
    450 
    451   multi = curl_multi_init ();
    452   if (multi == NULL)
    453   {
    454     curl_easy_cleanup (c);
    455     MHD_stop_daemon (d);
    456     return 512;
    457   }
    458   mret = curl_multi_add_handle (multi, c);
    459   if (mret != CURLM_OK)
    460   {
    461     curl_multi_cleanup (multi);
    462     curl_easy_cleanup (c);
    463     MHD_stop_daemon (d);
    464     return 1024;
    465   }
    466   start = time (NULL);
    467   while ((time (NULL) - start < 5) && (multi != NULL))
    468   {
    469     maxsock = MHD_INVALID_SOCKET;
    470     maxposixs = -1;
    471     FD_ZERO (&rs);
    472     FD_ZERO (&ws);
    473     FD_ZERO (&es);
    474     curl_multi_perform (multi, &running);
    475     mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
    476     if (mret != CURLM_OK)
    477     {
    478       curl_multi_remove_handle (multi, c);
    479       curl_multi_cleanup (multi);
    480       curl_easy_cleanup (c);
    481       MHD_stop_daemon (d);
    482       return 2048;
    483     }
    484     if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock))
    485     {
    486       curl_multi_remove_handle (multi, c);
    487       curl_multi_cleanup (multi);
    488       curl_easy_cleanup (c);
    489       MHD_stop_daemon (d);
    490       return 4096;
    491     }
    492     tv.tv_sec = 0;
    493     tv.tv_usec = 1000;
    494     if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
    495     {
    496 #ifdef MHD_POSIX_SOCKETS
    497       if (EINTR != errno)
    498       {
    499         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    500                  (int) errno, __LINE__);
    501         fflush (stderr);
    502         exit (99);
    503       }
    504 #else
    505       if ((WSAEINVAL != WSAGetLastError ()) ||
    506           (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
    507       {
    508         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    509                  (int) WSAGetLastError (), __LINE__);
    510         fflush (stderr);
    511         exit (99);
    512       }
    513       Sleep (1);
    514 #endif
    515     }
    516     curl_multi_perform (multi, &running);
    517     if (0 == running)
    518     {
    519       int pending;
    520       int curl_fine = 0;
    521       while (NULL != (msg = curl_multi_info_read (multi, &pending)))
    522       {
    523         if (msg->msg == CURLMSG_DONE)
    524         {
    525           if (msg->data.result == CURLE_OK)
    526             curl_fine = 1;
    527           else
    528           {
    529             fprintf (stderr,
    530                      "%s failed at %s:%d: `%s'\n",
    531                      "curl_multi_perform",
    532                      __FILE__,
    533                      __LINE__, curl_easy_strerror (msg->data.result));
    534             abort ();
    535           }
    536         }
    537       }
    538       if (! curl_fine)
    539       {
    540         fprintf (stderr, "libcurl haven't returned OK code\n");
    541         abort ();
    542       }
    543       curl_multi_remove_handle (multi, c);
    544       curl_multi_cleanup (multi);
    545       curl_easy_cleanup (c);
    546       c = NULL;
    547       multi = NULL;
    548     }
    549     MHD_run (d);
    550   }
    551   if (multi != NULL)
    552   {
    553     curl_multi_remove_handle (multi, c);
    554     curl_easy_cleanup (c);
    555     curl_multi_cleanup (multi);
    556   }
    557   MHD_stop_daemon (d);
    558   if (cbc.pos != strlen ("/hello_world"))
    559     return 8192;
    560   if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
    561     return 16384;
    562   return 0;
    563 }
    564 
    565 
    566 int
    567 main (int argc, char *const *argv)
    568 {
    569   unsigned int errorCount = 0;
    570   (void) argc;   /* Unused. Silent compiler warning. */
    571 
    572   if ((NULL == argv) || (0 == argv[0]))
    573     return 99;
    574   oneone = has_in_name (argv[0], "11");
    575   if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS))
    576   {
    577     errorCount += testInternalGet ();
    578     errorCount += testMultithreadedGet ();
    579     errorCount += testMultithreadedPoolGet ();
    580   }
    581   errorCount += testExternalGet ();
    582   if (errorCount != 0)
    583     fprintf (stderr, "Error (code: %u)\n", errorCount);
    584   curl_global_cleanup ();
    585   return (0 == errorCount) ? 0 : 1;       /* 0 == pass */
    586 }