libmicrohttpd

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

commit 0e0ad2b23834a348a7b04289579ff7cbdd9c8a61
parent 5df71088736f5695d0cbf570d7f9d6971a38349d
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Tue,  1 Dec 2020 21:22:02 +0300

Added: track TCP_NODELAY state of client sockets

Diffstat:
Msrc/microhttpd/daemon.c | 28++++++++++++++++++++--------
Msrc/microhttpd/internal.h | 16++++++++++++++++
Msrc/microhttpd/mhd_send.c | 9++++++---
3 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c @@ -2357,6 +2357,7 @@ psk_gnutls_adapter (gnutls_session_t session, * to receive an HTTP request from this socket next). * @param addr IP address of the client * @param addrlen number of bytes in @a addr + * @param external_add indicate that socket has been added externally * @param non_blck indicate that socket in non-blocking mode * @param pconnection pointer to variable that receive pointer to * the new connection structure. @@ -2370,6 +2371,7 @@ new_connection_prepare_ (struct MHD_Daemon *daemon, MHD_socket client_socket, const struct sockaddr *addr, socklen_t addrlen, + bool external_add, bool non_blck, struct MHD_Connection **pconnection) { @@ -2460,19 +2462,29 @@ new_connection_prepare_ (struct MHD_Daemon *daemon, } connection->sk_cork_on = false; #if defined(MHD_TCP_CORK_NOPUSH) || defined(MHD_USE_MSG_MORE) + (void) external_add; /* Mute compiler warning */ /* We will use TCP_CORK or TCP_NOPUSH or MSG_MORE to control transmission, disable Nagle's algorithm (always) */ - if ( (0 != MHD_socket_set_nodelay_ (client_socket, - true)) && - (EOPNOTSUPP != errno) ) + if (0 != MHD_socket_set_nodelay_ (client_socket, true)) { + if (EOPNOTSUPP != MHD_socket_get_error_ ()) + { #ifdef HAVE_MESSAGES - MHD_DLOG (daemon, - _ ("Failed to disable TCP Nagle on socket: %s\n"), - MHD_socket_last_strerr_ ()); + MHD_DLOG (daemon, + _ ("Failed to disable TCP Nagle on socket: %s\n"), + MHD_socket_last_strerr_ ()); #endif + } + connection->sk_nodelay = _MHD_UNKNOWN; } -#endif + else + connection->sk_nodelay = _MHD_ON; +#else /* !MHD_TCP_CORK_NOPUSH && !MHD_USE_MSG_MORE */ + if (! external_add) + connection->sk_nodelay = _MHD_OFF; + else + connection->sk_nodelay = _MHD_UNKNOWN; +#endif /* !MHD_TCP_CORK_NOPUSH && !MHD_USE_MSG_MORE */ connection->connection_timeout = daemon->connection_timeout; if (NULL == (connection->addr = malloc (addrlen))) @@ -2911,7 +2923,7 @@ internal_add_connection (struct MHD_Daemon *daemon, } if (MHD_NO == new_connection_prepare_ (daemon, client_socket, addr, addrlen, - non_blck, &connection)) + external_add, non_blck, &connection)) return MHD_NO; if ((external_add) && diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h @@ -167,6 +167,17 @@ extern void *mhd_panic_cls; /** + * Tri-state on/off/unknown + */ +enum MHD_tristate +{ + _MHD_UNKNOWN = -1, /**< State is not yet checked nor set */ + _MHD_OFF = false, /**< State is "off" / "disabled" */ + _MHD_ON = true /**< State is "on" / "enabled" */ +}; + + +/** * State of the socket with respect to epoll (bitmask). */ enum MHD_EpollState @@ -924,6 +935,11 @@ struct MHD_Connection bool sk_cork_on; /** + * Tracks TCP_NODELAY state of the connection socket. + */ + enum MHD_tristate sk_nodelay; + + /** * Has this socket been closed for reading (i.e. other side closed * the connection)? If so, we must completely close the connection * once we are done sending our response (and stop trying to read diff --git a/src/microhttpd/mhd_send.c b/src/microhttpd/mhd_send.c @@ -194,14 +194,17 @@ pre_send_setopt (struct MHD_Connection *connection, /* CORK/NOPUSH do not exist on this platform, Turning on/off of Naggle's algorithm (otherwise we keep it always off) */ - if (connection->sk_cork_on == buffer_data) + if (connection->sk_nodelay == push_data) { /* nothing to do, success! */ return; } if (0 == MHD_socket_set_nodelay_ (connection->socket_fd, (push_data))) - connection->sk_cork_on = !push_data; + { + connection->sk_cork_on = ! push_data; + connection->sk_nodelay = push_data; + } #endif } @@ -430,7 +433,7 @@ MHD_send_on_connection_ (struct MHD_Connection *connection, #endif /* EPOLL_SUPPORT */ } post_send_setopt (connection, tls_conn, - (push_data && (buffer_size == (size_t) ret)) ); + (push_data && (buffer_size == (size_t) ret)) ); return ret; }