libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 85775e29a91f2bd643803c2696758f6c9c90bf72
parent 81daa705ca9bc8bec6cf877c684021cabddcca61
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Tue, 19 Apr 2022 18:53:58 +0300

src/examples: Fixed drop of 'const' qualifiers and minor fixes.

* some minor fixes and improvements.

Diffstat:
Msrc/examples/authorization_example.c | 18++++++++++--------
Msrc/examples/benchmark.c | 7++++---
Msrc/examples/benchmark_https.c | 7++++---
Msrc/examples/demo.c | 27++++++++++++---------------
Msrc/examples/demo_https.c | 27++++++++++++---------------
Msrc/examples/digest_auth_example.c | 19++++++++++---------
Msrc/examples/dual_stack_example.c | 19++++++++++++++-----
Msrc/examples/fileserver_example.c | 9+++++----
Msrc/examples/fileserver_example_dirs.c | 7+++----
Msrc/examples/fileserver_example_external_select.c | 9+++++----
Msrc/examples/https_fileserver_example.c | 8+++++---
Msrc/examples/minimal_example.c | 22++++++++++++++++------
Msrc/examples/post_example.c | 19++++++++++---------
Msrc/examples/querystring_example.c | 10++++------
Msrc/examples/refuse_post_example.c | 27+++++++++++++++++++--------
Msrc/examples/suspend_resume_epoll.c | 8++++----
Msrc/examples/timeout.c | 10+++++-----
Msrc/examples/websocket_threaded_example.c | 19+++++++++++--------
18 files changed, 153 insertions(+), 119 deletions(-)

diff --git a/src/examples/authorization_example.c b/src/examples/authorization_example.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2008 Christian Grothoff (and other contributing authors) + Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -21,6 +22,7 @@ * @file authorization_example.c * @brief example for how to use libmicrohttpd with HTTP authentication * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" @@ -48,12 +50,12 @@ ahc_echo (void *cls, const char *upload_data, size_t *upload_data_size, void **req_cls) { static int aptr; - const char *me = cls; struct MHD_Response *response; enum MHD_Result ret; char *user; char *pass; int fail; + (void) cls; /* Unused. Silent compiler warning. */ (void) url; /* Unused. Silent compiler warning. */ (void) version; /* Unused. Silent compiler warning. */ (void) upload_data; /* Unused. Silent compiler warning. */ @@ -78,16 +80,16 @@ ahc_echo (void *cls, (0 != strcmp (pass, "open sesame") ) ); if (fail) { - response = MHD_create_response_from_buffer (strlen (DENIED), - (void *) DENIED, - MHD_RESPMEM_PERSISTENT); + response = + MHD_create_response_from_buffer_static (strlen (DENIED), + (const void *) DENIED); ret = MHD_queue_basic_auth_fail_response (connection,"TestRealm",response); } else { - response = MHD_create_response_from_buffer (strlen (me), - (void *) me, - MHD_RESPMEM_PERSISTENT); + response = + MHD_create_response_from_buffer_static (strlen (PAGE), + (const void *) PAGE); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); } if (NULL != user) @@ -117,7 +119,7 @@ main (int argc, char *const *argv) d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, atoi (argv[1]), - NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); + NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); if (d == NULL) return 1; fprintf (stderr, "HTTP server running. Press ENTER to stop the server.\n"); diff --git a/src/examples/benchmark.c b/src/examples/benchmark.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2007, 2013 Christian Grothoff (and other contributing authors) + Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -20,6 +21,7 @@ * @file benchmark.c * @brief minimal code to benchmark MHD GET performance * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" @@ -145,9 +147,8 @@ main (int argc, char *const *argv) printf ("%s PORT\n", argv[0]); return 1; } - response = MHD_create_response_from_buffer (strlen (PAGE), - (void *) PAGE, - MHD_RESPMEM_PERSISTENT); + response = MHD_create_response_from_buffer_static (strlen (PAGE), + (const void *) PAGE); #if 0 (void) MHD_add_response_header (response, MHD_HTTP_HEADER_CONNECTION, diff --git a/src/examples/benchmark_https.c b/src/examples/benchmark_https.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2007, 2013 Christian Grothoff (and other contributing authors) + Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -20,6 +21,7 @@ * @file benchmark_https.c * @brief minimal code to benchmark MHD GET performance with HTTPS * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" @@ -211,9 +213,8 @@ main (int argc, char *const *argv) printf ("%s PORT\n", argv[0]); return 1; } - response = MHD_create_response_from_buffer (strlen (PAGE), - (void *) PAGE, - MHD_RESPMEM_PERSISTENT); + response = MHD_create_response_from_buffer_static (strlen (PAGE), + (const void *) PAGE); d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS #ifdef EPOLL_SUPPORT | MHD_USE_EPOLL | MHD_USE_TURBO diff --git a/src/examples/demo.c b/src/examples/demo.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2013 Christian Grothoff (and other contributing authors) + Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -27,6 +28,7 @@ * run tests against. Note that the number of threads may need * to be adjusted depending on the number of available cores. * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) */ #include "MHD_config.h" #include "platform.h" @@ -909,23 +911,18 @@ main (int argc, char *const *argv) #endif /* MHD_HAVE_LIBMAGIC */ (void) pthread_mutex_init (&mutex, NULL); - file_not_found_response = MHD_create_response_from_buffer (strlen ( - FILE_NOT_FOUND_PAGE), - (void *) - FILE_NOT_FOUND_PAGE, - MHD_RESPMEM_PERSISTENT); + file_not_found_response = + MHD_create_response_from_buffer_static (strlen (FILE_NOT_FOUND_PAGE), + (const void *) FILE_NOT_FOUND_PAGE); mark_as_html (file_not_found_response); - request_refused_response = MHD_create_response_from_buffer (strlen ( - REQUEST_REFUSED_PAGE), - (void *) - REQUEST_REFUSED_PAGE, - MHD_RESPMEM_PERSISTENT); + request_refused_response = + MHD_create_response_from_buffer_static (strlen (REQUEST_REFUSED_PAGE), + (const void *) + REQUEST_REFUSED_PAGE); mark_as_html (request_refused_response); - internal_error_response = MHD_create_response_from_buffer (strlen ( - INTERNAL_ERROR_PAGE), - (void *) - INTERNAL_ERROR_PAGE, - MHD_RESPMEM_PERSISTENT); + internal_error_response = + MHD_create_response_from_buffer_static (strlen (INTERNAL_ERROR_PAGE), + (const void *) INTERNAL_ERROR_PAGE); mark_as_html (internal_error_response); update_directory (); d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD diff --git a/src/examples/demo_https.c b/src/examples/demo_https.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2013 Christian Grothoff (and other contributing authors) + Copyright (C) 2016-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -30,6 +31,7 @@ * This demonstration uses key/cert stored in static string. Optionally, * use gnutls_load_file() to load them from file. * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" #include <microhttpd.h> @@ -974,23 +976,18 @@ main (int argc, char *const *argv) #endif /* MHD_HAVE_LIBMAGIC */ (void) pthread_mutex_init (&mutex, NULL); - file_not_found_response = MHD_create_response_from_buffer (strlen ( - FILE_NOT_FOUND_PAGE), - (void *) - FILE_NOT_FOUND_PAGE, - MHD_RESPMEM_PERSISTENT); + file_not_found_response = + MHD_create_response_from_buffer_static (strlen (FILE_NOT_FOUND_PAGE), + (const void *) FILE_NOT_FOUND_PAGE); mark_as_html (file_not_found_response); - request_refused_response = MHD_create_response_from_buffer (strlen ( - REQUEST_REFUSED_PAGE), - (void *) - REQUEST_REFUSED_PAGE, - MHD_RESPMEM_PERSISTENT); + request_refused_response = + MHD_create_response_from_buffer_static (strlen (REQUEST_REFUSED_PAGE), + (const void *) + REQUEST_REFUSED_PAGE); mark_as_html (request_refused_response); - internal_error_response = MHD_create_response_from_buffer (strlen ( - INTERNAL_ERROR_PAGE), - (void *) - INTERNAL_ERROR_PAGE, - MHD_RESPMEM_PERSISTENT); + internal_error_response = + MHD_create_response_from_buffer_static (strlen (INTERNAL_ERROR_PAGE), + (const void *) INTERNAL_ERROR_PAGE); mark_as_html (internal_error_response); update_directory (); d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD diff --git a/src/examples/digest_auth_example.c b/src/examples/digest_auth_example.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2010 Christian Grothoff (and other contributing authors) + Copyright (C) 2016-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -20,6 +21,7 @@ * @file digest_auth_example.c * @brief minimal example for how to use digest auth with libmicrohttpd * @author Amr Ali + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" @@ -67,9 +69,9 @@ ahc_echo (void *cls, username = MHD_digest_auth_get_username (connection); if (NULL == username) { - response = MHD_create_response_from_buffer (strlen (DENIED), - DENIED, - MHD_RESPMEM_PERSISTENT); + response = + MHD_create_response_from_buffer_static (strlen (DENIED), + DENIED); ret = MHD_queue_auth_fail_response2 (connection, realm, MY_OPAQUE_STR, response, @@ -86,9 +88,9 @@ ahc_echo (void *cls, if ( (res == MHD_INVALID_NONCE) || (res == MHD_NO) ) { - response = MHD_create_response_from_buffer (strlen (DENIED), - DENIED, - MHD_RESPMEM_PERSISTENT); + response = + MHD_create_response_from_buffer_static (strlen (DENIED), + DENIED); if (NULL == response) return MHD_NO; ret = MHD_queue_auth_fail_response2 (connection, realm, @@ -100,8 +102,7 @@ ahc_echo (void *cls, MHD_destroy_response (response); return ret; } - response = MHD_create_response_from_buffer (strlen (PAGE), PAGE, - MHD_RESPMEM_PERSISTENT); + response = MHD_create_response_from_buffer_static (strlen (PAGE), PAGE); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; @@ -148,7 +149,7 @@ main (int argc, char *const *argv) d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, atoi (argv[1]), - NULL, NULL, &ahc_echo, PAGE, + NULL, NULL, &ahc_echo, NULL, MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof(rnd), rnd, MHD_OPTION_NONCE_NC_SIZE, 300, MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, diff --git a/src/examples/dual_stack_example.c b/src/examples/dual_stack_example.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2007, 2012 Christian Grothoff (and other contributing authors) + Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -20,6 +21,7 @@ * @file dual_stack_example.c * @brief how to use MHD with both IPv4 and IPv6 support (dual-stack) * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" @@ -28,6 +30,11 @@ #define PAGE \ "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" +struct handler_param +{ + const char *response_page; +}; + static enum MHD_Result ahc_echo (void *cls, struct MHD_Connection *connection, @@ -37,7 +44,7 @@ ahc_echo (void *cls, const char *upload_data, size_t *upload_data_size, void **req_cls) { static int aptr; - const char *me = cls; + struct handler_param *param = (struct handler_param *) cls; struct MHD_Response *response; enum MHD_Result ret; (void) url; /* Unused. Silent compiler warning. */ @@ -54,9 +61,9 @@ ahc_echo (void *cls, return MHD_YES; } *req_cls = NULL; /* reset when done */ - response = MHD_create_response_from_buffer (strlen (me), - (void *) me, - MHD_RESPMEM_PERSISTENT); + response = + MHD_create_response_from_buffer_static (strlen (param->response_page), + param->response_page); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; @@ -67,16 +74,18 @@ int main (int argc, char *const *argv) { struct MHD_Daemon *d; + struct handler_param data_for_handler; if (argc != 2) { printf ("%s PORT\n", argv[0]); return 1; } + data_for_handler.response_page = PAGE; d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_DUAL_STACK, atoi (argv[1]), - NULL, NULL, &ahc_echo, PAGE, + NULL, NULL, &ahc_echo, &data_for_handler, MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, MHD_OPTION_END); (void) getc (stdin); diff --git a/src/examples/fileserver_example.c b/src/examples/fileserver_example.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2007 Christian Grothoff (and other contributing authors) + Copyright (C) 2016-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -20,6 +21,7 @@ * @file fileserver_example.c * @brief minimal example for how to use libmicrohttpd to serve files * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" @@ -91,9 +93,8 @@ ahc_echo (void *cls, } if (-1 == fd) { - response = MHD_create_response_from_buffer (strlen (PAGE), - (void *) PAGE, - MHD_RESPMEM_PERSISTENT); + response = MHD_create_response_from_buffer_static (strlen (PAGE), + PAGE); ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); MHD_destroy_response (response); } @@ -126,7 +127,7 @@ main (int argc, char *const *argv) d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, atoi (argv[1]), - NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); + NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); if (d == NULL) return 1; (void) getc (stdin); diff --git a/src/examples/fileserver_example_dirs.c b/src/examples/fileserver_example_dirs.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2007 Christian Grothoff (and other contributing authors) + Copyright (C) 2016-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -21,6 +22,7 @@ * @file fileserver_example_dirs.c * @brief example for how to use libmicrohttpd to serve files (with directory support) * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" @@ -28,9 +30,6 @@ #include <microhttpd.h> #include <unistd.h> -#define PAGE \ - "<html><head><title>File not found</title></head><body>File not found</body></html>" - static ssize_t file_reader (void *cls, uint64_t pos, char *buf, size_t max) @@ -199,7 +198,7 @@ main (int argc, char *const *argv) d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, atoi (argv[1]), - NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); + NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); if (NULL == d) return 1; (void) getc (stdin); diff --git a/src/examples/fileserver_example_external_select.c b/src/examples/fileserver_example_external_select.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors) + Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -20,6 +21,7 @@ * @file fileserver_example_external_select.c * @brief minimal example for how to use libmicrohttpd to server files * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" @@ -98,9 +100,8 @@ ahc_echo (void *cls, if (NULL == file) { - response = MHD_create_response_from_buffer (strlen (PAGE), - (void *) PAGE, - MHD_RESPMEM_PERSISTENT); + response = MHD_create_response_from_buffer_static (strlen (PAGE), + PAGE); ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); MHD_destroy_response (response); } @@ -142,7 +143,7 @@ main (int argc, char *const *argv) } d = MHD_start_daemon (MHD_USE_ERROR_LOG, atoi (argv[1]), - NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); + NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); if (d == NULL) return 1; end = time (NULL) + atoi (argv[2]); diff --git a/src/examples/https_fileserver_example.c b/src/examples/https_fileserver_example.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors) + Copyright (C) 2016-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -30,6 +31,7 @@ * 'certtool' may be used to generate these if required. * * @author Sagie Amir + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" @@ -180,9 +182,9 @@ http_ahc (void *cls, if (NULL == file) { - response = MHD_create_response_from_buffer (strlen (EMPTY_PAGE), - (void *) EMPTY_PAGE, - MHD_RESPMEM_PERSISTENT); + response = + MHD_create_response_from_buffer_static (strlen (EMPTY_PAGE), + (const void *) EMPTY_PAGE); ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); MHD_destroy_response (response); } diff --git a/src/examples/minimal_example.c b/src/examples/minimal_example.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2007 Christian Grothoff (and other contributing authors) + Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -20,13 +21,20 @@ * @file minimal_example.c * @brief minimal example for how to use libmicrohttpd * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" #include <microhttpd.h> #define PAGE \ - "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" + "<html><head><title>libmicrohttpd demo</title></head>" \ + "<body>libmicrohttpd demo</body></html>" + +struct handler_param +{ + const char *response_page; +}; static enum MHD_Result ahc_echo (void *cls, @@ -39,7 +47,7 @@ ahc_echo (void *cls, void **req_cls) { static int aptr; - const char *me = cls; + struct handler_param *param = (struct handler_param *) cls; struct MHD_Response *response; enum MHD_Result ret; @@ -57,9 +65,9 @@ ahc_echo (void *cls, return MHD_YES; } *req_cls = NULL; /* reset when done */ - response = MHD_create_response_from_buffer (strlen (me), - (void *) me, - MHD_RESPMEM_PERSISTENT); + response = + MHD_create_response_from_buffer_static (strlen (param->response_page), + param->response_page); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); @@ -73,19 +81,21 @@ main (int argc, char *const *argv) { struct MHD_Daemon *d; + struct handler_param data_for_handler; if (argc != 2) { printf ("%s PORT\n", argv[0]); return 1; } + data_for_handler.response_page = PAGE; d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */ /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */ /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ atoi (argv[1]), - NULL, NULL, &ahc_echo, PAGE, + NULL, NULL, &ahc_echo, &data_for_handler, MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, MHD_OPTION_STRICT_FOR_CLIENT, (int) 1, MHD_OPTION_END); diff --git a/src/examples/post_example.c b/src/examples/post_example.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2011 Christian Grothoff (and other contributing authors) + Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -20,6 +21,7 @@ * @file post_example.c * @brief example for processing POST requests using libmicrohttpd * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) */ #include <stdlib.h> @@ -285,9 +287,8 @@ serve_simple_form (const void *cls, struct MHD_Response *response; /* return static form */ - response = MHD_create_response_from_buffer (strlen (form), - (void *) form, - MHD_RESPMEM_PERSISTENT); + response = MHD_create_response_from_buffer_static (strlen (form), + (const void *) form); if (NULL == response) return MHD_NO; add_session_cookie (session, response); @@ -422,9 +423,9 @@ not_found_page (const void *cls, (void) session; /* Unused. Silent compiler warning. */ /* unsupported HTTP method */ - response = MHD_create_response_from_buffer (strlen (NOT_FOUND_ERROR), - (void *) NOT_FOUND_ERROR, - MHD_RESPMEM_PERSISTENT); + response = + MHD_create_response_from_buffer_static (strlen (NOT_FOUND_ERROR), + (const void *) NOT_FOUND_ERROR); if (NULL == response) return MHD_NO; ret = MHD_queue_response (connection, @@ -643,9 +644,9 @@ create_response (void *cls, return ret; } /* unsupported HTTP method */ - response = MHD_create_response_from_buffer (strlen (METHOD_ERROR), - (void *) METHOD_ERROR, - MHD_RESPMEM_PERSISTENT); + response = + MHD_create_response_from_buffer_static (strlen (METHOD_ERROR), + (const void *) METHOD_ERROR); ret = MHD_queue_response (connection, MHD_HTTP_NOT_ACCEPTABLE, response); diff --git a/src/examples/querystring_example.c b/src/examples/querystring_example.c @@ -38,12 +38,12 @@ ahc_echo (void *cls, const char *upload_data, size_t *upload_data_size, void **req_cls) { static int aptr; - const char *fmt = cls; const char *val; char *me; struct MHD_Response *response; enum MHD_Result ret; int resp_len; + (void) cls; /* Unused. Silent compiler warning. */ (void) url; /* Unused. Silent compiler warning. */ (void) version; /* Unused. Silent compiler warning. */ (void) upload_data; /* Unused. Silent compiler warning. */ @@ -58,18 +58,16 @@ ahc_echo (void *cls, return MHD_YES; } *req_cls = NULL; /* reset when done */ - if (NULL == fmt) - return MHD_NO; /* The cls must not be NULL */ val = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "q"); if (NULL == val) return MHD_NO; /* No "q" argument was found */ - resp_len = snprintf (NULL, 0, fmt, "q", val); + resp_len = snprintf (NULL, 0, PAGE, "q", val); if (0 > resp_len) return MHD_NO; /* Error calculating response size */ me = malloc (resp_len + 1); if (me == NULL) return MHD_NO; /* Error allocating memory */ - if (resp_len != snprintf (me, resp_len + 1, fmt, "q", val)) + if (resp_len != snprintf (me, resp_len + 1, PAGE, "q", val)) { free (me); return MHD_NO; /* Error forming the response body */ @@ -108,7 +106,7 @@ main (int argc, char *const *argv) d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, (uint16_t) port, - NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); + NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); if (NULL == d) return 1; (void) getc (stdin); diff --git a/src/examples/refuse_post_example.c b/src/examples/refuse_post_example.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors) + Copyright (C) 2016-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -20,10 +21,16 @@ * @file refuse_post_example.c * @brief example for how to refuse a POST request properly * @author Christian Grothoff and Sebastian Gerhardt + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" #include <microhttpd.h> +struct handler_param +{ + const char *response_page; +}; + const char *askpage = "<html><body>\n\ Upload a file, please!<br>\n\ @@ -44,7 +51,7 @@ ahc_echo (void *cls, const char *upload_data, size_t *upload_data_size, void **req_cls) { static int aptr; - const char *me = cls; + struct handler_param *param = (struct handler_param *) cls; struct MHD_Response *response; enum MHD_Result ret; (void) cls; /* Unused. Silent compiler warning. */ @@ -63,9 +70,9 @@ ahc_echo (void *cls, /* always to busy for POST requests */ if (0 == strcmp (method, "POST")) { - response = MHD_create_response_from_buffer (strlen (BUSYPAGE), - (void *) BUSYPAGE, - MHD_RESPMEM_PERSISTENT); + response = + MHD_create_response_from_buffer_static (strlen (BUSYPAGE), + (const void *) BUSYPAGE); ret = MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE, response); @@ -75,9 +82,10 @@ ahc_echo (void *cls, } *req_cls = NULL; /* reset when done */ - response = MHD_create_response_from_buffer (strlen (me), - (void *) me, - MHD_RESPMEM_PERSISTENT); + response = + MHD_create_response_from_buffer_static (strlen (param->response_page), + (const void *) + param->response_page); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; @@ -89,15 +97,18 @@ main (int argc, char *const *argv) { struct MHD_Daemon *d; + struct handler_param data_for_handler; + if (argc != 2) { printf ("%s PORT\n", argv[0]); return 1; } + data_for_handler.response_page = askpage; d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, atoi (argv[1]), - NULL, NULL, &ahc_echo, (void *) askpage, + NULL, NULL, &ahc_echo, &data_for_handler, MHD_OPTION_END); if (d == NULL) return 1; diff --git a/src/examples/suspend_resume_epoll.c b/src/examples/suspend_resume_epoll.c @@ -1,6 +1,7 @@ /* This file is part of libmicrohttpd Copyright (C) 2018 Christian Grothoff (and other contributing authors) + Copyright (C) 2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -22,6 +23,7 @@ * resume a suspended connection * @author Robert D Kocisko * @author Christian Grothoff + * @author Karlson2k (Evgeny Grin) */ #include "platform.h" #include <microhttpd.h> @@ -57,7 +59,6 @@ ahc_echo (void *cls, struct itimerspec ts; (void) cls; - (void) url; /* Unused. Silence compiler warning. */ (void) method; (void) version; /* Unused. Silence compiler warning. */ (void) upload_data; /* Unused. Silence compiler warning. */ @@ -78,9 +79,8 @@ ahc_echo (void *cls, if (-1 != req->timerfd) { /* send response (echo request url) */ - response = MHD_create_response_from_buffer (strlen (url), - (void *) url, - MHD_RESPMEM_MUST_COPY); + response = MHD_create_response_from_buffer_copy (strlen (url), + (const void *) url); if (NULL == response) return MHD_NO; ret = MHD_queue_response (connection, diff --git a/src/examples/timeout.c b/src/examples/timeout.c @@ -1,7 +1,8 @@ /* This file is part of libmicrohttpd - Copyright (C) 2016, 2017 Christian Grothoff, - Silvio Clecio (silvioprog), Karlson2k (Evgeny Grin) + Copyright (C) 2016-2017 Christian Grothoff, + Silvio Clecio (silvioprog), Evgeny Grin (Karlson2k) + Copyright (C) 2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -50,9 +51,8 @@ answer_to_connection (void *cls, (void) upload_data_size; /* Unused. Silent compiler warning. */ (void) req_cls; /* Unused. Silent compiler warning. */ - response = MHD_create_response_from_buffer (strlen (page), - (void *) page, - MHD_RESPMEM_PERSISTENT); + response = MHD_create_response_from_buffer_static (strlen (page), + (const void *) page); MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html"); diff --git a/src/examples/websocket_threaded_example.c b/src/examples/websocket_threaded_example.c @@ -2,6 +2,7 @@ This file is part of libmicrohttpd Copyright (C) 2020 Christian Grothoff, Silvio Clecio (and other contributing authors) + Copyright (C) 2020-2022 Evgeny Grin (Karlson2k) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -22,6 +23,7 @@ * @file websocket_threaded_example.c * @brief example for how to provide a tiny threaded websocket server * @author Silvio Clecio (silvioprog) + * @author Karlson2k (Evgeny Grin) */ /* TODO: allow to send large messages. */ @@ -434,8 +436,8 @@ send_chat_page (struct MHD_Connection *con) struct MHD_Response *res; enum MHD_Result ret; - res = MHD_create_response_from_buffer (strlen (CHAT_PAGE), (void *) CHAT_PAGE, - MHD_RESPMEM_PERSISTENT); + res = MHD_create_response_from_buffer_static (strlen (CHAT_PAGE), + (const void *) CHAT_PAGE); ret = MHD_queue_response (con, MHD_HTTP_OK, res); MHD_destroy_response (res); return ret; @@ -448,9 +450,9 @@ send_bad_request (struct MHD_Connection *con) struct MHD_Response *res; enum MHD_Result ret; - res = MHD_create_response_from_buffer (strlen (BAD_REQUEST_PAGE), - (void *) BAD_REQUEST_PAGE, - MHD_RESPMEM_PERSISTENT); + res = + MHD_create_response_from_buffer_static (strlen (BAD_REQUEST_PAGE), + (const void *) BAD_REQUEST_PAGE); ret = MHD_queue_response (con, MHD_HTTP_BAD_REQUEST, res); MHD_destroy_response (res); return ret; @@ -463,9 +465,10 @@ send_upgrade_required (struct MHD_Connection *con) struct MHD_Response *res; enum MHD_Result ret; - res = MHD_create_response_from_buffer (strlen (UPGRADE_REQUIRED_PAGE), - (void *) UPGRADE_REQUIRED_PAGE, - MHD_RESPMEM_PERSISTENT); + res = + MHD_create_response_from_buffer_static (strlen (UPGRADE_REQUIRED_PAGE), + (const void *) + UPGRADE_REQUIRED_PAGE); MHD_add_response_header (res, MHD_HTTP_HEADER_SEC_WEBSOCKET_VERSION, WS_SEC_WEBSOCKET_VERSION); ret = MHD_queue_response (con, MHD_HTTP_UPGRADE_REQUIRED, res);