Imported Upstream version 2.53.3
[platform/upstream/glib.git] / gio / gproxyresolver.c
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 "giomodule.h"
31 #include "giomodule-priv.h"
32
33 /**
34  * SECTION:gproxyresolver
35  * @short_description: Asynchronous and cancellable network proxy resolver
36  * @include: gio/gio.h
37  *
38  * #GProxyResolver provides synchronous and asynchronous network proxy
39  * resolution. #GProxyResolver is used within #GSocketClient through
40  * the method g_socket_connectable_proxy_enumerate().
41  *
42  * Implementations of #GProxyResolver based on libproxy and GNOME settings can
43  * be found in glib-networking. GIO comes with an implementation for use inside
44  * Flatpak portals.
45  */
46
47 /**
48  * GProxyResolverInterface:
49  * @g_iface: The parent interface.
50  * @is_supported: the virtual function pointer for g_proxy_resolver_is_supported()
51  * @lookup: the virtual function pointer for g_proxy_resolver_lookup()
52  * @lookup_async: the virtual function pointer for
53  *  g_proxy_resolver_lookup_async()
54  * @lookup_finish: the virtual function pointer for
55  *  g_proxy_resolver_lookup_finish()
56  *
57  * The virtual function table for #GProxyResolver.
58  */
59
60 G_DEFINE_INTERFACE (GProxyResolver, g_proxy_resolver, G_TYPE_OBJECT)
61
62 static void
63 g_proxy_resolver_default_init (GProxyResolverInterface *iface)
64 {
65 }
66
67 /**
68  * g_proxy_resolver_get_default:
69  *
70  * Gets the default #GProxyResolver for the system.
71  *
72  * Returns: (transfer none): the default #GProxyResolver.
73  *
74  * Since: 2.26
75  */
76 GProxyResolver *
77 g_proxy_resolver_get_default (void)
78 {
79   return _g_io_module_get_default (G_PROXY_RESOLVER_EXTENSION_POINT_NAME,
80                                    "GIO_USE_PROXY_RESOLVER",
81                                    (GIOModuleVerifyFunc)g_proxy_resolver_is_supported);
82 }
83
84 /**
85  * g_proxy_resolver_is_supported:
86  * @resolver: a #GProxyResolver
87  *
88  * Checks if @resolver can be used on this system. (This is used
89  * internally; g_proxy_resolver_get_default() will only return a proxy
90  * resolver that returns %TRUE for this method.)
91  *
92  * Returns: %TRUE if @resolver is supported.
93  *
94  * Since: 2.26
95  */
96 gboolean
97 g_proxy_resolver_is_supported (GProxyResolver *resolver)
98 {
99   GProxyResolverInterface *iface;
100
101   g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), FALSE);
102
103   iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
104
105   return (* iface->is_supported) (resolver);
106 }
107
108 /**
109  * g_proxy_resolver_lookup:
110  * @resolver: a #GProxyResolver
111  * @uri: a URI representing the destination to connect to
112  * @cancellable: (nullable): a #GCancellable, or %NULL
113  * @error: return location for a #GError, or %NULL
114  *
115  * Looks into the system proxy configuration to determine what proxy,
116  * if any, to use to connect to @uri. The returned proxy URIs are of
117  * the form `<protocol>://[user[:password]@]host:port` or
118  * `direct://`, where <protocol> could be http, rtsp, socks
119  * or other proxying protocol.
120  *
121  * If you don't know what network protocol is being used on the
122  * socket, you should use `none` as the URI protocol.
123  * In this case, the resolver might still return a generic proxy type
124  * (such as SOCKS), but would not return protocol-specific proxy types
125  * (such as http).
126  *
127  * `direct://` is used when no proxy is needed.
128  * Direct connection should not be attempted unless it is part of the
129  * returned array of proxies.
130  *
131  * Returns: (transfer full) (array zero-terminated=1): A
132  *               NULL-terminated array of proxy URIs. Must be freed
133  *               with g_strfreev().
134  *
135  * Since: 2.26
136  */
137 gchar **
138 g_proxy_resolver_lookup (GProxyResolver  *resolver,
139                          const gchar     *uri,
140                          GCancellable    *cancellable,
141                          GError         **error)
142 {
143   GProxyResolverInterface *iface;
144
145   g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), NULL);
146   g_return_val_if_fail (uri != NULL, NULL);
147
148   iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
149
150   return (* iface->lookup) (resolver, uri, cancellable, error);
151 }
152
153 /**
154  * g_proxy_resolver_lookup_async:
155  * @resolver: a #GProxyResolver
156  * @uri: a URI representing the destination to connect to
157  * @cancellable: (nullable): a #GCancellable, or %NULL
158  * @callback: (scope async): callback to call after resolution completes
159  * @user_data: (closure): data for @callback
160  *
161  * Asynchronous lookup of proxy. See g_proxy_resolver_lookup() for more
162  * details.
163  *
164  * Since: 2.26
165  */
166 void
167 g_proxy_resolver_lookup_async (GProxyResolver      *resolver,
168                                const gchar         *uri,
169                                GCancellable        *cancellable,
170                                GAsyncReadyCallback  callback,
171                                gpointer             user_data)
172 {
173   GProxyResolverInterface *iface;
174
175   g_return_if_fail (G_IS_PROXY_RESOLVER (resolver));
176   g_return_if_fail (uri != NULL);
177
178   iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
179
180   (* iface->lookup_async) (resolver, uri, cancellable, callback, user_data);
181 }
182
183 /**
184  * g_proxy_resolver_lookup_finish:
185  * @resolver: a #GProxyResolver
186  * @result: the result passed to your #GAsyncReadyCallback
187  * @error: return location for a #GError, or %NULL
188  *
189  * Call this function to obtain the array of proxy URIs when
190  * g_proxy_resolver_lookup_async() is complete. See
191  * g_proxy_resolver_lookup() for more details.
192  *
193  * Returns: (transfer full) (array zero-terminated=1): A
194  *               NULL-terminated array of proxy URIs. Must be freed
195  *               with g_strfreev().
196  *
197  * Since: 2.26
198  */
199 gchar **
200 g_proxy_resolver_lookup_finish (GProxyResolver     *resolver,
201                                 GAsyncResult       *result,
202                                 GError            **error)
203 {
204   GProxyResolverInterface *iface;
205
206   g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), NULL);
207
208   iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
209
210   return (* iface->lookup_finish) (resolver, result, error);
211 }