• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2      This file is part of libmicrohttpd
3      Copyright (C) 2007, 2012 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 dual_stack_example.c
21  * @brief how to use MHD with both IPv4 and IPv6 support (dual-stack)
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
ahc_echo(void * cls,struct MHD_Connection * connection,const char * url,const char * method,const char * version,const char * upload_data,size_t * upload_data_size,void ** ptr)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 
61 int
main(int argc,char * const * argv)62 main (int argc, char *const *argv)
63 {
64   struct MHD_Daemon *d;
65 
66   if (argc != 2)
67     {
68       printf ("%s PORT\n", argv[0]);
69       return 1;
70     }
71   d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG | MHD_USE_DUAL_STACK,
72 			atoi (argv[1]),
73 			NULL, NULL, &ahc_echo, PAGE,
74 			MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
75 			MHD_OPTION_END);
76   (void) getc (stdin);
77   MHD_stop_daemon (d);
78   return 0;
79 }
80