GNU libmicrohttpd  0.9.5
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) && (MHD_monotonic_time() - timeout > 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  return MHD_YES;
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  return MHD_YES;
160 }
161 
162 
167 void
169 {
173 }
174 
175 /* end of connection_https.c */