libmicrohttpd

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

dual_stack_example.c (3335B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007, 2012 Christian Grothoff (and other contributing authors)
      4      Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
      5 
      6      This library 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      This library 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      You should have received a copy of the GNU Lesser General Public
     17      License along with this library; if not, write to the Free Software
     18      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     19 */
     20 /**
     21  * @file dual_stack_example.c
     22  * @brief how to use MHD with both IPv4 and IPv6 support (dual-stack)
     23  * @author Christian Grothoff
     24  * @author Karlson2k (Evgeny Grin)
     25  */
     26 
     27 #include "platform.h"
     28 #include <microhttpd.h>
     29 
     30 #define PAGE \
     31   "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
     32 
     33 struct handler_param
     34 {
     35   const char *response_page;
     36 };
     37 
     38 static enum MHD_Result
     39 ahc_echo (void *cls,
     40           struct MHD_Connection *connection,
     41           const char *url,
     42           const char *method,
     43           const char *version,
     44           const char *upload_data, size_t *upload_data_size, void **req_cls)
     45 {
     46   static int aptr;
     47   struct handler_param *param = (struct handler_param *) cls;
     48   struct MHD_Response *response;
     49   enum MHD_Result ret;
     50   (void) url;               /* Unused. Silent compiler warning. */
     51   (void) version;           /* Unused. Silent compiler warning. */
     52   (void) upload_data;       /* Unused. Silent compiler warning. */
     53   (void) upload_data_size;  /* Unused. Silent compiler warning. */
     54 
     55   if (0 != strcmp (method, "GET"))
     56     return MHD_NO;              /* unexpected method */
     57   if (&aptr != *req_cls)
     58   {
     59     /* do never respond on first call */
     60     *req_cls = &aptr;
     61     return MHD_YES;
     62   }
     63   *req_cls = NULL;                  /* reset when done */
     64   response =
     65     MHD_create_response_from_buffer_static (strlen (param->response_page),
     66                                             param->response_page);
     67   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
     68   MHD_destroy_response (response);
     69   return ret;
     70 }
     71 
     72 
     73 int
     74 main (int argc, char *const *argv)
     75 {
     76   struct MHD_Daemon *d;
     77   struct handler_param data_for_handler;
     78   int port;
     79 
     80   if (argc != 2)
     81   {
     82     printf ("%s PORT\n", argv[0]);
     83     return 1;
     84   }
     85   port = atoi (argv[1]);
     86   if ( (1 > port) || (port > 65535) )
     87   {
     88     fprintf (stderr,
     89              "Port must be a number between 1 and 65535.\n");
     90     return 1;
     91   }
     92   data_for_handler.response_page = PAGE;
     93   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
     94                         | MHD_USE_DUAL_STACK,
     95                         (uint16_t) port,
     96                         NULL, NULL, &ahc_echo, &data_for_handler,
     97                         MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
     98                         MHD_OPTION_END);
     99   (void) getc (stdin);
    100   MHD_stop_daemon (d);
    101   return 0;
    102 }