[libsoup/carlosgc/server-http2: 5/13] server: move io data ownership from SoupServerMessage to SoupServerConnection




commit 675c06596271dd55404d9dcb15b86e0c95934b61
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Wed Jul 6 14:09:18 2022 +0200

    server: move io data ownership from SoupServerMessage to SoupServerConnection

 libsoup/meson.build                                |  1 +
 .../server/http1/soup-server-message-io-http1.c    | 23 +++++++-------
 libsoup/server/soup-server-connection.c            | 35 ++++++++++++++++++++--
 libsoup/server/soup-server-connection.h            |  3 ++
 libsoup/server/soup-server-message-private.h       |  7 ++++-
 libsoup/server/soup-server-message.c               | 12 ++++++--
 6 files changed, 64 insertions(+), 17 deletions(-)
---
diff --git a/libsoup/meson.build b/libsoup/meson.build
index a47605cb..3ea9a5c3 100644
--- a/libsoup/meson.build
+++ b/libsoup/meson.build
@@ -201,6 +201,7 @@ libsoup_includes = [
     'http1',
     'http2',
     'server',
+    'server/http1',
     'websocket',
     '.'
   ]),
diff --git a/libsoup/server/http1/soup-server-message-io-http1.c 
b/libsoup/server/http1/soup-server-message-io-http1.c
index d2e3f324..4ac9ac66 100644
--- a/libsoup/server/http1/soup-server-message-io-http1.c
+++ b/libsoup/server/http1/soup-server-message-io-http1.c
@@ -22,6 +22,8 @@
 typedef struct {
         SoupMessageIOData base;
 
+        SoupServerMessage *msg;
+
         GBytes  *write_chunk;
        goffset  write_body_offset;
 
@@ -52,6 +54,8 @@ soup_message_io_http1_free (SoupMessageIOHTTP1 *msg_io)
                 msg_io->unpause_source = NULL;
         }
 
+        soup_server_message_set_io_data (msg_io->msg, NULL);
+        g_clear_object (&msg_io->msg);
         g_clear_pointer (&msg_io->async_context, g_main_context_unref);
         g_clear_pointer (&msg_io->write_chunk, g_bytes_unref);
 
@@ -92,6 +96,7 @@ soup_server_message_io_finished (SoupServerMessage *msg)
                completion = SOUP_MESSAGE_IO_INTERRUPTED;
 
         g_object_ref (msg);
+        g_clear_pointer (&io->msg_io, soup_message_io_http1_free);
         soup_server_message_set_io_data (msg, NULL);
        if (completion_cb)
                 completion_cb (G_OBJECT (msg), completion, completion_data);
@@ -115,7 +120,7 @@ soup_server_message_io_steal (SoupServerMessage *msg)
        completion_data = io->msg_io->base.completion_data;
 
         g_object_ref (msg);
-       soup_server_message_set_io_data (msg, NULL);
+        g_clear_pointer (&io->msg_io, soup_message_io_http1_free);
         if (completion_cb)
                 completion_cb (G_OBJECT (msg), SOUP_MESSAGE_IO_STOLEN, completion_data);
         g_object_unref (msg);
@@ -903,18 +908,16 @@ soup_server_message_io_http1_new (GIOStream *iostream)
 }
 
 void
