minimal_example_comet.c (3370B)
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 minimal_example.c 22 * @brief minimal example for how to generate an infinite stream with libmicrohttpd 23 * @author Christian Grothoff 24 * @author Karlson2k (Evgeny Grin) 25 */ 26 27 #include "platform.h" 28 #include <microhttpd.h> 29 30 static ssize_t 31 data_generator (void *cls, uint64_t pos, char *buf, size_t max) 32 { 33 (void) cls; /* Unused. Silent compiler warning. */ 34 (void) pos; /* Unused. Silent compiler warning. */ 35 if (max < 80) 36 return 0; 37 memset (buf, 'A', max - 1); 38 buf[79] = '\n'; 39 return 80; 40 } 41 42 43 static enum MHD_Result 44 ahc_echo (void *cls, 45 struct MHD_Connection *connection, 46 const char *url, 47 const char *method, 48 const char *version, 49 const char *upload_data, size_t *upload_data_size, void **req_cls) 50 { 51 static int aptr; 52 struct MHD_Response *response; 53 enum MHD_Result ret; 54 (void) cls; /* Unused. Silent compiler warning. */ 55 (void) url; /* Unused. Silent compiler warning. */ 56 (void) version; /* Unused. Silent compiler warning. */ 57 (void) upload_data; /* Unused. Silent compiler warning. */ 58 (void) upload_data_size; /* Unused. Silent compiler warning. */ 59 60 if (0 != strcmp (method, "GET")) 61 return MHD_NO; /* unexpected method */ 62 if (&aptr != *req_cls) 63 { 64 /* do never respond on first call */ 65 *req_cls = &aptr; 66 return MHD_YES; 67 } 68 *req_cls = NULL; /* reset when done */ 69 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 70 80, 71 &data_generator, NULL, NULL); 72 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 73 MHD_destroy_response (response); 74 return ret; 75 } 76 77 78 int 79 main (int argc, char *const *argv) 80 { 81 struct MHD_Daemon *d; 82 int port; 83 84 if (argc != 2) 85 { 86 printf ("%s PORT\n", argv[0]); 87 return 1; 88 } 89 port = atoi (argv[1]); 90 if ( (1 > port) || (port > 65535) ) 91 { 92 fprintf (stderr, 93 "Port must be a number between 1 and 65535.\n"); 94 return 1; 95 } 96 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_THREAD_PER_CONNECTION 97 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 98 (uint16_t) port, 99 NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); 100 if (d == NULL) 101 return 1; 102 (void) getc (stdin); 103 MHD_stop_daemon (d); 104 return 0; 105 }