summaryrefslogtreecommitdiff
path: root/libsoup/websocket/soup-websocket-extension-manager.c
blob: f213d6d612f3097212d5822be022f87f2ba2b8db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * soup-websocket-extension-manager.c
 *
 * Copyright (C) 2019 Igalia S.L.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "soup-websocket-extension-manager.h"
#include "soup-headers.h"
#include "soup-session-feature.h"
#include "soup-websocket.h"
#include "soup-websocket-extension.h"
#include "soup-websocket-extension-deflate.h"
#include "soup-websocket-extension-manager-private.h"

/**
 * SoupWebsocketExtensionManager:
 *
 * SoupWebsocketExtensionManager is the #SoupSessionFeature that handles WebSockets
 * extensions for a #SoupSession.
 *
 * A SoupWebsocketExtensionManager is added to the session by default, and normally
 * you don't need to worry about it at all. However, if you want to
 * disable WebSocket extensions, you can remove the feature from the
 * session with soup_session_remove_feature_by_type(), or disable it on
 * individual requests with soup_message_disable_feature().
 *
 * Since: 2.68
 **/

/**
 * SOUP_TYPE_WEBSOCKET_EXTENSION_MANAGER:
 *
 * The #GType of #SoupWebsocketExtensionManager; you can use this with
 * soup_session_remove_feature_by_type() or
 * soup_message_disable_feature().
 *
 * Since: 2.68
 */

static void soup_websocket_extension_manager_session_feature_init (SoupSessionFeatureInterface *feature_interface, gpointer interface_data);

struct _SoupWebsocketExtensionManager {
	GObject parent;
};

typedef struct {
        GPtrArray *extension_types;
} SoupWebsocketExtensionManagerPrivate;

G_DEFINE_TYPE_WITH_CODE (SoupWebsocketExtensionManager, soup_websocket_extension_manager, G_TYPE_OBJECT,
                         G_ADD_PRIVATE (SoupWebsocketExtensionManager)
                         G_IMPLEMENT_INTERFACE (SOUP_TYPE_SESSION_FEATURE,
                                                soup_websocket_extension_manager_session_feature_init))

static void
soup_websocket_extension_manager_init (SoupWebsocketExtensionManager *manager)
{
        SoupWebsocketExtensionManagerPrivate *priv = soup_websocket_extension_manager_get_instance_private (manager);

        priv->extension_types = g_ptr_array_new_with_free_func ((GDestroyNotify)g_type_class_unref);

        /* Use permessage-deflate extension by default */
        soup_session_feature_add_feature (SOUP_SESSION_FEATURE (manager), SOUP_TYPE_WEBSOCKET_EXTENSION_DEFLATE);
}

static void
soup_websocket_extension_manager_finalize (GObject *object)
{
        SoupWebsocketExtensionManagerPrivate *priv;

        priv = soup_websocket_extension_manager_get_instance_private (SOUP_WEBSOCKET_EXTENSION_MANAGER (object));
        g_ptr_array_free (priv->extension_types, TRUE);

        G_OBJECT_CLASS (soup_websocket_extension_manager_parent_class)->finalize (object);
}

static void
soup_websocket_extension_manager_class_init (SoupWebsocketExtensionManagerClass *websocket_extension_manager_class)
{
        GObjectClass *object_class = G_OBJECT_CLASS (websocket_extension_manager_class);

        object_class->finalize = soup_websocket_extension_manager_finalize;
}

static gboolean
soup_websocket_extension_manager_add_feature (SoupSessionFeature *feature, GType type)
{
        SoupWebsocketExtensionManagerPrivate *priv;

        if (!g_type_is_a (type, SOUP_TYPE_WEBSOCKET_EXTENSION))
                return FALSE;

        priv = soup_websocket_extension_manager_get_instance_private (SOUP_WEBSOCKET_EXTENSION_MANAGER (feature));
        g_ptr_array_add (priv->extension_types, g_type_class_ref (type));

        return TRUE;
}

static gboolean
soup_websocket_extension_manager_remove_feature (SoupSessionFeature *feature, GType type)
{
        SoupWebsocketExtensionManagerPrivate *priv;
        SoupWebsocketExtensionClass *extension_class;
        guint i;

        if (!g_type_is_a (type, SOUP_TYPE_WEBSOCKET_EXTENSION))
                return FALSE;

        priv = soup_websocket_extension_manager_get_instance_private (SOUP_WEBSOCKET_EXTENSION_MANAGER (feature));
        extension_class = g_type_class_peek (type);

        for (i = 0; i < priv->extension_types->len; i++) {
                if (priv->extension_types->pdata[i] == (gpointer)extension_class) {
                        g_ptr_array_remove_index (priv->extension_types, i);
                        return TRUE;
                }
        }

        return FALSE;
}

static gboolean
soup_websocket_extension_manager_has_feature (SoupSessionFeature *feature, GType type)
{
        SoupWebsocketExtensionManagerPrivate *priv;
        SoupWebsocketExtensionClass *extension_class;
        guint i;

        if (!g_type_is_a (type, SOUP_TYPE_WEBSOCKET_EXTENSION))
                return FALSE;

        priv = soup_websocket_extension_manager_get_instance_private (SOUP_WEBSOCKET_EXTENSION_MANAGER (feature));
        extension_class = g_type_class_peek (type);

        for (i = 0; i < priv->extension_types->len; i++) {
                if (priv->extension_types->pdata[i] == (gpointer)extension_class)
                        return TRUE;
        }

        return FALSE;
}

static void
soup_websocket_extension_manager_session_feature_init (SoupSessionFeatureInterface *feature_interface,
                                                       gpointer                     interface_data)
{
        feature_interface->add_feature = soup_websocket_extension_manager_add_feature;
        feature_interface->remove_feature = soup_websocket_extension_manager_remove_feature;
        feature_interface->has_feature = soup_websocket_extension_manager_has_feature;
}

GPtrArray *
soup_websocket_extension_manager_get_supported_extensions (SoupWebsocketExtensionManager *manager)
{
        SoupWebsocketExtensionManagerPrivate *priv;

        g_return_val_if_fail (SOUP_IS_WEBSOCKET_EXTENSION_MANAGER (manager), NULL);

        priv = soup_websocket_extension_manager_get_instance_private (manager);
        return priv->extension_types;
}