#include #include #include #include #define PORT 8080 #define SIZE 32 #define USLEEP 1000000 #define TIMEOUT 10 static void callback_streaming_close (void * stream_user_data) { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); fprintf(stdout, "end stream data at %s", asctime (timeinfo)); } static ssize_t callback_streaming (void * stream_user_data, uint64_t offset, char * out_buf, size_t max) { ssize_t my_size = max", asctime (timeinfo)); fprintf(stdout, "stream %zu bytes at %s", strlen(out_buf), asctime (timeinfo)); return strlen(out_buf); } static int callback_function (void * cls, struct MHD_Connection * connection, const char * url, const char * method, const char * version, const char * upload_data, size_t * upload_data_size, void ** con_cls) { int ret; struct MHD_Response * mhd_response = MHD_create_response_from_callback(-1, SIZE, &callback_streaming, NULL, &callback_streaming_close); if (mhd_response == NULL) { fprintf(stderr, "Error MHD_create_response_from_callback\n"); mhd_response = MHD_create_response_from_buffer (0, "", MHD_RESPMEM_PERSISTENT); } else { MHD_add_response_header (mhd_response, "Content-Type", "text/html; charset=utf-8"); MHD_set_connection_option(connection, MHD_CONNECTION_OPTION_TIMEOUT, TIMEOUT); } ret = MHD_queue_response(connection, 200, mhd_response); MHD_destroy_response (mhd_response); return ret; } int main(void) { struct MHD_Daemon *d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG | MHD_USE_INTERNAL_POLLING_THREAD, PORT, NULL, NULL, &callback_function, NULL, #if 0 MHD_OPTION_CONNECTION_TIMEOUT, TIMEOUT, #endif MHD_OPTION_END); if (d != NULL) { fprintf(stdout, "Daemon started on port %d\n", PORT); getchar(); MHD_stop_daemon (d); } else { fprintf(stderr, "Error starting MHD daemon\n"); } return 0; }