Fix #257: Windows 7: SIPE crashes after a minute (II)
[siplcs.git] / src / core / sipe-http-transport.c
blob7583015e895aab8e29c52e1626c7d777d6ee2546
1 /**
2 * @file sipe-http-transport.c
4 * pidgin-sipe
6 * Copyright (C) 2013-2014 SIPE Project <http://sipe.sourceforge.net/>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * SIPE HTTP transport layer implementation
26 * - connection handling: opening, closing, timeout
27 * - interface to backend: sending & receiving of raw messages
28 * - request queue pulling
31 #include <string.h>
32 #include <time.h>
34 #include <glib.h>
36 #include "sipmsg.h"
37 #include "sipe-backend.h"
38 #include "sipe-common.h"
39 #include "sipe-core.h"
40 #include "sipe-core-private.h"
41 #include "sipe-http.h"
42 #include "sipe-schedule.h"
43 #include "sipe-utils.h"
45 #define _SIPE_HTTP_PRIVATE_IF_REQUEST
46 #include "sipe-http-request.h"
47 #define _SIPE_HTTP_PRIVATE_IF_TRANSPORT
48 #include "sipe-http-transport.h"
50 #define SIPE_HTTP_CONNECTION ((struct sipe_http_connection *) connection->user_data)
51 #define SIPE_HTTP_CONNECTION_PRIVATE ((struct sipe_http_connection *) conn_public)
52 #define SIPE_HTTP_CONNECTION_PUBLIC ((struct sipe_http_connection_public *) conn)
54 #define SIPE_HTTP_TIMEOUT_ACTION "<+http-timeout>"
55 #define SIPE_HTTP_DEFAULT_TIMEOUT 60 /* in seconds */
57 struct sipe_http_connection {
58 struct sipe_http_connection_public public;
60 struct sipe_transport_connection *connection;
62 gchar *host_port;
63 time_t timeout; /* in seconds from epoch */
64 gboolean use_tls;
67 struct sipe_http {
68 GHashTable *connections;
69 GQueue *timeouts;
70 time_t next_timeout; /* in seconds from epoch, 0 if timer isn't running */
71 gboolean shutting_down;
74 static gint timeout_compare(gconstpointer a,
75 gconstpointer b,
76 SIPE_UNUSED_PARAMETER gpointer user_data)
78 return(((struct sipe_http_connection *) a)->timeout -
79 ((struct sipe_http_connection *) b)->timeout);
82 static void sipe_http_transport_update_timeout_queue(struct sipe_http_connection *conn,
83 gboolean remove);
84 static void sipe_http_transport_free(gpointer data)
86 struct sipe_http_connection *conn = data;
88 SIPE_DEBUG_INFO("sipe_http_transport_free: destroying connection '%s'",
89 conn->host_port);
91 if (conn->connection)
92 sipe_backend_transport_disconnect(conn->connection);
93 conn->connection = NULL;
95 sipe_http_transport_update_timeout_queue(conn, TRUE);
97 sipe_http_request_shutdown(SIPE_HTTP_CONNECTION_PUBLIC,
98 conn->public.sipe_private->http->shutting_down);
100 g_free(conn->public.host);
102 g_free(conn->host_port);
103 g_free(conn);
106 static void sipe_http_transport_drop(struct sipe_http *http,
107 struct sipe_http_connection *conn,
108 const gchar *message)
110 SIPE_DEBUG_INFO("sipe_http_transport_drop: dropping connection '%s': %s",
111 conn->host_port,
112 message ? message : "REASON UNKNOWN");
114 #if GLIB_CHECK_VERSION(2,30,0)
115 /* this triggers sipe_http_transport_free() */
116 g_hash_table_remove(http->connections, conn->host_port);
117 #else
118 /* GLIB < 2.30 calls destroy notifiers *before* removing the entry */
119 /* which can cause a race condition with sipe_http_transport_new() */
120 g_hash_table_steal(http->connections, conn->host_port);
121 sipe_http_transport_free(conn);
122 #endif
123 /* conn is no longer valid */
126 static void start_timer(struct sipe_core_private *sipe_private,
127 time_t current_time);
128 static void sipe_http_transport_timeout(struct sipe_core_private *sipe_private,
129 gpointer data)
131 struct sipe_http *http = sipe_private->http;
132 struct sipe_http_connection *conn = data;
133 time_t current_time = time(NULL);
135 /* timer has expired */
136 http->next_timeout = 0;
138 while (1) {
139 sipe_http_transport_drop(http, conn, "timeout");
140 /* conn is no longer valid */
142 /* is there another active connection? */
143 conn = g_queue_peek_head(http->timeouts);
144 if (!conn)
145 break;
147 /* restart timer for next connection */
148 if (conn->timeout > current_time) {
149 start_timer(sipe_private, current_time);
150 break;
153 /* next connection timed-out too, loop around */
157 static void start_timer(struct sipe_core_private *sipe_private,
158 time_t current_time)
160 struct sipe_http *http = sipe_private->http;
161 struct sipe_http_connection *conn = g_queue_peek_head(http->timeouts);
163 http->next_timeout = conn->timeout;
164 sipe_schedule_seconds(sipe_private,
165 SIPE_HTTP_TIMEOUT_ACTION,
166 conn,
167 http->next_timeout - current_time,
168 sipe_http_transport_timeout,
169 NULL);
172 static void sipe_http_transport_update_timeout_queue(struct sipe_http_connection *conn,
173 gboolean remove)
175 struct sipe_core_private *sipe_private = conn->public.sipe_private;
176 struct sipe_http *http = sipe_private->http;
177 GQueue *timeouts = http->timeouts;
178 time_t current_time = time(NULL);
180 /* is this connection at head of queue? */
181 gboolean update = (conn == g_queue_peek_head(timeouts));
183 /* update timeout queue */
184 if (remove) {
185 g_queue_remove(timeouts, conn);
186 } else {
187 conn->timeout = current_time + SIPE_HTTP_DEFAULT_TIMEOUT;
188 g_queue_sort(timeouts,
189 timeout_compare,
190 NULL);
193 /* update timer if necessary */
194 if (update) {
195 sipe_schedule_cancel(sipe_private, SIPE_HTTP_TIMEOUT_ACTION);
196 if (g_queue_is_empty(timeouts)) {
197 http->next_timeout = 0;
198 } else {
199 start_timer(sipe_private, current_time);
204 gboolean sipe_http_shutting_down(struct sipe_core_private *sipe_private)
206 struct sipe_http *http = sipe_private->http;
207 /* We need to return FALSE in case HTTP stack isn't initialized yet */
208 if (!http)
209 return(FALSE);
210 return(http->shutting_down);
213 void sipe_http_free(struct sipe_core_private *sipe_private)
215 struct sipe_http *http = sipe_private->http;
216 if (!http)
217 return;
219 /* HTTP stack is shutting down: reject all new requests */
220 http->shutting_down = TRUE;
222 sipe_schedule_cancel(sipe_private, SIPE_HTTP_TIMEOUT_ACTION);
223 g_hash_table_destroy(http->connections);
224 g_queue_free(http->timeouts);
225 g_free(http);
226 sipe_private->http = NULL;
229 static void sipe_http_init(struct sipe_core_private *sipe_private)
231 struct sipe_http *http;
232 if (sipe_private->http)
233 return;
235 sipe_private->http = http = g_new0(struct sipe_http, 1);
236 http->connections = g_hash_table_new_full(g_str_hash, g_str_equal,
237 NULL,
238 sipe_http_transport_free);
239 http->timeouts = g_queue_new();
242 static void sipe_http_transport_connected(struct sipe_transport_connection *connection)
244 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION;
245 struct sipe_core_private *sipe_private = conn->public.sipe_private;
246 struct sipe_http *http = sipe_private->http;
247 time_t current_time = time(NULL);
249 SIPE_DEBUG_INFO("sipe_http_transport_connected: %s", conn->host_port);
250 conn->public.connected = TRUE;
252 /* add active connection to timeout queue */
253 conn->timeout = current_time + SIPE_HTTP_DEFAULT_TIMEOUT;
254 g_queue_insert_sorted(http->timeouts,
255 conn,
256 timeout_compare,
257 NULL);
259 /* start timeout timer if necessary */
260 if (http->next_timeout == 0)
261 start_timer(sipe_private, current_time);
263 sipe_http_request_next(SIPE_HTTP_CONNECTION_PUBLIC);
266 static void sipe_http_transport_input(struct sipe_transport_connection *connection)
268 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION;
269 char *current = connection->buffer;
271 /* according to the RFC remove CRLF at the beginning */
272 while (*current == '\r' || *current == '\n') {
273 current++;
275 if (current != connection->buffer)
276 sipe_utils_shrink_buffer(connection, current);
278 if (conn->connection &&
279 (current = strstr(connection->buffer, "\r\n\r\n")) != NULL) {
280 struct sipmsg *msg;
281 gboolean drop = FALSE;
282 gboolean next;
284 current += 2;
285 current[0] = '\0';
286 msg = sipmsg_parse_header(connection->buffer);
287 if (!msg) {
288 /* restore header for next try */
289 current[0] = '\r';
290 return;
293 /* HTTP/1.1 Transfer-Encoding: chunked */
294 if (msg->bodylen == SIPMSG_BODYLEN_CHUNKED) {
295 gchar *start = current + 2;
296 GSList *chunks = NULL;
297 gboolean incomplete = TRUE;
299 msg->bodylen = 0;
300 while (strlen(start) > 0) {
301 gchar *tmp;
302 guint length = g_ascii_strtoll(start, &tmp, 16);
303 guint remainder;
304 struct _chunk {
305 guint length;
306 const gchar *start;
307 } *chunk;
309 /* Illegal number */
310 if ((length == 0) && (start == tmp))
311 break;
312 msg->bodylen += length;
314 /* Chunk header not finished yet */
315 tmp = strstr(tmp, "\r\n");
316 if (tmp == NULL)
317 break;
319 /* Chunk not finished yet */
320 tmp += 2;
321 remainder = connection->buffer_used - (tmp - connection->buffer);
322 if (remainder < length + 2)
323 break;
325 /* Next chunk */
326 start = tmp + length + 2;
328 /* Body completed */
329 if (length == 0) {
330 gchar *dummy = g_malloc(msg->bodylen + 1);
331 gchar *p = dummy;
332 GSList *entry = chunks;
334 while (entry) {
335 chunk = entry->data;
336 memcpy(p, chunk->start, chunk->length);
337 p += chunk->length;
338 entry = entry->next;
340 p[0] = '\0';
342 msg->body = dummy;
343 sipe_utils_message_debug("HTTP",
344 connection->buffer,
345 msg->body,
346 FALSE);
348 current = start;
349 sipe_utils_shrink_buffer(connection,
350 current);
352 incomplete = FALSE;
353 break;
356 /* Append completed chunk */
357 chunk = g_new0(struct _chunk, 1);
358 chunk->length = length;
359 chunk->start = tmp;
360 chunks = g_slist_append(chunks, chunk);
363 if (chunks)
364 sipe_utils_slist_free_full(chunks, g_free);
366 if (incomplete) {
367 /* restore header for next try */
368 sipmsg_free(msg);
369 current[0] = '\r';
370 return;
373 } else {
374 guint remainder = connection->buffer_used - (current + 2 - connection->buffer);
376 if (remainder >= (guint) msg->bodylen) {
377 char *dummy = g_malloc(msg->bodylen + 1);
378 current += 2;
379 memcpy(dummy, current, msg->bodylen);
380 dummy[msg->bodylen] = '\0';
381 msg->body = dummy;
382 current += msg->bodylen;
383 sipe_utils_message_debug("HTTP",
384 connection->buffer,
385 msg->body,
386 FALSE);
387 sipe_utils_shrink_buffer(connection, current);
388 } else {
389 SIPE_DEBUG_INFO("sipe_http_transport_input: body too short (%d < %d, strlen %" G_GSIZE_FORMAT ") - ignoring message",
390 remainder, msg->bodylen, strlen(connection->buffer));
392 /* restore header for next try */
393 sipmsg_free(msg);
394 current[0] = '\r';
395 return;
399 if (msg->response == SIPMSG_RESPONSE_FATAL_ERROR) {
400 /* fatal header parse error */
401 msg->response = SIPE_HTTP_STATUS_SERVER_ERROR;
402 drop = TRUE;
403 } else if (sipe_strcase_equal(sipmsg_find_header(msg, "Connection"), "close")) {
404 SIPE_DEBUG_INFO("sipe_http_transport_input: server requested close '%s'",
405 conn->host_port);
406 drop = TRUE;
409 sipe_http_request_response(SIPE_HTTP_CONNECTION_PUBLIC, msg);
410 next = sipe_http_request_pending(SIPE_HTTP_CONNECTION_PUBLIC);
412 if (drop) {
413 /* drop backend connection */
414 sipe_backend_transport_disconnect(conn->connection);
415 conn->connection = NULL;
416 conn->public.connected = FALSE;
418 /* if we have pending requests we need to trigger re-connect */
419 if (next)
420 sipe_http_transport_new(conn->public.sipe_private,
421 conn->public.host,
422 conn->public.port,
423 conn->use_tls);
425 } else if (next) {
426 /* trigger sending of next pending request */
427 sipe_http_request_next(SIPE_HTTP_CONNECTION_PUBLIC);
430 sipmsg_free(msg);
434 static void sipe_http_transport_error(struct sipe_transport_connection *connection,
435 const gchar *msg)
437 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION;
438 sipe_http_transport_drop(conn->public.sipe_private->http,
439 conn,
440 msg);
441 /* conn is no longer valid */
444 struct sipe_http_connection_public *sipe_http_transport_new(struct sipe_core_private *sipe_private,
445 const gchar *host_in,
446 const guint32 port,
447 gboolean use_tls)
449 struct sipe_http *http;
450 struct sipe_http_connection *conn = NULL;
451 /* host name matching should be case insensitive */
452 gchar *host = g_ascii_strdown(host_in, -1);
453 gchar *host_port = g_strdup_printf("%s:%" G_GUINT32_FORMAT, host, port);
455 sipe_http_init(sipe_private);
457 http = sipe_private->http;
458 if (http->shutting_down) {
459 SIPE_DEBUG_ERROR("sipe_http_transport_new: new connection requested during shutdown: THIS SHOULD NOT HAPPEN! Debugging information:\n"
460 "Host/Port: %s", host_port);
461 } else {
462 conn = g_hash_table_lookup(http->connections, host_port);
464 if (conn) {
465 /* re-establishing connection */
466 if (!conn->connection) {
467 SIPE_DEBUG_INFO("sipe_http_transport_new: re-establishing %s", host_port);
469 /* will be re-inserted after connect */
470 sipe_http_transport_update_timeout_queue(conn, TRUE);
473 } else {
474 /* new connection */
475 SIPE_DEBUG_INFO("sipe_http_transport_new: new %s", host_port);
477 conn = g_new0(struct sipe_http_connection, 1);
479 conn->public.sipe_private = sipe_private;
480 conn->public.host = g_strdup(host);
481 conn->public.port = port;
483 conn->host_port = host_port;
484 conn->use_tls = use_tls;
486 g_hash_table_insert(http->connections,
487 host_port,
488 conn);
489 host_port = NULL; /* conn_private takes ownership of the key */
492 if (!conn->connection) {
493 sipe_connect_setup setup = {
494 use_tls ? SIPE_TRANSPORT_TLS : SIPE_TRANSPORT_TCP,
495 host,
496 port,
497 conn,
498 sipe_http_transport_connected,
499 sipe_http_transport_input,
500 sipe_http_transport_error
503 conn->public.connected = FALSE;
504 conn->connection = sipe_backend_transport_connect(SIPE_CORE_PUBLIC,
505 &setup);
509 g_free(host_port);
510 g_free(host);
511 return(SIPE_HTTP_CONNECTION_PUBLIC);
514 void sipe_http_transport_send(struct sipe_http_connection_public *conn_public,
515 const gchar *header,
516 const gchar *body)
518 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION_PRIVATE;
519 GString *message = g_string_new(header);
521 g_string_append_printf(message, "\r\n%s", body ? body : "");
523 sipe_utils_message_debug("HTTP", message->str, NULL, TRUE);
524 sipe_backend_transport_message(conn->connection, message->str);
525 g_string_free(message, TRUE);
527 sipe_http_transport_update_timeout_queue(conn, FALSE);
531 Local Variables:
532 mode: c
533 c-file-style: "bsd"
534 indent-tabs-mode: t
535 tab-width: 8
536 End: