From 406c434c722668687d7d752c8d2b6054d4340f3d Mon Sep 17 00:00:00 2001 From: "Evgeny Grin (Karlson2k)" Date: Mon, 7 Mar 2022 21:08:53 +0300 Subject: [PATCH] response headers: do not add automatic "Content-Length" header if response already has it If application used MHD_RF_INSANITY_HEADER_CONTENT_LENGTH response flag and added manual "Content-Length" header, MHD will not add the correct header. --- src/microhttpd/connection.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c index f408d1f1..88b1f21d 100644 --- a/src/microhttpd/connection.c +++ b/src/microhttpd/connection.c @@ -2175,6 +2175,7 @@ build_header_response (struct MHD_Connection *connection) if (c->rp_props.use_reply_body_headers) { /* Body-specific headers */ + if (c->rp_props.chunked) { /* Chunked encoding is used */ if (0 == (r->flags_auto & MHD_RAF_HAS_TRANS_ENC_CHUNKED)) @@ -2185,23 +2186,26 @@ build_header_response (struct MHD_Connection *connection) return MHD_NO; } } - else - { /* Chunked encoding is not used */ + else /* Chunked encoding is not used */ + { if (MHD_SIZE_UNKNOWN != r->total_size) - { - if (! buffer_append_s (buf, &pos, buf_size, - MHD_HTTP_HEADER_CONTENT_LENGTH ": ")) - return MHD_NO; - el_size = MHD_uint64_to_str (r->total_size, buf + pos, - buf_size - pos); - if (0 == el_size) - return MHD_NO; - pos += el_size; + { /* The size is known */ + if (0 == (r->flags_auto & MHD_RAF_HAS_CONTENT_LENGTH)) + { /* The response does not have "Content-Length" header */ + if (! buffer_append_s (buf, &pos, buf_size, + MHD_HTTP_HEADER_CONTENT_LENGTH ": ")) + return MHD_NO; + el_size = MHD_uint64_to_str (r->total_size, buf + pos, + buf_size - pos); + if (0 == el_size) + return MHD_NO; + pos += el_size; - if (buf_size < pos + 2) - return MHD_NO; - buf[pos++] = '\r'; - buf[pos++] = '\n'; + if (buf_size < pos + 2) + return MHD_NO; + buf[pos++] = '\r'; + buf[pos++] = '\n'; + } } } }