gnunet-svn
[Top][All Lists]
Advanced

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

[libmicrohttpd] 01/06: response.c: added new internal function to avoid


From: gnunet
Subject: [libmicrohttpd] 01/06: response.c: added new internal function to avoid repetitive malloc()s
Date: Mon, 19 Dec 2022 16:17:58 +0100

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

karlson2k pushed a commit to branch master
in repository libmicrohttpd.

commit e4db5b26c71fa276437d7ddafa0b36c9a069f021
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
AuthorDate: Wed Nov 16 12:02:11 2022 +0300

    response.c: added new internal function to avoid repetitive malloc()s
---
 src/microhttpd/response.c | 81 ++++++++++++++++++++++++++++++++++++-----------
 src/microhttpd/response.h | 27 ++++++++++++++++
 2 files changed, 89 insertions(+), 19 deletions(-)

diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c
index d352a804..b29f48f3 100644
--- a/src/microhttpd/response.c
+++ b/src/microhttpd/response.c
@@ -144,6 +144,51 @@
   } \
 } while (0)
 
+/**
+ * Add preallocated strings a header or footer line to the response without
+ * checking.
+ *
+ * Header/footer strings are not checked and assumed to be correct.
+ *
+ * The string must not be statically allocated!
+ * The strings must be malloc()'ed and zero terminated. The strings will
+ * be free()'ed when the response is destroyed.
+ *
+ * @param response response to add a header to
+ * @param kind header or footer
+ * @param header the header string to add, must be malloc()'ed and
+ *               zero-terminated
+ * @param header_len the length of the @a header
+ * @param content the value string to add, must be malloc()'ed and
+ *                zero-terminated
+ * @param content_len the length of the @a content
+ */
+bool
+MHD_add_response_entry_no_alloc_ (struct MHD_Response *response,
+                                  enum MHD_ValueKind kind,
+                                  char *header,
+                                  size_t header_len,
+                                  char *content,
+                                  size_t content_len)
+{
+  struct MHD_HTTP_Res_Header *hdr;
+
+  mhd_assert (0 != header_len);
+  mhd_assert (0 != content_len);
+  if (NULL == (hdr = MHD_calloc_ (1, sizeof (struct MHD_HTTP_Res_Header))))
+    return false;
+
+  hdr->header = header;
+  hdr->header_size = header_len;
+  hdr->value = content;
+  hdr->value_size = content_len;
+  hdr->kind = kind;
+  _MHD_insert_header_last (response, hdr);
+
+  return true; /* Success exit point */
+}
+
+
 /**
  * Add a header or footer line to the response without checking.
  *
@@ -166,34 +211,32 @@ MHD_add_response_entry_no_check_ (struct MHD_Response 
*response,
                                   const char *content,
                                   size_t content_len)
 {
-  struct MHD_HTTP_Res_Header *hdr;
+  char *header_malloced;
+  char *value_malloced;
 
   mhd_assert (0 != header_len);
   mhd_assert (0 != content_len);
-  if (NULL == (hdr = MHD_calloc_ (1, sizeof (struct MHD_HTTP_Res_Header))))
+  header_malloced = malloc (header_len + 1);
+  if (NULL == header_malloced)
     return false;
-  hdr->header = malloc (header_len + 1);
-  if (NULL != hdr->header)
-  {
-    memcpy (hdr->header, header, header_len);
-    hdr->header[header_len] = 0;
-    hdr->header_size = header_len;
 
-    hdr->value = malloc (content_len + 1);
-    if (NULL != hdr->value)
-    {
-      memcpy (hdr->value, content, content_len);
-      hdr->value[content_len] = 0;
-      hdr->value_size = content_len;
+  memcpy (header_malloced, header, header_len);
+  header_malloced[header_len] = 0;
 
-      hdr->kind = kind;
-      _MHD_insert_header_last (response, hdr);
+  value_malloced = malloc (content_len + 1);
+  if (NULL != value_malloced)
+  {
+    memcpy (value_malloced, content, content_len);
+    value_malloced[content_len] = 0;
 
+    if (MHD_add_response_entry_no_alloc_ (response, kind,
+                                          header_malloced, header_len,
+                                          value_malloced, content_len))
       return true; /* Success exit point */
-    }
-    free (hdr->header);
+
+    free (value_malloced);
   }
-  free (hdr);
+  free (header_malloced);
 
   return false; /* Failure exit point */
 }
diff --git a/src/microhttpd/response.h b/src/microhttpd/response.h
index 4f9c32c9..ad614c1b 100644
--- a/src/microhttpd/response.h
+++ b/src/microhttpd/response.h
@@ -95,4 +95,31 @@ MHD_add_response_entry_no_check_ (struct MHD_Response 
*response,
                                   const char *content,
                                   size_t content_len);
 
+/**
+ * Add preallocated strings a header or footer line to the response without
+ * checking.
+ *
+ * Header/footer strings are not checked and assumed to be correct.
+ *
+ * The string must not be statically allocated!
+ * The strings must be malloc()'ed and zero terminated. The strings will
+ * be free()'ed when the response is destroyed.
+ *
+ * @param response response to add a header to
+ * @param kind header or footer
+ * @param header the header string to add, must be malloc()'ed and
+ *               zero-terminated
+ * @param header_len the length of the @a header
+ * @param content the value string to add, must be malloc()'ed and
+ *                zero-terminated
+ * @param content_len the length of the @a content
+ */
+bool
+MHD_add_response_entry_no_alloc_ (struct MHD_Response *response,
+                                  enum MHD_ValueKind kind,
+                                  char *header,
+                                  size_t header_len,
+                                  char *content,
+                                  size_t content_len);
+
 #endif

-- 
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]