diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-05-14 14:36:39 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-05-14 14:37:02 +0300 |
commit | a13647d1d0418d612010e8ceb6fe560ffaffcf2d (patch) | |
tree | 79526b4486e76b9589e2e504b669290064e0d833 | |
parent | f50b6be7a6088dbff621613bfec82d6fef62dca3 (diff) | |
download | libmicrohttpd-a13647d1d0418d612010e8ceb6fe560ffaffcf2d.tar.gz libmicrohttpd-a13647d1d0418d612010e8ceb6fe560ffaffcf2d.zip |
Replaced MHD_RESPMEM_MUST_FREE with more portable solution in examples
-rw-r--r-- | doc/examples/sessions.c | 32 | ||||
-rw-r--r-- | src/examples/demo.c | 7 | ||||
-rw-r--r-- | src/examples/demo_https.c | 7 | ||||
-rw-r--r-- | src/examples/http_compression.c | 8 | ||||
-rw-r--r-- | src/examples/post_example.c | 14 | ||||
-rw-r--r-- | src/examples/querystring_example.c | 6 | ||||
-rw-r--r-- | 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, | |||
347 | enum MHD_Result ret; | 347 | enum MHD_Result ret; |
348 | char *reply; | 348 | char *reply; |
349 | struct MHD_Response *response; | 349 | struct MHD_Response *response; |
350 | int reply_len; | ||
350 | (void) cls; /* Unused */ | 351 | (void) cls; /* Unused */ |
351 | 352 | ||
352 | if (-1 == MHD_asprintf (&reply, | 353 | reply_len = MHD_asprintf (&reply, |
353 | MAIN_PAGE, | 354 | MAIN_PAGE, |
354 | session->value_1)) | 355 | session->value_1); |
356 | if (0 > reply_len) | ||
355 | { | 357 | { |
356 | /* oops */ | 358 | /* oops */ |
357 | return MHD_NO; | 359 | return MHD_NO; |
358 | } | 360 | } |
359 | /* return static form */ | 361 | /* return static form */ |
360 | response = MHD_create_response_from_buffer (strlen (reply), | 362 | response = |
361 | (void *) reply, | 363 | MHD_create_response_from_buffer_with_free_callback ((size_t) reply_len, |
362 | MHD_RESPMEM_MUST_FREE); | 364 | (void *) reply, |
365 | &free); | ||
363 | add_session_cookie (session, response); | 366 | add_session_cookie (session, response); |
364 | MHD_add_response_header (response, | 367 | MHD_add_response_header (response, |
365 | MHD_HTTP_HEADER_CONTENT_ENCODING, | 368 | MHD_HTTP_HEADER_CONTENT_ENCODING, |
@@ -389,20 +392,23 @@ fill_v1_v2_form (const void *cls, | |||
389 | enum MHD_Result ret; | 392 | enum MHD_Result ret; |
390 | char *reply; | 393 | char *reply; |
391 | struct MHD_Response *response; | 394 | struct MHD_Response *response; |
395 | int reply_len; | ||
392 | (void) cls; /* Unused */ | 396 | (void) cls; /* Unused */ |
393 | 397 | ||
394 | if (-1 == MHD_asprintf (&reply, | 398 | reply_len = MHD_asprintf (&reply, |
395 | SECOND_PAGE, | 399 | SECOND_PAGE, |
396 | session->value_1, | 400 | session->value_1, |
397 | session->value_2)) | 401 | session->value_2); |
402 | if (0 > reply_len) | ||
398 | { | 403 | { |
399 | /* oops */ | 404 | /* oops */ |
400 | return MHD_NO; | 405 | return MHD_NO; |
401 | } | 406 | } |
402 | /* return static form */ | 407 | /* return static form */ |
403 | response = MHD_create_response_from_buffer (strlen (reply), | 408 | response = |
404 | (void *) reply, | 409 | MHD_create_response_from_buffer_with_free_callback (reply_len, |
405 | MHD_RESPMEM_MUST_FREE); | 410 | (void *) reply, |
411 | &free); | ||
406 | add_session_cookie (session, response); | 412 | add_session_cookie (session, response); |
407 | MHD_add_response_header (response, | 413 | MHD_add_response_header (response, |
408 | MHD_HTTP_HEADER_CONTENT_ENCODING, | 414 | 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) | |||
383 | "%s", | 383 | "%s", |
384 | INDEX_PAGE_FOOTER); | 384 | INDEX_PAGE_FOOTER); |
385 | initial_allocation = rdc.buf_len; /* remember for next time */ | 385 | initial_allocation = rdc.buf_len; /* remember for next time */ |
386 | response = MHD_create_response_from_buffer (rdc.off, | 386 | response = |
387 | rdc.buf, | 387 | MHD_create_response_from_buffer_with_free_callback (rdc.off, |
388 | MHD_RESPMEM_MUST_FREE); | 388 | rdc.buf, |
389 | &free); | ||
389 | mark_as_html (response); | 390 | mark_as_html (response); |
390 | #if FORCE_CLOSE | 391 | #if FORCE_CLOSE |
391 | (void) MHD_add_response_header (response, | 392 | (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) | |||
385 | "%s", | 385 | "%s", |
386 | INDEX_PAGE_FOOTER); | 386 | INDEX_PAGE_FOOTER); |
387 | initial_allocation = rdc.buf_len; /* remember for next time */ | 387 | initial_allocation = rdc.buf_len; /* remember for next time */ |
388 | response = MHD_create_response_from_buffer (rdc.off, | 388 | response = |
389 | rdc.buf, | 389 | MHD_create_response_from_buffer_with_free_callback (rdc.off, |
390 | MHD_RESPMEM_MUST_FREE); | 390 | rdc.buf, |
391 | &free); | ||
391 | mark_as_html (response); | 392 | mark_as_html (response); |
392 | #if FORCE_CLOSE | 393 | #if FORCE_CLOSE |
393 | (void) MHD_add_response_header (response, | 394 | (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, | |||
130 | can_compress (connection)) | 130 | can_compress (connection)) |
131 | comp = body_compress ((void **) &body_str, | 131 | comp = body_compress ((void **) &body_str, |
132 | &body_len); | 132 | &body_len); |
133 | response = MHD_create_response_from_buffer (body_len, | 133 | response = |
134 | body_str, | 134 | MHD_create_response_from_buffer_with_free_callback (body_len, |
135 | MHD_RESPMEM_MUST_FREE); | 135 | body_str, |
136 | &free); | ||
137 | |||
136 | if (NULL == response) | 138 | if (NULL == response) |
137 | { | 139 | { |
138 | free (body_str); | 140 | 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, | |||
332 | MAIN_PAGE, | 332 | MAIN_PAGE, |
333 | session->value_1); | 333 | session->value_1); |
334 | /* return static form */ | 334 | /* return static form */ |
335 | response = MHD_create_response_from_buffer (slen, | 335 | response = |
336 | (void *) reply, | 336 | MHD_create_response_from_buffer_with_free_callback (slen, |
337 | MHD_RESPMEM_MUST_FREE); | 337 | (void *) reply, |
338 | &free); | ||
338 | if (NULL == response) | 339 | if (NULL == response) |
339 | { | 340 | { |
340 | free (reply); | 341 | free (reply); |
@@ -383,9 +384,10 @@ fill_v1_v2_form (const void *cls, | |||
383 | session->value_1, | 384 | session->value_1, |
384 | session->value_2); | 385 | session->value_2); |
385 | /* return static form */ | 386 | /* return static form */ |
386 | response = MHD_create_response_from_buffer (slen, | 387 | response = |
387 | (void *) reply, | 388 | MHD_create_response_from_buffer_with_free_callback (slen, |
388 | MHD_RESPMEM_MUST_FREE); | 389 | (void *) reply, |
390 | &free); | ||
389 | if (NULL == response) | 391 | if (NULL == response) |
390 | { | 392 | { |
391 | free (reply); | 393 | 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, | |||
72 | free (me); | 72 | free (me); |
73 | return MHD_NO; /* Error forming the response body */ | 73 | return MHD_NO; /* Error forming the response body */ |
74 | } | 74 | } |
75 | response = MHD_create_response_from_buffer (resp_len, me, | 75 | response = |
76 | MHD_RESPMEM_MUST_FREE); | 76 | MHD_create_response_from_buffer_with_free_callback (resp_len, |
77 | (void *) me, | ||
78 | &free); | ||
77 | if (response == NULL) | 79 | if (response == NULL) |
78 | { | 80 | { |
79 | free (me); | 81 | 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 | |||
3624 | * Buffer is heap-allocated with `malloc()` (or equivalent) and | 3624 | * Buffer is heap-allocated with `malloc()` (or equivalent) and |
3625 | * should be freed by MHD after processing the response has | 3625 | * should be freed by MHD after processing the response has |
3626 | * concluded (response reference counter reaches zero). | 3626 | * concluded (response reference counter reaches zero). |
3627 | * @warning Make sure that your application and MHD are using the same | 3627 | * The more portable way to automatically free the buffer is function |
3628 | * C-runtime library (especially important for W32). if in doubt, | 3628 | * MHD_create_response_from_buffer_with_free_callback() with '&free' as |
3629 | * use function MHD_create_response_from_buffer_with_free_callback() | 3629 | * crfc parameter as it does not require to use the same runtime library. |
3630 | * with '&free' as crfc parameter. | 3630 | * @warning It is critical to make sure that the same C-runtime library |
3631 | * is used by both application and MHD (especially | ||
3632 | * important for W32). | ||
3631 | * @ingroup response | 3633 | * @ingroup response |
3632 | */ | 3634 | */ |
3633 | MHD_RESPMEM_MUST_FREE, | 3635 | MHD_RESPMEM_MUST_FREE, |