diff options
Diffstat (limited to 'src/examples/querystring_example.c')
-rw-r--r-- | src/examples/querystring_example.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/examples/querystring_example.c b/src/examples/querystring_example.c new file mode 100644 index 00000000..9baf03a8 --- /dev/null +++ b/src/examples/querystring_example.c | |||
@@ -0,0 +1,90 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (C) 2007, 2008 Christian Grothoff | ||
4 | |||
5 | libmicrohttpd is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | libmicrohttpd is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with libmicrohttpd; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file querystring_example.c | ||
23 | * @brief example for how to get the query string from libmicrohttpd | ||
24 | * Call with an URI ending with something like "?q=QUERY" | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | |||
28 | #include "config.h" | ||
29 | #include <microhttpd.h> | ||
30 | #include <stdlib.h> | ||
31 | #ifndef MINGW | ||
32 | #include <unistd.h> | ||
33 | #endif | ||
34 | #include <string.h> | ||
35 | #include <stdio.h> | ||
36 | |||
37 | #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>Query string for "%s" was "%s"</body></html>" | ||
38 | |||
39 | static int | ||
40 | ahc_echo (void *cls, | ||
41 | struct MHD_Connection *connection, | ||
42 | const char *url, | ||
43 | const char *method, | ||
44 | const char *version, | ||
45 | const char *upload_data, unsigned int *upload_data_size, void **ptr) | ||
46 | { | ||
47 | static int aptr; | ||
48 | const char *fmt = cls; | ||
49 | const char *val; | ||
50 | char *me; | ||
51 | struct MHD_Response *response; | ||
52 | int ret; | ||
53 | |||
54 | if (0 != strcmp (method, "GET")) | ||
55 | return MHD_NO; /* unexpected method */ | ||
56 | if (&aptr != *ptr) | ||
57 | { | ||
58 | /* do never respond on first call */ | ||
59 | *ptr = &aptr; | ||
60 | return MHD_YES; | ||
61 | } | ||
62 | *ptr = NULL; /* reset when done */ | ||
63 | val = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "q"); | ||
64 | me = malloc (snprintf (NULL, 0, fmt, "q", val) + 1); | ||
65 | sprintf (me, fmt, "q", val); | ||
66 | response = MHD_create_response_from_data (strlen (me), me, MHD_YES, MHD_NO); | ||
67 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
68 | MHD_destroy_response (response); | ||
69 | return ret; | ||
70 | } | ||
71 | |||
72 | int | ||
73 | main (int argc, char *const *argv) | ||
74 | { | ||
75 | struct MHD_Daemon *d; | ||
76 | |||
77 | if (argc != 3) | ||
78 | { | ||
79 | printf ("%s PORT SECONDS-TO-RUN\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 | sleep (atoi (argv[2])); | ||
88 | MHD_stop_daemon (d); | ||
89 | return 0; | ||
90 | } | ||