libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

test_client_server.c (11030B)


      1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */
      2 /*
      3   This file is part of GNU libmicrohttpd.
      4   Copyright (C) 2016, 2024, 2025 Christian Grothoff & Evgeny Grin (Karlson2k)
      5 
      6   GNU libmicrohttpd is free software; you can redistribute it and/or
      7   modify it under the terms of the GNU Lesser General Public
      8   License as published by the Free Software Foundation; either
      9   version 2.1 of the License, or (at your option) any later version.
     10 
     11   GNU libmicrohttpd is distributed in the hope that it will be useful,
     12   but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14   Lesser General Public License for more details.
     15 
     16   Alternatively, you can redistribute GNU libmicrohttpd and/or
     17   modify it under the terms of the GNU General Public License as
     18   published by the Free Software Foundation; either version 2 of
     19   the License, or (at your option) any later version, together
     20   with the eCos exception, as follows:
     21 
     22     As a special exception, if other files instantiate templates or
     23     use macros or inline functions from this file, or you compile this
     24     file and link it with other works to produce a work based on this
     25     file, this file does not by itself cause the resulting work to be
     26     covered by the GNU General Public License. However the source code
     27     for this file must still be made available in accordance with
     28     section (3) of the GNU General Public License v2.
     29 
     30     This exception does not invalidate any other reasons why a work
     31     based on this file might be covered by the GNU General Public
     32     License.
     33 
     34   You should have received copies of the GNU Lesser General Public
     35   License and the GNU General Public License along with this library;
     36   if not, see <https://www.gnu.org/licenses/>.
     37 */
     38 
     39 /**
     40  * @file test_client_server.c
     41  * @brief  test with client against server
     42  * @author Christian Grothoff
     43  */
     44 #include "libtest.h"
     45 
     46 
     47 int
     48 main (int argc, char *argv[])
     49 {
     50   struct MHD_DaemonOptionAndValue thread1select[] = {
     51     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_SELECT),
     52     MHD_D_OPTION_WM_WORKER_THREADS (1),
     53     MHD_D_OPTION_TERMINATE ()
     54   };
     55   struct MHD_DaemonOptionAndValue thread2select[] = {
     56     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_SELECT),
     57     MHD_D_OPTION_WM_WORKER_THREADS (2),
     58     MHD_D_OPTION_TERMINATE ()
     59   };
     60   struct MHD_DaemonOptionAndValue thread1poll[] = {
     61     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_POLL),
     62     MHD_D_OPTION_WM_WORKER_THREADS (1),
     63     MHD_D_OPTION_TERMINATE ()
     64   };
     65   struct MHD_DaemonOptionAndValue thread2poll[] = {
     66     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_POLL),
     67     MHD_D_OPTION_WM_WORKER_THREADS (2),
     68     MHD_D_OPTION_TERMINATE ()
     69   };
     70   struct MHD_DaemonOptionAndValue thread1epoll[] = {
     71     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_EPOLL),
     72     MHD_D_OPTION_WM_WORKER_THREADS (1),
     73     MHD_D_OPTION_TERMINATE ()
     74   };
     75   struct MHD_DaemonOptionAndValue thread2epoll[] = {
     76     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_EPOLL),
     77     MHD_D_OPTION_WM_WORKER_THREADS (2),
     78     MHD_D_OPTION_TERMINATE ()
     79   };
     80   struct MHD_DaemonOptionAndValue thread1kq[] = {
     81     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_KQUEUE),
     82     MHD_D_OPTION_WM_WORKER_THREADS (1),
     83     MHD_D_OPTION_TERMINATE ()
     84   };
     85   struct MHD_DaemonOptionAndValue thread2kq[] = {
     86     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_KQUEUE),
     87     MHD_D_OPTION_WM_WORKER_THREADS (2),
     88     MHD_D_OPTION_TERMINATE ()
     89   };
     90   struct MHD_DaemonOptionAndValue thread1auto[] = {
     91     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
     92     MHD_D_OPTION_WM_WORKER_THREADS (1),
     93     MHD_D_OPTION_TERMINATE ()
     94   };
     95   struct MHD_DaemonOptionAndValue thread2auto[] = {
     96     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
     97     MHD_D_OPTION_WM_WORKER_THREADS (2),
     98     MHD_D_OPTION_TERMINATE ()
     99   };
    100   struct MHD_DaemonOptionAndValue external0auto[] = {
    101     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
    102     MHD_D_OPTION_WM_EXTERNAL_PERIODIC (),
    103     MHD_D_OPTION_TERMINATE ()
    104   };
    105   struct ServerType
    106   {
    107     const char *label;
    108     MHDT_ServerSetup server_setup;
    109     void *server_setup_cls;
    110     MHDT_ServerRunner server_runner;
    111     void *server_runner_cls;
    112   } configs[] = {
    113 #ifdef MHD_SUPPORT_SELECT
    114     {
    115       .label = "single threaded select",
    116       .server_setup = &MHDT_server_setup_minimal,
    117       .server_setup_cls = thread1select,
    118       .server_runner = &MHDT_server_run_minimal,
    119     },
    120     {
    121       .label = "multi-threaded select",
    122       .server_setup = &MHDT_server_setup_minimal,
    123       .server_setup_cls = thread2select,
    124       .server_runner = &MHDT_server_run_minimal,
    125     },
    126 #endif
    127 #ifdef MHD_SUPPORT_POLL
    128     {
    129       .label = "single threaded poll",
    130       .server_setup = &MHDT_server_setup_minimal,
    131       .server_setup_cls = thread1poll,
    132       .server_runner = &MHDT_server_run_minimal,
    133     },
    134     {
    135       .label = "multi-threaded poll",
    136       .server_setup = &MHDT_server_setup_minimal,
    137       .server_setup_cls = thread2poll,
    138       .server_runner = &MHDT_server_run_minimal,
    139     },
    140 #endif
    141 #if MHD_SUPPORT_EPOLL
    142     {
    143       .label = "single threaded epoll",
    144       .server_setup = &MHDT_server_setup_minimal,
    145       .server_setup_cls = thread1epoll,
    146       .server_runner = &MHDT_server_run_minimal,
    147     },
    148     {
    149       .label = "multi-threaded epoll",
    150       .server_setup = &MHDT_server_setup_minimal,
    151       .server_setup_cls = thread2epoll,
    152       .server_runner = &MHDT_server_run_minimal,
    153     },
    154 #endif
    155 #if MHD_SUPPORT_KQUEUE
    156     {
    157       .label = "single threaded kqueue",
    158       .server_setup = &MHDT_server_setup_minimal,
    159       .server_setup_cls = thread1kq,
    160       .server_runner = &MHDT_server_run_minimal,
    161     },
    162     {
    163       .label = "multi-threaded kqueue",
    164       .server_setup = &MHDT_server_setup_minimal,
    165       .server_setup_cls = thread2kq,
    166       .server_runner = &MHDT_server_run_minimal,
    167     },
    168 #endif
    169     {
    170       .label = "auto-selected mode, single threaded",
    171       .server_setup = &MHDT_server_setup_minimal,
    172       .server_setup_cls = thread1auto,
    173       .server_runner = &MHDT_server_run_minimal,
    174     },
    175     {
    176       .label = "auto-selected mode, multi-threaded",
    177       .server_setup = &MHDT_server_setup_minimal,
    178       .server_setup_cls = thread1auto,
    179       .server_runner = &MHDT_server_run_minimal,
    180     },
    181 #ifdef MHD_SUPPORT_EPOLL
    182     {
    183       .label = "external events loop mode, no internal threads",
    184       .server_setup = &MHDT_server_setup_external,
    185       .server_setup_cls = NULL,
    186       .server_runner = &MHDT_server_run_external,
    187     },
    188 #endif /* MHD_SUPPORT_EPOLL */
    189 #if 1
    190     /* FIXME: remove once MHD_daemon_process_blocking
    191        has been implemented */
    192     {
    193       .label = "END"
    194     },
    195 #endif
    196     {
    197       .label = "auto-selected external event loop mode, no threads",
    198       .server_setup = &MHDT_server_setup_minimal,
    199       .server_setup_cls = external0auto,
    200       .server_runner = &MHDT_server_run_blocking,
    201     },
    202     {
    203       .label = "END"
    204     }
    205   };
    206   struct MHDT_Phase phases[] = {
    207     {
    208       .label = "simple get",
    209       .server_cb = &MHDT_server_reply_text,
    210       .server_cb_cls = (void *) "Hello world",
    211       .client_cb = &MHDT_client_get_root,
    212       .client_cb_cls = "Hello world",
    213       .timeout_ms = 2500,
    214     },
    215     {
    216       .label = "GET with sendfile",
    217       .server_cb = &MHDT_server_reply_file,
    218       .server_cb_cls = (void *) "Hello world",
    219       .client_cb = &MHDT_client_get_root,
    220       .client_cb_cls = "Hello world",
    221       .timeout_ms = 2500,
    222     },
    223     {
    224       .label = "client PUT with content-length",
    225       .server_cb = &MHDT_server_reply_check_upload,
    226       .server_cb_cls = (void *) "simple-upload-value",
    227       .client_cb = &MHDT_client_put_data,
    228       .client_cb_cls = "simple-upload-value",
    229       .timeout_ms = 2500,
    230     },
    231     {
    232       .label = "client PUT with 2 chunks",
    233       .server_cb = &MHDT_server_reply_check_upload,
    234       .server_cb_cls = (void *) "chunky-upload-value",
    235       .client_cb = &MHDT_client_chunk_data,
    236       .client_cb_cls = "chunky-upload-value",
    237       .timeout_ms = 2500,
    238     },
    239     {
    240       .label = "client request with custom header",
    241       .server_cb = &MHDT_server_reply_check_header,
    242       .server_cb_cls = (void *) "C-Header:testvalue",
    243       .client_cb = &MHDT_client_set_header,
    244       .client_cb_cls = "C-Header:testvalue",
    245       .timeout_ms = 2500,
    246     },
    247     {
    248       .label = "server response with custom header",
    249       .server_cb = &MHDT_server_reply_with_header,
    250       .server_cb_cls = (void *) "X-Header:testvalue",
    251       .client_cb = &MHDT_client_expect_header,
    252       .client_cb_cls = "X-Header:testvalue",
    253       .timeout_ms = 2500,
    254     },
    255     {
    256       .label = "URL with query parameters 1",
    257       .server_cb = &MHDT_server_reply_check_query,
    258       .server_cb_cls = (void *) "a=b&c",
    259       .client_cb = &MHDT_client_get_with_query,
    260       .client_cb_cls = "?a=b&c",
    261       .timeout_ms = 5000,
    262       .num_clients = 4
    263     },
    264     {
    265       .label = "URL with query parameters 2",
    266       .server_cb = &MHDT_server_reply_check_query,
    267       .server_cb_cls = (void *) "a=b&c", /* a => b, c => NULL */
    268       .client_cb = &MHDT_client_get_with_query,
    269       .client_cb_cls = "?c&a=b",
    270       .timeout_ms = 5000,
    271       .num_clients = 1
    272     },
    273     {
    274       .label = "URL with query parameters 3",
    275       .server_cb = &MHDT_server_reply_check_query,
    276       .server_cb_cls = (void *) "a=&c", /* a => "", c => NULL */
    277       .client_cb = &MHDT_client_get_with_query,
    278       .client_cb_cls = "?c&a=",
    279       .timeout_ms = 5000,
    280       .num_clients = 1
    281     },
    282     {
    283       .label = "URL with query parameters 4",
    284       .server_cb = &MHDT_server_reply_check_query,
    285       .server_cb_cls = (void *) "a=", /* a => "" */
    286       .client_cb = &MHDT_client_get_with_query,
    287       .client_cb_cls = "?a=",
    288       .timeout_ms = 5000,
    289       .num_clients = 1
    290     },
    291     {
    292       .label = "URL with query parameters 5",
    293       .server_cb = &MHDT_server_reply_check_query,
    294       .server_cb_cls = (void *) "a=b", /* a => "b" */
    295       .client_cb = &MHDT_client_get_with_query,
    296       .client_cb_cls = "?a=b",
    297       .timeout_ms = 5000,
    298       .num_clients = 1
    299     },
    300     {
    301       .label = "chunked response get",
    302       .server_cb = &MHDT_server_reply_chunked_text,
    303       .server_cb_cls = (void *) "Hello world",
    304       .client_cb = &MHDT_client_get_root,
    305       .client_cb_cls = "Hello world",
    306       .timeout_ms = 2500,
    307     },
    308     // TODO: chunked download
    309     {
    310       .label = NULL,
    311     },
    312   };
    313   unsigned int i;
    314 
    315   (void) argc; /* Unused. Silence compiler warning. */
    316   (void) argv; /* Unused. Silence compiler warning. */
    317 
    318   for (i = 0; NULL != configs[i].server_setup; i++)
    319   {
    320     int ret;
    321 
    322     fprintf (stderr,
    323              "Running tests with server setup '%s'\n",
    324              configs[i].label);
    325     ret = MHDT_test (configs[i].server_setup,
    326                      configs[i].server_setup_cls,
    327                      configs[i].server_runner,
    328                      configs[i].server_runner_cls,
    329                      phases);
    330     if (0 != ret)
    331     {
    332       fprintf (stderr,
    333                "Test failed with server of type '%s' (%u)\n",
    334                configs[i].label,
    335                i);
    336       return ret;
    337     }
    338   }
    339   return 0;
    340 }