GNU libmicrohttpd  0.9.29
connection_https.c
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  (C) 2007, 2008, 2010 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 
29 #include "internal.h"
30 #include "connection.h"
31 #include "memorypool.h"
32 #include "response.h"
33 #include "reason_phrase.h"
34 #include <gnutls/gnutls.h>
35 
36 
45 static int
46 run_tls_handshake (struct MHD_Connection *connection)
47 {
48  int ret;
49 
50  connection->last_activity = MHD_monotonic_time();
51  if (connection->state == MHD_TLS_CONNECTION_INIT)
52  {
53  ret = gnutls_handshake (connection->tls_session);
54  if (ret == GNUTLS_E_SUCCESS)
55  {
56  /* set connection state to enable HTTP processing */
57  connection->state = MHD_CONNECTION_INIT;
58  return MHD_YES;
59  }
60  if ( (ret == GNUTLS_E_AGAIN) ||
61  (ret == GNUTLS_E_INTERRUPTED) )
62  {
63  /* handshake not done */
64  return MHD_YES;
65  }
66  /* handshake failed */
67 #if HAVE_MESSAGES
68  MHD_DLOG (connection->daemon,
69  "Error: received handshake message out of context\n");
70 #endif
71  MHD_connection_close (connection,
73  return MHD_YES;
74  }
75  return MHD_NO;
76 }
77 
78 
95 static int
97 {
98  if (MHD_YES == run_tls_handshake (connection))
99  return MHD_YES;
100  return MHD_connection_handle_read (connection);
101 }
102 
103 
112 static int
114 {
115  if (MHD_YES == run_tls_handshake (connection))
116  return MHD_YES;
117  return MHD_connection_handle_write (connection);
118 }
119 
120 
131 static int
133 {
134  unsigned int timeout;
135 
136 #if DEBUG_STATES
137  MHD_DLOG (connection->daemon,
138  "%s: state: %s\n",
139  __FUNCTION__,
140  MHD_state_to_string (connection->state));
141 #endif
142  timeout = connection->connection_timeout;
143  if ( (timeout != 0) && (timeout <= (MHD_monotonic_time() - connection->last_activity)))
144  MHD_connection_close (connection,
146  switch (connection->state)
147  {
148  /* on newly created connections we might reach here before any reply has been received */
150  break;
151  /* close connection if necessary */
153  gnutls_bye (connection->tls_session, GNUTLS_SHUT_RDWR);
154  return MHD_connection_handle_idle (connection);
155  default:
156  if ( (0 != gnutls_record_check_pending (connection->tls_session)) &&
157  (MHD_YES != MHD_tls_connection_handle_read (connection)) )
158  return MHD_YES;
159  return MHD_connection_handle_idle (connection);
160  }
161 #if EPOLL_SUPPORT
162  return MHD_connection_epoll_update_ (connection);
163 #else
164  return MHD_YES;
165 #endif
166 }
167 
168 
175 void
177 {
181 }
182 
183 /* end of connection_https.c */
enum MHD_CONNECTION_STATE state
Definition: internal.h:755
int MHD_connection_handle_write(struct MHD_Connection *connection)
Definition: connection.c:2083
Methods for managing connections.
#define MHD_YES
Definition: microhttpd.h:138
Methods for managing response objects.
struct MHD_Daemon * daemon
Definition: internal.h:551
int(* idle_handler)(struct MHD_Connection *connection)
Definition: internal.h:813
static int MHD_tls_connection_handle_write(struct MHD_Connection *connection)
int MHD_connection_handle_read(struct MHD_Connection *connection)
Definition: connection.c:2020
internal shared structures
void MHD_set_https_callbacks(struct MHD_Connection *connection)
time_t MHD_monotonic_time(void)
Definition: internal.c:182
static int MHD_tls_connection_handle_read(struct MHD_Connection *connection)
int(* read_handler)(struct MHD_Connection *connection)
Definition: internal.h:803
int(* write_handler)(struct MHD_Connection *connection)
Definition: internal.h:808
static int run_tls_handshake(struct MHD_Connection *connection)
time_t last_activity
Definition: internal.h:705
int MHD_connection_handle_idle(struct MHD_Connection *connection)
Definition: connection.c:2288
unsigned int connection_timeout
Definition: internal.h:711
static int MHD_tls_connection_handle_idle(struct MHD_Connection *connection)
#define MHD_NO
Definition: microhttpd.h:143
void MHD_connection_close(struct MHD_Connection *connection, enum MHD_RequestTerminationCode termination_code)
Definition: connection.c:268
memory pool; mostly used for efficient (de)allocation for each connection and bounding memory use for...