-soup_server_message_read_request (SoupServerMessage        *msg,
-                                  SoupMessageIOCompletionFn completion_cb,
-                                  gpointer                  user_data)
+soup_server_message_io_read_request (SoupServerMessageIOData  *io,
+                                     SoupServerMessage        *msg,
+                                     SoupMessageIOCompletionFn completion_cb,
+                                     gpointer                  user_data)
 {
-        SoupServerMessageIOData *io;
-        SoupServerConnection *conn;
         SoupMessageIOHTTP1 *msg_io;
 
-        conn = soup_server_message_get_connection (msg);
-        io = soup_server_message_io_http1_new (soup_server_connection_get_iostream (conn));
-
         msg_io = g_new0 (SoupMessageIOHTTP1, 1);
+        msg_io->msg = g_object_ref (msg);
+
         msg_io->base.completion_cb = completion_cb;
         msg_io->base.completion_data = user_data;
 
@@ -927,8 +930,6 @@ soup_server_message_read_request (SoupServerMessage        *msg,
         msg_io->async_context = g_main_context_ref_thread_default ();
         io->msg_io = msg_io;
 
-        soup_server_message_set_io_data (msg, io);
-
         io_run (msg);
 }
 
diff --git a/libsoup/server/soup-server-connection.c b/libsoup/server/soup-server-connection.c
index 46d7727d..b76b33da 100644
--- a/libsoup/server/soup-server-connection.c
+++ b/libsoup/server/soup-server-connection.c
@@ -18,6 +18,8 @@
 #include "soup-server-connection.h"
 #include "soup.h"
 #include "soup-io-stream.h"
+#include "soup-server-message-private.h"
+#include "soup-server-message-io-http1.h"
 
 enum {
         DISCONNECTED,
@@ -53,6 +55,7 @@ typedef struct {
         GSocket *socket;
         GIOStream *conn;
         GIOStream *iostream;
+        SoupServerMessageIOData *io_data;
 
         GSocketAddress *local_addr;
         GSocketAddress *remote_addr;
@@ -79,6 +82,8 @@ disconnect_internal (SoupServerConnection *conn)
         g_io_stream_close (priv->conn, NULL, NULL);
         g_signal_handlers_disconnect_by_data (priv->conn, conn);
         g_clear_object (&priv->conn);
+
+        g_clear_pointer (&priv->io_data, soup_server_message_io_data_free);
 }
 
 static void
@@ -90,6 +95,8 @@ soup_server_connection_finalize (GObject *object)
         if (priv->conn)
                 disconnect_internal (conn);
 
+        g_clear_pointer (&priv->io_data, soup_server_message_io_data_free);
+
         g_clear_object (&priv->iostream);
 
         g_clear_object (&priv->local_addr);
@@ -116,8 +123,10 @@ soup_server_connection_set_property (GObject      *object,
                 break;
         case PROP_CONNECTION:
                 priv->conn = g_value_dup_object (value);
-                if (priv->conn)
+                if (priv->conn) {
                         priv->iostream = soup_io_stream_new (priv->conn, FALSE);
+                        priv->io_data = soup_server_message_io_http1_new (priv->iostream);
+                }
                 break;
         case PROP_LOCAL_ADDRESS:
                 priv->local_addr = g_value_dup_object (value);
@@ -328,6 +337,23 @@ soup_server_connection_new_for_connection (GIOStream      *connection,
                              NULL);
 }
 
+SoupServerMessageIOData *
+soup_server_connection_get_io_data (SoupServerConnection *conn)
+{
+        SoupServerConnectionPrivate *priv = soup_server_connection_get_instance_private (conn);
+
+        return priv->io_data;
+}
+
+static void
+soup_server_connection_create_io_data (SoupServerConnection *conn)
+{
+        SoupServerConnectionPrivate *priv = soup_server_connection_get_instance_private (conn);
+
+        g_assert (!priv->io_data);
+        priv->io_data = soup_server_message_io_http1_new (priv->iostream);
+}
+
 static gboolean
 tls_connection_accept_certificate (SoupServerConnection *conn,
                                    GTlsCertificate      *tls_certificate,
@@ -441,7 +467,12 @@ soup_server_connection_setup_finish (SoupServerConnection *conn,
                                      GAsyncResult         *result,
                                      GError              **error)
 {
-        return g_task_propagate_boolean (G_TASK (result), error);
+        GTask *task = G_TASK (result);
+
+        if (!g_task_had_error (task))
+                soup_server_connection_create_io_data (conn);
+
+        return g_task_propagate_boolean (task, error);
 }
 
 GSocket *
diff --git a/libsoup/server/soup-server-connection.h b/libsoup/server/soup-server-connection.h
index bebd3edd..5f14cbfe 100644
--- a/libsoup/server/soup-server-connection.h
+++ b/libsoup/server/soup-server-connection.h
@@ -11,6 +11,8 @@
 
 G_BEGIN_DECLS
 
+typedef struct _SoupServerMessageIOData SoupServerMessageIOData;
+
 #define SOUP_TYPE_SERVER_CONNECTION (soup_server_connection_get_type ())
 G_DECLARE_FINAL_TYPE (SoupServerConnection, soup_server_connection, SOUP, SERVER_CONNECTION, GObject)
 
@@ -28,6 +30,7 @@ void                  soup_server_connection_setup_async                     (So
 gboolean              soup_server_connection_setup_finish                    (SoupServerConnection  *conn,
                                                                               GAsyncResult          *result,
                                                                               GError               **error);
+SoupServerMessageIOData *soup_server_connection_get_io_data                  (SoupServerConnection  *conn);
 gboolean              soup_server_connection_is_ssl                          (SoupServerConnection  *conn);
 void                  soup_server_connection_disconnect                      (SoupServerConnection  *conn);
 gboolean              soup_server_connection_is_connected                    (SoupServerConnection  *conn);
diff --git a/libsoup/server/soup-server-message-private.h b/libsoup/server/soup-server-message-private.h
index d1efd645..52fd8722 100644
--- a/libsoup/server/soup-server-message-private.h
+++ b/libsoup/server/soup-server-message-private.h
@@ -40,13 +40,18 @@ void               soup_server_message_finished            (SoupServerMessage
 void               soup_server_message_read_request        (SoupServerMessage        *msg,
                                                             SoupMessageIOCompletionFn completion_cb,
                                                             gpointer                  user_data);
+
 void               soup_server_message_set_options_ping    (SoupServerMessage        *msg,
                                                             gboolean                  is_options_ping);
 
 typedef struct _SoupServerMessageIOData SoupServerMessageIOData;
-void                     soup_server_message_io_data_free  (SoupServerMessageIOData *io);
+void                     soup_server_message_io_data_free  (SoupServerMessageIOData  *io);
 void                     soup_server_message_set_io_data   (SoupServerMessage        *msg,
                                                             SoupServerMessageIOData  *io);
 SoupServerMessageIOData *soup_server_message_get_io_data   (SoupServerMessage        *msg);
+void                    soup_server_message_io_read_request(SoupServerMessageIOData  *io,
+                                                            SoupServerMessage        *msg,
+                                                            SoupMessageIOCompletionFn completion_cb,
+                                                            gpointer                  user_data);
 
 #endif /* __SOUP_SERVER_MESSAGE_PRIVATE_H__ */
diff --git a/libsoup/server/soup-server-message.c b/libsoup/server/soup-server-message.c
index 5fe12ba6..bf81317a 100644
--- a/libsoup/server/soup-server-message.c
+++ b/libsoup/server/soup-server-message.c
@@ -119,8 +119,6 @@ soup_server_message_finalize (GObject *object)
 {
         SoupServerMessage *msg = SOUP_SERVER_MESSAGE (object);
 
-        soup_server_message_io_data_free (msg->io_data);
-
         g_clear_object (&msg->auth_domain);
         g_clear_pointer (&msg->auth_user, g_free);
 
@@ -525,11 +523,19 @@ soup_server_message_is_keepalive (SoupServerMessage *msg)
         return TRUE;
 }
 
+void
+soup_server_message_read_request (SoupServerMessage        *msg,
+                                  SoupMessageIOCompletionFn completion_cb,
+                                  gpointer                  user_data)
+{
+        soup_server_message_set_io_data (msg, soup_server_connection_get_io_data (msg->conn));
+        soup_server_message_io_read_request (msg->io_data, msg, completion_cb, user_data);
+}
+
 void
 soup_server_message_set_io_data (SoupServerMessage       *msg,
                                  SoupServerMessageIOData *io)
 {
-        soup_server_message_io_data_free (msg->io_data);
         msg->io_data = io;
 }
 


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