1/* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors) 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18*/ 19/** 20 * @file minimal_example.c 21 * @brief minimal example for how to generate an infinite stream with libmicrohttpd 22 * @author Christian Grothoff 23 */ 24 25#include "platform.h" 26#include <microhttpd.h> 27 28static ssize_t 29data_generator (void *cls, uint64_t pos, char *buf, size_t max) 30{ 31 if (max < 80) 32 return 0; 33 memset (buf, 'A', max - 1); 34 buf[79] = '\n'; 35 return 80; 36} 37 38static int 39ahc_echo (void *cls, 40 struct MHD_Connection *connection, 41 const char *url, 42 const char *method, 43 const char *version, 44 const char *upload_data, size_t *upload_data_size, void **ptr) 45{ 46 static int aptr; 47 struct MHD_Response *response; 48 int ret; 49 50 if (0 != strcmp (method, "GET")) 51 return MHD_NO; /* unexpected method */ 52 if (&aptr != *ptr) 53 { 54 /* do never respond on first call */ 55 *ptr = &aptr; 56 return MHD_YES; 57 } 58 *ptr = NULL; /* reset when done */ 59 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 60 80, 61 &data_generator, NULL, NULL); 62 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 63 MHD_destroy_response (response); 64 return ret; 65} 66 67int 68main (int argc, char *const *argv) 69{ 70 struct MHD_Daemon *d; 71 72 if (argc != 2) 73 { 74 printf ("%s PORT\n", argv[0]); 75 return 1; 76 } 77 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, 78 atoi (argv[1]), 79 NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); 80 if (d == NULL) 81 return 1; 82 (void) getc (stdin); 83 MHD_stop_daemon (d); 84 return 0; 85} 86