aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2018-02-16 06:08:14 +0100
committerChristian Grothoff <christian@grothoff.org>2018-02-16 06:08:14 +0100
commit2207231ef2e361a4fa7c894348da6270c71df4bd (patch)
tree935ea95620a4603b228a643e8612bb6157faa1aa
parent0ca235616cb8f2ebfa7cebb7fabef1ff678a6377 (diff)
downloadlibmicrohttpd-2207231ef2e361a4fa7c894348da6270c71df4bd.tar.gz
libmicrohttpd-2207231ef2e361a4fa7c894348da6270c71df4bd.zip
implement connection_finish_forward
-rw-r--r--src/include/microhttpd_tls.h6
-rw-r--r--src/lib/Makefile.am3
-rw-r--r--src/lib/connection_add.c8
-rw-r--r--src/lib/connection_finish_forward.c94
-rw-r--r--src/lib/connection_finish_forward.h44
-rw-r--r--src/lib/connection_update_last_activity.c4
-rw-r--r--src/lib/daemon_epoll.c1
-rw-r--r--src/lib/daemon_poll.c4
-rw-r--r--src/lib/daemon_select.c1
-rw-r--r--src/lib/internal.h15
10 files changed, 165 insertions, 15 deletions
diff --git a/src/include/microhttpd_tls.h b/src/include/microhttpd_tls.h
index e59b983b..58734837 100644
--- a/src/include/microhttpd_tls.h
+++ b/src/include/microhttpd_tls.h
@@ -90,6 +90,12 @@ struct MHD_TLS_Plugin
90 (*setup_connection)(void *cls, 90 (*setup_connection)(void *cls,
91 ...); 91 ...);
92 92
93
94 enum MHD_Bool
95 (*shutdown_connection) (void *cls,
96 struct MHD_TLS_ConnectionState *cs);
97
98
93 void 99 void
94 (*teardown_connection)(void *cls, 100 (*teardown_connection)(void *cls,
95 struct MHD_TLS_ConnectionState *cs); 101 struct MHD_TLS_ConnectionState *cs);
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index 87208335..b53e00b4 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -59,8 +59,11 @@ libmicrohttpd_la_SOURCES = \
59 action_process_upload.c \ 59 action_process_upload.c \
60 action_suspend.c \ 60 action_suspend.c \
61 connection_add.c connection_add.h \ 61 connection_add.c connection_add.h \
62 connection_close.c connection_close.h \
63 connection_finish_forward.c connection_finish_forward.h \
62 connection_info.c \ 64 connection_info.c \
63 connection_options.c \ 65 connection_options.c \
66 connection_update_last_activity.c connection_update_last_activity.h \
64 daemon_create.c \ 67 daemon_create.c \
65 daemon_destroy.c \ 68 daemon_destroy.c \
66 daemon_epoll.c daemon_epoll.h \ 69 daemon_epoll.c daemon_epoll.h \
diff --git a/src/lib/connection_add.c b/src/lib/connection_add.c
index edca88f9..eb5984e3 100644
--- a/src/lib/connection_add.c
+++ b/src/lib/connection_add.c
@@ -23,6 +23,7 @@
23 */ 23 */
24#include "internal.h" 24#include "internal.h"
25#include "connection_add.h" 25#include "connection_add.h"
26#include "connection_update_last_activity.h"
26#include "daemon_ip_limit.h" 27#include "daemon_ip_limit.h"
27#include "daemon_select.h" 28#include "daemon_select.h"
28#include "daemon_poll.h" 29#include "daemon_poll.h"
@@ -187,7 +188,7 @@ thread_main_handle_connection (void *data)
187 188
188 if (was_suspended) 189 if (was_suspended)
189 { 190 {
190 MHD_update_last_activity_ (con); /* Reset timeout timer. */ 191 MHD_connection_update_last_activity_ (con); /* Reset timeout timer. */
191 /* Process response queued during suspend and update states. */ 192 /* Process response queued during suspend and update states. */
192 MHD_connection_handle_idle (con); 193 MHD_connection_handle_idle (con);
193 was_suspended = false; 194 was_suspended = false;
@@ -394,14 +395,11 @@ thread_main_handle_connection (void *data)
394 { 395 {
395 /* Normal HTTP processing is finished, 396 /* Normal HTTP processing is finished,
396 * notify application. */ 397 * notify application. */
397 if ( (NULL != con->request.response->termination_cb) && 398 if (NULL != con->request.response->termination_cb)
398 (con->request.client_aware) )
399 con->request.response->termination_cb 399 con->request.response->termination_cb
400 (con->request.response->termination_cb_cls, 400 (con->request.response->termination_cb_cls,
401 MHD_REQUEST_TERMINATED_COMPLETED_OK, 401 MHD_REQUEST_TERMINATED_COMPLETED_OK,
402 con->request.client_context); 402 con->request.client_context);
403 con->request.client_aware = false;
404
405 thread_main_connection_upgrade (con); 403 thread_main_connection_upgrade (con);
406 /* MHD_connection_finish_forward_() was called by thread_main_connection_upgrade(). */ 404 /* MHD_connection_finish_forward_() was called by thread_main_connection_upgrade(). */
407 405
diff --git a/src/lib/connection_finish_forward.c b/src/lib/connection_finish_forward.c
new file mode 100644
index 00000000..aac49f8c
--- /dev/null
+++ b/src/lib/connection_finish_forward.c
@@ -0,0 +1,94 @@
1/*
2 This file is part of libmicrohttpd
3 Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19/**
20 * @file lib/connection_finish_forward.c
21 * @brief complete upgrade socket forwarding operation in TLS mode
22 * @author Christian Grothoff
23 */
24#include "internal.h"
25#include "connection_finish_forward.h"
26
27
28#if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT)
29/**
30 * Stop TLS forwarding on upgraded connection and
31 * reflect remote disconnect state to socketpair.
32 * @remark In thread-per-connection mode this function
33 * can be called from any thread, in other modes this
34 * function must be called only from thread that process
35 * daemon's select()/poll()/etc.
36 *
37 * @param connection the upgraded connection
38 */
39void
40MHD_connection_finish_forward_ (struct MHD_Connection *connection)
41{
42 struct MHD_Daemon *daemon = connection->daemon;
43 struct MHD_UpgradeResponseHandle *urh = connection->request.urh;
44
45 if (NULL == daemon->tls_api)
46 return; /* Nothing to do with non-TLS connection. */
47
48 if (MHD_TM_THREAD_PER_CONNECTION != daemon->threading_model)
49 DLL_remove (daemon->urh_head,
50 daemon->urh_tail,
51 urh);
52#if EPOLL_SUPPORT
53 if ( (MHD_ELS_EPOLL == daemon->event_loop_syscall) &&
54 (0 != epoll_ctl (daemon->epoll_upgrade_fd,
55 EPOLL_CTL_DEL,
56 connection->socket_fd,
57 NULL)) )
58 {
59 MHD_PANIC (_("Failed to remove FD from epoll set\n"));
60 }
61 if (urh->in_eready_list)
62 {
63 EDLL_remove (daemon->eready_urh_head,
64 daemon->eready_urh_tail,
65 urh);
66 urh->in_eready_list = false;
67 }
68#endif /* EPOLL_SUPPORT */
69 if (MHD_INVALID_SOCKET != urh->mhd.socket)
70 {
71#if EPOLL_SUPPORT
72 if ( (MHD_ELS_EPOLL == daemon->event_loop_syscall) &&
73 (0 != epoll_ctl (daemon->epoll_upgrade_fd,
74 EPOLL_CTL_DEL,
75 urh->mhd.socket,
76 NULL)) )
77 {
78 MHD_PANIC (_("Failed to remove FD from epoll set\n"));
79 }
80#endif /* EPOLL_SUPPORT */
81 /* Reflect remote disconnect to application by breaking
82 * socketpair connection. */
83 shutdown (urh->mhd.socket,
84 SHUT_RDWR);
85 }
86 /* Socketpair sockets will remain open as they will be
87 * used with MHD_UPGRADE_ACTION_CLOSE. They will be
88 * closed by MHD_cleanup_upgraded_connection_() during
89 * connection's final cleanup.
90 */
91}
92#endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT*/
93
94/* end of connection_finish_forward.c */
diff --git a/src/lib/connection_finish_forward.h b/src/lib/connection_finish_forward.h
new file mode 100644
index 00000000..c6c04f42
--- /dev/null
+++ b/src/lib/connection_finish_forward.h
@@ -0,0 +1,44 @@
1/*
2 This file is part of libmicrohttpd
3 Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19/**
20 * @file lib/connection_finish_forward.h
21 * @brief complete upgrade socket forwarding operation in TLS mode
22 * @author Christian Grothoff
23 */
24
25#ifndef CONNECTION_FINISH_FORWARD_H
26#define CONNECTION_FINISH_FORWARD_H
27
28
29/**
30 * Stop TLS forwarding on upgraded connection and
31 * reflect remote disconnect state to socketpair.
32 *
33 * @remark In thread-per-connection mode this function
34 * can be called from any thread, in other modes this
35 * function must be called only from thread that process
36 * daemon's select()/poll()/etc.
37 *
38 * @param connection the upgraded connection
39 */
40void
41MHD_connection_finish_forward_ (struct MHD_Connection *connection)
42 MHD_NONNULL (1);
43
44#endif
diff --git a/src/lib/connection_update_last_activity.c b/src/lib/connection_update_last_activity.c
index e235d458..e406a9b0 100644
--- a/src/lib/connection_update_last_activity.c
+++ b/src/lib/connection_update_last_activity.c
@@ -44,10 +44,10 @@ MHD_connection_update_last_activity_ (struct MHD_Connection *connection)
44 return; /* no activity on suspended connections */ 44 return; /* no activity on suspended connections */
45 45
46 connection->last_activity = MHD_monotonic_sec_counter(); 46 connection->last_activity = MHD_monotonic_sec_counter();
47 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 47 if (MHD_TM_THREAD_PER_CONNECTION == daemon->threading_model)
48 return; /* each connection has personal timeout */ 48 return; /* each connection has personal timeout */
49 49
50 if (connection->connection_timeout != daemon->connection_timeout) 50 if (connection->connection_timeout != daemon->connection_default_timeout)
51 return; /* custom timeout, no need to move it in "normal" DLL */ 51 return; /* custom timeout, no need to move it in "normal" DLL */
52 52
53 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); 53 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
diff --git a/src/lib/daemon_epoll.c b/src/lib/daemon_epoll.c
index c23a34da..d85d54e7 100644
--- a/src/lib/daemon_epoll.c
+++ b/src/lib/daemon_epoll.c
@@ -25,6 +25,7 @@
25#include "internal.h" 25#include "internal.h"
26#include "daemon_epoll.h" 26#include "daemon_epoll.h"
27#include "request_resume.h" 27#include "request_resume.h"
28#include "connection_finish_forward.h"
28 29
29#ifdef EPOLL_SUPPORT 30#ifdef EPOLL_SUPPORT
30 31
diff --git a/src/lib/daemon_poll.c b/src/lib/daemon_poll.c
index ba061729..63747bd0 100644
--- a/src/lib/daemon_poll.c
+++ b/src/lib/daemon_poll.c
@@ -24,6 +24,9 @@
24#include "internal.h" 24#include "internal.h"
25#include "daemon_poll.h" 25#include "daemon_poll.h"
26#include "request_resume.h" 26#include "request_resume.h"
27#include "connection_add.h"
28#include "connection_finish_forward.h"
29
27 30
28#ifdef HAVE_POLL 31#ifdef HAVE_POLL
29 32
@@ -463,7 +466,6 @@ void
463MHD_daemon_upgrade_connection_with_poll_ (struct MHD_Connection *con) 466MHD_daemon_upgrade_connection_with_poll_ (struct MHD_Connection *con)
464{ 467{
465 struct MHD_UpgradeResponseHandle *urh = con->request.urh; 468 struct MHD_UpgradeResponseHandle *urh = con->request.urh;
466 struct MHD_Daemon *daemon = con->daemon;
467 struct pollfd p[2]; 469 struct pollfd p[2];
468 470
469 memset (p, 471 memset (p,
diff --git a/src/lib/daemon_select.c b/src/lib/daemon_select.c
index 1f3b86ad..9c80812c 100644
--- a/src/lib/daemon_select.c
+++ b/src/lib/daemon_select.c
@@ -25,6 +25,7 @@
25#include "internal.h" 25#include "internal.h"
26#include "daemon_select.h" 26#include "daemon_select.h"
27#include "request_resume.h" 27#include "request_resume.h"
28#include "connection_finish_forward.h"
28 29
29 30
30/** 31/**
diff --git a/src/lib/internal.h b/src/lib/internal.h
index 8d1f22d8..e958d2a5 100644
--- a/src/lib/internal.h
+++ b/src/lib/internal.h
@@ -607,13 +607,6 @@ struct MHD_Request
607 enum MHD_RequestEventLoopInfo event_loop_info; 607 enum MHD_RequestEventLoopInfo event_loop_info;
608 608
609 /** 609 /**
610 * Did we ever call the "default_handler" on this request? (this
611 * flag will determine if we call the #MHD_OPTION_NOTIFY_COMPLETED
612 * handler when the request closes down).
613 */
614 bool client_aware;
615
616 /**
617 * Are we currently inside the "idle" handler (to avoid recursively 610 * Are we currently inside the "idle" handler (to avoid recursively
618 * invoking it). 611 * invoking it).
619 */ 612 */
@@ -801,6 +794,14 @@ struct MHD_Connection
801 bool suspended; 794 bool suspended;
802 795
803 /** 796 /**
797 * Did we ever call the "default_handler" on this request? (this
798 * flag will determine if we call the
799 * #MHD_daemon_set_notify_connection() handler when the connection
800 * closes down).
801 */
802 bool client_aware;
803
804 /**
804 * Are we ready to read from TLS for this connection? 805 * Are we ready to read from TLS for this connection?
805 */ 806 */
806 bool tls_read_ready; 807 bool tls_read_ready;