gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] 01/14: refactoring: use 'const' for response buffers


From: gnunet
Subject: [libmicrohttpd] 01/14: refactoring: use 'const' for response buffers
Date: Tue, 19 Apr 2022 19:31:08 +0200

This is an automated email from the git hooks/post-receive script.

karlson2k pushed a commit to branch master
in repository libmicrohttpd.

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

    refactoring: use 'const' for response buffers
    
    The response buffers shouldn't be modifiable.
---
 src/include/microhttpd.h  |  5 +--
 src/microhttpd/internal.h |  2 +-
 src/microhttpd/response.c | 80 +++++++++++++++++++++++++----------------------
 3 files changed, 47 insertions(+), 40 deletions(-)

diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h
index e0351159..45e3524e 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 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
index 50c1a1ec..95d301f9 100644
--- 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
index 8ac0c58e..ab04061c 100644
--- 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;
 }
 

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]