[libsoup/wip/max-payload-size: 2/2] Add max-payload-size property to the websocket connection.



commit 8cbd2753d9ee443a7f7a45fb22c297df78c178d9
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Fri Aug 19 18:22:04 2016 +0200

    Add max-payload-size property to the websocket connection.
    
    This allows to change the limit for the received payload packets.
    Also change the unit test to set a higher limit.

 libsoup/soup-websocket-connection.c |   36 ++++++++++++++++++++++++++++++++--
 tests/websocket-test.c              |    1 +
 2 files changed, 34 insertions(+), 3 deletions(-)
---
diff --git a/libsoup/soup-websocket-connection.c b/libsoup/soup-websocket-connection.c
index 622aa68..b81830d 100644
--- a/libsoup/soup-websocket-connection.c
+++ b/libsoup/soup-websocket-connection.c
@@ -80,6 +80,7 @@ enum {
        PROP_ORIGIN,
        PROP_PROTOCOL,
        PROP_STATE,
+       PROP_MAX_PAYLOAD_SIZE,
 };
 
 enum {
@@ -105,6 +106,7 @@ struct _SoupWebsocketConnectionPrivate {
        SoupURI *uri;
        char *origin;
        char *protocol;
+       guint64 max_payload_size;
 
        gushort peer_close_code;
        char *peer_close_data;
@@ -500,9 +502,9 @@ too_big_error_and_close (SoupWebsocketConnection *self,
                                     self->pv->connection_type == SOUP_WEBSOCKET_CONNECTION_SERVER ?
                                     "Received extremely large WebSocket data from the client" :
                                     "Received extremely large WebSocket data from the server");
-       g_debug ("%s is trying to frame of size %" G_GUINT64_FORMAT " or greater, but max supported size is 
128KiB",
+       g_debug ("%s is trying to frame of size %" G_GUINT64_FORMAT " or greater, but max supported size is 
%" G_GUINT64_FORMAT,
                 self->pv->connection_type == SOUP_WEBSOCKET_CONNECTION_SERVER ? "server" : "client",
-                payload_len);
+                payload_len, self->pv->max_payload_size);
        emit_error_and_close (self, error, TRUE);
 
        /* The input is in an invalid state now */
@@ -728,7 +730,8 @@ process_frame (SoupWebsocketConnection *self)
        }
 
        /* Safety valve */
-       if (payload_len >= MAX_PAYLOAD) {
+       if (self->pv->max_payload_size > 0 &&
+           payload_len >= self->pv->max_payload_size) {
                too_big_error_and_close (self, payload_len);
                return FALSE;
        }
@@ -963,6 +966,7 @@ soup_websocket_connection_get_property (GObject *object,
                                        GParamSpec *pspec)
 {
        SoupWebsocketConnection *self = SOUP_WEBSOCKET_CONNECTION (object);
+       SoupWebsocketConnectionPrivate *pv = self->pv;
 
        switch (prop_id) {
        case PROP_IO_STREAM:
@@ -989,6 +993,10 @@ soup_websocket_connection_get_property (GObject *object,
                g_value_set_enum (value, soup_websocket_connection_get_state (self));
                break;
 
+       case PROP_MAX_PAYLOAD_SIZE:
+               g_value_set_uint64 (value, pv->max_payload_size);
+               break;
+
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
@@ -1029,6 +1037,10 @@ soup_websocket_connection_set_property (GObject *object,
                pv->protocol = g_value_dup_string (value);
                break;
 
+       case PROP_MAX_PAYLOAD_SIZE:
+               pv->max_payload_size = g_value_get_uint64 (value);
+               break;
+
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
@@ -1197,6 +1209,24 @@ soup_websocket_connection_class_init (SoupWebsocketConnectionClass *klass)
                                                            G_PARAM_STATIC_STRINGS));
 
        /**
+        * SoupWebsocketConnection:max-payload-size:
+        *
+        * The maximum payload size the protocol expects or 0 to not limit it.
+        *
+        * Since: 2.56
+        */
+       g_object_class_install_property (gobject_class, PROP_MAX_PAYLOAD_SIZE,
+                                        g_param_spec_uint64 ("max-payload-size",
+                                                             "Max payload size",
+                                                             "Max payload size ",
+                                                             0,
+                                                             G_MAXUINT64,
+                                                             MAX_PAYLOAD,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT |
+                                                             G_PARAM_STATIC_STRINGS));
+
+       /**
         * SoupWebsocketConnection::message:
         * @self: the WebSocket
         * @type: the type of message contents
diff --git a/tests/websocket-test.c b/tests/websocket-test.c
index 9748fed..6545f0d 100644
--- a/tests/websocket-test.c
+++ b/tests/websocket-test.c
@@ -382,6 +382,7 @@ test_send_big_packets (Test *test,
        g_bytes_unref (received);
        received = NULL;
 
+       g_object_set (G_OBJECT (test->server), "max-payload-size", 1000 * 1000 + 1, NULL);
        sent = g_bytes_new_take (g_strnfill (1000 * 1000, '?'), 1000 * 1000);
        soup_websocket_connection_send_text (test->server, g_bytes_get_data (sent, NULL));
        WAIT_UNTIL (received != NULL);


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