[libsoup/carlosgc/websockets-soup-io-stream-shutdown] WebSockets: closed signal not emitted when io stream is SoupIOStream



commit a18d9f2c78b1bab8a663890d7a950db1f0692ade
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Fri Jun 21 07:31:24 2019 +0200

    WebSockets: closed signal not emitted when io stream is SoupIOStream
    
    That's the case of connections created by SoupSession. In that case, if
    the server hasn't closed its end of the connection, we fail to shutdown
    the client end, because shutdown_wr_io_stream() does nothing when the io
    stream is not a GSocketConnection. So, for SoupIOStream we need to get
    the base io stream which is a GSocketConnection.

 libsoup/soup-websocket-connection.c | 13 +++++++++++--
 tests/websocket-test.c              |  3 +++
 2 files changed, 14 insertions(+), 2 deletions(-)
---
diff --git a/libsoup/soup-websocket-connection.c b/libsoup/soup-websocket-connection.c
index fb044656..5f1b119b 100644
--- a/libsoup/soup-websocket-connection.c
+++ b/libsoup/soup-websocket-connection.c
@@ -24,6 +24,7 @@
 
 #include "soup-websocket-connection.h"
 #include "soup-enum-types.h"
+#include "soup-io-stream.h"
 #include "soup-uri.h"
 
 /*
@@ -281,12 +282,18 @@ shutdown_wr_io_stream (SoupWebsocketConnection *self)
 {
        SoupWebsocketConnectionPrivate *pv = self->pv;
        GSocket *socket;
+       GIOStream *base_iostream = NULL;
        GError *error = NULL;
 
        stop_output (self);
 
-       if (G_IS_SOCKET_CONNECTION (pv->io_stream)) {
-               socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (pv->io_stream));
+       if (SOUP_IS_IO_STREAM (pv->io_stream))
+               g_object_get (pv->io_stream, "base-iostream", &base_iostream, NULL);
+       else
+               base_iostream = g_object_ref (pv->io_stream);
+
+       if (G_IS_SOCKET_CONNECTION (base_iostream)) {
+               socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (base_iostream));
                g_socket_shutdown (socket, FALSE, TRUE, &error);
                if (error != NULL) {
                        g_debug ("error shutting down io stream: %s", error->message);
@@ -294,6 +301,8 @@ shutdown_wr_io_stream (SoupWebsocketConnection *self)
                }
        }
 
+       g_clear_object (&base_iostream);
+
        g_object_notify (G_OBJECT (self), "state");
 }
 
diff --git a/tests/websocket-test.c b/tests/websocket-test.c
index e03ca651..1f2781af 100644
--- a/tests/websocket-test.c
+++ b/tests/websocket-test.c
@@ -498,9 +498,11 @@ test_send_bad_data (Test *test,
        GIOStream *io;
        gsize written;
        const char *frame;
+       gboolean close_event = FALSE;
 
        g_signal_handlers_disconnect_by_func (test->server, on_error_not_reached, NULL);
        g_signal_connect (test->server, "error", G_CALLBACK (on_error_copy), &error);
+       g_signal_connect (test->client, "closed", G_CALLBACK (on_close_set_flag), &close_event);
 
        io = soup_websocket_connection_get_io_stream (test->client);
 
@@ -516,6 +518,7 @@ test_send_bad_data (Test *test,
        g_clear_error (&error);
 
        WAIT_UNTIL (soup_websocket_connection_get_state (test->client) == SOUP_WEBSOCKET_STATE_CLOSED);
+       g_assert (close_event);
 
        g_assert_cmpuint (soup_websocket_connection_get_close_code (test->client), ==, 
SOUP_WEBSOCKET_CLOSE_BAD_DATA);
 }


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