libmicrohttpd

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

commit 1b435684a7a6ab12d2b85afa775ec629c0ce6a19
parent a761eb67b6e996003ed34bce3f35b4d9d437ad86
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Sat, 13 Nov 2021 13:48:55 +0300

Clarified termination reasons description

Updated termination reasons reporting.
Make sure that MHD_REQUEST_TERMINATED_READ_ERROR is really reported
Before this patch MHD_REQUEST_TERMINATED_WITH_ERROR was reported instead

Diffstat:
Msrc/include/microhttpd.h | 18+++++++++---------
Msrc/microhttpd/connection.c | 38+++++++++++++++++++++++++++++++-------
2 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h @@ -2004,8 +2004,9 @@ enum MHD_RequestTerminationCode /** * Error handling the connection (resources - * exhausted, other side closed connection, - * application error accepting request, etc.) + * exhausted, application error accepting request, + * decrypt error (for HTTPS), connection died when + * sending the response etc.) * @ingroup request */ MHD_REQUEST_TERMINATED_WITH_ERROR = 1, @@ -2026,19 +2027,18 @@ enum MHD_RequestTerminationCode MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN = 3, /** - * We tried to read additional data, but the other side closed the - * connection. This error is similar to - * #MHD_REQUEST_TERMINATED_WITH_ERROR, but specific to the case where - * the connection died because the other side did not send expected - * data. + * We tried to read additional data, but the connection became broken or + * the other side hard closed the connection. + * This error is similar to #MHD_REQUEST_TERMINATED_WITH_ERROR, but + * specific to the case where the connection died before request completely + * received. * @ingroup request */ MHD_REQUEST_TERMINATED_READ_ERROR = 4, /** * The client terminated the connection by closing the socket - * for writing (TCP half-closed); MHD aborted sending the - * response according to RFC 2616, section 8.1.4. + * for writing (TCP half-closed) while still sending request. * @ingroup request */ MHD_REQUEST_TERMINATED_CLIENT_ABORT = 5 diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c @@ -3646,11 +3646,17 @@ MHD_connection_handle_read (struct MHD_Connection *connection) return; /* No new data to process. */ if (MHD_ERR_CONNRESET_ == bytes_read) { - CONNECTION_CLOSE_ERROR (connection, - (MHD_CONNECTION_INIT == connection->state) ? - NULL : - _ ( - "Socket disconnected while reading request.")); + if ( (MHD_CONNECTION_INIT < connection->state) && + (MHD_CONNECTION_FULL_REQ_RECEIVED > connection->state) ) + { +#ifdef HAVE_MESSAGES + MHD_DLOG (connection->daemon, + _ ("Socket has been disconnected when reading request.\n")); +#endif + connection->discard_request = true; + } + MHD_connection_close_ (connection, + MHD_REQUEST_TERMINATED_READ_ERROR); return; } @@ -3669,8 +3675,26 @@ MHD_connection_handle_read (struct MHD_Connection *connection) if (0 == bytes_read) { /* Remote side closed connection. */ connection->read_closed = true; - MHD_connection_close_ (connection, - MHD_REQUEST_TERMINATED_CLIENT_ABORT); + if ( (MHD_CONNECTION_INIT < connection->state) && + (MHD_CONNECTION_FULL_REQ_RECEIVED > connection->state) ) + { +#ifdef HAVE_MESSAGES + MHD_DLOG (connection->daemon, + _ ("Connection was closed by remote side with incomplete " + "request.\n")); +#endif + connection->discard_request = true; + MHD_connection_close_ (connection, + MHD_REQUEST_TERMINATED_CLIENT_ABORT); + } + else if (MHD_CONNECTION_INIT == connection->state) + /* This termination code cannot be reported to the application + * because application has not been informed yet about this request */ + MHD_connection_close_ (connection, + MHD_REQUEST_TERMINATED_COMPLETED_OK); + else + MHD_connection_close_ (connection, + MHD_REQUEST_TERMINATED_WITH_ERROR); return; } connection->read_buffer_offset += bytes_read;