libmicrohttpd2

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

host-example.c (1479B)


      1 /* examples/host-example.c */
      2 
      3 #include <microhttpd2.h>
      4 #include <stdio.h>
      5 #include <assert.h>
      6 
      7 
      8 static struct MHD_Action *
      9 handle_request (void *cls,
     10                 struct MHD_Request *request,
     11                 const struct MHD_String *path,
     12                 enum MHD_HTTP_Method method,
     13                 uint_fast64_t upload_size)
     14 {
     15   struct MHD_StringNullable host;
     16 
     17   /* We passed NULL in main() for the closure */
     18   assert (NULL == cls);
     19   /* MHD_HTTP_HEADER_HOST is literally just "Host" */
     20   if (MHD_request_get_value (request,
     21                              MHD_VK_HEADER,
     22                              MHD_HTTP_HEADER_HOST,
     23                              &host))
     24   {
     25     fprintf (stderr,
     26              "'Host:' is %s\n",
     27              host->cstr);
     28   }
     29   else
     30   {
     31     /* In HTTP/1.0, the 'Host' header was optional! */
     32     fprintf (stderr,
     33              "No 'Host:' header provided\n");
     34   }
     35   /* This simply closes the connection after receiving
     36      the HTTP header, never return actually returning
     37      any data. */
     38   return MHD_action_abort_request (request);
     39 }
     40 
     41 
     42 int
     43 main ()
     44 {
     45   struct MHD_Daemon *d;
     46 
     47   /* Create an HTTP server and use "handle_request()" to
     48      handle all requests. */
     49   d = MHD_daemon_create (&handle_request,
     50                          NULL);
     51   /* We run with everything on default, so port 80, no TLS */
     52   MHD_daemon_start (d);
     53   /* Wait for input on stdin */
     54   (void) getchar ();
     55   /* Then just shut everything down */
     56   MHD_daemon_stop (d);
     57   return 0;
     58 }