[libsoup/websockets-fixes-2.64: 6/19] WebSockets: closed signal not emitted when io stream is SoupIOStream




commit 2986799540aaba76426fd0a7b908fea1d334715f
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-io-stream.c            |  8 ++++++++
 libsoup/soup-io-stream.h            |  2 ++
 libsoup/soup-websocket-connection.c | 10 ++++++++--
 tests/websocket-test.c              |  3 +++
 4 files changed, 21 insertions(+), 2 deletions(-)
---
diff --git a/libsoup/soup-io-stream.c b/libsoup/soup-io-stream.c
index 9c77b267..1c07262d 100644
--- a/libsoup/soup-io-stream.c
+++ b/libsoup/soup-io-stream.c
@@ -217,3 +217,11 @@ soup_io_stream_new (GIOStream *base_iostream,
                             "close-on-dispose", close_on_dispose,
                             NULL);
 }
+
+GIOStream *
+soup_io_stream_get_base_iostream (SoupIOStream *stream)
+{
+       g_return_val_if_fail (SOUP_IS_IO_STREAM (stream), NULL);
+
+       return stream->priv->base_iostream;
+}
diff --git a/libsoup/soup-io-stream.h b/libsoup/soup-io-stream.h
index 92c24c3b..e18fc8d8 100644
--- a/libsoup/soup-io-stream.h
+++ b/libsoup/soup-io-stream.h
@@ -35,6 +35,8 @@ GType soup_io_stream_get_type (void);
 GIOStream *soup_io_stream_new (GIOStream *base_iostream,
                               gboolean   close_on_dispose);
 
+GIOStream *soup_io_stream_get_base_iostream (SoupIOStream *stream);
+
 G_END_DECLS
 
 #endif /* __SOUP_IO_STREAM_H__ */
diff --git a/libsoup/soup-websocket-connection.c b/libsoup/soup-websocket-connection.c
index fb044656..66bd6871 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,17 @@ shutdown_wr_io_stream (SoupWebsocketConnection *self)
 {
        SoupWebsocketConnectionPrivate *pv = self->pv;
        GSocket *socket;
+       GIOStream *base_iostream;
        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));
+       base_iostream = SOUP_IS_IO_STREAM (pv->io_stream) ?
+               soup_io_stream_get_base_iostream (SOUP_IO_STREAM (pv->io_stream)) :
+               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);
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]