libmicrohttpd

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

connection_close.c (4047B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007 Christian Grothoff (and other contributing authors)
      4 
      5      This library is free software; you can redistribute it and/or
      6      modify it under the terms of the GNU Lesser General Public
      7      License as published by the Free Software Foundation; either
      8      version 2.1 of the License, or (at your option) any later version.
      9 
     10      This library is distributed in the hope that it will be useful,
     11      but WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      Lesser General Public License for more details.
     14 
     15      You should have received a copy of the GNU Lesser General Public
     16      License along with this library; if not, write to the Free Software
     17      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     18 */
     19 /**
     20  * @file connection_close.c
     21  * @brief minimal example for connection close notifications
     22  * @author Christian Grothoff
     23  */
     24 
     25 #include "platform.h"
     26 #include <microhttpd.h>
     27 
     28 #ifdef HAVE_INTTYPES_H
     29 #include <inttypes.h>
     30 #endif /* HAVE_INTTYPES_H */
     31 #ifndef PRIu64
     32 #define PRIu64  "llu"
     33 #endif /* ! PRIu64 */
     34 
     35 #define PAGE \
     36   "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
     37 
     38 static int
     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   const char *me = cls;
     48   struct MHD_Response *response;
     49   int 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 = MHD_create_response_from_buffer_static (strlen (me),
     65                                                      me);
     66   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
     67   MHD_destroy_response (response);
     68   return ret;
     69 }
     70 
     71 
     72 #include <x86intrin.h>
     73 
     74 static void
     75 request_completed (void *cls,
     76                    struct MHD_Connection *connection,
     77                    void **req_cls,
     78                    enum MHD_RequestTerminationCode toe)
     79 {
     80   fprintf (stderr,
     81            "%" PRIu64 " - RC: %d\n",
     82            (uint64_t) __rdtsc (),
     83            toe);
     84 }
     85 
     86 
     87 static void
     88 connection_completed (void *cls,
     89                       struct MHD_Connection *connection,
     90                       void **socket_context,
     91                       enum MHD_ConnectionNotificationCode toe)
     92 {
     93   fprintf (stderr,
     94            "%" PRIu64 " - CC: %d\n",
     95            (uint64_t) __rdtsc (),
     96            toe);
     97 }
     98 
     99 
    100 int
    101 main (int argc, char *const *argv)
    102 {
    103   struct MHD_Daemon *d;
    104 
    105   if (argc != 2)
    106   {
    107     printf ("%s PORT\n", argv[0]);
    108     return 1;
    109   }
    110   d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
    111     /* MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
    112     /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
    113     MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD
    114     | MHD_USE_ERROR_LOG | MHD_USE_POLL, /* | MHD_USE_ITC, */
    115     /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
    116     atoi (argv[1]),
    117     NULL, NULL, &ahc_echo, PAGE,
    118     MHD_OPTION_NOTIFY_COMPLETED, &request_completed, NULL,
    119     MHD_OPTION_NOTIFY_CONNECTION, &connection_completed, NULL,
    120     MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
    121     MHD_OPTION_CLIENT_DISCIPLINE_LVL, (int) 1,
    122     MHD_OPTION_END);
    123   if (d == NULL)
    124     return 1;
    125   (void) getc (stdin);
    126   MHD_stop_daemon (d);
    127   return 0;
    128 }