From e09b6df63edaa076388820d63a8cdccfd35e33cb Mon Sep 17 00:00:00 2001 From: "Evgeny Grin (Karlson2k)" Date: Sat, 12 Dec 2020 18:44:15 +0300 Subject: [PATCH] Fixed sending response body for the HEAD requests Sometimes response body as available but MHD must not send it, like for HEAD requests. This commit fixed regression caused by usage of MHD_send_on_connection2_ (). --- src/microhttpd/connection.c | 39 ++++++++++++++++++++++++++++--------- src/microhttpd/mhd_send.c | 1 + 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c index 74f07408..8a06f7e2 100644 --- a/src/microhttpd/connection.c +++ b/src/microhttpd/connection.c @@ -2920,15 +2920,35 @@ MHD_connection_handle_write (struct MHD_Connection *connection) mhd_assert (NULL != connection->response); mhd_assert ( (0 == connection->response->data_size) || \ (0 == connection->response->data_start) ); + mhd_assert ( (0 == connection->response_write_position) || \ + (response->total_size == + connection->response_write_position) || \ + (MHD_SIZE_UNKNOWN == + connection->response_write_position) ); - /* Send response headers alongside the response body, if the body - * data is available. */ - ret = MHD_send_on_connection2_ (connection, - &connection->write_buffer - [connection->write_buffer_send_offset], - wb_ready, - connection->response->data, - connection->response->data_size); + if (0 == connection->response_write_position) + { + /* Send response headers alongside the response body, if the body + * data is available. */ + ret = MHD_send_on_connection2_ (connection, + &connection->write_buffer + [connection->write_buffer_send_offset], + wb_ready, + connection->response->data, + connection->response->data_size); + } + else + { + /* This is response for HEAD request or reply body is not allowed + * for any other reason. */ + /* Do not send the body data even if it's available. */ + ret = MHD_send_on_connection2_ (connection, + &connection->write_buffer + [connection->write_buffer_send_offset], + wb_ready, + NULL, + 0); + } if (ret < 0) { @@ -2944,9 +2964,10 @@ MHD_connection_handle_write (struct MHD_Connection *connection) { /* The complete header and some response data have been sent, * update both offsets. */ + mhd_assert (0 == connection->response_write_position); mhd_assert (! connection->have_chunked_upload); connection->write_buffer_send_offset += wb_ready; - connection->response_write_position += ret - wb_ready; + connection->response_write_position = ret - wb_ready; } else connection->write_buffer_send_offset += ret; diff --git a/src/microhttpd/mhd_send.c b/src/microhttpd/mhd_send.c index 2bf9a071..f0cdfb58 100644 --- a/src/microhttpd/mhd_send.c +++ b/src/microhttpd/mhd_send.c @@ -841,6 +841,7 @@ MHD_send_on_connection2_ (struct MHD_Connection *connection, const bool no_vec = false; #endif /* ! HTTPS_SUPPORT */ #endif /* HAVE_SENDMSG || HAVE_WRITEV */ + mhd_assert ( (NULL != buffer) || (0 == buffer_size) ); if ( #if defined(HAVE_SENDMSG) || defined(HAVE_WRITEV)