Commit dbac8627 authored by Carlos Garcia Campos's avatar Carlos Garcia Campos Committed by Carlos Garcia Campos

WebSockets: allow to send empty binary data

Passing data=NULL and length=0 which is consistent with g_bytes_new().
parent b8f9a004
Pipeline #89278 passed with stage
in 49 seconds
......@@ -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);
}
......
......@@ -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,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment