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

This commit is contained in:
Christian Grothoff
2018-02-16 04:39:07 +01:00
parent 9bf8e55fe1
commit 0ca235616c
5 changed files with 269 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
enum MHD_Bool
(*shutdown_connection) (void *cls,
struct MHD_TLS_ConnectionState *cs);
see: MHD_tls_connection_shutdown()
+104
View File
@@ -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 */
+55
View File
@@ -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
+65
View File
@@ -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 */
+40
View File
@@ -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