aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/connection.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/connection.c')
-rw-r--r--src/daemon/connection.c91
1 files changed, 65 insertions, 26 deletions
diff --git a/src/daemon/connection.c b/src/daemon/connection.c
index 15a9be42..6735147a 100644
--- a/src/daemon/connection.c
+++ b/src/daemon/connection.c
@@ -156,6 +156,58 @@ MHD_need_100_continue (struct MHD_Connection *connection)
156} 156}
157 157
158/** 158/**
159 * Prepare the response buffer of this connection for
160 * sending. Assumes that the response mutex is
161 * already held. If the transmission is complete,
162 * this function may close the socket (and return
163 * MHD_NO).
164 *
165 * @return MHD_NO if readying the response failed
166 */
167static int
168ready_response (struct MHD_Connection *connection)
169{
170 int ret;
171 struct MHD_Response *response;
172
173 response = connection->response;
174 ret = response->crc (response->crc_cls,
175 connection->messagePos,
176 response->data,
177 MIN (response->data_buffer_size,
178 response->total_size - connection->messagePos));
179 if (ret == -1)
180 {
181 /* end of message, signal other side by closing! */
182 response->total_size = connection->messagePos;
183 CLOSE (connection->socket_fd);
184 connection->socket_fd = -1;
185 return MHD_NO;
186 }
187 response->data_start = connection->messagePos;
188 response->data_size = ret;
189 if (ret == 0)
190 {
191 /* avoid busy-waiting when using external select
192 (we assume that the main application will
193 wake up the external select once more data
194 is ready). With internal selects, we
195 have no choice; if the app uses a thread
196 per connection, ret==0 is likely a bug --
197 the application should block until data
198 is ready! */
199 if ((0 ==
200 (connection->daemon->
201 options & (MHD_USE_SELECT_INTERNALLY |
202 MHD_USE_THREAD_PER_CONNECTION))))
203 connection->response_unready = MHD_YES;
204 return MHD_NO;
205 }
206 connection->response_unready = MHD_NO;
207 return MHD_YES;
208}
209
210/**
159 * Obtain the select sets for this connection 211 * Obtain the select sets for this connection
160 * 212 *
161 * @return MHD_YES on success 213 * @return MHD_YES on success
@@ -205,7 +257,16 @@ MHD_connection_get_fdset (struct MHD_Connection *connection,
205 } 257 }
206 } 258 }
207 } 259 }
208 if ((connection->response != NULL) || MHD_need_100_continue (connection)) 260 if ((connection->response != NULL) &&
261 (connection->response_unready == MHD_YES))
262 {
263 pthread_mutex_lock (&connection->response->mutex);
264 ready_response (connection);
265 pthread_mutex_unlock (&connection->response->mutex);
266 }
267 if (((connection->response != NULL) &&
268 (connection->response_unready == MHD_NO)) ||
269 MHD_need_100_continue (connection))
209 { 270 {
210 FD_SET (fd, write_fd_set); 271 FD_SET (fd, write_fd_set);
211 if (fd > *max_fd) 272 if (fd > *max_fd)
@@ -1090,32 +1151,10 @@ MHD_connection_handle_write (struct MHD_Connection *connection)
1090 if ((response->crc != NULL) && 1151 if ((response->crc != NULL) &&
1091 ((response->data_start > connection->messagePos) || 1152 ((response->data_start > connection->messagePos) ||
1092 (response->data_start + response->data_size <= 1153 (response->data_start + response->data_size <=
1093 connection->messagePos))) 1154 connection->messagePos)) && (MHD_YES != ready_response (connection)))
1094 { 1155 {
1095 ret = response->crc (response->crc_cls, 1156 pthread_mutex_unlock (&response->mutex);
1096 connection->messagePos, 1157 return MHD_YES;
1097 response->data,
1098 MIN (response->data_buffer_size,
1099 response->total_size -
1100 connection->messagePos));
1101 if (ret == -1)
1102 {
1103 /* end of message, signal other side by closing! */
1104 response->total_size = connection->messagePos;
1105 CLOSE (connection->socket_fd);
1106 connection->socket_fd = -1;
1107 if (response->crc != NULL)
1108 pthread_mutex_unlock (&response->mutex);
1109 return MHD_YES;
1110 }
1111 response->data_start = connection->messagePos;
1112 response->data_size = ret;
1113 if (ret == 0)
1114 {
1115 if (response->crc != NULL)
1116 pthread_mutex_unlock (&response->mutex);
1117 return MHD_YES;
1118 }
1119 } 1158 }
1120 /* transmit */ 1159 /* transmit */
1121 ret = SEND (connection->socket_fd, 1160 ret = SEND (connection->socket_fd,