http: implement stack shutdown flag
[siplcs.git] / src / core / sipe-http-transport.c
blobb4f47e6b29335a1c035189458bfb503a40289142
1 /**
2 * @file sipe-http-transport.c
4 * pidgin-sipe
6 * Copyright (C) 2013 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 */
66 struct sipe_http {
67 GHashTable *connections;
68 GQueue *timeouts;
69 time_t next_timeout; /* in seconds from epoch, 0 if timer isn't running */
70 gboolean shutting_down;
73 static gint timeout_compare(gconstpointer a,
74 gconstpointer b,
75 SIPE_UNUSED_PARAMETER gpointer user_data)
77 return(((struct sipe_http_connection *) a)->timeout -
78 ((struct sipe_http_connection *) b)->timeout);
81 static void sipe_http_transport_free(gpointer data)
83 struct sipe_http_connection *conn = data;
84 struct sipe_http *http = conn->public.sipe_private->http;
86 SIPE_DEBUG_INFO("sipe_http_transport_free: destroying connection '%s'",
87 conn->host_port);
89 if (conn->connection)
90 sipe_backend_transport_disconnect(conn->connection);
91 conn->connection = NULL;
93 g_queue_remove(http->timeouts, conn);
95 sipe_http_request_shutdown(SIPE_HTTP_CONNECTION_PUBLIC);
97 g_free(conn->public.host);
99 g_free(conn->host_port);
100 g_free(conn);
103 static void sipe_http_transport_drop(struct sipe_http *http,
104 struct sipe_http_connection *conn,
105 const gchar *message)
107 SIPE_DEBUG_INFO("sipe_http_transport_drop: dropping connection '%s': %s",
108 conn->host_port,
109 message ? message : "REASON UNKNOWN");
111 /* this triggers sipe_http_transport_new */
112 g_hash_table_remove(http->connections,
113 conn->host_port);
116 static void start_timer(struct sipe_core_private *sipe_private,
117 time_t current_time);
118 static void sipe_http_transport_timeout(struct sipe_core_private *sipe_private,
119 gpointer data)
121 struct sipe_http *http = sipe_private->http;
122 struct sipe_http_connection *conn = data;
123 time_t current_time = time(NULL);
125 /* timer has expired */
126 http->next_timeout = 0;
128 while (1) {
129 sipe_http_transport_drop(http, conn, "timeout");
130 /* conn is no longer valid */
132 /* is there another active connection? */
133 conn = g_queue_peek_head(http->timeouts);
134 if (!conn)
135 break;
137 /* restart timer for next connection */
138 if (conn->timeout > current_time) {
139 start_timer(sipe_private, current_time);
140 break;
143 /* next connection timed-out too, loop around */
147 static void start_timer(struct sipe_core_private *sipe_private,
148 time_t current_time)
150 struct sipe_http *http = sipe_private->http;
151 struct sipe_http_connection *conn = g_queue_peek_head(http->timeouts);
153 http->next_timeout = conn->timeout;
154 sipe_schedule_seconds(sipe_private,
155 SIPE_HTTP_TIMEOUT_ACTION,
156 conn,
157 http->next_timeout - current_time,
158 sipe_http_transport_timeout,
159 NULL);
162 static void sipe_http_transport_update_timeout_queue(struct sipe_http_connection *conn,
163 gboolean remove)
165 struct sipe_core_private *sipe_private = conn->public.sipe_private;
166 struct sipe_http *http = sipe_private->http;
167 GQueue *timeouts = http->timeouts;
168 time_t current_time = time(NULL);
170 /* is this connection at head of queue? */
171 gboolean update = (conn == g_queue_peek_head(timeouts));
173 /* update timeout queue */
174 if (remove) {
175 g_queue_remove(timeouts, conn);
176 } else {
177 conn->timeout = current_time + SIPE_HTTP_DEFAULT_TIMEOUT;
178 g_queue_sort(timeouts,
179 timeout_compare,
180 NULL);
183 /* update timer if necessary */
184 if (update) {
185 sipe_schedule_cancel(sipe_private, SIPE_HTTP_TIMEOUT_ACTION);
186 if (g_queue_is_empty(timeouts)) {
187 http->next_timeout = 0;
188 } else {
189 start_timer(sipe_private, current_time);
194 gboolean sipe_http_shutting_down(struct sipe_core_private *sipe_private)
196 struct sipe_http *http = sipe_private->http;
197 /* We need to return FALSE in case HTTP stack isn't initialized yet */
198 if (!http)
199 return(FALSE);
200 return(http->shutting_down);
203 void sipe_http_free(struct sipe_core_private *sipe_private)
205 struct sipe_http *http = sipe_private->http;
206 if (!http)
207 return;
209 /* HTTP stack is shutting down: reject all new requests */
210 http->shutting_down = TRUE;
212 sipe_schedule_cancel(sipe_private, SIPE_HTTP_TIMEOUT_ACTION);
213 g_hash_table_destroy(http->connections);
214 g_queue_free(http->timeouts);
215 g_free(http);
216 sipe_private->http = NULL;
219 static void sipe_http_init(struct sipe_core_private *sipe_private)
221 struct sipe_http *http;
222 if (sipe_private->http)
223 return;
225 sipe_private->http = http = g_new0(struct sipe_http, 1);
226 http->connections = g_hash_table_new_full(g_str_hash, g_str_equal,
227 NULL,
228 sipe_http_transport_free);
229 http->timeouts = g_queue_new();
232 static void sipe_http_transport_connected(struct sipe_transport_connection *connection)
234 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION;
235 struct sipe_core_private *sipe_private = conn->public.sipe_private;
236 struct sipe_http *http = sipe_private->http;
237 time_t current_time = time(NULL);
239 SIPE_DEBUG_INFO("sipe_http_transport_connected: %s", conn->host_port);
240 conn->public.connected = TRUE;
242 /* add active connection to timeout queue */
243 conn->timeout = current_time + SIPE_HTTP_DEFAULT_TIMEOUT;
244 g_queue_insert_sorted(http->timeouts,
245 conn,
246 timeout_compare,
247 NULL);
249 /* start timeout timer if necessary */
250 if (http->next_timeout == 0)
251 start_timer(sipe_private, current_time);
253 sipe_http_request_next(SIPE_HTTP_CONNECTION_PUBLIC);
256 static void sipe_http_transport_input(struct sipe_transport_connection *connection)
258 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION;
259 char *current = connection->buffer;
261 /* according to the RFC remove CRLF at the beginning */
262 while (*current == '\r' || *current == '\n') {
263 current++;
265 if (current != connection->buffer)
266 sipe_utils_shrink_buffer(connection, current);
268 if ((current = strstr(connection->buffer, "\r\n\r\n")) != NULL) {
269 struct sipmsg *msg;
270 gboolean next;
272 current += 2;
273 current[0] = '\0';
274 msg = sipmsg_parse_header(connection->buffer);
275 if (!msg) {
276 /* restore header for next try */
277 current[0] = '\r';
278 return;
281 /* HTTP/1.1 Transfer-Encoding: chunked */
282 if (msg->bodylen == SIPMSG_BODYLEN_CHUNKED) {
283 gchar *start = current + 2;
284 GSList *chunks = NULL;
285 gboolean incomplete = TRUE;
287 msg->bodylen = 0;
288 while (strlen(start) > 0) {
289 gchar *tmp;
290 guint length = g_ascii_strtoll(start, &tmp, 16);
291 guint remainder;
292 struct _chunk {
293 guint length;
294 const gchar *start;
295 } *chunk;
297 /* Illegal number */
298 if ((length == 0) && (start == tmp))
299 break;
300 msg->bodylen += length;
302 /* Chunk header not finished yet */
303 tmp = strstr(tmp, "\r\n");
304 if (tmp == NULL)
305 break;
307 /* Chunk not finished yet */
308 tmp += 2;
309 remainder = connection->buffer_used - (tmp - connection->buffer);
310 if (remainder < length + 2)
311 break;
313 /* Next chunk */
314 start = tmp + length + 2;
316 /* Body completed */
317 if (length == 0) {
318 gchar *dummy = g_malloc(msg->bodylen + 1);
319 gchar *p = dummy;
320 GSList *entry = chunks;
322 while (entry) {
323 chunk = entry->data;
324 memcpy(p, chunk->start, chunk->length);
325 p += chunk->length;
326 entry = entry->next;
328 p[0] = '\0';
330 msg->body = dummy;
331 sipe_utils_message_debug("HTTP",
332 connection->buffer,
333 msg->body,
334 FALSE);
336 current = start;
337 sipe_utils_shrink_buffer(connection,
338 current);
340 incomplete = FALSE;
341 break;
344 /* Append completed chunk */
345 chunk = g_new0(struct _chunk, 1);
346 chunk->length = length;
347 chunk->start = tmp;
348 chunks = g_slist_append(chunks, chunk);
351 if (chunks) {
352 GSList *entry = chunks;
353 while (entry) {
354 g_free(entry->data);
355 entry = entry->next;
357 g_slist_free(chunks);
360 if (incomplete) {
361 /* restore header for next try */
362 sipmsg_free(msg);
363 current[0] = '\r';
364 return;
367 } else {
368 guint remainder = connection->buffer_used - (current + 2 - connection->buffer);
370 if (remainder >= (guint) msg->bodylen) {
371 char *dummy = g_malloc(msg->bodylen + 1);
372 current += 2;
373 memcpy(dummy, current, msg->bodylen);
374 dummy[msg->bodylen] = '\0';
375 msg->body = dummy;
376 current += msg->bodylen;
377 sipe_utils_message_debug("HTTP",
378 connection->buffer,
379 msg->body,
380 FALSE);
381 sipe_utils_shrink_buffer(connection, current);
382 } else {
383 SIPE_DEBUG_INFO("sipe_http_transport_input: body too short (%d < %d, strlen %" G_GSIZE_FORMAT ") - ignoring message",
384 remainder, msg->bodylen, strlen(connection->buffer));
386 /* restore header for next try */
387 sipmsg_free(msg);
388 current[0] = '\r';
389 return;
393 sipe_http_request_response(SIPE_HTTP_CONNECTION_PUBLIC, msg);
394 next = sipe_http_request_pending(SIPE_HTTP_CONNECTION_PUBLIC);
396 if (sipe_strcase_equal(sipmsg_find_header(msg, "Connection"), "close")) {
397 /* drop backend connection */
398 SIPE_DEBUG_INFO("sipe_http_transport_input: server requested close '%s'",
399 conn->host_port);
400 sipe_backend_transport_disconnect(conn->connection);
401 conn->connection = NULL;
402 conn->public.connected = FALSE;
404 /* if we have pending requests we need to trigger re-connect */
405 if (next)
406 sipe_http_transport_new(conn->public.sipe_private,
407 conn->public.host,
408 conn->public.port);
410 } else if (next) {
411 /* trigger sending of next pending request */
412 sipe_http_request_next(SIPE_HTTP_CONNECTION_PUBLIC);
415 sipmsg_free(msg);
419 static void sipe_http_transport_error(struct sipe_transport_connection *connection,
420 const gchar *msg)
422 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION;
423 sipe_http_transport_drop(conn->public.sipe_private->http,
424 conn,
425 msg);
426 /* conn is no longer valid */
429 struct sipe_http_connection_public *sipe_http_transport_new(struct sipe_core_private *sipe_private,
430 const gchar *host_in,
431 const guint32 port)
433 struct sipe_http *http;
434 struct sipe_http_connection *conn = NULL;
435 /* host name matching should be case insensitive */
436 gchar *host = g_ascii_strdown(host_in, -1);
437 gchar *host_port = g_strdup_printf("%s:%" G_GUINT32_FORMAT, host, port);
439 sipe_http_init(sipe_private);
441 http = sipe_private->http;
442 if (http->shutting_down) {
443 SIPE_DEBUG_ERROR("sipe_http_transport_new: new connection requested during shutdown: THIS SHOULD NOT HAPPEN! Debugging information:\n"
444 "Host/Port: %s", host_port);
445 } else {
446 conn = g_hash_table_lookup(http->connections, host_port);
448 if (conn) {
449 /* re-establishing connection */
450 if (!conn->connection) {
451 SIPE_DEBUG_INFO("sipe_http_transport_new: re-establishing %s", host_port);
453 /* will be re-inserted after connect */
454 sipe_http_transport_update_timeout_queue(conn, TRUE);
457 } else {
458 /* new connection */
459 SIPE_DEBUG_INFO("sipe_http_transport_new: new %s", host_port);
461 conn = g_new0(struct sipe_http_connection, 1);
463 conn->public.sipe_private = sipe_private;
464 conn->public.host = g_strdup(host);
465 conn->public.port = port;
467 conn->host_port = host_port;
469 g_hash_table_insert(http->connections,
470 host_port,
471 conn);
472 host_port = NULL; /* conn_private takes ownership of the key */
475 if (!conn->connection) {
476 sipe_connect_setup setup = {
477 SIPE_TRANSPORT_TLS, /* TBD: we only support TLS for now */
478 host,
479 port,
480 conn,
481 sipe_http_transport_connected,
482 sipe_http_transport_input,
483 sipe_http_transport_error
486 conn->public.connected = FALSE;
487 conn->connection = sipe_backend_transport_connect(SIPE_CORE_PUBLIC,
488 &setup);
492 g_free(host_port);
493 g_free(host);
494 return(SIPE_HTTP_CONNECTION_PUBLIC);
497 void sipe_http_transport_send(struct sipe_http_connection_public *conn_public,
498 const gchar *header,
499 const gchar *body)
501 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION_PRIVATE;
502 GString *message = g_string_new(header);
504 g_string_append_printf(message, "\r\n%s", body ? body : "");
506 sipe_utils_message_debug("HTTP", message->str, NULL, TRUE);
507 sipe_backend_transport_message(conn->connection, message->str);
508 g_string_free(message, TRUE);
510 sipe_http_transport_update_timeout_queue(conn, FALSE);
514 Local Variables:
515 mode: c
516 c-file-style: "bsd"
517 indent-tabs-mode: t
518 tab-width: 8
519 End: