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 
46 static void
48  enum MHD_RequestTerminationCode termination_code)
49 {
50  gnutls_bye (connection->tls_session, GNUTLS_SHUT_RDWR);
51  MHD_connection_close (connection, termination_code);
52 }
53 
54 
72 static int
74 {
75  int ret;
76 
77  connection->last_activity = time (NULL);
78  if (connection->state == MHD_TLS_CONNECTION_INIT)
79  {
80  ret = gnutls_handshake (connection->tls_session);
81  if (ret == GNUTLS_E_SUCCESS)
82  {
83  /* set connection state to enable HTTP processing */
84  connection->state = MHD_CONNECTION_INIT;
85  return MHD_YES;
86  }
87  if ( (ret == GNUTLS_E_AGAIN) ||
88  (ret == GNUTLS_E_INTERRUPTED) )
89  {
90  /* handshake not done */
91  return MHD_YES;
92  }
93  /* handshake failed */
94 #if HAVE_MESSAGES
95  MHD_DLOG (connection->daemon,
96  "Error: received handshake message out of context\n");
97 #endif
98  MHD_tls_connection_close (connection,
100  return MHD_NO;
101  }
102  return MHD_connection_handle_read (connection);
103 }
104 
105 
115 static int
117 {
118  int ret;
119 
120  connection->last_activity = time (NULL);
121 #if DEBUG_STATES
122  MHD_DLOG (connection->daemon, "%s: state: %s\n",
123  __FUNCTION__, MHD_state_to_string (connection->state));
124 #endif
125  if (connection->state == MHD_TLS_CONNECTION_INIT)
126  {
127  ret = gnutls_handshake (connection->tls_session);
128  if (ret == GNUTLS_E_SUCCESS)
129  {
130  /* set connection state to enable HTTP processing */
131  connection->state = MHD_CONNECTION_INIT;
132  return MHD_YES;
133  }
134  if ( (ret == GNUTLS_E_AGAIN) ||
135  (ret == GNUTLS_E_INTERRUPTED) )
136  {
137  /* handshake not done */
138  return MHD_YES;
139  }
140  /* handshake failed */
141 #if HAVE_MESSAGES
142  MHD_DLOG (connection->daemon,
143  "Error: received handshake message out of context\n");
144 #endif
145  MHD_tls_connection_close (connection,
147  return MHD_NO;
148  }
149  return MHD_connection_handle_write (connection);
150 }
151 
152 
163 static int
165 {
166  unsigned int timeout;
167 
168 #if DEBUG_STATES
169  MHD_DLOG (connection->daemon, "%s: state: %s\n",
170  __FUNCTION__, MHD_state_to_string (connection->state));
171 #endif
172  timeout = connection->daemon->connection_timeout;
173  if ((connection->socket_fd != -1) && (timeout != 0)
174  && (time (NULL) - timeout > connection->last_activity))
175  {
176  MHD_tls_connection_close (connection,
178  return MHD_NO;
179  }
180  switch (connection->state)
181  {
182  /* on newly created connections we might reach here before any reply has been received */
184  return MHD_YES;
185  /* close connection if necessary */
187  if (connection->socket_fd != -1)
188  MHD_tls_connection_close (connection,
190  return MHD_NO;
191  default:
192  if ( (0 != gnutls_record_check_pending (connection->tls_session)) &&
193  (MHD_YES != MHD_tls_connection_handle_read (connection)) )
194  return MHD_NO;
195  return MHD_connection_handle_idle (connection);
196  }
197  return MHD_YES;
198 }
199 
200 
205 void
207 {
211 }
212 
213 /* end of connection_https.c */