Home | History | Annotate | Download | only in examples
      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 minimal_example.c
     21  * @brief minimal example for how to use libmicrohttpd
     22  * @author Christian Grothoff
     23  */
     24 
     25 #include "platform.h"
     26 #include <microhttpd.h>
     27 
     28 #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
     29 
     30 static int
     31 ahc_echo (void *cls,
     32           struct MHD_Connection *connection,
     33           const char *url,
     34           const char *method,
     35           const char *version,
     36           const char *upload_data, size_t *upload_data_size, void **ptr)
     37 {
     38   static int aptr;
     39   const char *me = cls;
     40   struct MHD_Response *response;
     41   int ret;
     42 
     43   if (0 != strcmp (method, "GET"))
     44     return MHD_NO;              /* unexpected method */
     45   if (&aptr != *ptr)
     46     {
     47       /* do never respond on first call */
     48       *ptr = &aptr;
     49       return MHD_YES;
     50     }
     51   *ptr = NULL;                  /* reset when done */
     52   response = MHD_create_response_from_buffer (strlen (me),
     53 					      (void *) me,
     54 					      MHD_RESPMEM_PERSISTENT);
     55   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
     56   MHD_destroy_response (response);
     57   return ret;
     58 }
     59 
     60 int
     61 main (int argc, char *const *argv)
     62 {
     63   struct MHD_Daemon *d;
     64 
     65   if (argc != 2)
     66     {
     67       printf ("%s PORT\n", argv[0]);
     68       return 1;
     69     }
     70   d = MHD_start_daemon (// MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG | MHD_USE_POLL,
     71 			MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
     72 			// MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG | MHD_USE_POLL,
     73 			// MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
     74                         atoi (argv[1]),
     75                         NULL, NULL, &ahc_echo, PAGE,
     76 			MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
     77 			MHD_OPTION_END);
     78   if (d == NULL)
     79     return 1;
     80   (void) getc (stdin);
     81   MHD_stop_daemon (d);
     82   return 0;
     83 }
     84