libmicrohttpd2

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

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

Response header building: minor optimisation and refactoring

Diffstat:
Msrc/mhd2/stream_process_reply.c | 62+++++++++++++++++++++++++++++++-------------------------------
1 file changed, 31 insertions(+), 31 deletions(-)

diff --git a/src/mhd2/stream_process_reply.c b/src/mhd2/stream_process_reply.c @@ -505,18 +505,18 @@ get_date_header (char *header) * Append data 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 + * @param[in,out] ppos the pointer to position in the @a buffer * @param append the data to append * @param append_size the size of the @a append * @return true if data has been added and position has been updated, * false if not enough space is available */ static MHD_FN_PAR_NONNULL_ALL_ bool -buffer_append (char *buf, - size_t *ppos, +buffer_append (char *restrict buf, size_t buf_size, - const char *append, + size_t *restrict ppos, + const char *restrict append, size_t append_size) { mhd_assert (NULL != buf); /* Mute static analyzer */ @@ -534,15 +534,15 @@ 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 + * @param[in,out] ppos the pointer to position in 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) + size_t buf_size, + size_t *restrict ppos) { mhd_assert (NULL != buf); /* Mute static analyzer */ if (mhd_COND_HARDLY_EVER (*ppos + 2 < 2)) /* Check for overflow */ @@ -684,28 +684,28 @@ add_user_headers (char *restrict buf, * Append static string 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 + * @param[in,out] ppos the pointer to position in the @a buffer * @param str the static string to append * @return true if data has been added and position has been updated, * false if not enough space is available */ -#define buffer_append_s(buf,ppos,buf_size,str) \ - buffer_append (buf,ppos,buf_size,str, mhd_SSTR_LEN (str)) +#define buffer_append_s(buf,buf_size,ppos,str) \ + buffer_append ((buf),(buf_size),(ppos),(str),mhd_SSTR_LEN (str)) /** * Append MHD_String 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 + * @param[in,out] ppos the pointer to position in the @a buffer * @param pmhdstr the pointer to string to append * @return true if data has been added and position has been updated, * false if not enough space is available */ -#define buffer_append_mstr(buf,ppos,buf_size,pmhdstr) \ - buffer_append ((buf),(ppos),(buf_size), \ +#define buffer_append_mstr(buf,buf_size,ppos,pmhdstr) \ + buffer_append ((buf),(buf_size),(ppos), \ (pmhdstr)->cstr, (pmhdstr)->len) /** @@ -805,18 +805,18 @@ build_header_response_inn (struct MHD_Connection *restrict c) { /* HTTP/1.1 reply */ /* Use HTTP/1.1 responses for HTTP/1.0 clients. * See https://datatracker.ietf.org/doc/html/rfc7230#section-2.6 */ - if (! buffer_append_s (buf, &pos, buf_size, MHD_HTTP_VERSION_1_1_STR)) + if (! buffer_append_s (buf, buf_size, &pos, MHD_HTTP_VERSION_1_1_STR)) return false; } else { /* HTTP/1.0 reply */ - if (! buffer_append_s (buf, &pos, buf_size, MHD_HTTP_VERSION_1_0_STR)) + if (! buffer_append_s (buf, buf_size, &pos, MHD_HTTP_VERSION_1_0_STR)) return false; } } else { /* ICY reply */ - if (! buffer_append_s (buf, &pos, buf_size, "ICY")) + if (! buffer_append_s (buf, buf_size, &pos, "ICY")) return false; } @@ -836,15 +836,15 @@ build_header_response_inn (struct MHD_Connection *restrict c) const struct MHD_String *stat_str; stat_str = mhd_HTTP_status_code_to_string_int (rcode); mhd_assert (0 != stat_str->len); - if (! buffer_append (buf, &pos, buf_size, + if (! buffer_append (buf, buf_size, &pos, stat_str->cstr, stat_str->len)) return false; } /* The linefeed */ if (! buffer_append_crlf (buf, - &pos, - buf_size)) + buf_size, + &pos)) return false; /* * The headers * */ @@ -853,13 +853,13 @@ 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 (! buffer_append (buf, &pos, buf_size, + if (! buffer_append (buf, buf_size, &pos, r->special_resp.spec_hdr, r->special_resp.spec_hdr_len)) return false; if (! buffer_append_crlf (buf, - &pos, - buf_size)) + buf_size, + &pos)) return false; } @@ -884,13 +884,13 @@ build_header_response_inn (struct MHD_Connection *restrict c) { if (use_conn_close) { - if (! buffer_append_s (buf, &pos, buf_size, + if (! buffer_append_s (buf, buf_size, &pos, MHD_HTTP_HEADER_CONNECTION ": close\r\n")) return false; } else if (use_conn_k_alive) { - if (! buffer_append_s (buf, &pos, buf_size, + if (! buffer_append_s (buf, buf_size, &pos, MHD_HTTP_HEADER_CONNECTION ": Keep-Alive\r\n")) return false; } @@ -918,7 +918,7 @@ build_header_response_inn (struct MHD_Connection *restrict c) { size_t nonce_pos; nonce_pos = pos + dg_hdr->nonce_pos; - if (! buffer_append_mstr (buf, &pos, buf_size, \ + if (! buffer_append_mstr (buf, buf_size, &pos, \ &(dg_hdr->hdr))) return false; memcpy (buf + nonce_pos, @@ -945,7 +945,7 @@ build_header_response_inn (struct MHD_Connection *restrict c) if (c->rp.props.chunked) { /* Chunked encoding is used */ mhd_assert (! c->rp.props.end_by_closing); - if (! buffer_append_s (buf, &pos, buf_size, + if (! buffer_append_s (buf, buf_size, &pos, MHD_HTTP_HEADER_TRANSFER_ENCODING ": " \ "chunked\r\n")) return false; @@ -959,7 +959,7 @@ build_header_response_inn (struct MHD_Connection *restrict c) { /* The size is known and can be indicated by the header */ if (! r->cfg.cnt_len_by_app) { /* The response does not have app-defined "Content-Length" header */ - if (! buffer_append_s (buf, &pos, buf_size, + if (! buffer_append_s (buf, buf_size, &pos, MHD_HTTP_HEADER_CONTENT_LENGTH ": ")) return false; el_size = mhd_uint64_to_str (r->cntn_size, @@ -970,8 +970,8 @@ build_header_response_inn (struct MHD_Connection *restrict c) pos += el_size; if (! buffer_append_crlf (buf, - &pos, - buf_size)) + buf_size, + &pos)) return false; } } @@ -986,8 +986,8 @@ build_header_response_inn (struct MHD_Connection *restrict c) /* * Header termination * */ if (! buffer_append_crlf (buf, - &pos, - buf_size)) + buf_size, + &pos)) return false; c->write_buffer_append_offset = pos;