libmicrohttpd

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

test_process_arguments.c (8950B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007, 2013 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 3, 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_arguments.c
     24  * @brief  Testcase for HTTP URI arguments
     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 static int oneone;
     48 
     49 struct CBC
     50 {
     51   char *buf;
     52   size_t pos;
     53   size_t size;
     54 };
     55 
     56 
     57 static size_t
     58 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
     59 {
     60   struct CBC *cbc = ctx;
     61 
     62   if (cbc->pos + size * nmemb > cbc->size)
     63     return 0;                   /* overflow */
     64   memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
     65   cbc->pos += size * nmemb;
     66   return size * nmemb;
     67 }
     68 
     69 
     70 static enum MHD_Result
     71 ahc_echo (void *cls,
     72           struct MHD_Connection *connection,
     73           const char *url,
     74           const char *method,
     75           const char *version,
     76           const char *upload_data, size_t *upload_data_size,
     77           void **req_cls)
     78 {
     79   static int ptr;
     80   struct MHD_Response *response;
     81   enum MHD_Result ret;
     82   const char *hdr;
     83   (void) cls;
     84   (void) version; (void) upload_data; (void) upload_data_size;       /* Unused. Silent compiler warning. */
     85 
     86   if (0 != strcmp (MHD_HTTP_METHOD_GET, method))
     87     return MHD_NO;              /* unexpected method */
     88   if (&ptr != *req_cls)
     89   {
     90     *req_cls = &ptr;
     91     return MHD_YES;
     92   }
     93   *req_cls = NULL;
     94   hdr = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "k");
     95   if ((hdr == NULL) || (0 != strcmp (hdr, "v x")))
     96     abort ();
     97   hdr = MHD_lookup_connection_value (connection,
     98                                      MHD_GET_ARGUMENT_KIND, "hash");
     99   if ((hdr == NULL) || (0 != strcmp (hdr, "#foo")))
    100     abort ();
    101   hdr = MHD_lookup_connection_value (connection,
    102                                      MHD_GET_ARGUMENT_KIND, "space");
    103   if ((hdr == NULL) || (0 != strcmp (hdr, "\240bar")))
    104     abort ();
    105   if (3 != MHD_get_connection_values (connection,
    106                                       MHD_GET_ARGUMENT_KIND,
    107                                       NULL, NULL))
    108     abort ();
    109   response = MHD_create_response_from_buffer_copy (strlen (url),
    110                                                    (const void *) url);
    111   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
    112   MHD_destroy_response (response);
    113   if (ret == MHD_NO)
    114     abort ();
    115   return ret;
    116 }
    117 
    118 
    119 static unsigned int
    120 testExternalGet (void)
    121 {
    122   struct MHD_Daemon *d;
    123   CURL *c;
    124   char buf[2048];
    125   struct CBC cbc;
    126   CURLM *multi;
    127   CURLMcode mret;
    128   fd_set rs;
    129   fd_set ws;
    130   fd_set es;
    131   MHD_socket maxsock;
    132 #ifdef MHD_WINSOCK_SOCKETS
    133   int maxposixs; /* Max socket number unused on W32 */
    134 #else  /* MHD_POSIX_SOCKETS */
    135 #define maxposixs maxsock
    136 #endif /* MHD_POSIX_SOCKETS */
    137   int running;
    138   struct CURLMsg *msg;
    139   time_t start;
    140   struct timeval tv;
    141   uint16_t port;
    142 
    143   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    144     port = 0;
    145   else
    146   {
    147     port = 1410;
    148     if (oneone)
    149       port += 5;
    150   }
    151 
    152   multi = NULL;
    153   cbc.buf = buf;
    154   cbc.size = 2048;
    155   cbc.pos = 0;
    156   d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_NO_THREAD_SAFETY,
    157                         port, NULL, NULL, &ahc_echo, NULL,
    158                         MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE,
    159                         MHD_OPTION_END);
    160   if (d == NULL)
    161     return 256;
    162   if (0 == port)
    163   {
    164     const union MHD_DaemonInfo *dinfo;
    165     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    166     if ((NULL == dinfo) || (0 == dinfo->port) )
    167     {
    168       MHD_stop_daemon (d); return 32;
    169     }
    170     port = dinfo->port;
    171   }
    172   c = curl_easy_init ();
    173   curl_easy_setopt (c, CURLOPT_URL,
    174                     "http://127.0.0.1/hello+world?k=v+x&hash=%23foo&space=%A0bar");
    175   curl_easy_setopt (c, CURLOPT_PORT, (long) port);
    176   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    177   curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
    178   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    179   if (oneone)
    180     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    181   else
    182     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    183   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    184   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    185   /* NOTE: use of CONNECTTIMEOUT without also
    186      setting NOSIGNAL results in really weird
    187      crashes on my system! */
    188   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    189   curl_easy_setopt (c, CURLOPT_PROXY, "");
    190 
    191 
    192   multi = curl_multi_init ();
    193   if (multi == NULL)
    194   {
    195     curl_easy_cleanup (c);
    196     MHD_stop_daemon (d);
    197     return 512;
    198   }
    199   mret = curl_multi_add_handle (multi, c);
    200   if (mret != CURLM_OK)
    201   {
    202     curl_multi_cleanup (multi);
    203     curl_easy_cleanup (c);
    204     MHD_stop_daemon (d);
    205     return 1024;
    206   }
    207   start = time (NULL);
    208   while ((time (NULL) - start < 5) && (multi != NULL))
    209   {
    210     maxsock = MHD_INVALID_SOCKET;
    211     maxposixs = -1;
    212     FD_ZERO (&rs);
    213     FD_ZERO (&ws);
    214     FD_ZERO (&es);
    215     curl_multi_perform (multi, &running);
    216     mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
    217     if (mret != CURLM_OK)
    218     {
    219       curl_multi_remove_handle (multi, c);
    220       curl_multi_cleanup (multi);
    221       curl_easy_cleanup (c);
    222       MHD_stop_daemon (d);
    223       return 2048;
    224     }
    225     if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock))
    226     {
    227       curl_multi_remove_handle (multi, c);
    228       curl_multi_cleanup (multi);
    229       curl_easy_cleanup (c);
    230       MHD_stop_daemon (d);
    231       return 4096;
    232     }
    233     tv.tv_sec = 0;
    234     tv.tv_usec = 1000;
    235     if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
    236     {
    237 #ifdef MHD_POSIX_SOCKETS
    238       if (EINTR != errno)
    239       {
    240         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    241                  (int) errno, __LINE__);
    242         fflush (stderr);
    243         exit (99);
    244       }
    245 #else
    246       if ((WSAEINVAL != WSAGetLastError ()) ||
    247           (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
    248       {
    249         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    250                  (int) WSAGetLastError (), __LINE__);
    251         fflush (stderr);
    252         exit (99);
    253       }
    254       Sleep (1);
    255 #endif
    256     }
    257     curl_multi_perform (multi, &running);
    258     if (0 == running)
    259     {
    260       int pending;
    261       int curl_fine = 0;
    262       while (NULL != (msg = curl_multi_info_read (multi, &pending)))
    263       {
    264         if (msg->msg == CURLMSG_DONE)
    265         {
    266           if (msg->data.result == CURLE_OK)
    267             curl_fine = 1;
    268           else
    269           {
    270             fprintf (stderr,
    271                      "%s failed at %s:%d: `%s'\n",
    272                      "curl_multi_perform",
    273                      __FILE__,
    274                      __LINE__, curl_easy_strerror (msg->data.result));
    275             abort ();
    276           }
    277         }
    278       }
    279       if (! curl_fine)
    280       {
    281         fprintf (stderr, "libcurl haven't returned OK code\n");
    282         abort ();
    283       }
    284       curl_multi_remove_handle (multi, c);
    285       curl_multi_cleanup (multi);
    286       curl_easy_cleanup (c);
    287       c = NULL;
    288       multi = NULL;
    289     }
    290     MHD_run (d);
    291   }
    292   if (multi != NULL)
    293   {
    294     curl_multi_remove_handle (multi, c);
    295     curl_easy_cleanup (c);
    296     curl_multi_cleanup (multi);
    297   }
    298   MHD_stop_daemon (d);
    299   if (cbc.pos != strlen ("/hello+world"))
    300     return 8192;
    301   if (0 != strncmp ("/hello+world", cbc.buf, strlen ("/hello+world")))
    302     return 16384;
    303   return 0;
    304 }
    305 
    306 
    307 int
    308 main (int argc, char *const *argv)
    309 {
    310   unsigned int errorCount = 0;
    311   (void) argc;   /* Unused. Silent compiler warning. */
    312 
    313   if ((NULL == argv) || (0 == argv[0]))
    314     return 99;
    315   oneone = has_in_name (argv[0], "11");
    316   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
    317     return 2;
    318   errorCount += testExternalGet ();
    319   if (errorCount != 0)
    320     fprintf (stderr, "Error (code: %u)\n", errorCount);
    321   curl_global_cleanup ();
    322   return (0 == errorCount) ? 0 : 1;       /* 0 == pass */
    323 }