libmicrohttpd2

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

minimal_example2.c (2795B)


      1 /* SPDX-License-Identifier: 0BSD */
      2 /*
      3   This file is part of GNU libmicrohttpd.
      4   Copyright (C) 2024 Evgeny Grin (Karlson2k)
      5 
      6   Permission to use, copy, modify, and/or distribute this software for
      7   any purpose with or without fee is hereby granted.
      8 
      9   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
     10   WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
     11   OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
     12   FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
     13   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
     14   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     15   OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     16 */
     17 /**
     18  * @file minimal_example2.c
     19  * @brief  Minimal example for libmicrohttpd v2
     20  * @author Karlson2k (Evgeny Grin)
     21  */
     22 
     23 #include <stdio.h>
     24 #include <stdlib.h>
     25 #include <microhttpd2.h>
     26 
     27 static MHD_FN_PAR_NONNULL_ (2) MHD_FN_PAR_NONNULL_ (3)
     28 const struct MHD_Action *
     29 req_cb (void *cls,
     30         struct MHD_Request *MHD_RESTRICT request,
     31         const struct MHD_String *MHD_RESTRICT path,
     32         enum MHD_HTTP_Method method,
     33         uint_fast64_t upload_size)
     34 {
     35   static const char res_msg[] = "Hello there!\n";
     36 
     37   (void) cls;
     38   (void) path;
     39   (void) method;
     40   (void) upload_size; /* Unused */
     41 
     42   return MHD_action_from_response (
     43     request,
     44     MHD_response_from_buffer_static (
     45       MHD_HTTP_STATUS_OK,
     46       sizeof(res_msg) / sizeof(char) - 1,
     47       res_msg));
     48 }
     49 
     50 
     51 int
     52 main (int argc,
     53       char *const *argv)
     54 {
     55   struct MHD_Daemon *d;
     56   int port;
     57 
     58   if (argc != 2)
     59   {
     60     fprintf (stderr,
     61              "Usage:\n%s [PORT]\n",
     62              argv[0]);
     63     port = 8080;
     64   }
     65   else
     66     port = atoi (argv[1]);
     67   if ((1 > port) || (65535 < port))
     68   {
     69     fprintf (stderr,
     70              "The PORT must be a numeric value between 1 and 65535.\n");
     71     return 2;
     72   }
     73   d = MHD_daemon_create (&req_cb,
     74                          NULL);
     75   if (NULL == d)
     76   {
     77     fprintf (stderr,
     78              "Failed to create MHD daemon.\n");
     79     return 3;
     80   }
     81   if (MHD_SC_OK !=
     82       MHD_DAEMON_SET_OPTIONS (
     83         d,
     84         MHD_D_OPTION_WM_WORKER_THREADS (1),
     85         MHD_D_OPTION_BIND_PORT (MHD_AF_AUTO,
     86                                 (uint_least16_t) port)))
     87   {
     88     fprintf (stderr,
     89              "Failed to set MHD daemon run parameters.\n");
     90   }
     91   else
     92   {
     93     if (MHD_SC_OK !=
     94         MHD_daemon_start (d))
     95     {
     96       fprintf (stderr,
     97                "Failed to start MHD daemon.\n");
     98     }
     99     else
    100     {
    101       printf ("The MHD daemon is listening on port %d\n"
    102               "Press ENTER to stop.\n", port);
    103       (void) fgetc (stdin);
    104     }
    105   }
    106   printf ("Stopping... ");
    107   fflush (stdout);
    108   MHD_daemon_destroy (d);
    109   printf ("OK\n");
    110   return 0;
    111 }