libmicrohttpd2

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

test_postparser.c (6195B)


      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 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_postprocessor.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 thread1auto[] = {
     51     MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO),
     52     MHD_D_OPTION_WM_WORKER_THREADS (1),
     53     MHD_D_OPTION_TERMINATE ()
     54   };
     55   struct ServerType
     56   {
     57     const char *label;
     58     MHDT_ServerSetup server_setup;
     59     void *server_setup_cls;
     60     MHDT_ServerRunner server_runner;
     61     void *server_runner_cls;
     62   } configs[] = {
     63     {
     64       .label = "auto-selected mode, single threaded",
     65       .server_setup = &MHDT_server_setup_minimal,
     66       .server_setup_cls = thread1auto,
     67       .server_runner = &MHDT_server_run_minimal,
     68     },
     69     {
     70       .label = "END"
     71     }
     72   };
     73 #define MHDT_SOME_BIN_DATA "\x1\x2\x3\x4\x5"
     74   struct MHDT_PostWant simple_wants[] = {
     75     {
     76       .key = "V1",
     77       .value = "One"
     78     },
     79     {
     80       .key = "V2",
     81       .value = "Two"
     82     },
     83     {
     84       .key = NULL
     85     }
     86   };
     87   struct MHDT_PostWant mpart_wants[] = {
     88     {
     89       .key = "username",
     90       .value = "Bob"
     91     },
     92     {
     93       .key = "password",
     94       .value = "Passwo3d"
     95     },
     96     {
     97       .key = "file",
     98       .filename = "image.jpg",
     99       .content_type = "image/jpeg",
    100       .value = MHDT_SOME_BIN_DATA,
    101       .value_size = sizeof(MHDT_SOME_BIN_DATA) / sizeof(char) - 1
    102     },
    103     {
    104       .key = NULL
    105     }
    106   };
    107   struct MHDT_PostInstructions simple_pi = {
    108     .enc = MHD_HTTP_POST_ENCODING_FORM_URLENCODED,
    109     .postdata = "V1=One&V2=Two",
    110     .postheader = MHD_HTTP_HEADER_CONTENT_TYPE
    111                   ": application/x-www-form-urlencoded",
    112     .buffer_size = 32,
    113     .auto_stream_size = 16,
    114     .wants = simple_wants
    115   };
    116   struct MHDT_PostInstructions simple_mp = {
    117     .enc = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA,
    118     .postdata = "--XXXX\r\n"
    119                 "Content-Disposition: form-data; name=\"username\"\r\n"
    120                 "\r\n"
    121                 "Bob\r\n"
    122                 "--XXXX\r\n"
    123                 "Content-Disposition: form-data; name=\"password\"\r\n"
    124                 "\r\n"
    125                 "Passwo3d\r\n"
    126                 "--XXXX\r\n"
    127                 "Content-Disposition: form-data; name=\"file\"; filename=\"image.jpg\"\r\n"
    128                 "Content-Type: image/jpeg\r\n"
    129                 "\r\n"
    130                 MHDT_SOME_BIN_DATA "\r\n"
    131                 "--XXXX--\r\n",
    132     .postheader = MHD_HTTP_HEADER_CONTENT_TYPE
    133                   ": multipart/form-data; boundary=XXXX",
    134     .buffer_size = 512,
    135     .auto_stream_size = 128,
    136     .wants = mpart_wants
    137   };
    138   struct MHDT_PostInstructions simple_tp = {
    139     .enc = MHD_HTTP_POST_ENCODING_TEXT_PLAIN,
    140     .postdata = "V1=One\r\nV2=Two\r\n",
    141     .postheader = MHD_HTTP_HEADER_CONTENT_TYPE ": text/plain",
    142     .buffer_size = 32,
    143     .auto_stream_size = 16,
    144     .wants = simple_wants
    145   };
    146   struct MHDT_Phase phases[] = {
    147     {
    148       .label = "simple post",
    149       .server_cb = &MHDT_server_reply_check_post,
    150       .server_cb_cls = &simple_pi,
    151       .client_cb = &MHDT_client_do_post,
    152       .client_cb_cls = &simple_pi,
    153       .timeout_ms = 2500,
    154     },
    155     {
    156       .label = "multipart post",
    157       .server_cb = &MHDT_server_reply_check_post,
    158       .server_cb_cls = &simple_mp,
    159       .client_cb = &MHDT_client_do_post,
    160       .client_cb_cls = &simple_mp,
    161       .timeout_ms = 2500,
    162     },
    163     {
    164       .label = "plain text post",
    165       .server_cb = &MHDT_server_reply_check_post,
    166       .server_cb_cls = &simple_tp,
    167       .client_cb = &MHDT_client_do_post,
    168       .client_cb_cls = &simple_tp,
    169       .timeout_ms = 2500,
    170     },
    171     {
    172       .label = NULL,
    173     },
    174   };
    175   unsigned int i;
    176 
    177   (void) argc; /* Unused. Silence compiler warning. */
    178   (void) argv; /* Unused. Silence compiler warning. */
    179 
    180   for (i = 0; NULL != configs[i].server_setup; i++)
    181   {
    182     int ret;
    183 
    184     fprintf (stderr,
    185              "Running tests with server setup `%s'\n",
    186              configs[i].label);
    187     ret = MHDT_test (configs[i].server_setup,
    188                      configs[i].server_setup_cls,
    189                      configs[i].server_runner,
    190                      configs[i].server_runner_cls,
    191                      phases);
    192     if (0 != ret)
    193     {
    194       fprintf (stderr,
    195                "Test failed with server of type `%s' (%u)\n",
    196                configs[i].label,
    197                i);
    198       return ret;
    199     }
    200   }
    201   return 0;
    202 }