diff options
Diffstat (limited to 'src/lib/connection_options.c')
-rw-r--r-- | src/lib/connection_options.c | 111 |
1 files changed, 104 insertions, 7 deletions
diff --git a/src/lib/connection_options.c b/src/lib/connection_options.c index 163c956a..24301ddf 100644 --- a/src/lib/connection_options.c +++ b/src/lib/connection_options.c | |||
@@ -1,16 +1,113 @@ | |||
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 | /** | ||
21 | * @file lib/connection_options.c | ||
22 | * @brief functions to set per-connection options | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "internal.h" | ||
26 | |||
1 | 27 | ||
2 | /** | 28 | /** |
3 | * Generate option to set a custom timeout for the given connection. | 29 | * Set custom timeout for the given connection. Specified as the |
4 | * Specified as the number of seconds. Use zero for no timeout. If | 30 | * number of seconds. Use zero for no timeout. Calling this function |
5 | * timeout was set to zero (or unset) before, setting of a new value | 31 | * will reset timeout timer. |
6 | * by MHD_connection_set_option() will reset timeout timer. | ||
7 | * | 32 | * |
8 | * @param connection connection to configure timeout for | 33 | * @param connection connection to configure timeout for |
9 | * @param timeout_s new timeout in seconds | 34 | * @param timeout_s new timeout in seconds |
10 | */ | 35 | */ |
11 | struct MHD_ConnectionOption | 36 | void |
12 | MHD_connection_timeout (struct MHD_Connection *connection, | 37 | MHD_connection_set_timeout (struct MHD_Connection *connection, |
13 | unsigned int timeout_s); | 38 | unsigned int timeout_s) |
39 | { | ||
40 | struct MHD_Daemon *daemon = connection->daemon; | ||
41 | |||
42 | connection->last_activity = MHD_monotonic_sec_counter(); | ||
43 | if (MHD_TM_THREAD_PER_CONNECTION == daemon->threading_model) | ||
44 | { | ||
45 | /* Simple case, no need to lock to update DLLs */ | ||
46 | connection->connection_timeout = (time_t) timeout_s; | ||
47 | return; | ||
48 | } | ||
49 | MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); | ||
50 | if (! connection->suspended) | ||
51 | { | ||
52 | if (connection->connection_timeout == daemon->connection_timeout) | ||
53 | XDLL_remove (daemon->normal_timeout_head, | ||
54 | daemon->normal_timeout_tail, | ||
55 | connection); | ||
56 | else | ||
57 | XDLL_remove (daemon->manual_timeout_head, | ||
58 | daemon->manual_timeout_tail, | ||
59 | connection); | ||
60 | } | ||
61 | connection->connection_timeout = (time_t) timeout_s; | ||
62 | if (! connection->suspended) | ||
63 | { | ||
64 | if (connection->connection_timeout == daemon->connection_timeout) | ||
65 | XDLL_insert (daemon->normal_timeout_head, | ||
66 | daemon->normal_timeout_tail, | ||
67 | connection); | ||
68 | else | ||
69 | XDLL_insert (daemon->manual_timeout_head, | ||
70 | daemon->manual_timeout_tail, | ||
71 | connection); | ||
72 | } | ||
73 | MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex); | ||
74 | } | ||
75 | |||
76 | |||
77 | /** | ||
78 | * Update the 'last_activity' field of the connection to the current | ||
79 | * time and move the connection to the head of the 'normal_timeout' | ||
80 | * list if the timeout for the connection uses the default value. | ||
81 | * | ||
82 | * @param connection the connection that saw some activity | ||
83 | */ | ||
84 | void | ||
85 | MHD_update_last_activity_ (struct MHD_Connection *connection) | ||
86 | { | ||
87 | struct MHD_Daemon *daemon = connection->daemon; | ||
88 | |||
89 | if (0 == connection->connection_timeout) | ||
90 | return; /* Skip update of activity for connections | ||
91 | without timeout timer. */ | ||
92 | if (connection->suspended) | ||
93 | return; /* no activity on suspended connections */ | ||
94 | |||
95 | connection->last_activity = MHD_monotonic_sec_counter(); | ||
96 | if (MHD_TM_THREAD_PER_CONNECTION == daemon->threading_model) | ||
97 | return; /* each connection has personal timeout */ | ||
14 | 98 | ||
99 | if (connection->connection_timeout != daemon->connection_timeout) | ||
100 | return; /* custom timeout, no need to move it in "normal" DLL */ | ||
15 | 101 | ||
102 | MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); | ||
103 | /* move connection to head of timeout list (by remove + add operation) */ | ||
104 | XDLL_remove (daemon->normal_timeout_head, | ||
105 | daemon->normal_timeout_tail, | ||
106 | connection); | ||
107 | XDLL_insert (daemon->normal_timeout_head, | ||
108 | daemon->normal_timeout_tail, | ||
109 | connection); | ||
110 | MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex); | ||
111 | } | ||
16 | 112 | ||
113 | /* end of connection_options.c */ | ||