libmicrohttpd

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

commit 43cca25d4484ce137c95410be28e4cdf83a3a3b0
parent ae4fcbad73324878198b8d5d3a156281d1bcd125
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Tue, 19 Apr 2022 18:51:11 +0300

Added new API function MHD_create_response_from_buffer_copy()

Diffstat:
Msrc/include/microhttpd.h | 29++++++++++++++++++++++++++++-
Msrc/microhttpd/response.c | 113+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
2 files changed, 98 insertions(+), 44 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 0x00097506 +#define MHD_VERSION 0x00097507 /* If generic headers don't work on your platform, include headers which define 'va_list', 'size_t', 'ssize_t', 'intptr_t', @@ -3565,6 +3565,33 @@ MHD_create_response_from_buffer_static (size_t size, /** + * Create a response object with the content of provided statically allocated + * buffer used as the response body. + * + * An internal copy of the buffer will be made automatically, so buffer have + * to be valid only during the call of this function (as a typical example: + * buffer is a local (non-static) array). + * + * The response object can be extended with header information and then + * be used any number of times. + * + * If response object is used to answer HEAD request then the body + * of the response is not used, while all headers (including automatic + * headers) are used. + * + * @param size the size of the data in @a buffer, can be zero + * @param buffer the buffer with the data for the response body, can be NULL + * if @a size is zero + * @return NULL on error (i.e. invalid arguments, out of memory) + * @note Available since #MHD_VERSION 0x00097507 + * @ingroup response + */ +_MHD_EXTERN struct MHD_Response * +MHD_create_response_from_buffer_copy (size_t size, + const void *buffer); + + +/** * Create a response object with the content of provided buffer used as * the response body. * diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c @@ -1331,50 +1331,19 @@ MHD_create_response_from_buffer (size_t size, void *buffer, enum MHD_ResponseMemoryMode mode) { - struct MHD_Response *r; - void *mhd_copy; - MHD_ContentReaderFreeCallback crfc; - void *crfc_cls; - - if ((MHD_RESPMEM_MUST_COPY == mode) && (size > 0)) - { - if (NULL == buffer) - return NULL; - mhd_copy = malloc (size); - if (NULL == mhd_copy) - return NULL; - crfc = &free; - crfc_cls = mhd_copy; - memcpy (mhd_copy, buffer, size); - buffer = mhd_copy; - } - else if (MHD_RESPMEM_MUST_FREE == mode) - { - mhd_copy = NULL; - crfc = &free; - crfc_cls = buffer; - } - else - { - mhd_copy = NULL; - crfc = NULL; - crfc_cls = NULL; - } - - r = MHD_create_response_from_buffer_with_free_callback_cls (size, - buffer, - crfc, - crfc_cls); - if (NULL == r) - { - if (NULL != mhd_copy) - free (mhd_copy); - return NULL; - } - /* TODO: remove the next check, the buffer should not be modifiable */ + if (MHD_RESPMEM_MUST_FREE == mode) + return MHD_create_response_from_buffer_with_free_callback_cls (size, + buffer, + &free, + buffer); if (MHD_RESPMEM_MUST_COPY == mode) - r->data_buffer_size = size; - return r; + return MHD_create_response_from_buffer_copy (size, + buffer); + + return MHD_create_response_from_buffer_with_free_callback_cls (size, + buffer, + NULL, + NULL); } @@ -1411,6 +1380,64 @@ MHD_create_response_from_buffer_static (size_t size, /** + * Create a response object with the content of provided statically allocated + * buffer used as the response body. + * + * An internal copy of the buffer will be made automatically, so buffer have + * to be valid only during the call of this function (as a typical example: + * buffer is a local (non-static) array). + * + * The response object can be extended with header information and then + * be used any number of times. + * + * If response object is used to answer HEAD request then the body + * of the response is not used, while all headers (including automatic + * headers) are used. + * + * @param size the size of the data in @a buffer, can be zero + * @param buffer the buffer with the data for the response body, can be NULL + * if @a size is zero + * @return NULL on error (i.e. invalid arguments, out of memory) + * @note Available since #MHD_VERSION 0x00097507 + * @ingroup response + */ +_MHD_EXTERN struct MHD_Response * +MHD_create_response_from_buffer_copy (size_t size, + const void *buffer) +{ + struct MHD_Response *r; + void *mhd_copy; + + if (0 == size) + return MHD_create_response_from_buffer_with_free_callback_cls (0, + NULL, + NULL, + NULL); + if (NULL == buffer) + return NULL; + + mhd_copy = malloc (size); + if (NULL == mhd_copy) + return NULL; + memcpy (mhd_copy, buffer, size); + + r = MHD_create_response_from_buffer_with_free_callback_cls (size, + mhd_copy, + &free, + mhd_copy); + if (NULL == r) + free (mhd_copy); + else + { + /* TODO: remove the next assignment, the buffer should not be modifiable */ + r->data_buffer_size = size; + } + + return r; +} + + +/** * Create a response object with the content of provided buffer used as * the response body. *