group: disable sipe_core_group_set_alias for UCS
[siplcs.git] / src / core / sipe-http-transport.c
blobc882736a93ba241a250b92662899950cefac115b
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 */
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 /* this triggers sipe_http_transport_free */
115 g_hash_table_remove(http->connections,
116 conn->host_port);
119 static void start_timer(struct sipe_core_private *sipe_private,
120 time_t current_time);
121 static void sipe_http_transport_timeout(struct sipe_core_private *sipe_private,
122 gpointer data)
124 struct sipe_http *http = sipe_private->http;
125 struct sipe_http_connection *conn = data;
126 time_t current_time = time(NULL);
128 /* timer has expired */
129 http->next_timeout = 0;
131 while (1) {
132 sipe_http_transport_drop(http, conn, "timeout");
133 /* conn is no longer valid */
135 /* is there another active connection? */
136 conn = g_queue_peek_head(http->timeouts);
137 if (!conn)
138 break;
140 /* restart timer for next connection */
141 if (conn->timeout > current_time) {
142 start_timer(sipe_private, current_time);
143 break;
146 /* next connection timed-out too, loop around */
150 static void start_timer(struct sipe_core_private *sipe_private,
151 time_t current_time)
153 struct sipe_http *http = sipe_private->http;
154 struct sipe_http_connection *conn = g_queue_peek_head(http->timeouts);
156 http->next_timeout = conn->timeout;
157 sipe_schedule_seconds(sipe_private,
158 SIPE_HTTP_TIMEOUT_ACTION,
159 conn,
160 http->next_timeout - current_time,
161 sipe_http_transport_timeout,
162 NULL);
165 static void sipe_http_transport_update_timeout_queue(struct sipe_http_connection *conn,
166 gboolean remove)
168 struct sipe_core_private *sipe_private = conn->public.sipe_private;
169 struct sipe_http *http = sipe_private->http;
170 GQueue *timeouts = http->timeouts;
171 time_t current_time = time(NULL);
173 /* is this connection at head of queue? */
174 gboolean update = (conn == g_queue_peek_head(timeouts));
176 /* update timeout queue */
177 if (remove) {
178 g_queue_remove(timeouts, conn);
179 } else {
180 conn->timeout = current_time + SIPE_HTTP_DEFAULT_TIMEOUT;
181 g_queue_sort(timeouts,
182 timeout_compare,
183 NULL);
186 /* update timer if necessary */
187 if (update) {
188 sipe_schedule_cancel(sipe_private, SIPE_HTTP_TIMEOUT_ACTION);
189 if (g_queue_is_empty(timeouts)) {
190 http->next_timeout = 0;
191 } else {
192 start_timer(sipe_private, current_time);
197 gboolean sipe_http_shutting_down(struct sipe_core_private *sipe_private)
199 struct sipe_http *http = sipe_private->http;
200 /* We need to return FALSE in case HTTP stack isn't initialized yet */
201 if (!http)
202 return(FALSE);
203 return(http->shutting_down);
206 void sipe_http_free(struct sipe_core_private *sipe_private)
208 struct sipe_http *http = sipe_private->http;
209 if (!http)
210 return;
212 /* HTTP stack is shutting down: reject all new requests */
213 http->shutting_down = TRUE;
215 sipe_schedule_cancel(sipe_private, SIPE_HTTP_TIMEOUT_ACTION);
216 g_hash_table_destroy(http->connections);
217 g_queue_free(http->timeouts);
218 g_free(http);
219 sipe_private->http = NULL;
222 static void sipe_http_init(struct sipe_core_private *sipe_private)
224 struct sipe_http *http;
225 if (sipe_private->http)
226 return;
228 sipe_private->http = http = g_new0(struct sipe_http, 1);
229 http->connections = g_hash_table_new_full(g_str_hash, g_str_equal,
230 NULL,
231 sipe_http_transport_free);
232 http->timeouts = g_queue_new();
235 static void sipe_http_transport_connected(struct sipe_transport_connection *connection)
237 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION;
238 struct sipe_core_private *sipe_private = conn->public.sipe_private;
239 struct sipe_http *http = sipe_private->http;
240 time_t current_time = time(NULL);
242 SIPE_DEBUG_INFO("sipe_http_transport_connected: %s", conn->host_port);
243 conn->public.connected = TRUE;
245 /* add active connection to timeout queue */
246 conn->timeout = current_time + SIPE_HTTP_DEFAULT_TIMEOUT;
247 g_queue_insert_sorted(http->timeouts,
248 conn,
249 timeout_compare,
250 NULL);
252 /* start timeout timer if necessary */
253 if (http->next_timeout == 0)
254 start_timer(sipe_private, current_time);
256 sipe_http_request_next(SIPE_HTTP_CONNECTION_PUBLIC);
259 static void sipe_http_transport_input(struct sipe_transport_connection *connection)
261 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION;
262 char *current = connection->buffer;
264 /* according to the RFC remove CRLF at the beginning */
265 while (*current == '\r' || *current == '\n') {
266 current++;
268 if (current != connection->buffer)
269 sipe_utils_shrink_buffer(connection, current);
271 if ((current = strstr(connection->buffer, "\r\n\r\n")) != NULL) {
272 struct sipmsg *msg;
273 gboolean next;
275 current += 2;
276 current[0] = '\0';
277 msg = sipmsg_parse_header(connection->buffer);
278 if (!msg) {
279 /* restore header for next try */
280 current[0] = '\r';
281 return;
284 /* HTTP/1.1 Transfer-Encoding: chunked */
285 if (msg->bodylen == SIPMSG_BODYLEN_CHUNKED) {
286 gchar *start = current + 2;
287 GSList *chunks = NULL;
288 gboolean incomplete = TRUE;
290 msg->bodylen = 0;
291 while (strlen(start) > 0) {
292 gchar *tmp;
293 guint length = g_ascii_strtoll(start, &tmp, 16);
294 guint remainder;
295 struct _chunk {
296 guint length;
297 const gchar *start;
298 } *chunk;
300 /* Illegal number */
301 if ((length == 0) && (start == tmp))
302 break;
303 msg->bodylen += length;
305 /* Chunk header not finished yet */
306 tmp = strstr(tmp, "\r\n");
307 if (tmp == NULL)
308 break;
310 /* Chunk not finished yet */
311 tmp += 2;
312 remainder = connection->buffer_used - (tmp - connection->buffer);
313 if (remainder < length + 2)
314 break;
316 /* Next chunk */
317 start = tmp + length + 2;
319 /* Body completed */
320 if (length == 0) {
321 gchar *dummy = g_malloc(msg->bodylen + 1);
322 gchar *p = dummy;
323 GSList *entry = chunks;
325 while (entry) {
326 chunk = entry->data;
327 memcpy(p, chunk->start, chunk->length);
328 p += chunk->length;
329 entry = entry->next;
331 p[0] = '\0';
333 msg->body = dummy;
334 sipe_utils_message_debug("HTTP",
335 connection->buffer,
336 msg->body,
337 FALSE);
339 current = start;
340 sipe_utils_shrink_buffer(connection,
341 current);
343 incomplete = FALSE;
344 break;
347 /* Append completed chunk */
348 chunk = g_new0(struct _chunk, 1);
349 chunk->length = length;
350 chunk->start = tmp;
351 chunks = g_slist_append(chunks, chunk);
354 if (chunks)
355 sipe_utils_slist_free_full(chunks, g_free);
357 if (incomplete) {
358 /* restore header for next try */
359 sipmsg_free(msg);
360 current[0] = '\r';
361 return;
364 } else {
365 guint remainder = connection->buffer_used - (current + 2 - connection->buffer);
367 if (remainder >= (guint) msg->bodylen) {
368 char *dummy = g_malloc(msg->bodylen + 1);
369 current += 2;
370 memcpy(dummy, current, msg->bodylen);
371 dummy[msg->bodylen] = '\0';
372 msg->body = dummy;
373 current += msg->bodylen;
374 sipe_utils_message_debug("HTTP",
375 connection->buffer,
376 msg->body,
377 FALSE);
378 sipe_utils_shrink_buffer(connection, current);
379 } else {
380 SIPE_DEBUG_INFO("sipe_http_transport_input: body too short (%d < %d, strlen %" G_GSIZE_FORMAT ") - ignoring message",
381 remainder, msg->bodylen, strlen(connection->buffer));
383 /* restore header for next try */
384 sipmsg_free(msg);
385 current[0] = '\r';
386 return;
390 sipe_http_request_response(SIPE_HTTP_CONNECTION_PUBLIC, msg);
391 next = sipe_http_request_pending(SIPE_HTTP_CONNECTION_PUBLIC);
393 if (sipe_strcase_equal(sipmsg_find_header(msg, "Connection"), "close")) {
394 /* drop backend connection */
395 SIPE_DEBUG_INFO("sipe_http_transport_input: server requested close '%s'",
396 conn->host_port);
397 sipe_backend_transport_disconnect(conn->connection);
398 conn->connection = NULL;
399 conn->public.connected = FALSE;
401 /* if we have pending requests we need to trigger re-connect */
402 if (next)
403 sipe_http_transport_new(conn->public.sipe_private,
404 conn->public.host,
405 conn->public.port,
406 conn->use_tls);
408 } else if (next) {
409 /* trigger sending of next pending request */
410 sipe_http_request_next(SIPE_HTTP_CONNECTION_PUBLIC);
413 sipmsg_free(msg);
417 static void sipe_http_transport_error(struct sipe_transport_connection *connection,
418 const gchar *msg)
420 struct sipe_http_connection *conn = SIPE_HTTP_CONNECTION;
421 sipe_http_transport_drop(conn->public.sipe_private->http,
422 conn,
423 msg);
424 /* conn is no longer valid */
427 struct sipe_http_connection_public *sipe_http_transport_new(struct sipe_core_private *sipe_private,
428 const gchar *host_in,
429 const guint32 port,
430 gboolean use_tls)
432 struct sipe_http *http;
433 struct sipe_http_connection *conn = NULL;
434 /* host name matching should be case insensitive */
435 gchar *host = g_ascii_strdown(host_in, -1);
436 gchar *host_port = g_strdup_printf("%s:%" G_GUINT32_FORMAT, host, port);
438 sipe_http_init(sipe_private);
440 http = sipe_private->http;
441 if (http->shutting_down) {
442 SIPE_DEBUG_ERROR("sipe_http_transport_new: new connection requested during shutdown: THIS SHOULD NOT HAPPEN! Debugging information:\n"
443 "Host/Port: %s", host_port);
444 } else {
445 conn = g_hash_table_lookup(http->connections, host_port);
447 if (conn) {
448 /* re-establishing connection */
449 if (!conn->connection) {
450 SIPE_DEBUG_INFO("sipe_http_transport_new: re-establishing %s", host_port);
452 /* will be re-inserted after connect */
453 sipe_http_transport_update_timeout_queue(conn, TRUE);
456 } else {
457 /* new connection */
458 SIPE_DEBUG_INFO("sipe_http_transport_new: new %s", host_port);
460 conn = g_new0(struct sipe_http_connection, 1);
462 conn->public.sipe_private = sipe_private;
463 conn->public.host = g_strdup(host);
464 conn->public.port = port;
466 conn->host_port = host_port;
467 conn->use_tls = use_tls;
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 use_tls ? SIPE_TRANSPORT_TLS : SIPE_TRANSPORT_TCP,
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: