libmicrohttpd

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

commit 4ff1024763a306200de3fd1e5581377aca9047e1
parent 03c29b63e0e4c4036dafaa382cd0db1fe400a7a2
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Sun, 17 Apr 2022 19:58:42 +0300

refactoring: use 'const' for response buffers

The response buffers shouldn't be modifiable.

Diffstat:
Msrc/include/microhttpd.h | 5+++--
Msrc/microhttpd/internal.h | 2+-
Msrc/microhttpd/response.c | 80++++++++++++++++++++++++++++++++++++++++++-------------------------------------
3 files changed, 47 insertions(+), 40 deletions(-)

diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h @@ -96,7 +96,7 @@ extern "C" * they are parsed as decimal numbers. * Example: 0x01093001 = 1.9.30-1. */ -#define MHD_VERSION 0x00097503 +#define MHD_VERSION 0x00097504 /* If generic headers don't work on your platform, include headers which define 'va_list', 'size_t', 'ssize_t', 'intptr_t', @@ -3582,11 +3582,12 @@ MHD_create_response_from_buffer_with_free_callback (size_t size, * @param crfc_cls an argument for @a crfc * @return NULL on error (i.e. invalid arguments, out of memory) * @note Available since #MHD_VERSION 0x00097302 + * @note 'const' qualifier is used for @a buffer since #MHD_VERSION 0x00097504 * @ingroup response */ _MHD_EXTERN struct MHD_Response * MHD_create_response_from_buffer_with_free_callback_cls (size_t size, - void *buffer, + const void *buffer, MHD_ContentReaderFreeCallback crfc, void *crfc_cls); diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h @@ -449,7 +449,7 @@ struct MHD_Response * Buffer pointing to data that we are supposed * to send as a response. */ - char *data; + const char *data; /** * Closure to give to the content reader @e crc diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c @@ -1297,45 +1297,34 @@ MHD_create_response_from_data (size_t size, int must_copy) { struct MHD_Response *response; - void *tmp; + void *mhd_copy; - if ((NULL == data) && (size > 0)) - return NULL; - if (MHD_SIZE_UNKNOWN == size) - return NULL; - if (NULL == (response = MHD_calloc_ (1, sizeof (struct MHD_Response)))) - return NULL; - response->fd = -1; -#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) - if (! MHD_mutex_init_ (&response->mutex)) - { - free (response); - return NULL; - } -#endif if ((must_copy) && (size > 0)) { - if (NULL == (tmp = malloc (size))) - { -#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) - MHD_mutex_destroy_chk_ (&response->mutex); -#endif - free (response); + if (NULL == data) return NULL; - } - memcpy (tmp, data, size); + mhd_copy = malloc (size); + if (NULL == mhd_copy) + return NULL; + memcpy (mhd_copy, data, size); must_free = MHD_YES; - data = tmp; + data = mhd_copy; } - if (must_free) + else + mhd_copy = NULL; + + response = + MHD_create_response_from_buffer_with_free_callback_cls (size, + data, + must_free ? + &free : NULL, + data); + if (NULL == response) { - response->crfc = &free; - response->crc_cls = data; + if (NULL != mhd_copy) + free (mhd_copy); + return NULL; } - response->reference_count = 1; - response->total_size = size; - response->data = data; - response->data_size = size; if (must_copy) response->data_buffer_size = size; return response; @@ -1425,22 +1414,39 @@ MHD_create_response_from_buffer_with_free_callback (size_t size, * @param crfc_cls an argument for @a crfc * @return NULL on error (i.e. invalid arguments, out of memory) * @note Available since #MHD_VERSION 0x00097302 + * @note 'const' qualifier is used for @a buffer since #MHD_VERSION 0x00097504 * @ingroup response */ _MHD_EXTERN struct MHD_Response * MHD_create_response_from_buffer_with_free_callback_cls (size_t size, - void *buffer, + const void *buffer, MHD_ContentReaderFreeCallback crfc, void *crfc_cls) { struct MHD_Response *r; - r = MHD_create_response_from_buffer_with_free_callback (size, - buffer, - crfc); - if (NULL != r) - r->crc_cls = crfc_cls; + if ((NULL == buffer) && (size > 0)) + return NULL; + if (MHD_SIZE_UNKNOWN == size) + return NULL; + r = MHD_calloc_ (1, sizeof (struct MHD_Response)); + if (NULL == r) + return NULL; +#if defined(MHD_USE_THREADS) + if (! MHD_mutex_init_ (&r->mutex)) + { + free (r); + return NULL; + } +#endif + r->fd = -1; + r->reference_count = 1; + r->total_size = size; + r->data = buffer; + r->data_size = size; + r->crfc = crfc; + r->crc_cls = crfc_cls; return r; }