querystring_example.c (3957B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors) 4 Copyright (C) 2016-2022 Evgeny Grin (Karlson2k) 5 6 This library is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 This library is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public 17 License along with this library; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 /** 21 * @file querystring_example.c 22 * @brief example for how to get the query string from libmicrohttpd 23 * Call with an URI ending with something like "?q=QUERY" 24 * @author Christian Grothoff 25 * @author Karlson2k (Evgeny Grin) 26 */ 27 28 #include "platform.h" 29 #include <microhttpd.h> 30 31 #define PAGE \ 32 "<html><head><title>libmicrohttpd demo</title></head><body>Query string for "%s" was "%s"</body></html>" 33 34 static enum MHD_Result 35 ahc_echo (void *cls, 36 struct MHD_Connection *connection, 37 const char *url, 38 const char *method, 39 const char *version, 40 const char *upload_data, size_t *upload_data_size, void **req_cls) 41 { 42 static int aptr; 43 const char *val; 44 char *me; 45 struct MHD_Response *response; 46 enum MHD_Result ret; 47 int resp_len; 48 size_t buf_size; 49 (void) cls; /* Unused. Silent compiler warning. */ 50 (void) url; /* Unused. Silent compiler warning. */ 51 (void) version; /* Unused. Silent compiler warning. */ 52 (void) upload_data; /* Unused. Silent compiler warning. */ 53 (void) upload_data_size; /* Unused. Silent compiler warning. */ 54 55 if (0 != strcmp (method, "GET")) 56 return MHD_NO; /* unexpected method */ 57 if (&aptr != *req_cls) 58 { 59 /* do never respond on first call */ 60 *req_cls = &aptr; 61 return MHD_YES; 62 } 63 *req_cls = NULL; /* reset when done */ 64 val = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "q"); 65 if (NULL == val) 66 return MHD_NO; /* No "q" argument was found */ 67 resp_len = snprintf (NULL, 0, PAGE, "q", val); 68 if (0 >= resp_len) 69 return MHD_NO; /* Error calculating response size */ 70 buf_size = (size_t) resp_len + 1; /* Add one byte for zero-termination */ 71 me = malloc (buf_size); 72 if (me == NULL) 73 return MHD_NO; /* Error allocating memory */ 74 if (resp_len != snprintf (me, buf_size, PAGE, "q", val)) 75 { 76 free (me); 77 return MHD_NO; /* Error forming the response body */ 78 } 79 response = 80 MHD_create_response_from_buffer_with_free_callback (buf_size - 1, 81 (void *) me, 82 &free); 83 if (response == NULL) 84 { 85 free (me); 86 return MHD_NO; 87 } 88 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 89 MHD_destroy_response (response); 90 return ret; 91 } 92 93 94 int 95 main (int argc, char *const *argv) 96 { 97 struct MHD_Daemon *d; 98 int port; 99 100 if (argc != 2) 101 { 102 printf ("%s PORT\n", argv[0]); 103 return 1; 104 } 105 port = atoi (argv[1]); 106 if ( (port < 0) || 107 (port > UINT16_MAX) ) 108 { 109 printf ("%s PORT\n", argv[0]); 110 return 1; 111 } 112 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 113 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 114 (uint16_t) port, 115 NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); 116 if (NULL == d) 117 return 1; 118 (void) getc (stdin); 119 MHD_stop_daemon (d); 120 return 0; 121 }