minimal_example_empty.c (3307B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007 Christian Grothoff (and other contributing authors) 4 Copyright (C) 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 31 static enum MHD_Result 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, 38 size_t *upload_data_size, 39 void **req_cls) 40 { 41 static int aptr; 42 struct MHD_Response *response; 43 enum MHD_Result ret; 44 45 (void) cls; /* Unused. Silent compiler warning. */ 46 (void) url; /* Unused. Silent compiler warning. */ 47 (void) version; /* Unused. Silent compiler warning. */ 48 (void) upload_data; /* Unused. Silent compiler warning. */ 49 (void) upload_data_size; /* Unused. Silent compiler warning. */ 50 51 if (0 != strcmp (method, "GET")) 52 return MHD_NO; /* unexpected method */ 53 if (&aptr != *req_cls) 54 { 55 /* do never respond on first call */ 56 *req_cls = &aptr; 57 return MHD_YES; 58 } 59 *req_cls = NULL; /* reset when done */ 60 response = MHD_create_response_empty (MHD_RF_NONE); 61 ret = MHD_queue_response (connection, 62 MHD_HTTP_NO_CONTENT, 63 response); 64 MHD_destroy_response (response); 65 return ret; 66 } 67 68 69 int 70 main (int argc, 71 char *const *argv) 72 { 73 struct MHD_Daemon *d; 74 int port; 75 76 if (argc != 2) 77 { 78 printf ("%s PORT\n", argv[0]); 79 return 1; 80 } 81 port = atoi (argv[1]); 82 if ( (1 > port) || (port > 65535) ) 83 { 84 fprintf (stderr, 85 "Port must be a number between 1 and 65535.\n"); 86 return 1; 87 } 88 d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ 89 MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 90 /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */ 91 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */ 92 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ 93 (uint16_t) port, 94 NULL, NULL, &ahc_echo, NULL, 95 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 96 MHD_OPTION_END); 97 if (d == NULL) 98 return 1; 99 (void) getc (stdin); 100 MHD_stop_daemon (d); 101 return 0; 102 }