minimal_example.c (3668B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007 Christian Grothoff (and other contributing authors) 4 Copyright (C) 2014-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 minimal_example.c 22 * @brief minimal example for how to use libmicrohttpd 23 * @author Christian Grothoff 24 * @author Karlson2k (Evgeny Grin) 25 */ 26 27 #include "platform.h" 28 #include <microhttpd.h> 29 30 #define PAGE \ 31 "<html><head><title>libmicrohttpd demo</title></head>" \ 32 "<body>libmicrohttpd demo</body></html>" 33 34 struct handler_param 35 { 36 const char *response_page; 37 }; 38 39 static enum MHD_Result 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, 46 size_t *upload_data_size, 47 void **req_cls) 48 { 49 static int aptr; 50 struct handler_param *param = (struct handler_param *) cls; 51 struct MHD_Response *response; 52 enum MHD_Result ret; 53 54 (void) url; /* Unused. Silent compiler warning. */ 55 (void) version; /* Unused. Silent compiler warning. */ 56 (void) upload_data; /* Unused. Silent compiler warning. */ 57 (void) upload_data_size; /* Unused. Silent compiler warning. */ 58 59 if (0 != strcmp (method, "GET")) 60 return MHD_NO; /* unexpected method */ 61 if (&aptr != *req_cls) 62 { 63 /* do never respond on first call */ 64 *req_cls = &aptr; 65 return MHD_YES; 66 } 67 *req_cls = NULL; /* reset when done */ 68 response = 69 MHD_create_response_from_buffer_static (strlen (param->response_page), 70 param->response_page); 71 ret = MHD_queue_response (connection, 72 MHD_HTTP_OK, 73 response); 74 MHD_destroy_response (response); 75 return ret; 76 } 77 78 79 int 80 main (int argc, 81 char *const *argv) 82 { 83 struct MHD_Daemon *d; 84 struct handler_param data_for_handler; 85 int port; 86 87 if (argc != 2) 88 { 89 printf ("%s PORT\n", argv[0]); 90 return 1; 91 } 92 port = atoi (argv[1]); 93 if ( (1 > port) || (port > 65535) ) 94 { 95 fprintf (stderr, 96 "Port must be a number between 1 and 65535.\n"); 97 return 1; 98 } 99 data_for_handler.response_page = PAGE; 100 d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ 101 MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 102 /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */ 103 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */ 104 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ 105 (uint16_t) port, 106 NULL, NULL, &ahc_echo, &data_for_handler, 107 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 108 MHD_OPTION_END); 109 if (d == NULL) 110 return 1; 111 (void) getc (stdin); 112 MHD_stop_daemon (d); 113 return 0; 114 }