libmicrohttpd

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

commit 0ca235616cb8f2ebfa7cebb7fabef1ff678a6377
parent 9bf8e55fe1b8306e1319956dab78b462477a4b63
Author: Christian Grothoff <christian@grothoff.org>
Date:   Fri, 16 Feb 2018 04:39:07 +0100

more work on new src/lib implementation: connection_update_last_activity and connection_close handling

Diffstat:
Asrc/gnutls/shutdown_connection.c | 5+++++
Asrc/lib/connection_close.c | 104+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/lib/connection_close.h | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/lib/connection_update_last_activity.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/lib/connection_update_last_activity.h | 40++++++++++++++++++++++++++++++++++++++++
5 files changed, 269 insertions(+), 0 deletions(-)

diff --git a/src/gnutls/shutdown_connection.c b/src/gnutls/shutdown_connection.c @@ -0,0 +1,5 @@ + enum MHD_Bool + (*shutdown_connection) (void *cls, + struct MHD_TLS_ConnectionState *cs); + +see: MHD_tls_connection_shutdown() diff --git a/src/lib/connection_close.c b/src/lib/connection_close.c @@ -0,0 +1,104 @@ +/* + This file is part of libmicrohttpd + Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +/** + * @file lib/connection_close.c + * @brief functions to close a connection + * @author Christian Grothoff + */ +#include "internal.h" +#include "connection_close.h" + + +/** + * Mark connection as "closed". + * + * @remark To be called from any thread. + * + * @param connection connection to close + */ +void +MHD_connection_mark_closed_ (struct MHD_Connection *connection) +{ + const struct MHD_Daemon *daemon = connection->daemon; + + connection->request.state = MHD_REQUEST_CLOSED; + connection->request.event_loop_info = MHD_EVENT_LOOP_INFO_CLEANUP; + if (! daemon->enable_turbo) + { +#ifdef HTTPS_SUPPORT + struct MHD_TLS_Plugin *tls; + + /* For TLS connection use shutdown of TLS layer + * and do not shutdown TCP socket. This give more + * chances to send TLS closure data to remote side. + * Closure of TLS layer will be interpreted by + * remote side as end of transmission. */ + if (NULL != (tls = daemon->tls_api)) + { + if (MHD_YES != + tls->shutdown_connection (tls->cls, + connection->tls_cs)) + { + (void) shutdown (connection->socket_fd, + SHUT_WR); + /* FIXME: log errors */ + } + } + else /* Combined with next 'shutdown()'. */ +#endif /* HTTPS_SUPPORT */ + { + (void) shutdown (connection->socket_fd, + SHUT_WR); /* FIXME: log errors */ + } + } +} + + +/** + * Close the given connection and give the specified termination code + * to the user. + * + * @remark To be called only from thread that process + * connection's recv(), send() and response. + * + * @param connection connection to close + * @param cnc termination reason to give + */ +void +MHD_connection_close_ (struct MHD_Connection *connection, + enum MHD_ConnectionNotificationCode cnc) +{ + struct MHD_Daemon *daemon = connection->daemon; + struct MHD_Response *resp = connection->request.response; + + MHD_connection_mark_closed_ (connection); + if (NULL != resp) + { + connection->request.response = NULL; + MHD_response_queue_for_destroy (resp); + } + if ( (NULL != daemon->notify_connection_cb) && + (connection->client_aware) ) + daemon->notify_connection_cb (daemon->notify_connection_cb_cls, + connection, + cnc); + connection->client_aware = false; +} + +/* end of connection_close.c */ diff --git a/src/lib/connection_close.h b/src/lib/connection_close.h @@ -0,0 +1,55 @@ +/* + This file is part of libmicrohttpd + Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +/** + * @file lib/connection_close.h + * @brief functions to close connection + * @author Christian Grothoff + */ + +#ifndef CONNECTION_CLOSE_H +#define CONNECTION_CLOSE_H + +/** + * Mark connection as "closed". + * + * @remark To be called from any thread. + * + * @param connection connection to close + */ +void +MHD_connection_mark_closed_ (struct MHD_Connection *connection) + MHD_NONNULL (1); + + +/** + * Close the given connection and give the specified termination code + * to the user. + * + * @remark To be called only from thread that process + * connection's recv(), send() and response. + * + * @param connection connection to close + * @param cnc termination reason to give + */ +void +MHD_connection_close_ (struct MHD_Connection *connection, + enum MHD_ConnectionNotificationCode cnc) + MHD_NONNULL (1); + +#endif diff --git a/src/lib/connection_update_last_activity.c b/src/lib/connection_update_last_activity.c @@ -0,0 +1,65 @@ +/* + This file is part of libmicrohttpd + Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +/** + * @file lib/connection_add.c + * @brief functions to add connection to our active set + * @author Christian Grothoff + */ +#include "internal.h" +#include "connection_update_last_activity.h" + + +/** + * Update the 'last_activity' field of the connection to the current time + * and move the connection to the head of the 'normal_timeout' list if + * the timeout for the connection uses the default value. + * + * @param connection the connection that saw some activity + */ +void +MHD_connection_update_last_activity_ (struct MHD_Connection *connection) +{ + struct MHD_Daemon *daemon = connection->daemon; + + if (0 == connection->connection_timeout) + return; /* Skip update of activity for connections + without timeout timer. */ + if (connection->suspended) + return; /* no activity on suspended connections */ + + connection->last_activity = MHD_monotonic_sec_counter(); + if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) + return; /* each connection has personal timeout */ + + if (connection->connection_timeout != daemon->connection_timeout) + return; /* custom timeout, no need to move it in "normal" DLL */ + + MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); + /* move connection to head of timeout list (by remove + add operation) */ + XDLL_remove (daemon->normal_timeout_head, + daemon->normal_timeout_tail, + connection); + XDLL_insert (daemon->normal_timeout_head, + daemon->normal_timeout_tail, + connection); + MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex); +} + +/* end of connection_update_last_activity.c */ + diff --git a/src/lib/connection_update_last_activity.h b/src/lib/connection_update_last_activity.h @@ -0,0 +1,40 @@ +/* + This file is part of libmicrohttpd + Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +/** + * @file lib/connection_update_last_activity.h + * @brief function to update last activity of a connection + * @author Christian Grothoff + */ + +#ifndef CONNECTION_UPDATE_LAST_ACTIVITY_H +#define CONNECTION_UPDATE_LAST_ACTIVITY_H + + +/** + * Update the 'last_activity' field of the connection to the current time + * and move the connection to the head of the 'normal_timeout' list if + * the timeout for the connection uses the default value. + * + * @param connection the connection that saw some activity + */ +void +MHD_connection_update_last_activity_ (struct MHD_Connection *connection) + MHD_NONNULL (1); + +#endif