From a13647d1d0418d612010e8ceb6fe560ffaffcf2d Mon Sep 17 00:00:00 2001 From: "Evgeny Grin (Karlson2k)" Date: Sat, 14 May 2022 14:36:39 +0300 Subject: Replaced MHD_RESPMEM_MUST_FREE with more portable solution in examples --- doc/examples/sessions.c | 32 +++++++++++++++++++------------- src/examples/demo.c | 7 ++++--- src/examples/demo_https.c | 7 ++++--- src/examples/http_compression.c | 8 +++++--- src/examples/post_example.c | 14 ++++++++------ src/examples/querystring_example.c | 6 ++++-- src/include/microhttpd.h | 10 ++++++---- 7 files changed, 50 insertions(+), 34 deletions(-) diff --git a/doc/examples/sessions.c b/doc/examples/sessions.c index 9b36c485..121cf2e0 100644 --- a/doc/examples/sessions.c +++ b/doc/examples/sessions.c @@ -347,19 +347,22 @@ fill_v1_form (const void *cls, enum MHD_Result ret; char *reply; struct MHD_Response *response; + int reply_len; (void) cls; /* Unused */ - if (-1 == MHD_asprintf (&reply, - MAIN_PAGE, - session->value_1)) + reply_len = MHD_asprintf (&reply, + MAIN_PAGE, + session->value_1); + if (0 > reply_len) { /* oops */ return MHD_NO; } /* return static form */ - response = MHD_create_response_from_buffer (strlen (reply), - (void *) reply, - MHD_RESPMEM_MUST_FREE); + response = + MHD_create_response_from_buffer_with_free_callback ((size_t) reply_len, + (void *) reply, + &free); add_session_cookie (session, response); MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_ENCODING, @@ -389,20 +392,23 @@ fill_v1_v2_form (const void *cls, enum MHD_Result ret; char *reply; struct MHD_Response *response; + int reply_len; (void) cls; /* Unused */ - if (-1 == MHD_asprintf (&reply, - SECOND_PAGE, - session->value_1, - session->value_2)) + reply_len = MHD_asprintf (&reply, + SECOND_PAGE, + session->value_1, + session->value_2); + if (0 > reply_len) { /* oops */ return MHD_NO; } /* return static form */ - response = MHD_create_response_from_buffer (strlen (reply), - (void *) reply, - MHD_RESPMEM_MUST_FREE); + response = + MHD_create_response_from_buffer_with_free_callback (reply_len, + (void *) reply, + &free); add_session_cookie (session, response); MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_ENCODING, diff --git a/src/examples/demo.c b/src/examples/demo.c index 1c24a57e..9aef573a 100644 --- a/src/examples/demo.c +++ b/src/examples/demo.c @@ -383,9 +383,10 @@ update_directory (void) "%s", INDEX_PAGE_FOOTER); initial_allocation = rdc.buf_len; /* remember for next time */ - response = MHD_create_response_from_buffer (rdc.off, - rdc.buf, - MHD_RESPMEM_MUST_FREE); + response = + MHD_create_response_from_buffer_with_free_callback (rdc.off, + rdc.buf, + &free); mark_as_html (response); #if FORCE_CLOSE (void) MHD_add_response_header (response, diff --git a/src/examples/demo_https.c b/src/examples/demo_https.c index 1a09d2c2..56008d7f 100644 --- a/src/examples/demo_https.c +++ b/src/examples/demo_https.c @@ -385,9 +385,10 @@ update_directory (void) "%s", INDEX_PAGE_FOOTER); initial_allocation = rdc.buf_len; /* remember for next time */ - response = MHD_create_response_from_buffer (rdc.off, - rdc.buf, - MHD_RESPMEM_MUST_FREE); + response = + MHD_create_response_from_buffer_with_free_callback (rdc.off, + rdc.buf, + &free); mark_as_html (response); #if FORCE_CLOSE (void) MHD_add_response_header (response, diff --git a/src/examples/http_compression.c b/src/examples/http_compression.c index a91a39a3..0f532cf0 100644 --- a/src/examples/http_compression.c +++ b/src/examples/http_compression.c @@ -130,9 +130,11 @@ ahc_echo (void *cls, can_compress (connection)) comp = body_compress ((void **) &body_str, &body_len); - response = MHD_create_response_from_buffer (body_len, - body_str, - MHD_RESPMEM_MUST_FREE); + response = + MHD_create_response_from_buffer_with_free_callback (body_len, + body_str, + &free); + if (NULL == response) { free (body_str); diff --git a/src/examples/post_example.c b/src/examples/post_example.c index 709fc918..1b6a5a03 100644 --- a/src/examples/post_example.c +++ b/src/examples/post_example.c @@ -332,9 +332,10 @@ fill_v1_form (const void *cls, MAIN_PAGE, session->value_1); /* return static form */ - response = MHD_create_response_from_buffer (slen, - (void *) reply, - MHD_RESPMEM_MUST_FREE); + response = + MHD_create_response_from_buffer_with_free_callback (slen, + (void *) reply, + &free); if (NULL == response) { free (reply); @@ -383,9 +384,10 @@ fill_v1_v2_form (const void *cls, session->value_1, session->value_2); /* return static form */ - response = MHD_create_response_from_buffer (slen, - (void *) reply, - MHD_RESPMEM_MUST_FREE); + response = + MHD_create_response_from_buffer_with_free_callback (slen, + (void *) reply, + &free); if (NULL == response) { free (reply); diff --git a/src/examples/querystring_example.c b/src/examples/querystring_example.c index 57de5aa7..97a92ae2 100644 --- a/src/examples/querystring_example.c +++ b/src/examples/querystring_example.c @@ -72,8 +72,10 @@ ahc_echo (void *cls, free (me); return MHD_NO; /* Error forming the response body */ } - response = MHD_create_response_from_buffer (resp_len, me, - MHD_RESPMEM_MUST_FREE); + response = + MHD_create_response_from_buffer_with_free_callback (resp_len, + (void *) me, + &free); if (response == NULL) { free (me); diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h index 921ab56b..2f3a99a9 100644 --- a/src/include/microhttpd.h +++ b/src/include/microhttpd.h @@ -3624,10 +3624,12 @@ enum MHD_ResponseMemoryMode * Buffer is heap-allocated with `malloc()` (or equivalent) and * should be freed by MHD after processing the response has * concluded (response reference counter reaches zero). - * @warning Make sure that your application and MHD are using the same - * C-runtime library (especially important for W32). if in doubt, - * use function MHD_create_response_from_buffer_with_free_callback() - * with '&free' as crfc parameter. + * The more portable way to automatically free the buffer is function + * MHD_create_response_from_buffer_with_free_callback() with '&free' as + * crfc parameter as it does not require to use the same runtime library. + * @warning It is critical to make sure that the same C-runtime library + * is used by both application and MHD (especially + * important for W32). * @ingroup response */ MHD_RESPMEM_MUST_FREE, -- cgit v1.2.3