2 * @file sipe-http-transport.c
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
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
;
63 time_t timeout
; /* in seconds from epoch */
68 GHashTable
*connections
;
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
,
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
,
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'",
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
);
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",
112 message
? message
: "REASON UNKNOWN");
114 /* this triggers sipe_http_transport_free */
115 g_hash_table_remove(http
->connections
,
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
,
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;
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
);
140 /* restart timer for next connection */
141 if (conn
->timeout
> current_time
) {
142 start_timer(sipe_private
, current_time
);
146 /* next connection timed-out too, loop around */
150 static void start_timer(struct sipe_core_private
*sipe_private
,
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
,
160 http
->next_timeout
- current_time
,
161 sipe_http_transport_timeout
,
165 static void sipe_http_transport_update_timeout_queue(struct sipe_http_connection
*conn
,
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 */
178 g_queue_remove(timeouts
, conn
);
180 conn
->timeout
= current_time
+ SIPE_HTTP_DEFAULT_TIMEOUT
;
181 g_queue_sort(timeouts
,
186 /* update timer if necessary */
188 sipe_schedule_cancel(sipe_private
, SIPE_HTTP_TIMEOUT_ACTION
);
189 if (g_queue_is_empty(timeouts
)) {
190 http
->next_timeout
= 0;
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 */
203 return(http
->shutting_down
);
206 void sipe_http_free(struct sipe_core_private
*sipe_private
)
208 struct sipe_http
*http
= sipe_private
->http
;
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
);
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
)
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
,
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
,
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') {
268 if (current
!= connection
->buffer
)
269 sipe_utils_shrink_buffer(connection
, current
);
271 if ((current
= strstr(connection
->buffer
, "\r\n\r\n")) != NULL
) {
277 msg
= sipmsg_parse_header(connection
->buffer
);
279 /* restore header for next try */
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
;
291 while (strlen(start
) > 0) {
293 guint length
= g_ascii_strtoll(start
, &tmp
, 16);
301 if ((length
== 0) && (start
== tmp
))
303 msg
->bodylen
+= length
;
305 /* Chunk header not finished yet */
306 tmp
= strstr(tmp
, "\r\n");
310 /* Chunk not finished yet */
312 remainder
= connection
->buffer_used
- (tmp
- connection
->buffer
);
313 if (remainder
< length
+ 2)
317 start
= tmp
+ length
+ 2;
321 gchar
*dummy
= g_malloc(msg
->bodylen
+ 1);
323 GSList
*entry
= chunks
;
327 memcpy(p
, chunk
->start
, chunk
->length
);
334 sipe_utils_message_debug("HTTP",
340 sipe_utils_shrink_buffer(connection
,
347 /* Append completed chunk */
348 chunk
= g_new0(struct _chunk
, 1);
349 chunk
->length
= length
;
351 chunks
= g_slist_append(chunks
, chunk
);
355 sipe_utils_slist_free_full(chunks
, g_free
);
358 /* restore header for next try */
365 guint remainder
= connection
->buffer_used
- (current
+ 2 - connection
->buffer
);
367 if (remainder
>= (guint
) msg
->bodylen
) {
368 char *dummy
= g_malloc(msg
->bodylen
+ 1);
370 memcpy(dummy
, current
, msg
->bodylen
);
371 dummy
[msg
->bodylen
] = '\0';
373 current
+= msg
->bodylen
;
374 sipe_utils_message_debug("HTTP",
378 sipe_utils_shrink_buffer(connection
, current
);
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 */
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'",
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 */
403 sipe_http_transport_new(conn
->public.sipe_private
,
409 /* trigger sending of next pending request */
410 sipe_http_request_next(SIPE_HTTP_CONNECTION_PUBLIC
);
417 static void sipe_http_transport_error(struct sipe_transport_connection
*connection
,
420 struct sipe_http_connection
*conn
= SIPE_HTTP_CONNECTION
;
421 sipe_http_transport_drop(conn
->public.sipe_private
->http
,
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
,
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
);
445 conn
= g_hash_table_lookup(http
->connections
, host_port
);
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
);
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
,
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
,
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
,
494 return(SIPE_HTTP_CONNECTION_PUBLIC
);
497 void sipe_http_transport_send(struct sipe_http_connection_public
*conn_public
,
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
);