libmicrohttpd

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

commit 523c712cc4164e84fc896cbffe0534c0fccabcdf
parent 84b48d0f9bbba9b9227d94886ca1861502d82772
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date:   Thu, 26 Nov 2026 14:09:03 +0100

MHD_add_response_header(): fixed "Date:" header processing when replacing the header

If the "Date:" header could not be replaced, the HTTP response was sent
without a "Date:" header.
The next attempt to replace the "Date:" header would trigger a crash.

Diffstat:
Msrc/microhttpd/response.c | 36+++++++++++++++++++++---------------
1 file changed, 21 insertions(+), 15 deletions(-)

diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c @@ -652,29 +652,35 @@ MHD_add_response_header (struct MHD_Response *response, if (MHD_str_equal_caseless_ (header, MHD_HTTP_HEADER_DATE)) { + struct MHD_HTTP_Res_Header *old_hdr; + + old_hdr = NULL; if (0 != (response->flags_auto & MHD_RAF_HAS_DATE_HDR)) { - struct MHD_HTTP_Res_Header *hdr; - hdr = MHD_get_response_element_n_ (response, MHD_HEADER_KIND, - MHD_HTTP_HEADER_DATE, - MHD_STATICSTR_LEN_ ( \ - MHD_HTTP_HEADER_DATE)); - mhd_assert (NULL != hdr); - _MHD_remove_header (response, hdr); - if (NULL != hdr->value) - free (hdr->value); - free (hdr->header); - free (hdr); + old_hdr = + MHD_get_response_element_n_ (response, MHD_HEADER_KIND, + MHD_HTTP_HEADER_DATE, + MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_DATE)); + mhd_assert (NULL != old_hdr); } - if (MHD_NO != add_response_entry (response, + + if (MHD_NO == add_response_entry (response, MHD_HEADER_KIND, header, content)) + return MHD_NO; + + response->flags_auto |= MHD_RAF_HAS_DATE_HDR; + + if (NULL != old_hdr) { - response->flags_auto |= MHD_RAF_HAS_DATE_HDR; - return MHD_YES; + _MHD_remove_header (response, old_hdr); + if (NULL != old_hdr->value) + free (old_hdr->value); + free (old_hdr->header); + free (old_hdr); } - return MHD_NO; + return MHD_YES; } if (MHD_str_equal_caseless_ (header,