[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libmicrohttpd] 11/14: Added new API function MHD_create_response_from_b
From: |
gnunet |
Subject: |
[libmicrohttpd] 11/14: Added new API function MHD_create_response_from_buffer_copy() |
Date: |
Tue, 19 Apr 2022 19:31:18 +0200 |
This is an automated email from the git hooks/post-receive script.
karlson2k pushed a commit to branch master
in repository libmicrohttpd.
commit 43cca25d4484ce137c95410be28e4cdf83a3a3b0
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
AuthorDate: Tue Apr 19 18:51:11 2022 +0300
Added new API function MHD_create_response_from_buffer_copy()
---
src/include/microhttpd.h | 29 +++++++++++-
src/microhttpd/response.c | 113 ++++++++++++++++++++++++++++------------------
2 files changed, 98 insertions(+), 44 deletions(-)
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h
index 8178584c..bd928ace 100644
--- 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',
@@ -3564,6 +3564,33 @@ MHD_create_response_from_buffer_static (size_t size,
const void *buffer);
+/**
+ * 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
index 8ddde763..47e28046 100644
--- 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);
}
@@ -1410,6 +1379,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.
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [libmicrohttpd] 01/14: refactoring: use 'const' for response buffers, (continued)
- [libmicrohttpd] 01/14: refactoring: use 'const' for response buffers, gnunet, 2022/04/19
- [libmicrohttpd] 04/14: microhttpd.h: removed empty line, gnunet, 2022/04/19
- [libmicrohttpd] 03/14: Refactored create response functions., gnunet, 2022/04/19
- [libmicrohttpd] 02/14: Added _MHD_EXTERN to the all public functions definitions, gnunet, 2022/04/19
- [libmicrohttpd] 06/14: Added new API function MHD_create_response_from_buffer_static(), gnunet, 2022/04/19
- [libmicrohttpd] 07/14: Fixed drop of 'const' when building internal error response, gnunet, 2022/04/19
- [libmicrohttpd] 12/14: microhttpd.h: minor doxy improvement, gnunet, 2022/04/19
- [libmicrohttpd] 08/14: Fixed missing 'const' qualifiers, gnunet, 2022/04/19
- [libmicrohttpd] 05/14: MHD_get_version(): fixed signed value bit shift, gnunet, 2022/04/19
- [libmicrohttpd] 09/14: Added workaround for external APIs, gnunet, 2022/04/19
- [libmicrohttpd] 11/14: Added new API function MHD_create_response_from_buffer_copy(),
gnunet <=
- [libmicrohttpd] 10/14: Minor simplification for ALPN protocols code, gnunet, 2022/04/19
- [libmicrohttpd] 14/14: doc/examples: Fixed drop of 'const' qualifiers, gnunet, 2022/04/19
- [libmicrohttpd] 13/14: src/examples: Fixed drop of 'const' qualifiers and minor fixes., gnunet, 2022/04/19