Check for overflow when allocating RcBox
[glib.git] / gio / gproxyresolver.c
blobbd9528dd0e1462e2daa7331129e94bec72837147
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Collabora, Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
21 #include "config.h"
23 #include "gproxyresolver.h"
25 #include <glib.h>
26 #include "glibintl.h"
28 #include "gasyncresult.h"
29 #include "gcancellable.h"
30 #include "gtask.h"
31 #include "giomodule.h"
32 #include "giomodule-priv.h"
33 #include "gnetworkingprivate.h"
35 /**
36 * SECTION:gproxyresolver
37 * @short_description: Asynchronous and cancellable network proxy resolver
38 * @include: gio/gio.h
40 * #GProxyResolver provides synchronous and asynchronous network proxy
41 * resolution. #GProxyResolver is used within #GSocketClient through
42 * the method g_socket_connectable_proxy_enumerate().
44 * Implementations of #GProxyResolver based on libproxy and GNOME settings can
45 * be found in glib-networking. GIO comes with an implementation for use inside
46 * Flatpak portals.
49 /**
50 * GProxyResolverInterface:
51 * @g_iface: The parent interface.
52 * @is_supported: the virtual function pointer for g_proxy_resolver_is_supported()
53 * @lookup: the virtual function pointer for g_proxy_resolver_lookup()
54 * @lookup_async: the virtual function pointer for
55 * g_proxy_resolver_lookup_async()
56 * @lookup_finish: the virtual function pointer for
57 * g_proxy_resolver_lookup_finish()
59 * The virtual function table for #GProxyResolver.
62 G_DEFINE_INTERFACE (GProxyResolver, g_proxy_resolver, G_TYPE_OBJECT)
64 static void
65 g_proxy_resolver_default_init (GProxyResolverInterface *iface)
69 /**
70 * g_proxy_resolver_get_default:
72 * Gets the default #GProxyResolver for the system.
74 * Returns: (transfer none): the default #GProxyResolver.
76 * Since: 2.26
78 GProxyResolver *
79 g_proxy_resolver_get_default (void)
81 return _g_io_module_get_default (G_PROXY_RESOLVER_EXTENSION_POINT_NAME,
82 "GIO_USE_PROXY_RESOLVER",
83 (GIOModuleVerifyFunc)g_proxy_resolver_is_supported);
86 /**
87 * g_proxy_resolver_is_supported:
88 * @resolver: a #GProxyResolver
90 * Checks if @resolver can be used on this system. (This is used
91 * internally; g_proxy_resolver_get_default() will only return a proxy
92 * resolver that returns %TRUE for this method.)
94 * Returns: %TRUE if @resolver is supported.
96 * Since: 2.26
98 gboolean
99 g_proxy_resolver_is_supported (GProxyResolver *resolver)
101 GProxyResolverInterface *iface;
103 g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), FALSE);
105 iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
107 return (* iface->is_supported) (resolver);
111 * g_proxy_resolver_lookup:
112 * @resolver: a #GProxyResolver
113 * @uri: a URI representing the destination to connect to
114 * @cancellable: (nullable): a #GCancellable, or %NULL
115 * @error: return location for a #GError, or %NULL
117 * Looks into the system proxy configuration to determine what proxy,
118 * if any, to use to connect to @uri. The returned proxy URIs are of
119 * the form `<protocol>://[user[:password]@]host:port` or
120 * `direct://`, where <protocol> could be http, rtsp, socks
121 * or other proxying protocol.
123 * If you don't know what network protocol is being used on the
124 * socket, you should use `none` as the URI protocol.
125 * In this case, the resolver might still return a generic proxy type
126 * (such as SOCKS), but would not return protocol-specific proxy types
127 * (such as http).
129 * `direct://` is used when no proxy is needed.
130 * Direct connection should not be attempted unless it is part of the
131 * returned array of proxies.
133 * Returns: (transfer full) (array zero-terminated=1): A
134 * NULL-terminated array of proxy URIs. Must be freed
135 * with g_strfreev().
137 * Since: 2.26
139 gchar **
140 g_proxy_resolver_lookup (GProxyResolver *resolver,
141 const gchar *uri,
142 GCancellable *cancellable,
143 GError **error)
145 GProxyResolverInterface *iface;
147 g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), NULL);
148 g_return_val_if_fail (uri != NULL, NULL);
150 if (!_g_uri_parse_authority (uri, NULL, NULL, NULL, error))
151 return NULL;
153 iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
155 return (* iface->lookup) (resolver, uri, cancellable, error);
159 * g_proxy_resolver_lookup_async:
160 * @resolver: a #GProxyResolver
161 * @uri: a URI representing the destination to connect to
162 * @cancellable: (nullable): a #GCancellable, or %NULL
163 * @callback: (scope async): callback to call after resolution completes
164 * @user_data: (closure): data for @callback
166 * Asynchronous lookup of proxy. See g_proxy_resolver_lookup() for more
167 * details.
169 * Since: 2.26
171 void
172 g_proxy_resolver_lookup_async (GProxyResolver *resolver,
173 const gchar *uri,
174 GCancellable *cancellable,
175 GAsyncReadyCallback callback,
176 gpointer user_data)
178 GProxyResolverInterface *iface;
179 GError *error = NULL;
181 g_return_if_fail (G_IS_PROXY_RESOLVER (resolver));
182 g_return_if_fail (uri != NULL);
184 if (!_g_uri_parse_authority (uri, NULL, NULL, NULL, &error))
186 g_task_report_error (resolver, callback, user_data,
187 g_proxy_resolver_lookup_async,
188 g_steal_pointer (&error));
189 return;
192 iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
194 (* iface->lookup_async) (resolver, uri, cancellable, callback, user_data);
198 * g_proxy_resolver_lookup_finish:
199 * @resolver: a #GProxyResolver
200 * @result: the result passed to your #GAsyncReadyCallback
201 * @error: return location for a #GError, or %NULL
203 * Call this function to obtain the array of proxy URIs when
204 * g_proxy_resolver_lookup_async() is complete. See
205 * g_proxy_resolver_lookup() for more details.
207 * Returns: (transfer full) (array zero-terminated=1): A
208 * NULL-terminated array of proxy URIs. Must be freed
209 * with g_strfreev().
211 * Since: 2.26
213 gchar **
214 g_proxy_resolver_lookup_finish (GProxyResolver *resolver,
215 GAsyncResult *result,
216 GError **error)
218 GProxyResolverInterface *iface;
220 g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), NULL);
222 iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
224 return (* iface->lookup_finish) (resolver, result, error);