1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2015 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 chunked_example.c 21 * @brief example for generating chunked encoding with libmicrohttpd 22 * @author Christian Grothoff 23 */ 24 25 #include "platform.h" 26 #include <microhttpd.h> 27 28 29 static ssize_t 30 callback (void *cls, 31 uint64_t pos, 32 char *buf, 33 size_t max) 34 { 35 return MHD_CONTENT_READER_END_OF_STREAM; 36 } 37 38 39 40 static int 41 ahc_echo (void *cls, 42 struct MHD_Connection *connection, 43 const char *url, 44 const char *method, 45 const char *version, 46 const char *upload_data, size_t *upload_data_size, void **ptr) 47 { 48 static int aptr; 49 struct MHD_Response *response; 50 int ret; 51 52 if (0 != strcmp (method, "GET")) 53 return MHD_NO; /* unexpected method */ 54 if (&aptr != *ptr) 55 { 56 /* do never respond on first call */ 57 *ptr = &aptr; 58 return MHD_YES; 59 } 60 *ptr = NULL; /* reset when done */ 61 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 62 1024, 63 &callback, 64 NULL, 65 NULL); 66 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 67 MHD_destroy_response (response); 68 return ret; 69 } 70 71 int 72 main (int argc, char *const *argv) 73 { 74 struct MHD_Daemon *d; 75 76 if (argc != 2) 77 { 78 printf ("%s PORT\n", argv[0]); 79 return 1; 80 } 81 d = MHD_start_daemon (// MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG | MHD_USE_POLL, 82 MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, 83 // MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG | MHD_USE_POLL, 84 // MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, 85 atoi (argv[1]), 86 NULL, NULL, &ahc_echo, NULL, 87 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 88 MHD_OPTION_END); 89 if (d == NULL) 90 return 1; 91 (void) getc (stdin); 92 MHD_stop_daemon (d); 93 return 0; 94 } 95