[libsoup] SoupSession: move some code from SoupMessageQueue here



commit 6008e4c5fd5e0d3e08d8c82f0d0ffad059d98c06
Author: Dan Winship <danw gnome org>
Date:   Sat Sep 29 08:56:48 2012 -0400

    SoupSession: move some code from SoupMessageQueue here
    
    Move soup_message_queue_item_set_connection() into SoupSession, since
    connection management is the session's job, not the queue's. Likewise,
    move the non-queue-item-specific parts of queue_message_restarted()
    into SoupSession.

 libsoup/soup-message-queue.c   |   53 +++----------------------------------
 libsoup/soup-message-queue.h   |    2 -
 libsoup/soup-session-async.c   |    4 +-
 libsoup/soup-session-private.h |    3 ++
 libsoup/soup-session-sync.c    |    6 ++--
 libsoup/soup-session.c         |   56 +++++++++++++++++++++++++++++++++++++---
 6 files changed, 65 insertions(+), 59 deletions(-)
---
diff --git a/libsoup/soup-message-queue.c b/libsoup/soup-message-queue.c
index 9afa31e..ac00f16 100644
--- a/libsoup/soup-message-queue.c
+++ b/libsoup/soup-message-queue.c
@@ -67,19 +67,7 @@ queue_message_restarted (SoupMessage *msg, gpointer user_data)
 		item->proxy_uri = NULL;
 	}
 
-	if (item->conn &&
-	    (!soup_message_is_keepalive (msg) ||
-	     SOUP_STATUS_IS_REDIRECTION (msg->status_code))) {
-		if (soup_connection_get_state (item->conn) == SOUP_CONNECTION_IN_USE)
-			soup_connection_set_state (item->conn, SOUP_CONNECTION_IDLE);
-		soup_message_queue_item_set_connection (item, NULL);
-	}
-
-	soup_message_cleanup_response (msg);
-
 	g_cancellable_reset (item->cancellable);
-
-	item->state = SOUP_MESSAGE_STARTING;
 }
 
 /**
@@ -163,6 +151,8 @@ soup_message_queue_item_unref (SoupMessageQueueItem *item)
 		return;
 	}
 
+	g_warn_if_fail (item->conn == NULL);
+
 	/* OK, @item is dead. Rewrite @queue around it */
 	if (item->prev)
 		item->prev->next = item->next;
@@ -181,49 +171,16 @@ soup_message_queue_item_unref (SoupMessageQueueItem *item)
 	g_object_unref (item->session);
 	g_object_unref (item->msg);
 	g_object_unref (item->cancellable);
-	if (item->proxy_addr)
-		g_object_unref (item->proxy_addr);
-	if (item->proxy_uri)
-		soup_uri_free (item->proxy_uri);
-	if (item->task)
-		g_object_unref (item->task);
+	g_clear_object (&item->proxy_addr);
+	g_clear_pointer (&item->proxy_uri, soup_uri_free);
+	g_clear_object (&item->task);
 	if (item->io_source) {
 		g_source_destroy (item->io_source);
 		g_source_unref (item->io_source);
 	}
-	soup_message_queue_item_set_connection (item, NULL);
 	g_slice_free (SoupMessageQueueItem, item);
 }
 
