GNU libmicrohttpd  0.9.29
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
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, "%s: state: %s\n",
138  __FUNCTION__, MHD_state_to_string (connection->state));
139 #endif
140  timeout = connection->connection_timeout;
141  if ( (timeout != 0) && (timeout <= (MHD_monotonic_time() - connection->last_activity)))
142  MHD_connection_close (connection,
144  switch (connection->state)
145  {
146  /* on newly created connections we might reach here before any reply has been received */
148  break;
149  /* close connection if necessary */
151  gnutls_bye (connection->tls_session, GNUTLS_SHUT_RDWR);
152  return MHD_connection_handle_idle (connection);
153  default:
154  if ( (0 != gnutls_record_check_pending (connection->tls_session)) &&
155  (MHD_YES != MHD_tls_connection_handle_read (connection)) )
156  return MHD_YES;
157  return MHD_connection_handle_idle (connection);
158  }
159 #if EPOLL_SUPPORT
160  return MHD_connection_epoll_update_ (connection);
161 #else
162  return MHD_YES;
163 #endif
164 }
165 
166 
173 void
175 {
179 }
180 
181 /* end of connection_https.c */
int(* write_handler)(struct MHD_Connection *connection)
Definition: internal.h:806
enum MHD_CONNECTION_STATE state
Definition: internal.h:753
int(* idle_handler)(struct MHD_Connection *connection)
Definition: internal.h:811
int MHD_connection_handle_write(struct MHD_Connection *connection)
Definition: connection.c:2016
Methods for managing connections.
#define MHD_YES
Definition: microhttpd.h:134
Methods for managing response objects.
int(* read_handler)(struct MHD_Connection *connection)
Definition: internal.h:801
struct MHD_Daemon * daemon
Definition: internal.h:549
static int MHD_tls_connection_handle_write(struct MHD_Connection *connection)
int MHD_connection_handle_read(struct MHD_Connection *connection)
Definition: connection.c:1953
internal shared structures
void MHD_set_https_callbacks(struct MHD_Connection *connection)
time_t MHD_monotonic_time(void)
Definition: internal.c:169
static int MHD_tls_connection_handle_read(struct MHD_Connection *connection)
static int run_tls_handshake(struct MHD_Connection *connection)
time_t last_activity
Definition: internal.h:703
int MHD_connection_handle_idle(struct MHD_Connection *connection)
Definition: connection.c:2221
unsigned int connection_timeout
Definition: internal.h:709
static int MHD_tls_connection_handle_idle(struct MHD_Connection *connection)
#define MHD_NO
Definition: microhttpd.h:139
void MHD_connection_close(struct MHD_Connection *connection, enum MHD_RequestTerminationCode termination_code)
Definition: connection.c:261
memory pool; mostly used for efficient (de)allocation for each connection and bounding memory use for...