[libsoup/websockets-fixes-2.64: 3/19] WebSockets: allow to send empty binary data
- From: Claudio Saavedra <csaavedra src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libsoup/websockets-fixes-2.64: 3/19] WebSockets: allow to send empty binary data
- Date: Fri, 11 Dec 2020 14:43:47 +0000 (UTC)
commit c949ff53edacf1cc0337a438f8315cc24a414a1d
Author: Carlos Garcia Campos <cgarcia igalia com>
Date: Tue Jun 18 11:20:18 2019 +0200
WebSockets: allow to send empty binary data
Passing data=NULL and length=0 which is consistent with g_bytes_new().
libsoup/soup-websocket-connection.c | 6 ++--
tests/websocket-test.c | 55 +++++++++++++++++++++++++++++++++++++
2 files changed, 58 insertions(+), 3 deletions(-)
---
diff --git a/libsoup/soup-websocket-connection.c b/libsoup/soup-websocket-connection.c
index 35eee6a6..899e0651 100644
--- a/libsoup/soup-websocket-connection.c
+++ b/libsoup/soup-websocket-connection.c
@@ -1722,10 +1722,10 @@ soup_websocket_connection_send_text (SoupWebsocketConnection *self,
/**
* soup_websocket_connection_send_binary:
* @self: the WebSocket
- * @data: (array length=length) (element-type guint8): the message contents
+ * @data: (array length=length) (element-type guint8) (nullable): the message contents
* @length: the length of @data
*
- * Send a binary message to the peer.
+ * Send a binary message to the peer. If @length is 0, @data may be %NULL.
*
* The message is queued to be sent and will be sent when the main loop
* is run.
@@ -1739,7 +1739,7 @@ soup_websocket_connection_send_binary (SoupWebsocketConnection *self,
{
g_return_if_fail (SOUP_IS_WEBSOCKET_CONNECTION (self));
g_return_if_fail (soup_websocket_connection_get_state (self) == SOUP_WEBSOCKET_STATE_OPEN);
- g_return_if_fail (data != NULL);
+ g_return_if_fail (data != NULL || length == 0);
send_message (self, SOUP_WEBSOCKET_QUEUE_NORMAL, 0x02, data, length);
}
diff --git a/tests/websocket-test.c b/tests/websocket-test.c
index 2347ed5c..4015a96d 100644
--- a/tests/websocket-test.c
+++ b/tests/websocket-test.c
@@ -291,6 +291,21 @@ on_text_message (SoupWebsocketConnection *ws,
*receive = g_bytes_ref (message);
}
+static void
+on_binary_message (SoupWebsocketConnection *ws,
+ SoupWebsocketDataType type,
+ GBytes *message,
+ gpointer user_data)
+{
+ GBytes **receive = user_data;
+
+ g_assert_cmpint (type, ==, SOUP_WEBSOCKET_DATA_BINARY);
+ g_assert (*receive == NULL);
+ g_assert (message != NULL);
+
+ *receive = g_bytes_ref (message);
+}
+
static void
on_close_set_flag (SoupWebsocketConnection *ws,
gpointer user_data)
@@ -444,6 +459,37 @@ test_send_big_packets (Test *test,
g_bytes_unref (received);
}
+static void
+test_send_empty_packets (Test *test,
+ gconstpointer data)
+{
+ GBytes *received = NULL;
+ gulong id;
+
+ id = g_signal_connect (test->client, "message", G_CALLBACK (on_text_message), &received);
+
+ soup_websocket_connection_send_text (test->server, "\0");
+ WAIT_UNTIL (received != NULL);
+ g_assert_nonnull (g_bytes_get_data (received, NULL));
+ g_assert_cmpuint (((char *) g_bytes_get_data (received, NULL))[0], ==, '\0');
+ g_assert_cmpuint (g_bytes_get_size (received), ==, 0);
+ g_bytes_unref (received);
+ received = NULL;
+ g_signal_handler_disconnect (test->client, id);
+
+ id = g_signal_connect (test->client, "message", G_CALLBACK (on_binary_message), &received);
+
+ soup_websocket_connection_send_binary (test->server, NULL, 0);
+ WAIT_UNTIL (received != NULL);
+ /* We always include at least a null character */
+ g_assert_nonnull (g_bytes_get_data (received, NULL));
+ g_assert_cmpuint (((char *) g_bytes_get_data (received, NULL))[0], ==, '\0');
+ g_assert_cmpuint (g_bytes_get_size (received), ==, 0);
+ g_bytes_unref (received);
+ received = NULL;
+ g_signal_handler_disconnect (test->client, id);
+}
+
static void
test_send_bad_data (Test *test,
gconstpointer unused)
@@ -935,6 +981,15 @@ main (int argc,
test_send_big_packets,
teardown_soup_connection);
+ g_test_add ("/websocket/direct/send-empty-packets", Test, NULL,
+ setup_direct_connection,
+ test_send_empty_packets,
+ teardown_direct_connection);
+ g_test_add ("/websocket/soup/send-empty-packets", Test, NULL,
+ setup_soup_connection,
+ test_send_empty_packets,
+ teardown_soup_connection);
+
g_test_add ("/websocket/direct/send-bad-data", Test, NULL,
setup_direct_connection,
test_send_bad_data,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]