aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2022-04-19 18:53:58 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2022-04-19 20:27:37 +0300
commit85775e29a91f2bd643803c2696758f6c9c90bf72 (patch)
treef9719b2158e0ef25ea070ad1eda9b17c443a7c3e /src
parent81daa705ca9bc8bec6cf877c684021cabddcca61 (diff)
downloadlibmicrohttpd-85775e29a91f2bd643803c2696758f6c9c90bf72.tar.gz
libmicrohttpd-85775e29a91f2bd643803c2696758f6c9c90bf72.zip
src/examples: Fixed drop of 'const' qualifiers and minor fixes.
* some minor fixes and improvements.
Diffstat (limited to 'src')
-rw-r--r--src/examples/authorization_example.c18
-rw-r--r--src/examples/benchmark.c7
-rw-r--r--src/examples/benchmark_https.c7
-rw-r--r--src/examples/demo.c27
-rw-r--r--src/examples/demo_https.c27
-rw-r--r--src/examples/digest_auth_example.c19
-rw-r--r--src/examples/dual_stack_example.c19
-rw-r--r--src/examples/fileserver_example.c9
-rw-r--r--src/examples/fileserver_example_dirs.c7
-rw-r--r--src/examples/fileserver_example_external_select.c9
-rw-r--r--src/examples/https_fileserver_example.c8
-rw-r--r--src/examples/minimal_example.c22
-rw-r--r--src/examples/post_example.c19
-rw-r--r--src/examples/querystring_example.c10
-rw-r--r--src/examples/refuse_post_example.c27
-rw-r--r--src/examples/suspend_resume_epoll.c8
-rw-r--r--src/examples/timeout.c10
-rw-r--r--src/examples/websocket_threaded_example.c19
18 files changed, 153 insertions, 119 deletions
diff --git a/src/examples/authorization_example.c b/src/examples/authorization_example.c
index e7a099ed..d5011e5d 100644
--- a/src/examples/authorization_example.c
+++ b/src/examples/authorization_example.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2008 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2008 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -21,6 +22,7 @@
21 * @file authorization_example.c 22 * @file authorization_example.c
22 * @brief example for how to use libmicrohttpd with HTTP authentication 23 * @brief example for how to use libmicrohttpd with HTTP authentication
23 * @author Christian Grothoff 24 * @author Christian Grothoff
25 * @author Karlson2k (Evgeny Grin)
24 */ 26 */
25 27
26#include "platform.h" 28#include "platform.h"
@@ -48,12 +50,12 @@ ahc_echo (void *cls,
48 const char *upload_data, size_t *upload_data_size, void **req_cls) 50 const char *upload_data, size_t *upload_data_size, void **req_cls)
49{ 51{
50 static int aptr; 52 static int aptr;
51 const char *me = cls;
52 struct MHD_Response *response; 53 struct MHD_Response *response;
53 enum MHD_Result ret; 54 enum MHD_Result ret;
54 char *user; 55 char *user;
55 char *pass; 56 char *pass;
56 int fail; 57 int fail;
58 (void) cls; /* Unused. Silent compiler warning. */
57 (void) url; /* Unused. Silent compiler warning. */ 59 (void) url; /* Unused. Silent compiler warning. */
58 (void) version; /* Unused. Silent compiler warning. */ 60 (void) version; /* Unused. Silent compiler warning. */
59 (void) upload_data; /* Unused. Silent compiler warning. */ 61 (void) upload_data; /* Unused. Silent compiler warning. */
@@ -78,16 +80,16 @@ ahc_echo (void *cls,
78 (0 != strcmp (pass, "open sesame") ) ); 80 (0 != strcmp (pass, "open sesame") ) );
79 if (fail) 81 if (fail)
80 { 82 {
81 response = MHD_create_response_from_buffer (strlen (DENIED), 83 response =
82 (void *) DENIED, 84 MHD_create_response_from_buffer_static (strlen (DENIED),
83 MHD_RESPMEM_PERSISTENT); 85 (const void *) DENIED);
84 ret = MHD_queue_basic_auth_fail_response (connection,"TestRealm",response); 86 ret = MHD_queue_basic_auth_fail_response (connection,"TestRealm",response);
85 } 87 }
86 else 88 else
87 { 89 {
88 response = MHD_create_response_from_buffer (strlen (me), 90 response =
89 (void *) me, 91 MHD_create_response_from_buffer_static (strlen (PAGE),
90 MHD_RESPMEM_PERSISTENT); 92 (const void *) PAGE);
91 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 93 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
92 } 94 }
93 if (NULL != user) 95 if (NULL != user)
@@ -117,7 +119,7 @@ main (int argc, char *const *argv)
117 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 119 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
118 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 120 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
119 atoi (argv[1]), 121 atoi (argv[1]),
120 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 122 NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
121 if (d == NULL) 123 if (d == NULL)
122 return 1; 124 return 1;
123 fprintf (stderr, "HTTP server running. Press ENTER to stop the server.\n"); 125 fprintf (stderr, "HTTP server running. Press ENTER to stop the server.\n");
diff --git a/src/examples/benchmark.c b/src/examples/benchmark.c
index 185e3824..1abdad24 100644
--- a/src/examples/benchmark.c
+++ b/src/examples/benchmark.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2007, 2013 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2007, 2013 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,7 @@
20 * @file benchmark.c 21 * @file benchmark.c
21 * @brief minimal code to benchmark MHD GET performance 22 * @brief minimal code to benchmark MHD GET performance
22 * @author Christian Grothoff 23 * @author Christian Grothoff
24 * @author Karlson2k (Evgeny Grin)
23 */ 25 */
24 26
25#include "platform.h" 27#include "platform.h"
@@ -145,9 +147,8 @@ main (int argc, char *const *argv)
145 printf ("%s PORT\n", argv[0]); 147 printf ("%s PORT\n", argv[0]);
146 return 1; 148 return 1;
147 } 149 }
148 response = MHD_create_response_from_buffer (strlen (PAGE), 150 response = MHD_create_response_from_buffer_static (strlen (PAGE),
149 (void *) PAGE, 151 (const void *) PAGE);
150 MHD_RESPMEM_PERSISTENT);
151#if 0 152#if 0
152 (void) MHD_add_response_header (response, 153 (void) MHD_add_response_header (response,
153 MHD_HTTP_HEADER_CONNECTION, 154 MHD_HTTP_HEADER_CONNECTION,
diff --git a/src/examples/benchmark_https.c b/src/examples/benchmark_https.c
index 36a46aa8..c25e46c3 100644
--- a/src/examples/benchmark_https.c
+++ b/src/examples/benchmark_https.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2007, 2013 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2007, 2013 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,7 @@
20 * @file benchmark_https.c 21 * @file benchmark_https.c
21 * @brief minimal code to benchmark MHD GET performance with HTTPS 22 * @brief minimal code to benchmark MHD GET performance with HTTPS
22 * @author Christian Grothoff 23 * @author Christian Grothoff
24 * @author Karlson2k (Evgeny Grin)
23 */ 25 */
24 26
25#include "platform.h" 27#include "platform.h"
@@ -211,9 +213,8 @@ main (int argc, char *const *argv)
211 printf ("%s PORT\n", argv[0]); 213 printf ("%s PORT\n", argv[0]);
212 return 1; 214 return 1;
213 } 215 }
214 response = MHD_create_response_from_buffer (strlen (PAGE), 216 response = MHD_create_response_from_buffer_static (strlen (PAGE),
215 (void *) PAGE, 217 (const void *) PAGE);
216 MHD_RESPMEM_PERSISTENT);
217 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS 218 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS
218#ifdef EPOLL_SUPPORT 219#ifdef EPOLL_SUPPORT
219 | MHD_USE_EPOLL | MHD_USE_TURBO 220 | MHD_USE_EPOLL | MHD_USE_TURBO
diff --git a/src/examples/demo.c b/src/examples/demo.c
index 6ff7adae..5b3e75d1 100644
--- a/src/examples/demo.c
+++ b/src/examples/demo.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2013 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2013 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -27,6 +28,7 @@
27 * run tests against. Note that the number of threads may need 28 * run tests against. Note that the number of threads may need
28 * to be adjusted depending on the number of available cores. 29 * to be adjusted depending on the number of available cores.
29 * @author Christian Grothoff 30 * @author Christian Grothoff
31 * @author Karlson2k (Evgeny Grin)
30 */ 32 */
31#include "MHD_config.h" 33#include "MHD_config.h"
32#include "platform.h" 34#include "platform.h"
@@ -909,23 +911,18 @@ main (int argc, char *const *argv)
909#endif /* MHD_HAVE_LIBMAGIC */ 911#endif /* MHD_HAVE_LIBMAGIC */
910 912
911 (void) pthread_mutex_init (&mutex, NULL); 913 (void) pthread_mutex_init (&mutex, NULL);
912 file_not_found_response = MHD_create_response_from_buffer (strlen ( 914 file_not_found_response =
913 FILE_NOT_FOUND_PAGE), 915 MHD_create_response_from_buffer_static (strlen (FILE_NOT_FOUND_PAGE),
914 (void *) 916 (const void *) FILE_NOT_FOUND_PAGE);
915 FILE_NOT_FOUND_PAGE,
916 MHD_RESPMEM_PERSISTENT);
917 mark_as_html (file_not_found_response); 917 mark_as_html (file_not_found_response);
918 request_refused_response = MHD_create_response_from_buffer (strlen ( 918 request_refused_response =
919 REQUEST_REFUSED_PAGE), 919 MHD_create_response_from_buffer_static (strlen (REQUEST_REFUSED_PAGE),
920 (void *) 920 (const void *)
921 REQUEST_REFUSED_PAGE, 921 REQUEST_REFUSED_PAGE);
922 MHD_RESPMEM_PERSISTENT);
923 mark_as_html (request_refused_response); 922 mark_as_html (request_refused_response);
924 internal_error_response = MHD_create_response_from_buffer (strlen ( 923 internal_error_response =
925 INTERNAL_ERROR_PAGE), 924 MHD_create_response_from_buffer_static (strlen (INTERNAL_ERROR_PAGE),
926 (void *) 925 (const void *) INTERNAL_ERROR_PAGE);
927 INTERNAL_ERROR_PAGE,
928 MHD_RESPMEM_PERSISTENT);
929 mark_as_html (internal_error_response); 926 mark_as_html (internal_error_response);
930 update_directory (); 927 update_directory ();
931 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD 928 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
index ef59e7d9..397bd770 100644
--- a/src/examples/demo_https.c
+++ b/src/examples/demo_https.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2013 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2013 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2016-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -30,6 +31,7 @@
30 * This demonstration uses key/cert stored in static string. Optionally, 31 * This demonstration uses key/cert stored in static string. Optionally,
31 * use gnutls_load_file() to load them from file. 32 * use gnutls_load_file() to load them from file.
32 * @author Christian Grothoff 33 * @author Christian Grothoff
34 * @author Karlson2k (Evgeny Grin)
33 */ 35 */
34#include "platform.h" 36#include "platform.h"
35#include <microhttpd.h> 37#include <microhttpd.h>
@@ -974,23 +976,18 @@ main (int argc, char *const *argv)
974#endif /* MHD_HAVE_LIBMAGIC */ 976#endif /* MHD_HAVE_LIBMAGIC */
975 977
976 (void) pthread_mutex_init (&mutex, NULL); 978 (void) pthread_mutex_init (&mutex, NULL);
977 file_not_found_response = MHD_create_response_from_buffer (strlen ( 979 file_not_found_response =
978 FILE_NOT_FOUND_PAGE), 980 MHD_create_response_from_buffer_static (strlen (FILE_NOT_FOUND_PAGE),
979 (void *) 981 (const void *) FILE_NOT_FOUND_PAGE);
980 FILE_NOT_FOUND_PAGE,
981 MHD_RESPMEM_PERSISTENT);
982 mark_as_html (file_not_found_response); 982 mark_as_html (file_not_found_response);
983 request_refused_response = MHD_create_response_from_buffer (strlen ( 983 request_refused_response =
984 REQUEST_REFUSED_PAGE), 984 MHD_create_response_from_buffer_static (strlen (REQUEST_REFUSED_PAGE),
985 (void *) 985 (const void *)
986 REQUEST_REFUSED_PAGE, 986 REQUEST_REFUSED_PAGE);
987 MHD_RESPMEM_PERSISTENT);
988 mark_as_html (request_refused_response); 987 mark_as_html (request_refused_response);
989 internal_error_response = MHD_create_response_from_buffer (strlen ( 988 internal_error_response =
990 INTERNAL_ERROR_PAGE), 989 MHD_create_response_from_buffer_static (strlen (INTERNAL_ERROR_PAGE),
991 (void *) 990 (const void *) INTERNAL_ERROR_PAGE);
992 INTERNAL_ERROR_PAGE,
993 MHD_RESPMEM_PERSISTENT);
994 mark_as_html (internal_error_response); 991 mark_as_html (internal_error_response);
995 update_directory (); 992 update_directory ();
996 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD 993 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
index 68d13c84..f8208d97 100644
--- a/src/examples/digest_auth_example.c
+++ b/src/examples/digest_auth_example.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2010 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2010 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2016-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,7 @@
20 * @file digest_auth_example.c 21 * @file digest_auth_example.c
21 * @brief minimal example for how to use digest auth with libmicrohttpd 22 * @brief minimal example for how to use digest auth with libmicrohttpd
22 * @author Amr Ali 23 * @author Amr Ali
24 * @author Karlson2k (Evgeny Grin)
23 */ 25 */
24 26
25#include "platform.h" 27#include "platform.h"
@@ -67,9 +69,9 @@ ahc_echo (void *cls,
67 username = MHD_digest_auth_get_username (connection); 69 username = MHD_digest_auth_get_username (connection);
68 if (NULL == username) 70 if (NULL == username)
69 { 71 {
70 response = MHD_create_response_from_buffer (strlen (DENIED), 72 response =
71 DENIED, 73 MHD_create_response_from_buffer_static (strlen (DENIED),
72 MHD_RESPMEM_PERSISTENT); 74 DENIED);
73 ret = MHD_queue_auth_fail_response2 (connection, realm, 75 ret = MHD_queue_auth_fail_response2 (connection, realm,
74 MY_OPAQUE_STR, 76 MY_OPAQUE_STR,
75 response, 77 response,
@@ -86,9 +88,9 @@ ahc_echo (void *cls,
86 if ( (res == MHD_INVALID_NONCE) || 88 if ( (res == MHD_INVALID_NONCE) ||
87 (res == MHD_NO) ) 89 (res == MHD_NO) )
88 { 90 {
89 response = MHD_create_response_from_buffer (strlen (DENIED), 91 response =
90 DENIED, 92 MHD_create_response_from_buffer_static (strlen (DENIED),
91 MHD_RESPMEM_PERSISTENT); 93 DENIED);
92 if (NULL == response) 94 if (NULL == response)
93 return MHD_NO; 95 return MHD_NO;
94 ret = MHD_queue_auth_fail_response2 (connection, realm, 96 ret = MHD_queue_auth_fail_response2 (connection, realm,
@@ -100,8 +102,7 @@ ahc_echo (void *cls,
100 MHD_destroy_response (response); 102 MHD_destroy_response (response);
101 return ret; 103 return ret;
102 } 104 }
103 response = MHD_create_response_from_buffer (strlen (PAGE), PAGE, 105 response = MHD_create_response_from_buffer_static (strlen (PAGE), PAGE);
104 MHD_RESPMEM_PERSISTENT);
105 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 106 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
106 MHD_destroy_response (response); 107 MHD_destroy_response (response);
107 return ret; 108 return ret;
@@ -148,7 +149,7 @@ main (int argc, char *const *argv)
148 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 149 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
149 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 150 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
150 atoi (argv[1]), 151 atoi (argv[1]),
151 NULL, NULL, &ahc_echo, PAGE, 152 NULL, NULL, &ahc_echo, NULL,
152 MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof(rnd), rnd, 153 MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof(rnd), rnd,
153 MHD_OPTION_NONCE_NC_SIZE, 300, 154 MHD_OPTION_NONCE_NC_SIZE, 300,
154 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 155 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
diff --git a/src/examples/dual_stack_example.c b/src/examples/dual_stack_example.c
index 175583ac..957daf07 100644
--- a/src/examples/dual_stack_example.c
+++ b/src/examples/dual_stack_example.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2007, 2012 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2007, 2012 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,7 @@
20 * @file dual_stack_example.c 21 * @file dual_stack_example.c
21 * @brief how to use MHD with both IPv4 and IPv6 support (dual-stack) 22 * @brief how to use MHD with both IPv4 and IPv6 support (dual-stack)
22 * @author Christian Grothoff 23 * @author Christian Grothoff
24 * @author Karlson2k (Evgeny Grin)
23 */ 25 */
24 26
25#include "platform.h" 27#include "platform.h"
@@ -28,6 +30,11 @@
28#define PAGE \ 30#define PAGE \
29 "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" 31 "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
30 32
33struct handler_param
34{
35 const char *response_page;
36};
37
31static enum MHD_Result 38static enum MHD_Result
32ahc_echo (void *cls, 39ahc_echo (void *cls,
33 struct MHD_Connection *connection, 40 struct MHD_Connection *connection,
@@ -37,7 +44,7 @@ ahc_echo (void *cls,
37 const char *upload_data, size_t *upload_data_size, void **req_cls) 44 const char *upload_data, size_t *upload_data_size, void **req_cls)
38{ 45{
39 static int aptr; 46 static int aptr;
40 const char *me = cls; 47 struct handler_param *param = (struct handler_param *) cls;
41 struct MHD_Response *response; 48 struct MHD_Response *response;
42 enum MHD_Result ret; 49 enum MHD_Result ret;
43 (void) url; /* Unused. Silent compiler warning. */ 50 (void) url; /* Unused. Silent compiler warning. */
@@ -54,9 +61,9 @@ ahc_echo (void *cls,
54 return MHD_YES; 61 return MHD_YES;
55 } 62 }
56 *req_cls = NULL; /* reset when done */ 63 *req_cls = NULL; /* reset when done */
57 response = MHD_create_response_from_buffer (strlen (me), 64 response =
58 (void *) me, 65 MHD_create_response_from_buffer_static (strlen (param->response_page),
59 MHD_RESPMEM_PERSISTENT); 66 param->response_page);
60 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 67 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
61 MHD_destroy_response (response); 68 MHD_destroy_response (response);
62 return ret; 69 return ret;
@@ -67,16 +74,18 @@ int
67main (int argc, char *const *argv) 74main (int argc, char *const *argv)
68{ 75{
69 struct MHD_Daemon *d; 76 struct MHD_Daemon *d;
77 struct handler_param data_for_handler;
70 78
71 if (argc != 2) 79 if (argc != 2)
72 { 80 {
73 printf ("%s PORT\n", argv[0]); 81 printf ("%s PORT\n", argv[0]);
74 return 1; 82 return 1;
75 } 83 }
84 data_for_handler.response_page = PAGE;
76 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG 85 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
77 | MHD_USE_DUAL_STACK, 86 | MHD_USE_DUAL_STACK,
78 atoi (argv[1]), 87 atoi (argv[1]),
79 NULL, NULL, &ahc_echo, PAGE, 88 NULL, NULL, &ahc_echo, &data_for_handler,
80 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 89 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
81 MHD_OPTION_END); 90 MHD_OPTION_END);
82 (void) getc (stdin); 91 (void) getc (stdin);
diff --git a/src/examples/fileserver_example.c b/src/examples/fileserver_example.c
index 76879eec..e300f6ed 100644
--- a/src/examples/fileserver_example.c
+++ b/src/examples/fileserver_example.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2007 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2007 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2016-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,7 @@
20 * @file fileserver_example.c 21 * @file fileserver_example.c
21 * @brief minimal example for how to use libmicrohttpd to serve files 22 * @brief minimal example for how to use libmicrohttpd to serve files
22 * @author Christian Grothoff 23 * @author Christian Grothoff
24 * @author Karlson2k (Evgeny Grin)
23 */ 25 */
24 26
25#include "platform.h" 27#include "platform.h"
@@ -91,9 +93,8 @@ ahc_echo (void *cls,
91 } 93 }
92 if (-1 == fd) 94 if (-1 == fd)
93 { 95 {
94 response = MHD_create_response_from_buffer (strlen (PAGE), 96 response = MHD_create_response_from_buffer_static (strlen (PAGE),
95 (void *) PAGE, 97 PAGE);
96 MHD_RESPMEM_PERSISTENT);
97 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); 98 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
98 MHD_destroy_response (response); 99 MHD_destroy_response (response);
99 } 100 }
@@ -126,7 +127,7 @@ main (int argc, char *const *argv)
126 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 127 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
127 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 128 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
128 atoi (argv[1]), 129 atoi (argv[1]),
129 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 130 NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
130 if (d == NULL) 131 if (d == NULL)
131 return 1; 132 return 1;
132 (void) getc (stdin); 133 (void) getc (stdin);
diff --git a/src/examples/fileserver_example_dirs.c b/src/examples/fileserver_example_dirs.c
index 796cb42e..0a977ddf 100644
--- a/src/examples/fileserver_example_dirs.c
+++ b/src/examples/fileserver_example_dirs.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2007 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2007 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2016-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -21,6 +22,7 @@
21 * @file fileserver_example_dirs.c 22 * @file fileserver_example_dirs.c
22 * @brief example for how to use libmicrohttpd to serve files (with directory support) 23 * @brief example for how to use libmicrohttpd to serve files (with directory support)
23 * @author Christian Grothoff 24 * @author Christian Grothoff
25 * @author Karlson2k (Evgeny Grin)
24 */ 26 */
25 27
26#include "platform.h" 28#include "platform.h"
@@ -28,9 +30,6 @@
28#include <microhttpd.h> 30#include <microhttpd.h>
29#include <unistd.h> 31#include <unistd.h>
30 32
31#define PAGE \
32 "<html><head><title>File not found</title></head><body>File not found</body></html>"
33
34 33
35static ssize_t 34static ssize_t
36file_reader (void *cls, uint64_t pos, char *buf, size_t max) 35file_reader (void *cls, uint64_t pos, char *buf, size_t max)
@@ -199,7 +198,7 @@ main (int argc, char *const *argv)
199 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 198 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
200 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 199 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
201 atoi (argv[1]), 200 atoi (argv[1]),
202 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 201 NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
203 if (NULL == d) 202 if (NULL == d)
204 return 1; 203 return 1;
205 (void) getc (stdin); 204 (void) getc (stdin);
diff --git a/src/examples/fileserver_example_external_select.c b/src/examples/fileserver_example_external_select.c
index 09f7be0a..cdd455ec 100644
--- a/src/examples/fileserver_example_external_select.c
+++ b/src/examples/fileserver_example_external_select.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,7 @@
20 * @file fileserver_example_external_select.c 21 * @file fileserver_example_external_select.c
21 * @brief minimal example for how to use libmicrohttpd to server files 22 * @brief minimal example for how to use libmicrohttpd to server files
22 * @author Christian Grothoff 23 * @author Christian Grothoff
24 * @author Karlson2k (Evgeny Grin)
23 */ 25 */
24 26
25#include "platform.h" 27#include "platform.h"
@@ -98,9 +100,8 @@ ahc_echo (void *cls,
98 100
99 if (NULL == file) 101 if (NULL == file)
100 { 102 {
101 response = MHD_create_response_from_buffer (strlen (PAGE), 103 response = MHD_create_response_from_buffer_static (strlen (PAGE),
102 (void *) PAGE, 104 PAGE);
103 MHD_RESPMEM_PERSISTENT);
104 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); 105 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
105 MHD_destroy_response (response); 106 MHD_destroy_response (response);
106 } 107 }
@@ -142,7 +143,7 @@ main (int argc, char *const *argv)
142 } 143 }
143 d = MHD_start_daemon (MHD_USE_ERROR_LOG, 144 d = MHD_start_daemon (MHD_USE_ERROR_LOG,
144 atoi (argv[1]), 145 atoi (argv[1]),
145 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 146 NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
146 if (d == NULL) 147 if (d == NULL)
147 return 1; 148 return 1;
148 end = time (NULL) + atoi (argv[2]); 149 end = time (NULL) + atoi (argv[2]);
diff --git a/src/examples/https_fileserver_example.c b/src/examples/https_fileserver_example.c
index 5dc340f7..9f17d0a7 100644
--- a/src/examples/https_fileserver_example.c
+++ b/src/examples/https_fileserver_example.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2016-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -30,6 +31,7 @@
30 * 'certtool' may be used to generate these if required. 31 * 'certtool' may be used to generate these if required.
31 * 32 *
32 * @author Sagie Amir 33 * @author Sagie Amir
34 * @author Karlson2k (Evgeny Grin)
33 */ 35 */
34 36
35#include "platform.h" 37#include "platform.h"
@@ -180,9 +182,9 @@ http_ahc (void *cls,
180 182
181 if (NULL == file) 183 if (NULL == file)
182 { 184 {
183 response = MHD_create_response_from_buffer (strlen (EMPTY_PAGE), 185 response =
184 (void *) EMPTY_PAGE, 186 MHD_create_response_from_buffer_static (strlen (EMPTY_PAGE),
185 MHD_RESPMEM_PERSISTENT); 187 (const void *) EMPTY_PAGE);
186 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); 188 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
187 MHD_destroy_response (response); 189 MHD_destroy_response (response);
188 } 190 }
diff --git a/src/examples/minimal_example.c b/src/examples/minimal_example.c
index ce604555..1e3f63f7 100644
--- a/src/examples/minimal_example.c
+++ b/src/examples/minimal_example.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2007 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2007 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -20,13 +21,20 @@
20 * @file minimal_example.c 21 * @file minimal_example.c
21 * @brief minimal example for how to use libmicrohttpd 22 * @brief minimal example for how to use libmicrohttpd
22 * @author Christian Grothoff 23 * @author Christian Grothoff
24 * @author Karlson2k (Evgeny Grin)
23 */ 25 */
24 26
25#include "platform.h" 27#include "platform.h"
26#include <microhttpd.h> 28#include <microhttpd.h>
27 29
28#define PAGE \ 30#define PAGE \
29 "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" 31 "<html><head><title>libmicrohttpd demo</title></head>" \
32 "<body>libmicrohttpd demo</body></html>"
33
34struct handler_param
35{
36 const char *response_page;
37};
30 38
31static enum MHD_Result 39static enum MHD_Result
32ahc_echo (void *cls, 40ahc_echo (void *cls,
@@ -39,7 +47,7 @@ ahc_echo (void *cls,
39 void **req_cls) 47 void **req_cls)
40{ 48{
41 static int aptr; 49 static int aptr;
42 const char *me = cls; 50 struct handler_param *param = (struct handler_param *) cls;
43 struct MHD_Response *response; 51 struct MHD_Response *response;
44 enum MHD_Result ret; 52 enum MHD_Result ret;
45 53
@@ -57,9 +65,9 @@ ahc_echo (void *cls,
57 return MHD_YES; 65 return MHD_YES;
58 } 66 }
59 *req_cls = NULL; /* reset when done */ 67 *req_cls = NULL; /* reset when done */
60 response = MHD_create_response_from_buffer (strlen (me), 68 response =
61 (void *) me, 69 MHD_create_response_from_buffer_static (strlen (param->response_page),
62 MHD_RESPMEM_PERSISTENT); 70 param->response_page);
63 ret = MHD_queue_response (connection, 71 ret = MHD_queue_response (connection,
64 MHD_HTTP_OK, 72 MHD_HTTP_OK,
65 response); 73 response);
@@ -73,19 +81,21 @@ main (int argc,
73 char *const *argv) 81 char *const *argv)
74{ 82{
75 struct MHD_Daemon *d; 83 struct MHD_Daemon *d;
84 struct handler_param data_for_handler;
76 85
77 if (argc != 2) 86 if (argc != 2)
78 { 87 {
79 printf ("%s PORT\n", argv[0]); 88 printf ("%s PORT\n", argv[0]);
80 return 1; 89 return 1;
81 } 90 }
91 data_for_handler.response_page = PAGE;
82 d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ 92 d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
83 MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 93 MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
84 /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */ 94 /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
85 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */ 95 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
86 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ 96 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
87 atoi (argv[1]), 97 atoi (argv[1]),
88 NULL, NULL, &ahc_echo, PAGE, 98 NULL, NULL, &ahc_echo, &data_for_handler,
89 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 99 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
90 MHD_OPTION_STRICT_FOR_CLIENT, (int) 1, 100 MHD_OPTION_STRICT_FOR_CLIENT, (int) 1,
91 MHD_OPTION_END); 101 MHD_OPTION_END);
diff --git a/src/examples/post_example.c b/src/examples/post_example.c
index d8f58d49..5df56afb 100644
--- a/src/examples/post_example.c
+++ b/src/examples/post_example.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2011 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2011 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,7 @@
20 * @file post_example.c 21 * @file post_example.c
21 * @brief example for processing POST requests using libmicrohttpd 22 * @brief example for processing POST requests using libmicrohttpd
22 * @author Christian Grothoff 23 * @author Christian Grothoff
24 * @author Karlson2k (Evgeny Grin)
23 */ 25 */
24 26
25#include <stdlib.h> 27#include <stdlib.h>
@@ -285,9 +287,8 @@ serve_simple_form (const void *cls,
285 struct MHD_Response *response; 287 struct MHD_Response *response;
286 288
287 /* return static form */ 289 /* return static form */
288 response = MHD_create_response_from_buffer (strlen (form), 290 response = MHD_create_response_from_buffer_static (strlen (form),
289 (void *) form, 291 (const void *) form);
290 MHD_RESPMEM_PERSISTENT);
291 if (NULL == response) 292 if (NULL == response)
292 return MHD_NO; 293 return MHD_NO;
293 add_session_cookie (session, response); 294 add_session_cookie (session, response);
@@ -422,9 +423,9 @@ not_found_page (const void *cls,
422 (void) session; /* Unused. Silent compiler warning. */ 423 (void) session; /* Unused. Silent compiler warning. */
423 424
424 /* unsupported HTTP method */ 425 /* unsupported HTTP method */
425 response = MHD_create_response_from_buffer (strlen (NOT_FOUND_ERROR), 426 response =
426 (void *) NOT_FOUND_ERROR, 427 MHD_create_response_from_buffer_static (strlen (NOT_FOUND_ERROR),
427 MHD_RESPMEM_PERSISTENT); 428 (const void *) NOT_FOUND_ERROR);
428 if (NULL == response) 429 if (NULL == response)
429 return MHD_NO; 430 return MHD_NO;
430 ret = MHD_queue_response (connection, 431 ret = MHD_queue_response (connection,
@@ -643,9 +644,9 @@ create_response (void *cls,
643 return ret; 644 return ret;
644 } 645 }
645 /* unsupported HTTP method */ 646 /* unsupported HTTP method */
646 response = MHD_create_response_from_buffer (strlen (METHOD_ERROR), 647 response =
647 (void *) METHOD_ERROR, 648 MHD_create_response_from_buffer_static (strlen (METHOD_ERROR),
648 MHD_RESPMEM_PERSISTENT); 649 (const void *) METHOD_ERROR);
649 ret = MHD_queue_response (connection, 650 ret = MHD_queue_response (connection,
650 MHD_HTTP_NOT_ACCEPTABLE, 651 MHD_HTTP_NOT_ACCEPTABLE,
651 response); 652 response);
diff --git a/src/examples/querystring_example.c b/src/examples/querystring_example.c
index d33767cb..57de5aa7 100644
--- a/src/examples/querystring_example.c
+++ b/src/examples/querystring_example.c
@@ -38,12 +38,12 @@ ahc_echo (void *cls,
38 const char *upload_data, size_t *upload_data_size, void **req_cls) 38 const char *upload_data, size_t *upload_data_size, void **req_cls)
39{ 39{
40 static int aptr; 40 static int aptr;
41 const char *fmt = cls;
42 const char *val; 41 const char *val;
43 char *me; 42 char *me;
44 struct MHD_Response *response; 43 struct MHD_Response *response;
45 enum MHD_Result ret; 44 enum MHD_Result ret;
46 int resp_len; 45 int resp_len;
46 (void) cls; /* Unused. Silent compiler warning. */
47 (void) url; /* Unused. Silent compiler warning. */ 47 (void) url; /* Unused. Silent compiler warning. */
48 (void) version; /* Unused. Silent compiler warning. */ 48 (void) version; /* Unused. Silent compiler warning. */
49 (void) upload_data; /* Unused. Silent compiler warning. */ 49 (void) upload_data; /* Unused. Silent compiler warning. */
@@ -58,18 +58,16 @@ ahc_echo (void *cls,
58 return MHD_YES; 58 return MHD_YES;
59 } 59 }
60 *req_cls = NULL; /* reset when done */ 60 *req_cls = NULL; /* reset when done */
61 if (NULL == fmt)
62 return MHD_NO; /* The cls must not be NULL */
63 val = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "q"); 61 val = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "q");
64 if (NULL == val) 62 if (NULL == val)
65 return MHD_NO; /* No "q" argument was found */ 63 return MHD_NO; /* No "q" argument was found */
66 resp_len = snprintf (NULL, 0, fmt, "q", val); 64 resp_len = snprintf (NULL, 0, PAGE, "q", val);
67 if (0 > resp_len) 65 if (0 > resp_len)
68 return MHD_NO; /* Error calculating response size */ 66 return MHD_NO; /* Error calculating response size */
69 me = malloc (resp_len + 1); 67 me = malloc (resp_len + 1);
70 if (me == NULL) 68 if (me == NULL)
71 return MHD_NO; /* Error allocating memory */ 69 return MHD_NO; /* Error allocating memory */
72 if (resp_len != snprintf (me, resp_len + 1, fmt, "q", val)) 70 if (resp_len != snprintf (me, resp_len + 1, PAGE, "q", val))
73 { 71 {
74 free (me); 72 free (me);
75 return MHD_NO; /* Error forming the response body */ 73 return MHD_NO; /* Error forming the response body */
@@ -108,7 +106,7 @@ main (int argc, char *const *argv)
108 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 106 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
109 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 107 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
110 (uint16_t) port, 108 (uint16_t) port,
111 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 109 NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
112 if (NULL == d) 110 if (NULL == d)
113 return 1; 111 return 1;
114 (void) getc (stdin); 112 (void) getc (stdin);
diff --git a/src/examples/refuse_post_example.c b/src/examples/refuse_post_example.c
index dc261575..70cfe4b3 100644
--- a/src/examples/refuse_post_example.c
+++ b/src/examples/refuse_post_example.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2016-2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -20,10 +21,16 @@
20 * @file refuse_post_example.c 21 * @file refuse_post_example.c
21 * @brief example for how to refuse a POST request properly 22 * @brief example for how to refuse a POST request properly
22 * @author Christian Grothoff and Sebastian Gerhardt 23 * @author Christian Grothoff and Sebastian Gerhardt
24 * @author Karlson2k (Evgeny Grin)
23 */ 25 */
24#include "platform.h" 26#include "platform.h"
25#include <microhttpd.h> 27#include <microhttpd.h>
26 28
29struct handler_param
30{
31 const char *response_page;
32};
33
27const char *askpage = 34const char *askpage =
28 "<html><body>\n\ 35 "<html><body>\n\
29 Upload a file, please!<br>\n\ 36 Upload a file, please!<br>\n\
@@ -44,7 +51,7 @@ ahc_echo (void *cls,
44 const char *upload_data, size_t *upload_data_size, void **req_cls) 51 const char *upload_data, size_t *upload_data_size, void **req_cls)
45{ 52{
46 static int aptr; 53 static int aptr;
47 const char *me = cls; 54 struct handler_param *param = (struct handler_param *) cls;
48 struct MHD_Response *response; 55 struct MHD_Response *response;
49 enum MHD_Result ret; 56 enum MHD_Result ret;
50 (void) cls; /* Unused. Silent compiler warning. */ 57 (void) cls; /* Unused. Silent compiler warning. */
@@ -63,9 +70,9 @@ ahc_echo (void *cls,
63 /* always to busy for POST requests */ 70 /* always to busy for POST requests */
64 if (0 == strcmp (method, "POST")) 71 if (0 == strcmp (method, "POST"))
65 { 72 {
66 response = MHD_create_response_from_buffer (strlen (BUSYPAGE), 73 response =
67 (void *) BUSYPAGE, 74 MHD_create_response_from_buffer_static (strlen (BUSYPAGE),
68 MHD_RESPMEM_PERSISTENT); 75 (const void *) BUSYPAGE);
69 ret = 76 ret =
70 MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE, 77 MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE,
71 response); 78 response);
@@ -75,9 +82,10 @@ ahc_echo (void *cls,
75 } 82 }
76 83
77 *req_cls = NULL; /* reset when done */ 84 *req_cls = NULL; /* reset when done */
78 response = MHD_create_response_from_buffer (strlen (me), 85 response =
79 (void *) me, 86 MHD_create_response_from_buffer_static (strlen (param->response_page),
80 MHD_RESPMEM_PERSISTENT); 87 (const void *)
88 param->response_page);
81 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 89 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
82 MHD_destroy_response (response); 90 MHD_destroy_response (response);
83 return ret; 91 return ret;
@@ -89,15 +97,18 @@ main (int argc, char *const *argv)
89{ 97{
90 struct MHD_Daemon *d; 98 struct MHD_Daemon *d;
91 99
100 struct handler_param data_for_handler;
101
92 if (argc != 2) 102 if (argc != 2)
93 { 103 {
94 printf ("%s PORT\n", argv[0]); 104 printf ("%s PORT\n", argv[0]);
95 return 1; 105 return 1;
96 } 106 }
107 data_for_handler.response_page = askpage;
97 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 108 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
98 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 109 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
99 atoi (argv[1]), 110 atoi (argv[1]),
100 NULL, NULL, &ahc_echo, (void *) askpage, 111 NULL, NULL, &ahc_echo, &data_for_handler,
101 MHD_OPTION_END); 112 MHD_OPTION_END);
102 if (d == NULL) 113 if (d == NULL)
103 return 1; 114 return 1;
diff --git a/src/examples/suspend_resume_epoll.c b/src/examples/suspend_resume_epoll.c
index 6b660b00..b63f80be 100644
--- a/src/examples/suspend_resume_epoll.c
+++ b/src/examples/suspend_resume_epoll.c
@@ -1,6 +1,7 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2018 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2018 Christian Grothoff (and other contributing authors)
4 Copyright (C) 2022 Evgeny Grin (Karlson2k)
4 5
5 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public 7 modify it under the terms of the GNU Lesser General Public
@@ -22,6 +23,7 @@
22 * resume a suspended connection 23 * resume a suspended connection
23 * @author Robert D Kocisko 24 * @author Robert D Kocisko
24 * @author Christian Grothoff 25 * @author Christian Grothoff
26 * @author Karlson2k (Evgeny Grin)
25 */ 27 */
26#include "platform.h" 28#include "platform.h"
27#include <microhttpd.h> 29#include <microhttpd.h>
@@ -57,7 +59,6 @@ ahc_echo (void *cls,
57 struct itimerspec ts; 59 struct itimerspec ts;
58 60
59 (void) cls; 61 (void) cls;
60 (void) url; /* Unused. Silence compiler warning. */
61 (void) method; 62 (void) method;
62 (void) version; /* Unused. Silence compiler warning. */ 63 (void) version; /* Unused. Silence compiler warning. */
63 (void) upload_data; /* Unused. Silence compiler warning. */ 64 (void) upload_data; /* Unused. Silence compiler warning. */
@@ -78,9 +79,8 @@ ahc_echo (void *cls,
78 if (-1 != req->timerfd) 79 if (-1 != req->timerfd)
79 { 80 {
80 /* send response (echo request url) */ 81 /* send response (echo request url) */
81 response = MHD_create_response_from_buffer (strlen (url), 82 response = MHD_create_response_from_buffer_copy (strlen (url),
82 (void *) url, 83 (const void *) url);
83 MHD_RESPMEM_MUST_COPY);
84 if (NULL == response) 84 if (NULL == response)
85 return MHD_NO; 85 return MHD_NO;
86 ret = MHD_queue_response (connection, 86 ret = MHD_queue_response (connection,
diff --git a/src/examples/timeout.c b/src/examples/timeout.c
index b52f508f..dacc93fd 100644
--- a/src/examples/timeout.c
+++ b/src/examples/timeout.c
@@ -1,7 +1,8 @@
1/* 1/*
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2016, 2017 Christian Grothoff, 3 Copyright (C) 2016-2017 Christian Grothoff,
4 Silvio Clecio (silvioprog), Karlson2k (Evgeny Grin) 4 Silvio Clecio (silvioprog), Evgeny Grin (Karlson2k)
5 Copyright (C) 2022 Evgeny Grin (Karlson2k)
5 6
6 This library is free software; you can redistribute it and/or 7 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public 8 modify it under the terms of the GNU Lesser General Public
@@ -50,9 +51,8 @@ answer_to_connection (void *cls,
50 (void) upload_data_size; /* Unused. Silent compiler warning. */ 51 (void) upload_data_size; /* Unused. Silent compiler warning. */
51 (void) req_cls; /* Unused. Silent compiler warning. */ 52 (void) req_cls; /* Unused. Silent compiler warning. */
52 53
53 response = MHD_create_response_from_buffer (strlen (page), 54 response = MHD_create_response_from_buffer_static (strlen (page),
54 (void *) page, 55 (const void *) page);
55 MHD_RESPMEM_PERSISTENT);
56 MHD_add_response_header (response, 56 MHD_add_response_header (response,
57 MHD_HTTP_HEADER_CONTENT_TYPE, 57 MHD_HTTP_HEADER_CONTENT_TYPE,
58 "text/html"); 58 "text/html");
diff --git a/src/examples/websocket_threaded_example.c b/src/examples/websocket_threaded_example.c
index 04b972d4..542d7c3a 100644
--- a/src/examples/websocket_threaded_example.c
+++ b/src/examples/websocket_threaded_example.c
@@ -2,6 +2,7 @@
2 This file is part of libmicrohttpd 2 This file is part of libmicrohttpd
3 Copyright (C) 2020 Christian Grothoff, Silvio Clecio (and other 3 Copyright (C) 2020 Christian Grothoff, Silvio Clecio (and other
4 contributing authors) 4 contributing authors)
5 Copyright (C) 2020-2022 Evgeny Grin (Karlson2k)
5 6
6 This library is free software; you can redistribute it and/or 7 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public 8 modify it under the terms of the GNU Lesser General Public
@@ -22,6 +23,7 @@
22 * @file websocket_threaded_example.c 23 * @file websocket_threaded_example.c
23 * @brief example for how to provide a tiny threaded websocket server 24 * @brief example for how to provide a tiny threaded websocket server
24 * @author Silvio Clecio (silvioprog) 25 * @author Silvio Clecio (silvioprog)
26 * @author Karlson2k (Evgeny Grin)
25 */ 27 */
26 28
27/* TODO: allow to send large messages. */ 29/* TODO: allow to send large messages. */
@@ -434,8 +436,8 @@ send_chat_page (struct MHD_Connection *con)
434 struct MHD_Response *res; 436 struct MHD_Response *res;
435 enum MHD_Result ret; 437 enum MHD_Result ret;
436 438
437 res = MHD_create_response_from_buffer (strlen (CHAT_PAGE), (void *) CHAT_PAGE, 439 res = MHD_create_response_from_buffer_static (strlen (CHAT_PAGE),
438 MHD_RESPMEM_PERSISTENT); 440 (const void *) CHAT_PAGE);
439 ret = MHD_queue_response (con, MHD_HTTP_OK, res); 441 ret = MHD_queue_response (con, MHD_HTTP_OK, res);
440 MHD_destroy_response (res); 442 MHD_destroy_response (res);
441 return ret; 443 return ret;
@@ -448,9 +450,9 @@ send_bad_request (struct MHD_Connection *con)
448 struct MHD_Response *res; 450 struct MHD_Response *res;
449 enum MHD_Result ret; 451 enum MHD_Result ret;
450 452
451 res = MHD_create_response_from_buffer (strlen (BAD_REQUEST_PAGE), 453 res =
452 (void *) BAD_REQUEST_PAGE, 454 MHD_create_response_from_buffer_static (strlen (BAD_REQUEST_PAGE),
453 MHD_RESPMEM_PERSISTENT); 455 (const void *) BAD_REQUEST_PAGE);
454 ret = MHD_queue_response (con, MHD_HTTP_BAD_REQUEST, res); 456 ret = MHD_queue_response (con, MHD_HTTP_BAD_REQUEST, res);
455 MHD_destroy_response (res); 457 MHD_destroy_response (res);
456 return ret; 458 return ret;
@@ -463,9 +465,10 @@ send_upgrade_required (struct MHD_Connection *con)
463 struct MHD_Response *res; 465 struct MHD_Response *res;
464 enum MHD_Result ret; 466 enum MHD_Result ret;
465 467
466 res = MHD_create_response_from_buffer (strlen (UPGRADE_REQUIRED_PAGE), 468 res =
467 (void *) UPGRADE_REQUIRED_PAGE, 469 MHD_create_response_from_buffer_static (strlen (UPGRADE_REQUIRED_PAGE),
468 MHD_RESPMEM_PERSISTENT); 470 (const void *)
471 UPGRADE_REQUIRED_PAGE);
469 MHD_add_response_header (res, MHD_HTTP_HEADER_SEC_WEBSOCKET_VERSION, 472 MHD_add_response_header (res, MHD_HTTP_HEADER_SEC_WEBSOCKET_VERSION,
470 WS_SEC_WEBSOCKET_VERSION); 473 WS_SEC_WEBSOCKET_VERSION);
471 ret = MHD_queue_response (con, MHD_HTTP_UPGRADE_REQUIRED, res); 474 ret = MHD_queue_response (con, MHD_HTTP_UPGRADE_REQUIRED, res);