• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2      This file is part of libmicrohttpd
3      Copyright (C) 2007, 2008 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 querystring_example.c
21  * @brief example for how to get the query string from libmicrohttpd
22  *        Call with an URI ending with something like "?q=QUERY"
23  * @author Christian Grothoff
24  */
25 
26 #include "platform.h"
27 #include <microhttpd.h>
28 
29 #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>Query string for &quot;%s&quot; was &quot;%s&quot;</body></html>"
30 
31 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)32 ahc_echo (void *cls,
33           struct MHD_Connection *connection,
34           const char *url,
35           const char *method,
36           const char *version,
37           const char *upload_data, size_t *upload_data_size, void **ptr)
38 {
39   static int aptr;
40   const char *fmt = cls;
41   const char *val;
42   char *me;
43   struct MHD_Response *response;
44   int ret;
45 
46   if (0 != strcmp (method, "GET"))
47     return MHD_NO;              /* unexpected method */
48   if (&aptr != *ptr)
49     {
50       /* do never respond on first call */
51       *ptr = &aptr;
52       return MHD_YES;
53     }
54   *ptr = NULL;                  /* reset when done */
55   val = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "q");
56   me = malloc (snprintf (NULL, 0, fmt, "q", val) + 1);
57   if (me == NULL)
58     return MHD_NO;
59   sprintf (me, fmt, "q", val);
60   response = MHD_create_response_from_buffer (strlen (me), me,
61 					      MHD_RESPMEM_MUST_FREE);
62   if (response == NULL)
63     {
64       free (me);
65       return MHD_NO;
66     }
67   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
68   MHD_destroy_response (response);
69   return ret;
70 }
71 
72 int
main(int argc,char * const * argv)73 main (int argc, char *const *argv)
74 {
75   struct MHD_Daemon *d;
76 
77   if (argc != 2)
78     {
79       printf ("%s PORT\n", argv[0]);
80       return 1;
81     }
82   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
83                         atoi (argv[1]),
84                         NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
85   if (d == NULL)
86     return 1;
87   (void) getc (stdin);
88   MHD_stop_daemon (d);
89   return 0;
90 }
91