libmicrohttpd2

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

test_client_server.c (9898B)


      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 (1),
     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 (1),
     78     MHD_D_OPTION_TERMINATE ()
     79   };
     80   struct MHD_DaemonOptionAndValue thread1auto[] = {
     81     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
     82     MHD_D_OPTION_WM_WORKER_THREADS (1),
     83     MHD_D_OPTION_TERMINATE ()
     84   };
     85   struct MHD_DaemonOptionAndValue external0auto[] = {
     86     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
     87     MHD_D_OPTION_WM_EXTERNAL_PERIODIC (),
     88     MHD_D_OPTION_TERMINATE ()
     89   };
     90   struct ServerType
     91   {
     92     const char *label;
     93     MHDT_ServerSetup server_setup;
     94     void *server_setup_cls;
     95     MHDT_ServerRunner server_runner;
     96     void *server_runner_cls;
     97   } configs[] = {
     98 #ifdef MHD_SUPPORT_SELECT
     99     {
    100       .label = "single threaded select",
    101       .server_setup = &MHDT_server_setup_minimal,
    102       .server_setup_cls = thread1select,
    103       .server_runner = &MHDT_server_run_minimal,
    104     },
    105     {
    106       .label = "multi-threaded select",
    107       .server_setup = &MHDT_server_setup_minimal,
    108       .server_setup_cls = thread2select,
    109       .server_runner = &MHDT_server_run_minimal,
    110     },
    111 #endif
    112 #ifdef MHD_SUPPORT_POLL
    113     {
    114       .label = "single threaded poll",
    115       .server_setup = &MHDT_server_setup_minimal,
    116       .server_setup_cls = thread1poll,
    117       .server_runner = &MHDT_server_run_minimal,
    118     },
    119     {
    120       .label = "multi-threaded poll",
    121       .server_setup = &MHDT_server_setup_minimal,
    122       .server_setup_cls = thread2poll,
    123       .server_runner = &MHDT_server_run_minimal,
    124     },
    125 #endif
    126 #if MHD_SUPPORT_EPOLL
    127     {
    128       .label = "single threaded epoll",
    129       .server_setup = &MHDT_server_setup_minimal,
    130       .server_setup_cls = thread1epoll,
    131       .server_runner = &MHDT_server_run_minimal,
    132     },
    133     {
    134       .label = "multi-threaded epoll",
    135       .server_setup = &MHDT_server_setup_minimal,
    136       .server_setup_cls = thread2epoll,
    137       .server_runner = &MHDT_server_run_minimal,
    138     },
    139 #endif
    140     {
    141       .label = "auto-selected mode, single threaded",
    142       .server_setup = &MHDT_server_setup_minimal,
    143       .server_setup_cls = thread1auto,
    144       .server_runner = &MHDT_server_run_minimal,
    145     },
    146 #ifdef MHD_SUPPORT_EPOLL
    147     {
    148       .label = "external events loop mode, no internal threads",
    149       .server_setup = &MHDT_server_setup_external,
    150       .server_setup_cls = NULL,
    151       .server_runner = &MHDT_server_run_external,
    152     },
    153 #endif /* MHD_SUPPORT_EPOLL */
    154 #if 1
    155     /* FIXME: remove once MHD_daemon_process_blocking
    156        has been implemented */
    157     {
    158       .label = "END"
    159     },
    160 #endif
    161     {
    162       .label = "auto-selected external event loop mode, no threads",
    163       .server_setup = &MHDT_server_setup_minimal,
    164       .server_setup_cls = external0auto,
    165       .server_runner = &MHDT_server_run_blocking,
    166     },
    167     {
    168       .label = "END"
    169     }
    170   };
    171   struct MHDT_Phase phases[] = {
    172     {
    173       .label = "simple get",
    174       .server_cb = &MHDT_server_reply_text,
    175       .server_cb_cls = (void *) "Hello world",
    176       .client_cb = &MHDT_client_get_root,
    177       .client_cb_cls = "Hello world",
    178       .timeout_ms = 2500,
    179     },
    180     {
    181       .label = "GET with sendfile",
    182       .server_cb = &MHDT_server_reply_file,
    183       .server_cb_cls = (void *) "Hello world",
    184       .client_cb = &MHDT_client_get_root,
    185       .client_cb_cls = "Hello world",
    186       .timeout_ms = 2500,
    187     },
    188     {
    189       .label = "client PUT with content-length",
    190       .server_cb = &MHDT_server_reply_check_upload,
    191       .server_cb_cls = (void *) "simple-upload-value",
    192       .client_cb = &MHDT_client_put_data,
    193       .client_cb_cls = "simple-upload-value",
    194       .timeout_ms = 2500,
    195     },
    196     {
    197       .label = "client PUT with 2 chunks",
    198       .server_cb = &MHDT_server_reply_check_upload,
    199       .server_cb_cls = (void *) "chunky-upload-value",
    200       .client_cb = &MHDT_client_chunk_data,
    201       .client_cb_cls = "chunky-upload-value",
    202       .timeout_ms = 2500,
    203     },
    204     {
    205       .label = "client request with custom header",
    206       .server_cb = &MHDT_server_reply_check_header,
    207       .server_cb_cls = (void *) "C-Header:testvalue",
    208       .client_cb = &MHDT_client_set_header,
    209       .client_cb_cls = "C-Header:testvalue",
    210       .timeout_ms = 2500,
    211     },
    212     {
    213       .label = "server response with custom header",
    214       .server_cb = &MHDT_server_reply_with_header,
    215       .server_cb_cls = (void *) "X-Header:testvalue",
    216       .client_cb = &MHDT_client_expect_header,
    217       .client_cb_cls = "X-Header:testvalue",
    218       .timeout_ms = 2500,
    219     },
    220     {
    221       .label = "URL with query parameters 1",
    222       .server_cb = &MHDT_server_reply_check_query,
    223       .server_cb_cls = (void *) "a=b&c",
    224       .client_cb = &MHDT_client_get_with_query,
    225       .client_cb_cls = "?a=b&c",
    226       .timeout_ms = 5000,
    227       .num_clients = 4
    228     },
    229     {
    230       .label = "URL with query parameters 2",
    231       .server_cb = &MHDT_server_reply_check_query,
    232       .server_cb_cls = (void *) "a=b&c", /* a => b, c => NULL */
    233       .client_cb = &MHDT_client_get_with_query,
    234       .client_cb_cls = "?c&a=b",
    235       .timeout_ms = 5000,
    236       .num_clients = 1
    237     },
    238     {
    239       .label = "URL with query parameters 3",
    240       .server_cb = &MHDT_server_reply_check_query,
    241       .server_cb_cls = (void *) "a=&c", /* a => "", c => NULL */
    242       .client_cb = &MHDT_client_get_with_query,
    243       .client_cb_cls = "?c&a=",
    244       .timeout_ms = 5000,
    245       .num_clients = 1
    246     },
    247     {
    248       .label = "URL with query parameters 4",
    249       .server_cb = &MHDT_server_reply_check_query,
    250       .server_cb_cls = (void *) "a=", /* a => "" */
    251       .client_cb = &MHDT_client_get_with_query,
    252       .client_cb_cls = "?a=",
    253       .timeout_ms = 5000,
    254       .num_clients = 1
    255     },
    256     {
    257       .label = "URL with query parameters 5",
    258       .server_cb = &MHDT_server_reply_check_query,
    259       .server_cb_cls = (void *) "a=b", /* a => "b" */
    260       .client_cb = &MHDT_client_get_with_query,
    261       .client_cb_cls = "?a=b",
    262       .timeout_ms = 5000,
    263       .num_clients = 1
    264     },
    265     {
    266       .label = "chunked response get",
    267       .server_cb = &MHDT_server_reply_chunked_text,
    268       .server_cb_cls = (void *) "Hello world",
    269       .client_cb = &MHDT_client_get_root,
    270       .client_cb_cls = "Hello world",
    271       .timeout_ms = 2500,
    272     },
    273     // TODO: chunked download
    274     {
    275       .label = NULL,
    276     },
    277   };
    278   unsigned int i;
    279 
    280   (void) argc; /* Unused. Silence compiler warning. */
    281   (void) argv; /* Unused. Silence compiler warning. */
    282 
    283   for (i = 0; NULL != configs[i].server_setup; i++)
    284   {
    285     int ret;
    286 
    287     fprintf (stderr,
    288              "Running tests with server setup '%s'\n",
    289              configs[i].label);
    290     ret = MHDT_test (configs[i].server_setup,
    291                      configs[i].server_setup_cls,
    292                      configs[i].server_runner,
    293                      configs[i].server_runner_cls,
    294                      phases);
    295     if (0 != ret)
    296     {
    297       fprintf (stderr,
    298                "Test failed with server of type '%s' (%u)\n",
    299                configs[i].label,
    300                i);
    301       return ret;
    302     }
    303   }
    304   return 0;
    305 }