[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libmicrohttpd] 14/19: MHD_add_response_entry(): refactoring + added int
From: |
gnunet |
Subject: |
[libmicrohttpd] 14/19: MHD_add_response_entry(): refactoring + added internal function |
Date: |
Thu, 28 Jul 2022 06:26: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 13db035fc9e191e99dbaf3507e45516eb4ffeabd
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
AuthorDate: Fri Jul 22 16:32:08 2022 +0300
MHD_add_response_entry(): refactoring + added internal function
---
src/microhttpd/response.c | 136 ++++++++++++++++++++++++++++++++++++----------
src/microhttpd/response.h | 22 ++++++++
2 files changed, 128 insertions(+), 30 deletions(-)
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c
index 3bdd2a59..cffb859a 100644
--- a/src/microhttpd/response.c
+++ b/src/microhttpd/response.c
@@ -144,6 +144,103 @@
} \
} while (0)
+/**
+ * Add a header or footer line to the response without checking.
+ *
+ * It is assumed that parameters are correct.
+ *
+ * @param response response to add a header to
+ * @param kind header or footer
+ * @param header the header to add, does not need to be zero-terminated
+ * @param header_len the length of the @a header
+ * @param content value to add, does not need to be zero-terminated
+ * @param content_len the length of the @a content
+ * @return false on error (like out-of-memory),
+ * true if succeed
+ */
+bool
+MHD_add_response_entry_no_check_ (struct MHD_Response *response,
+ enum MHD_ValueKind kind,
+ const char *header,
+ size_t header_len,
+ const 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 = 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;
+
+ hdr->kind = kind;
+ _MHD_insert_header_last (response, hdr);
+
+ return true; /* Success exit point */
+ }
+ free (hdr->header);
+ }
+ free (hdr);
+
+ return false; /* Failure exit point */
+}
+
+
+/**
+ * Add a header or footer line to the response.
+ *
+ * @param header the header to add, does not need to be zero-terminated
+ * @param header_len the length of the @a header
+ * @param content value to add, does not need to be zero-terminated
+ * @param content_len the length of the @a content
+ * @return false on error (out-of-memory, invalid header or content),
+ * true if succeed
+ */
+static bool
+add_response_entry_n (struct MHD_Response *response,
+ enum MHD_ValueKind kind,
+ const char *header,
+ size_t header_len,
+ const char *content,
+ size_t content_len)
+{
+ if (NULL == response)
+ return false;
+ if (0 == header_len)
+ return false;
+ if (0 == content_len)
+ return false;
+ if (NULL != memchr (header, '\t', header_len))
+ return false;
+ if (NULL != memchr (header, ' ', header_len))
+ return false;
+ if (NULL != memchr (header, '\r', header_len))
+ return false;
+ if (NULL != memchr (header, '\n', header_len))
+ return false;
+ if (NULL != memchr (content, '\r', content_len))
+ return false;
+ if (NULL != memchr (content, '\n', content_len))
+ return false;
+
+ return MHD_add_response_entry_no_check_ (response, kind, header, header_len,
+ content, content_len);
+}
+
+
/**
* Add a header or footer line to the response.
*
@@ -159,38 +256,17 @@ add_response_entry (struct MHD_Response *response,
const char *header,
const char *content)
{
- struct MHD_HTTP_Res_Header *hdr;
+ size_t header_len;
+ size_t content_len;
- if ( (NULL == response) ||
- (NULL == header) ||
- (NULL == content) ||
- (0 == header[0]) ||
- (0 == content[0]) ||
- (NULL != strchr (header, '\t')) ||
- (NULL != strchr (header, ' ')) ||
- (NULL != strchr (header, '\r')) ||
- (NULL != strchr (header, '\n')) ||
- (NULL != strchr (content, '\r')) ||
- (NULL != strchr (content, '\n')) )
+ if (NULL == content)
return MHD_NO;
- if (NULL == (hdr = MHD_calloc_ (1, sizeof (struct MHD_HTTP_Res_Header))))
- return MHD_NO;
- if (NULL == (hdr->header = strdup (header)))
- {
- free (hdr);
- return MHD_NO;
- }
- hdr->header_size = strlen (header);
- if (NULL == (hdr->value = strdup (content)))
- {
- free (hdr->header);
- free (hdr);
- return MHD_NO;
- }
- hdr->value_size = strlen (content);
- hdr->kind = kind;
- _MHD_insert_header_last (response, hdr);
- return MHD_YES;
+
+ header_len = strlen (header);
+ content_len = strlen (content);
+ return add_response_entry_n (response, kind, header,
+ header_len, content,
+ content_len) ? MHD_YES : MHD_NO;
}
diff --git a/src/microhttpd/response.h b/src/microhttpd/response.h
index ba6c4ab8..4f9c32c9 100644
--- a/src/microhttpd/response.h
+++ b/src/microhttpd/response.h
@@ -73,4 +73,26 @@ MHD_get_response_element_n_ (struct MHD_Response *response,
const char *key,
size_t key_len);
+/**
+ * Add a header or footer line to the response without checking.
+ *
+ * It is assumed that parameters are correct.
+ *
+ * @param response response to add a header to
+ * @param kind header or footer
+ * @param header the header to add, does not need to be zero-terminated
+ * @param header_len the length of the @a header
+ * @param content value to add, does not need to be zero-terminated
+ * @param content_len the length of the @a content
+ * @return false on error (like out-of-memory),
+ * true if succeed
+ */
+bool
+MHD_add_response_entry_no_check_ (struct MHD_Response *response,
+ enum MHD_ValueKind kind,
+ const char *header,
+ size_t header_len,
+ const char *content,
+ size_t content_len);
+
#endif
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [libmicrohttpd] 04/19: digest_auth_check(): reduced scope of one-time variable, (continued)
- [libmicrohttpd] 04/19: digest_auth_check(): reduced scope of one-time variable, gnunet, 2022/07/28
- [libmicrohttpd] 06/19: digest_auth_check(): added check for too large realm value, gnunet, 2022/07/28
- [libmicrohttpd] 02/19: gen_auth: fixed detection of userhash in Digest Auth requests, gnunet, 2022/07/28
- [libmicrohttpd] 07/19: Updated doxy for old Digest Auth API function, gnunet, 2022/07/28
- [libmicrohttpd] 10/19: test_digestauth_emu_ext: cosmetics, additional check, gnunet, 2022/07/28
- [libmicrohttpd] 11/19: tests_digestauth*: added workarounds for libcurl bug, gnunet, 2022/07/28
- [libmicrohttpd] 01/19: digestauth: fixed copy-paste error in request algo parsing, gnunet, 2022/07/28
- [libmicrohttpd] 08/19: digestauth: term correction in comment, gnunet, 2022/07/28
- [libmicrohttpd] 05/19: digest_auth_check(): added support for userhash, gnunet, 2022/07/28
- [libmicrohttpd] 09/19: test_digestauth{,_sha256,_with_aguments}: moved back to Digest Auth APIv2, gnunet, 2022/07/28
- [libmicrohttpd] 14/19: MHD_add_response_entry(): refactoring + added internal function,
gnunet <=
- [libmicrohttpd] 12/19: test_digestauth_concurrent: fixed compiler warnings, gnunet, 2022/07/28
- [libmicrohttpd] 17/19: test_digestauth2: added new group of tests for Digest Auth checking, gnunet, 2022/07/28
- [libmicrohttpd] 16/19: Added MHD_queue_auth_required_response3(); Refactored public Digest Auth API v3, gnunet, 2022/07/28
- [libmicrohttpd] 13/19: Updated Digest Auth enums in the header, gnunet, 2022/07/28
- [libmicrohttpd] 18/19: MHD_FEATURE_*: added some values related to Digest Auth, gnunet, 2022/07/28
- [libmicrohttpd] 15/19: Digest Auth public structs: removed redundant member, gnunet, 2022/07/28
- [libmicrohttpd] 19/19: Makefile: cosmetics, gnunet, 2022/07/28