-static void
-proxy_connection_event (SoupConnection      *conn,
-			GSocketClientEvent   event,
-			GIOStream           *connection,
-			gpointer             user_data)
-{
-	SoupMessageQueueItem *item = user_data;
-
-	soup_message_network_event (item->msg, event, connection);
-}
-
-void
-soup_message_queue_item_set_connection (SoupMessageQueueItem *item,
-					SoupConnection       *conn)
-{
-	if (item->conn) {
-		g_signal_handlers_disconnect_by_func (item->conn, proxy_connection_event, item);
-		g_object_unref (item->conn);
-	}
-
-	item->conn = conn;
-
-	if (item->conn) {
-		g_object_ref (item->conn);
-		g_signal_connect (item->conn, "event",
-				  G_CALLBACK (proxy_connection_event), item);
-	}
-}
-
 /**
  * soup_message_queue_lookup:
  * @queue: a #SoupMessageQueue
diff --git a/libsoup/soup-message-queue.h b/libsoup/soup-message-queue.h
index 627fd2d..d3341bd 100644
--- a/libsoup/soup-message-queue.h
+++ b/libsoup/soup-message-queue.h
@@ -77,8 +77,6 @@ void                  soup_message_queue_destroy    (SoupMessageQueue     *queue
 
 void soup_message_queue_item_ref            (SoupMessageQueueItem *item);
 void soup_message_queue_item_unref          (SoupMessageQueueItem *item);
-void soup_message_queue_item_set_connection (SoupMessageQueueItem *item,
-					     SoupConnection       *conn);
 
 G_END_DECLS
 
diff --git a/libsoup/soup-session-async.c b/libsoup/soup-session-async.c
index 57d8617..8706091 100644
--- a/libsoup/soup-session-async.c
+++ b/libsoup/soup-session-async.c
@@ -128,7 +128,7 @@ ssl_tunnel_completed (SoupConnection *conn, guint status, gpointer user_data)
 
 	if (!SOUP_STATUS_IS_SUCCESSFUL (status)) {
 		soup_connection_disconnect (item->conn);
-		soup_message_queue_item_set_connection (item, NULL);
+		soup_session_set_item_connection (session, item, NULL);
 		soup_message_set_status (item->msg, status);
 	}
 
@@ -179,7 +179,7 @@ got_connection (SoupConnection *conn, guint status, gpointer user_data)
 	} else if (status != SOUP_STATUS_OK) {
 		soup_session_set_item_status (session, item, status);
 		soup_connection_disconnect (item->conn);
-		soup_message_queue_item_set_connection (item, NULL);
+		soup_session_set_item_connection (session, item, NULL);
 		item->state = SOUP_MESSAGE_READY;
 	} else
 		item->state = SOUP_MESSAGE_CONNECTED;
diff --git a/libsoup/soup-session-private.h b/libsoup/soup-session-private.h
index b5e24e3..297faf5 100644
--- a/libsoup/soup-session-private.h
+++ b/libsoup/soup-session-private.h
@@ -31,6 +31,9 @@ void                  soup_session_send_queue_item      (SoupSession          *s
 							 SoupMessageCompletionFn completion_cb);
 void                  soup_session_unqueue_item         (SoupSession          *session,
 							 SoupMessageQueueItem *item);
+void                  soup_session_set_item_connection  (SoupSession          *session,
+							 SoupMessageQueueItem *item,
+							 SoupConnection       *conn);
 void                  soup_session_set_item_status      (SoupSession          *session,
 							 SoupMessageQueueItem *item,
 							 guint                 status_code);
diff --git a/libsoup/soup-session-sync.c b/libsoup/soup-session-sync.c
index d753f57..c532c97 100644
--- a/libsoup/soup-session-sync.c
+++ b/libsoup/soup-session-sync.c
@@ -175,7 +175,7 @@ try_again:
 	status = soup_connection_connect_sync (item->conn, item->cancellable);
 	if (status == SOUP_STATUS_TRY_AGAIN) {
 		soup_connection_disconnect (item->conn);
-		soup_message_queue_item_set_connection (item, NULL);
+		soup_session_set_item_connection (session, item, NULL);
 		goto try_again;
 	}
 
@@ -186,7 +186,7 @@ try_again:
 			soup_session_set_item_status (session, item, status);
 		item->state = SOUP_MESSAGE_FINISHING;
 		soup_connection_disconnect (item->conn);
-		soup_message_queue_item_set_connection (item, NULL);
+		soup_session_set_item_connection (session, item, NULL);
 		return;
 	}
 
@@ -194,7 +194,7 @@ try_again:
 		status = tunnel_connect (session, item);
 		if (!SOUP_STATUS_IS_SUCCESSFUL (status)) {
 			soup_connection_disconnect (item->conn);
-			soup_message_queue_item_set_connection (item, NULL);
+			soup_session_set_item_connection (session, item, NULL);
 			if (status == SOUP_STATUS_TRY_AGAIN)
 				goto try_again;
 			soup_session_set_item_status (session, item, status);
diff --git a/libsoup/soup-session.c b/libsoup/soup-session.c
index fe51a40..9715688 100644
--- a/libsoup/soup-session.c
+++ b/libsoup/soup-session.c
@@ -1020,6 +1020,22 @@ redirect_handler (SoupMessage *msg, gpointer user_data)
 	soup_session_redirect_message (session, msg);
 }
 
+static void
+message_restarted (SoupMessage *msg, gpointer user_data)
+{
+	SoupMessageQueueItem *item = user_data;
+
+	if (item->conn &&
+	    (!soup_message_is_keepalive (msg) ||
+	     SOUP_STATUS_IS_REDIRECTION (msg->status_code))) {
+		if (soup_connection_get_state (item->conn) == SOUP_CONNECTION_IN_USE)
+			soup_connection_set_state (item->conn, SOUP_CONNECTION_IDLE);
+		soup_session_set_item_connection (item->session, item, NULL);
+	}
+
+	soup_message_cleanup_response (msg);
+}
+
 SoupMessageQueueItem *
 soup_session_append_queue_item (SoupSession *session, SoupMessage *msg,
 				SoupSessionCallback callback, gpointer user_data)
@@ -1042,6 +1058,8 @@ soup_session_append_queue_item (SoupSession *session, SoupMessage *msg,
 			msg, "got_body", "Location",
 			G_CALLBACK (redirect_handler), item);
 	}
+	g_signal_connect (msg, "restarted",
+			  G_CALLBACK (message_restarted), item);
 
 	g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
 
@@ -1214,7 +1232,7 @@ soup_session_make_connect_message (SoupSession    *session,
 	soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT);
 
 	item = soup_session_append_queue_item (session, msg, NULL, NULL);
-	soup_message_queue_item_set_connection (item, conn);
+	soup_session_set_item_connection (session, item, conn);
 	g_object_unref (msg);
 	item->state = SOUP_MESSAGE_RUNNING;
 
@@ -1254,7 +1272,7 @@ soup_session_get_connection (SoupSession *session,
 		if (!need_new_connection && soup_connection_get_state (conns->data) == SOUP_CONNECTION_IDLE) {
 			soup_connection_set_state (conns->data, SOUP_CONNECTION_IN_USE);
 			g_mutex_unlock (&priv->conn_lock);
-			soup_message_queue_item_set_connection (item, conns->data);
+			soup_session_set_item_connection (session, item, conns->data);
 			soup_message_set_https_status (item->msg, item->conn);
 			return TRUE;
 		} else if (soup_connection_get_state (conns->data) == SOUP_CONNECTION_CONNECTING)
@@ -1317,7 +1335,7 @@ soup_session_get_connection (SoupSession *session,
 	}
 
 	g_mutex_unlock (&priv->conn_lock);
-	soup_message_queue_item_set_connection (item, conn);
+	soup_session_set_item_connection (session, item, conn);
 	return TRUE;
 }
 
@@ -1341,7 +1359,7 @@ soup_session_unqueue_item (SoupSession          *session,
 		    (item->msg->method != SOUP_METHOD_CONNECT ||
 		     !SOUP_STATUS_IS_SUCCESSFUL (item->msg->status_code)))
 			soup_connection_set_state (item->conn, SOUP_CONNECTION_IDLE);
-		soup_message_queue_item_set_connection (item, NULL);
+		soup_session_set_item_connection (session, item, NULL);
 	}
 
 	if (item->state != SOUP_MESSAGE_FINISHED) {
@@ -1366,6 +1384,36 @@ soup_session_unqueue_item (SoupSession          *session,
 	soup_message_queue_item_unref (item);
 }
 
+static void
+proxy_connection_event (SoupConnection      *conn,
+			GSocketClientEvent   event,
+			GIOStream           *connection,
+			gpointer             user_data)
+{
+	SoupMessageQueueItem *item = user_data;
+
+	soup_message_network_event (item->msg, event, connection);
+}
+
+void
+soup_session_set_item_connection (SoupSession          *session,
+				  SoupMessageQueueItem *item,
+				  SoupConnection       *conn)
+{
+	if (item->conn) {
+		g_signal_handlers_disconnect_by_func (item->conn, proxy_connection_event, item);
+		g_object_unref (item->conn);
+	}
+
+	item->conn = conn;
+
+	if (item->conn) {
+		g_object_ref (item->conn);
+		g_signal_connect (item->conn, "event",
+				  G_CALLBACK (proxy_connection_event), item);
+	}
+}
+
 void
 soup_session_set_item_status (SoupSession          *session,
 			      SoupMessageQueueItem *item,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]