twister

HTTP fault injector for testing
Log | Files | Refs | README | LICENSE

test_twister_webserver.c (2875B)


      1 /*
      2   This file is part of Taler.
      3   Copyright (C) 2009, 2010, 2011, 2016 GNUnet e.V.
      4   Copyright (C) 2018 Taler Systems SA
      5 
      6   Taler is free software; you can redistribute it and/or modify
      7   it under the terms of the GNU General Public License as
      8   published by the Free Software Foundation; either version 3,
      9   or (at your option) any later version.
     10 
     11   Taler 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
     14   GNU General Public License for more details.
     15 
     16   You should have received a copy of the GNU General Public
     17   License along with Taler; see the file COPYING.  If not,
     18   write to the Free Software Foundation, Inc., 51 Franklin
     19   Street, Fifth Floor, Boston, MA 02110-1301, USA.
     20 */
     21 
     22 /**
     23  * This code is in the public domain.
     24  */
     25 #include <limits.h>
     26 #include <sys/types.h>
     27 #ifndef _WIN32
     28 #include <sys/select.h>
     29 #include <sys/socket.h>
     30 #else
     31 #include <winsock2.h>
     32 #endif
     33 #include <string.h>
     34 #include <microhttpd.h>
     35 #include <stdio.h>
     36 
     37 #define PORT 8080
     38 
     39 static enum MHD_Result
     40 answer_to_connection
     41   (void *cls, struct MHD_Connection *connection,
     42   const char *url, const char *method,
     43   const char *version, const char *upload_data,
     44   size_t *upload_data_size, void **con_cls)
     45 {
     46 #if 0
     47   const char *page = "<html><body>Hello, browser!</body></html>";
     48 #endif
     49   const char *page = "{\"hello\": [{\"this\": \"browser!\"}]," \
     50                      "\"when\": \"today\"}";
     51   struct MHD_Response *response;
     52   int ret;
     53 
     54   (void) cls;               /* Unused. Silent compiler warning. */
     55   (void) url;               /* Unused. Silent compiler warning. */
     56   (void) method;            /* Unused. Silent compiler warning. */
     57   (void) version;           /* Unused. Silent compiler warning. */
     58   (void) upload_data;       /* Unused. Silent compiler warning. */
     59 
     60   if (NULL == (*con_cls))
     61   {
     62     *con_cls = (void *) "first";
     63     return MHD_YES;
     64   }
     65   if (0 != *upload_data_size)
     66   {
     67     *upload_data_size = 0;
     68     return MHD_YES;
     69   }
     70   response = MHD_create_response_from_buffer
     71                (strlen (page),
     72                (void *) page,
     73                MHD_RESPMEM_PERSISTENT);
     74   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
     75   MHD_destroy_response (response);
     76 
     77   return ret;
     78 }
     79 
     80 
     81 /**
     82  * This minimalist Web server listens on port PORT and responds
     83  * with a HTTP status 200 OK and some static "hello world" body,
     84  * _always_.
     85  */
     86 int
     87 main (void)
     88 {
     89   struct MHD_Daemon *daemon;
     90 
     91   daemon = MHD_start_daemon (MHD_USE_AUTO
     92                              | MHD_USE_INTERNAL_POLLING_THREAD,
     93                              PORT, NULL, NULL,
     94                              &answer_to_connection, NULL,
     95                              MHD_OPTION_END);
     96   if (NULL == daemon)
     97     return 1;
     98   sleep (UINT_MAX);
     99 
    100   MHD_stop_daemon (daemon);
    101   return 0;
    102 }