1/* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2010 Collabora, Ltd.
4 *
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.
9 *
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.
14 *
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/>.
17 *
18 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
19 */
20
21#include "config.h"
22
23#include "gproxyresolver.h"
24
25#include <glib.h>
26#include "glibintl.h"
27
28#include "gasyncresult.h"
29#include "gcancellable.h"
30#include "gtask.h"
31#include "giomodule.h"
32#include "gioerror.h"
33#include "giomodule-priv.h"
34#include "gnetworkingprivate.h"
35
36/**
37 * SECTION:gproxyresolver
38 * @short_description: Asynchronous and cancellable network proxy resolver
39 * @include: gio/gio.h
40 *
41 * #GProxyResolver provides synchronous and asynchronous network proxy
42 * resolution. #GProxyResolver is used within #GSocketClient through
43 * the method g_socket_connectable_proxy_enumerate().
44 *
45 * Implementations of #GProxyResolver based on libproxy and GNOME settings can
46 * be found in glib-networking. GIO comes with an implementation for use inside
47 * Flatpak portals.
48 */
49
50/**
51 * GProxyResolverInterface:
52 * @g_iface: The parent interface.
53 * @is_supported: the virtual function pointer for g_proxy_resolver_is_supported()
54 * @lookup: the virtual function pointer for g_proxy_resolver_lookup()
55 * @lookup_async: the virtual function pointer for
56 * g_proxy_resolver_lookup_async()
57 * @lookup_finish: the virtual function pointer for
58 * g_proxy_resolver_lookup_finish()
59 *
60 * The virtual function table for #GProxyResolver.
61 */
62
63G_DEFINE_INTERFACE (GProxyResolver, g_proxy_resolver, G_TYPE_OBJECT)
64
65static void
66g_proxy_resolver_default_init (GProxyResolverInterface *iface)
67{
68}
69
70static GProxyResolver *proxy_resolver_default_singleton = NULL; /* (owned) (atomic) */
71
72/**
73 * g_proxy_resolver_get_default:
74 *
75 * Gets the default #GProxyResolver for the system.
76 *
77 * Returns: (not nullable) (transfer none): the default #GProxyResolver, which
78 * will be a dummy object if no proxy resolver is available
79 *
80 * Since: 2.26
81 */
82GProxyResolver *
83g_proxy_resolver_get_default (void)
84{
85 if (g_once_init_enter (&proxy_resolver_default_singleton))
86 {
87 GProxyResolver *singleton;
88
89 singleton = _g_io_module_get_default (G_PROXY_RESOLVER_EXTENSION_POINT_NAME,
90 envvar: "GIO_USE_PROXY_RESOLVER",
91 verify_func: (GIOModuleVerifyFunc) g_proxy_resolver_is_supported);
92
93 g_once_init_leave (&proxy_resolver_default_singleton, singleton);
94 }
95
96 return proxy_resolver_default_singleton;
97}
98
99/**
100 * g_proxy_resolver_is_supported:
101 * @resolver: a #GProxyResolver
102 *
103 * Checks if @resolver can be used on this system. (This is used
104 * internally; g_proxy_resolver_get_default() will only return a proxy
105 * resolver that returns %TRUE for this method.)
106 *
107 * Returns: %TRUE if @resolver is supported.
108 *
109 * Since: 2.26
110 */
111gboolean
112g_proxy_resolver_is_supported (GProxyResolver *resolver)
113{
114 GProxyResolverInterface *iface;
115
116 g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), FALSE);
117
118 iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
119
120 return (* iface->is_supported) (resolver);
121}
122
123/**
124 * g_proxy_resolver_lookup:
125 * @resolver: a #GProxyResolver
126 * @uri: a URI representing the destination to connect to
127 * @cancellable: (nullable): a #GCancellable, or %NULL
128 * @error: return location for a #GError, or %NULL
129 *
130 * Looks into the system proxy configuration to determine what proxy,
131 * if any, to use to connect to @uri. The returned proxy URIs are of
132 * the form `<protocol>://[user[:password]@]host:port` or
133 * `direct://`, where <protocol> could be http, rtsp, socks
134 * or other proxying protocol.
135 *
136 * If you don't know what network protocol is being used on the
137 * socket, you should use `none` as the URI protocol.
138 * In this case, the resolver might still return a generic proxy type
139 * (such as SOCKS), but would not return protocol-specific proxy types
140 * (such as http).
141 *
142 * `direct://` is used when no proxy is needed.
143 * Direct connection should not be attempted unless it is part of the
144 * returned array of proxies.
145 *
146 * Returns: (transfer full) (array zero-terminated=1): A
147 * NULL-terminated array of proxy URIs. Must be freed
148 * with g_strfreev().
149 *
150 * Since: 2.26
151 */
152gchar **
153g_proxy_resolver_lookup (GProxyResolver *resolver,
154 const gchar *uri,
155 GCancellable *cancellable,
156 GError **error)
157{
158 GProxyResolverInterface *iface;
159
160 g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), NULL);
161 g_return_val_if_fail (uri != NULL, NULL);
162
163 if (!g_uri_is_valid (uri_string: uri, flags: G_URI_FLAGS_NONE, NULL))
164 {
165 g_set_error (err: error, G_IO_ERROR, code: G_IO_ERROR_INVALID_ARGUMENT,
166 format: "Invalid URI ā€˜%sā€™", uri);
167 return NULL;
168 }
169
170 iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
171
172 return (* iface->lookup) (resolver, uri, cancellable, error);
173}
174
175/**
176 * g_proxy_resolver_lookup_async:
177 * @resolver: a #GProxyResolver
178 * @uri: a URI representing the destination to connect to
179 * @cancellable: (nullable): a #GCancellable, or %NULL
180 * @callback: (scope async): callback to call after resolution completes
181 * @user_data: (closure): data for @callback
182 *
183 * Asynchronous lookup of proxy. See g_proxy_resolver_lookup() for more
184 * details.
185 *
186 * Since: 2.26
187 */
188void
189g_proxy_resolver_lookup_async (GProxyResolver *resolver,
190 const gchar *uri,
191 GCancellable *cancellable,
192 GAsyncReadyCallback callback,
193 gpointer user_data)
194{
195 GProxyResolverInterface *iface;
196 GError *error = NULL;
197
198 g_return_if_fail (G_IS_PROXY_RESOLVER (resolver));
199 g_return_if_fail (uri != NULL);
200
201 if (!g_uri_is_valid (uri_string: uri, flags: G_URI_FLAGS_NONE, NULL))
202 {
203 g_set_error (err: &error, G_IO_ERROR, code: G_IO_ERROR_INVALID_ARGUMENT,
204 format: "Invalid URI ā€˜%sā€™", uri);
205 g_task_report_error (source_object: resolver, callback, callback_data: user_data,
206 source_tag: g_proxy_resolver_lookup_async,
207 g_steal_pointer (&error));
208 return;
209 }
210
211 iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
212
213 (* iface->lookup_async) (resolver, uri, cancellable, callback, user_data);
214}
215
216/**
217 * g_proxy_resolver_lookup_finish:
218 * @resolver: a #GProxyResolver
219 * @result: the result passed to your #GAsyncReadyCallback
220 * @error: return location for a #GError, or %NULL
221 *
222 * Call this function to obtain the array of proxy URIs when
223 * g_proxy_resolver_lookup_async() is complete. See
224 * g_proxy_resolver_lookup() for more details.
225 *
226 * Returns: (transfer full) (array zero-terminated=1): A
227 * NULL-terminated array of proxy URIs. Must be freed
228 * with g_strfreev().
229 *
230 * Since: 2.26
231 */
232gchar **
233g_proxy_resolver_lookup_finish (GProxyResolver *resolver,
234 GAsyncResult *result,
235 GError **error)
236{
237 GProxyResolverInterface *iface;
238
239 g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), NULL);
240
241 iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
242
243 return (* iface->lookup_finish) (resolver, result, error);
244}
245

source code of gtk/subprojects/glib/gio/gproxyresolver.c