libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

commit db299f58f31bce5a6e19ab4e6effe5287cf41175
parent 75f0be5f084c39e6596c4f1ead827d30509ab60f
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date:   Sat, 27 Jun 2026 20:38:27 +0200

Response header building: simplified and re-used some code

Diffstat:
Msrc/mhd2/stream_process_reply.c | 66+++++++++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 41 insertions(+), 25 deletions(-)

diff --git a/src/mhd2/stream_process_reply.c b/src/mhd2/stream_process_reply.c @@ -531,6 +531,32 @@ buffer_append (char *buf, /** + * Append CRLF to the buffer if enough space is available, + * update position. + * @param[out] buf the buffer to append data to + * @param[in,out] ppos the pointer to position in the @a buffer + * @param buf_size the size of the @a buffer + * @return true if CRLF has been added and position has been updated, + * false if not enough space is available + */ +mhd_static_inline MHD_FN_PAR_NONNULL_ALL_ bool +buffer_append_crlf (char *restrict buf, + size_t *restrict ppos, + size_t buf_size) +{ + mhd_assert (NULL != buf); /* Mute static analyzer */ + if (mhd_COND_HARDLY_EVER (*ppos + 2 < 2)) /* Check for overflow */ + return false; + if (mhd_COND_ALMOST_NEVER (buf_size < *ppos + 2)) + return false; + buf[*ppos] = '\r'; + buf[*ppos + 1] = '\n'; + *ppos += 2; + return true; +} + + +/** * Add user-defined headers from response object to * the text buffer. * @@ -816,12 +842,10 @@ build_header_response_inn (struct MHD_Connection *restrict c) return false; } /* The linefeed */ - if (mhd_COND_HARDLY_EVER (pos + 2 < 2)) /* Check for overflow */ + if (! buffer_append_crlf (buf, + &pos, + buf_size)) return false; - if (buf_size < pos + 2) - return false; - buf[pos++] = '\r'; - buf[pos++] = '\n'; /* * The headers * */ @@ -829,18 +853,14 @@ build_header_response_inn (struct MHD_Connection *restrict c) if (0 != r->special_resp.spec_hdr_len) { mhd_assert (r->cfg.int_err_resp); - if (mhd_COND_HARDLY_EVER (r->special_resp.spec_hdr_len + 2 < 2)) /* Check for overflow */ - return false; - if (mhd_COND_HARDLY_EVER (pos + r->special_resp.spec_hdr_len + 2 < pos)) /* Check for overflow */ + if (! buffer_append (buf, &pos, buf_size, + r->special_resp.spec_hdr, + r->special_resp.spec_hdr_len)) return false; - if (buf_size < pos + r->special_resp.spec_hdr_len + 2) + if (! buffer_append_crlf (buf, + &pos, + buf_size)) return false; - memcpy (buf + pos, - r->special_resp.spec_hdr, - r->special_resp.spec_hdr_len); - pos += r->special_resp.spec_hdr_len; - buf[pos++] = '\r'; - buf[pos++] = '\n'; } /* Main automatic headers */ @@ -949,12 +969,10 @@ build_header_response_inn (struct MHD_Connection *restrict c) return false; pos += el_size; - if (pos + 2 < 2) /* Check for overflow */ + if (! buffer_append_crlf (buf, + &pos, + buf_size)) return false; - if (buf_size < pos + 2) - return false; - buf[pos++] = '\r'; - buf[pos++] = '\n'; } } else @@ -967,12 +985,10 @@ build_header_response_inn (struct MHD_Connection *restrict c) } /* * Header termination * */ - if (mhd_COND_HARDLY_EVER (pos + 2 < 2)) /* Check for overflow */ - return false; - if (buf_size < pos + 2) + if (! buffer_append_crlf (buf, + &pos, + buf_size)) return false; - buf[pos++] = '\r'; - buf[pos++] = '\n'; c->write_buffer_append_offset = pos; return true;