libmicrohttpd

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

timeout.c (3316B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2016-2017 Christian Grothoff,
      4      Silvio Clecio (silvioprog), Evgeny Grin (Karlson2k)
      5      Copyright (C) 2022 Evgeny Grin (Karlson2k)
      6 
      7      This library is free software; you can redistribute it and/or
      8      modify it under the terms of the GNU Lesser General Public
      9      License as published by the Free Software Foundation; either
     10      version 2.1 of the License, or (at your option) any later version.
     11 
     12      This library is distributed in the hope that it will be useful,
     13      but WITHOUT ANY WARRANTY; without even the implied warranty of
     14      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15      Lesser General Public License for more details.
     16 
     17      You should have received a copy of the GNU Lesser General Public
     18      License along with this library; if not, write to the Free Software
     19      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     20 */
     21 /**
     22  * @file timeout.c
     23  * @brief example for how to use libmicrohttpd request timeout
     24  * @author Christian Grothoff, Silvio Clecio (silvioprog), Karlson2k (Evgeny Grin)
     25  */
     26 
     27 #include <microhttpd.h>
     28 #include <stdio.h>
     29 #include <string.h>
     30 
     31 #define PORT 8080
     32 
     33 static enum MHD_Result
     34 answer_to_connection (void *cls,
     35                       struct MHD_Connection *connection,
     36                       const char *url,
     37                       const char *method,
     38                       const char *version,
     39                       const char *upload_data,
     40                       size_t *upload_data_size,
     41                       void **req_cls)
     42 {
     43   const char *page = "<html><body>Hello timeout!</body></html>";
     44   struct MHD_Response *response;
     45   enum MHD_Result ret;
     46   (void) cls;               /* Unused. Silent compiler warning. */
     47   (void) url;               /* Unused. Silent compiler warning. */
     48   (void) version;           /* Unused. Silent compiler warning. */
     49   (void) method;            /* Unused. Silent compiler warning. */
     50   (void) upload_data;       /* Unused. Silent compiler warning. */
     51   (void) upload_data_size;  /* Unused. Silent compiler warning. */
     52   (void) req_cls;           /* Unused. Silent compiler warning. */
     53 
     54   response = MHD_create_response_from_buffer_static (strlen (page),
     55                                                      (const void *) page);
     56   if (MHD_YES !=
     57       MHD_add_response_header (response,
     58                                MHD_HTTP_HEADER_CONTENT_TYPE,
     59                                "text/html"))
     60   {
     61     fprintf (stderr,
     62              "Failed to set content type header!\n");
     63   }
     64   ret = MHD_queue_response (connection,
     65                             MHD_HTTP_OK,
     66                             response);
     67   MHD_destroy_response (response);
     68   return ret;
     69 }
     70 
     71 
     72 int
     73 main (void)
     74 {
     75   struct MHD_Daemon *daemon;
     76 
     77   daemon = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
     78                              | MHD_USE_INTERNAL_POLLING_THREAD,
     79                              PORT,
     80                              NULL, NULL,
     81                              &answer_to_connection, NULL,
     82                              /* 3 seconds */
     83                              MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 3,
     84                              MHD_OPTION_END);
     85   if (NULL == daemon)
     86     return 1;
     87   (void) getchar ();
     88   MHD_stop_daemon (daemon);
     89   return 0;
     90 }