libmicrohttpd

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

commit 79efcac17e7046f34e9ac5c889d43a8daf228c2f
parent a3c8290981902dd5872d16d7f511df67fe2d8a82
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date:   Fri, 27 Nov 2026 13:48:11 +0100

Allowed empty header values (but not empty names) in responses

Diffstat:
Mdoc/libmicrohttpd.texi | 8++++++--
Msrc/include/microhttpd.h | 8+++++---
Msrc/microhttpd/response.c | 19+++++++++++++------
Msrc/microhttpd/test_response_entries.c | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 96 insertions(+), 11 deletions(-)

diff --git a/doc/libmicrohttpd.texi b/doc/libmicrohttpd.texi @@ -2211,7 +2211,10 @@ Return @code{NULL} on error (i.e. invalid arguments, out of memory). Add a header line to the response. The strings @var{header} and @var{content} are copied to the response. -The field name and value must be non-empty and conform to HTTP field syntax. +The field name must be non-empty. The field value can be an empty string. +Both must conform to HTTP field syntax. Empty value is not accepted for +``Connection'', ``Transfer-Encoding'', ``Date'' and ``Content-Length'' +headers. MHD_add_response_header() prevents applications from setting a ``Transfer-Encoding'' header to values other than ``identity'' or @@ -2233,7 +2236,8 @@ memory allocation error). Add a footer line to the response. The strings @var{footer} and @var{content} are copied to the response. -The field name and value must be non-empty and conform to HTTP field syntax. +The field name must be non-empty. The field value can be an empty string. +Both must conform to HTTP field syntax. You can add response footers at any time before signalling the end of the response to MHD (not just before calling 'MHD_queue_response'). diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h @@ -101,7 +101,7 @@ MHD_C_DECLRATIONS_START_HERE_ * they are parsed as decimal numbers. * Example: 0x01093001 = 1.9.30-1. */ -#define MHD_VERSION 0x01001103 +#define MHD_VERSION 0x01001104 /* If generic headers don't work on your platform, include headers which define 'va_list', 'size_t', 'ssize_t', 'intptr_t', 'off_t', @@ -4607,11 +4607,13 @@ MHD_destroy_response (struct MHD_Response *response); * * "Content-Length" application-defined header is not allowed. * @see #MHD_RF_INSANITY_HEADER_CONTENT_LENGTH * + * Empty value is not accepted for these pre-processed headers. + * * Headers are used in order as they were added. * * @param response the response to add a header to * @param header the header name to add - * @param content the header value to add + * @param content the header value to add, can be an empty string * @return #MHD_YES on success, * #MHD_NO on error (i.e. invalid header or content format), * or out of memory @@ -4630,7 +4632,7 @@ MHD_add_response_header (struct MHD_Response *response, * * @param response the response to add the footer to * @param footer the footer name to add - * @param content the footer value to add + * @param content the footer value to add, can be an empty string * @return #MHD_NO on error (i.e. invalid footer or content format). * @ingroup response */ diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c @@ -174,7 +174,6 @@ MHD_add_response_entry_no_alloc_ (struct MHD_Response *response, struct MHD_HTTP_Res_Header *hdr; mhd_assert (0 != name_len); - mhd_assert (0 != value_len); if (NULL == (hdr = MHD_calloc_ (1, sizeof (struct MHD_HTTP_Res_Header)))) return false; @@ -215,7 +214,6 @@ MHD_add_response_entry_no_check_ (struct MHD_Response *response, char *value_malloced; mhd_assert (0 != name_len); - mhd_assert (0 != value_len); name_malloced = malloc (name_len + 1); if (NULL == name_malloced) return false; @@ -264,8 +262,6 @@ add_response_entry_n (struct MHD_Response *response, return false; if (0 == name_len) return false; - if (0 == value_len) - return false; if (NULL != memchr (name, '\t', name_len)) return false; if (NULL != memchr (name, ' ', name_len)) @@ -609,11 +605,13 @@ del_response_header_connection (struct MHD_Response *response, * * "Content-Length" application-defined header is not allowed. * @see #MHD_RF_INSANITY_HEADER_CONTENT_LENGTH * + * Empty value is not accepted for these pre-processed headers. + * * Headers are used in order as they were added. * * @param response the response to add a header to * @param header the header name to add - * @param content the header value to add + * @param content the header value to add, can be an empty string * @return #MHD_YES on success, * #MHD_NO on error (i.e. invalid header or content format), * or out of memory @@ -652,6 +650,11 @@ MHD_add_response_header (struct MHD_Response *response, { struct MHD_HTTP_Res_Header *old_hdr; + /* The value is HTTP-date and cannot be empty. + See RFC 9110, Section 6.6.1 */ + if ('\0' == content[0]) + return MHD_NO; + old_hdr = NULL; if (0 != (response->flags_auto & MHD_RAF_HAS_DATE_HDR)) { @@ -692,6 +695,10 @@ MHD_add_response_header (struct MHD_Response *response, (0 == (response->flags_auto & (MHD_RAF_HAS_TRANS_ENC_CHUNKED | MHD_RAF_HAS_CONTENT_LENGTH)))) ) { + /* The value is 1*DIGIT and cannot be empty. + See RFC 9110, Section 8.6 */ + if ('\0' == content[0]) + return MHD_NO; if (MHD_NO != add_response_entry (response, MHD_HEADER_KIND, header, @@ -718,7 +725,7 @@ MHD_add_response_header (struct MHD_Response *response, * * @param response the response to add the footer to * @param footer the footer name to add - * @param content the footer value to add + * @param content the footer value to add, can be an empty string * @return #MHD_NO on error (i.e. invalid footer or content format). * @ingroup response */ diff --git a/src/microhttpd/test_response_entries.c b/src/microhttpd/test_response_entries.c @@ -147,8 +147,41 @@ main (int argc, return 2; } + if ((MHD_YES != MHD_add_response_header (r, "Empty-Header", "")) || + (! expect_str (MHD_get_response_header (r, "Empty-Header"), "")) || + (MHD_YES != MHD_del_response_header (r, "Empty-Header", "")) || + (! expect_str (MHD_get_response_header (r, "Empty-Header"), NULL))) + { + fprintf (stderr, "Cannot handle a response header with an empty value.\n"); + MHD_destroy_response (r); + return 2; + } + if ((MHD_YES != MHD_add_response_footer (r, "Empty-Footer", "")) || + (! expect_str (MHD_get_response_header (r, "Empty-Footer"), "")) || + (MHD_YES != MHD_del_response_header (r, "Empty-Footer", "")) || + (! expect_str (MHD_get_response_header (r, "Empty-Footer"), NULL))) + { + fprintf (stderr, "Cannot handle a response footer with an empty value.\n"); + MHD_destroy_response (r); + return 2; + } + if ((MHD_NO != MHD_add_response_header (r, "", "value")) || + (MHD_NO != MHD_add_response_footer (r, "", "value"))) + { + fprintf (stderr, "Accepted a response field with an empty name.\n"); + MHD_destroy_response (r); + return 2; + } + /* ** Test "Connection:" header ** */ + if ((MHD_NO != MHD_add_response_header (r, "Connection", "")) || + (! expect_str (MHD_get_response_header (r, "Connection"), NULL))) + { + fprintf (stderr, "Accepted an empty \"Connection\" header value.\n"); + MHD_destroy_response (r); + return 3; + } if (MHD_YES != MHD_add_response_header (r, "Connection", "a,b,c,d,e")) { fprintf (stderr, "Cannot add \"Connection\" header with simple values.\n"); @@ -160,6 +193,15 @@ main (int argc, MHD_destroy_response (r); return 3; } + if ((MHD_NO != MHD_add_response_header (r, "Connection", "")) || + (! expect_str (MHD_get_response_header (r, "Connection"), + "a, b, c, d, e"))) + { + fprintf (stderr, "Accepted an empty value for existing " + "\"Connection\" header.\n"); + MHD_destroy_response (r); + return 3; + } if (MHD_YES != MHD_del_response_header (r, "Connection", "e,b,c,d,a")) { fprintf (stderr, @@ -789,6 +831,12 @@ main (int argc, MHD_destroy_response (r); return 5; } + if (MHD_NO != MHD_add_response_header (r, "Date", "")) + { + fprintf (stderr, "Accepted an empty \"Date\" header value.\n"); + MHD_destroy_response (r); + return 5; + } if (! expect_str (MHD_get_response_header (r, "Date"), "Wed, 01 Apr 2015 00:00:00 GMT")) { @@ -855,6 +903,15 @@ main (int argc, return 6; } if (MHD_NO != MHD_add_response_header (r, MHD_HTTP_HEADER_TRANSFER_ENCODING, + "")) + { + fprintf (stderr, + "Successfully added \"" MHD_HTTP_HEADER_TRANSFER_ENCODING \ + "\" header with an empty value.\n"); + MHD_destroy_response (r); + return 6; + } + if (MHD_NO != MHD_add_response_header (r, MHD_HTTP_HEADER_TRANSFER_ENCODING, "identity")) { fprintf (stderr, @@ -887,6 +944,21 @@ main (int argc, } MHD_destroy_response (r); + + r = MHD_create_response_empty (MHD_RF_HEAD_ONLY_RESPONSE); + if (NULL == r) + { + fprintf (stderr, "Cannot create a HEAD-only response.\n"); + return 7; + } + if (MHD_NO != MHD_add_response_header (r, MHD_HTTP_HEADER_CONTENT_LENGTH, "")) + { + fprintf (stderr, "Accepted an empty \"Content-Length\" header value.\n"); + MHD_destroy_response (r); + return 7; + } + MHD_destroy_response (r); + printf ("All tests has been successfully passed.\n"); return 0; }