• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * soup-websocket-extension-manager.c
4  *
5  * Copyright (C) 2019 Igalia S.L.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "soup-websocket-extension-manager.h"
28 #include "soup-headers.h"
29 #include "soup-session-feature.h"
30 #include "soup-websocket.h"
31 #include "soup-websocket-extension.h"
32 #include "soup-websocket-extension-deflate.h"
33 #include "soup-websocket-extension-manager-private.h"
34 
35 /**
36  * SECTION:soup-websocket-extension-manager
37  * @title: SoupWebsocketExtensionManager
38  * @short_description: WebSocket extensions manager
39  * @see_also: #SoupSession, #SoupWebsocketExtension
40  *
41  * SoupWebsocketExtensionManager is the #SoupSessionFeature that handles WebSockets
42  * extensions for a #SoupSession.
43  *
44  * A SoupWebsocketExtensionManager is added to the session by default, and normally
45  * you don't need to worry about it at all. However, if you want to
46  * disable WebSocket extensions, you can remove the feature from the
47  * session with soup_session_remove_feature_by_type(), or disable it on
48  * individual requests with soup_message_disable_feature().
49  *
50  * Since: 2.68
51  **/
52 
53 /**
54  * SOUP_TYPE_WEBSOCKET_EXTENSION_MANAGER:
55  *
56  * The #GType of #SoupWebsocketExtensionManager; you can use this with
57  * soup_session_remove_feature_by_type() or
58  * soup_message_disable_feature().
59  *
60  * Since: 2.68
61  */
62 
63 static void soup_websocket_extension_manager_session_feature_init (SoupSessionFeatureInterface *feature_interface, gpointer interface_data);
64 
65 typedef struct {
66         GPtrArray *extension_types;
67 } SoupWebsocketExtensionManagerPrivate;
68 
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))69 G_DEFINE_TYPE_WITH_CODE (SoupWebsocketExtensionManager, soup_websocket_extension_manager, G_TYPE_OBJECT,
70                          G_ADD_PRIVATE (SoupWebsocketExtensionManager)
71                          G_IMPLEMENT_INTERFACE (SOUP_TYPE_SESSION_FEATURE,
72                                                 soup_websocket_extension_manager_session_feature_init))
73 
74 static void
75 soup_websocket_extension_manager_init (SoupWebsocketExtensionManager *manager)
76 {
77         SoupWebsocketExtensionManagerPrivate *priv = soup_websocket_extension_manager_get_instance_private (manager);
78 
79         priv->extension_types = g_ptr_array_new_with_free_func ((GDestroyNotify)g_type_class_unref);
80 
81         /* Use permessage-deflate extension by default */
82         soup_session_feature_add_feature (SOUP_SESSION_FEATURE (manager), SOUP_TYPE_WEBSOCKET_EXTENSION_DEFLATE);
83 }
84 
85 static void
soup_websocket_extension_manager_finalize(GObject * object)86 soup_websocket_extension_manager_finalize (GObject *object)
87 {
88         SoupWebsocketExtensionManagerPrivate *priv;
89 
90         priv = soup_websocket_extension_manager_get_instance_private (SOUP_WEBSOCKET_EXTENSION_MANAGER (object));
91         g_ptr_array_free (priv->extension_types, TRUE);
92 
93         G_OBJECT_CLASS (soup_websocket_extension_manager_parent_class)->finalize (object);
94 }
95 
96 static void
soup_websocket_extension_manager_class_init(SoupWebsocketExtensionManagerClass * websocket_extension_manager_class)97 soup_websocket_extension_manager_class_init (SoupWebsocketExtensionManagerClass *websocket_extension_manager_class)
98 {
99         GObjectClass *object_class = G_OBJECT_CLASS (websocket_extension_manager_class);
100 
101         object_class->finalize = soup_websocket_extension_manager_finalize;
102 }
103 
104 static gboolean
soup_websocket_extension_manager_add_feature(SoupSessionFeature * feature,GType type)105 soup_websocket_extension_manager_add_feature (SoupSessionFeature *feature, GType type)
106 {
107         SoupWebsocketExtensionManagerPrivate *priv;
108 
109         if (!g_type_is_a (type, SOUP_TYPE_WEBSOCKET_EXTENSION))
110                 return FALSE;
111 
112         priv = soup_websocket_extension_manager_get_instance_private (SOUP_WEBSOCKET_EXTENSION_MANAGER (feature));
113         g_ptr_array_add (priv->extension_types, g_type_class_ref (type));
114 
115         return TRUE;
116 }
117 
118 static gboolean
soup_websocket_extension_manager_remove_feature(SoupSessionFeature * feature,GType type)119 soup_websocket_extension_manager_remove_feature (SoupSessionFeature *feature, GType type)
120 {
121         SoupWebsocketExtensionManagerPrivate *priv;
122         SoupWebsocketExtensionClass *extension_class;
123         guint i;
124 
125         if (!g_type_is_a (type, SOUP_TYPE_WEBSOCKET_EXTENSION))
126                 return FALSE;
127 
128         priv = soup_websocket_extension_manager_get_instance_private (SOUP_WEBSOCKET_EXTENSION_MANAGER (feature));
129         extension_class = g_type_class_peek (type);
130 
131         for (i = 0; i < priv->extension_types->len; i++) {
132                 if (priv->extension_types->pdata[i] == (gpointer)extension_class) {
133                         g_ptr_array_remove_index (priv->extension_types, i);
134                         return TRUE;
135                 }
136         }
137 
138         return FALSE;
139 }
140 
141 static gboolean
soup_websocket_extension_manager_has_feature(SoupSessionFeature * feature,GType type)142 soup_websocket_extension_manager_has_feature (SoupSessionFeature *feature, GType type)
143 {
144         SoupWebsocketExtensionManagerPrivate *priv;
145         SoupWebsocketExtensionClass *extension_class;
146         guint i;
147 
148         if (!g_type_is_a (type, SOUP_TYPE_WEBSOCKET_EXTENSION))
149                 return FALSE;
150 
151         priv = soup_websocket_extension_manager_get_instance_private (SOUP_WEBSOCKET_EXTENSION_MANAGER (feature));
152         extension_class = g_type_class_peek (type);
153 
154         for (i = 0; i < priv->extension_types->len; i++) {
155                 if (priv->extension_types->pdata[i] == (gpointer)extension_class)
156                         return TRUE;
157         }
158 
159         return FALSE;
160 }
161 
162 static void
soup_websocket_extension_manager_session_feature_init(SoupSessionFeatureInterface * feature_interface,gpointer interface_data)163 soup_websocket_extension_manager_session_feature_init (SoupSessionFeatureInterface *feature_interface,
164                                                        gpointer                     interface_data)
165 {
166         feature_interface->add_feature = soup_websocket_extension_manager_add_feature;
167         feature_interface->remove_feature = soup_websocket_extension_manager_remove_feature;
168         feature_interface->has_feature = soup_websocket_extension_manager_has_feature;
169 }
170 
171 GPtrArray *
soup_websocket_extension_manager_get_supported_extensions(SoupWebsocketExtensionManager * manager)172 soup_websocket_extension_manager_get_supported_extensions (SoupWebsocketExtensionManager *manager)
173 {
174         SoupWebsocketExtensionManagerPrivate *priv;
175 
176         g_return_val_if_fail (SOUP_IS_WEBSOCKET_EXTENSION_MANAGER (manager), NULL);
177 
178         priv = soup_websocket_extension_manager_get_instance_private (manager);
179         return priv->extension_types;
180 }
181