Implemented proxy-resolver extension point

This extension point allow extending GLib with library like LibProxy that
interprets system proxy settings and finds the appropriate configuration
based on the type of connection being made.

Reviewed-by: Dan Winship <danw@gnome.org>
This commit is contained in:
Nicolas Dufresne 2010-05-07 15:55:54 -04:00
parent f82740f7be
commit f3debedfd2
12 changed files with 601 additions and 0 deletions

View File

@ -123,6 +123,7 @@
<chapter id="resolver">
<title>DNS resolution</title>
<xi:include href="xml/gresolver.xml"/>
<xi:include href="xml/gproxyresolver.xml"/>
<xi:include href="xml/gsocketconnectable.xml"/>
<xi:include href="xml/gnetworkaddress.xml"/>
<xi:include href="xml/gnetworkservice.xml"/>

View File

@ -2766,3 +2766,23 @@ G_SIMPLE_ACTION_GROUP_GET_CLASS
G_IS_SIMPLE_ACTION_GROUP_CLASS
G_SIMPLE_ACTION_GROUP
</SECTION>
<SECTION>
<FILE>gproxyresolver</FILE>
<TITLE>GProxyResolver</TITLE>
GProxyResolver
GProxyResolverIface
G_PROXY_RESOLVER_EXTENSION_POINT_NAME
g_proxy_resolver_get_default
g_proxy_resolver_is_supported
g_proxy_resolver_lookup
g_proxy_resolver_lookup_async
g_proxy_resolver_lookup_finish
<SUBSECTION Standard>
G_PROXY_RESOLVER
G_IS_PROXY_RESOLVER
G_TYPE_PROXY_RESOLVER
G_PROXY_RESOLVER_GET_IFACE
<SUBSECTION Private>
g_proxy_resolver_get_type
</SECTION>

View File

@ -75,6 +75,7 @@ g_output_stream_get_type
g_output_stream_splice_flags_get_type
g_password_save_get_type
g_permission_get_type
g_proxy_resolver_get_type
g_resolver_error_get_type
g_resolver_get_type
g_seekable_get_type

View File

@ -288,6 +288,8 @@ libgio_2_0_la_SOURCES = \
gdrive.c \
gdummyfile.h \
gdummyfile.c \
gdummyproxyresolver.c \
gdummyproxyresolver.h \
gemblem.h \
gemblem.c \
gemblemedicon.h \
@ -331,6 +333,7 @@ libgio_2_0_la_SOURCES = \
gpermission.c \
gpollfilemonitor.c \
gpollfilemonitor.h \
gproxyresolver.c \
gresolver.c \
gseekable.c \
gsimpleasyncresult.c \
@ -478,6 +481,7 @@ gio_headers = \
gnetworkservice.h \
goutputstream.h \
gpermission.h \
gproxyresolver.h \
gresolver.h \
gseekable.h \
gsimpleasyncresult.h \

156
gio/gdummyproxyresolver.c Normal file
View File

@ -0,0 +1,156 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2010 Collabora, Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
*/
#include "config.h"
#include "gdummyproxyresolver.h"
#include <glib.h>
#include "gasyncresult.h"
#include "gcancellable.h"
#include "gproxyresolver.h"
#include "gsimpleasyncresult.h"
#include "giomodule.h"
#include "giomodule-priv.h"
struct _GDummyProxyResolver {
GObject parent_instance;
};
static void g_dummy_proxy_resolver_iface_init (GProxyResolverInterface *iface);
#define g_dummy_proxy_resolver_get_type _g_dummy_proxy_resolver_get_type
G_DEFINE_TYPE_WITH_CODE (GDummyProxyResolver, g_dummy_proxy_resolver, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER,
g_dummy_proxy_resolver_iface_init)
_g_io_modules_ensure_extension_points_registered ();
g_io_extension_point_implement (G_PROXY_RESOLVER_EXTENSION_POINT_NAME,
g_define_type_id,
"dummy",
-100))
static void
g_dummy_proxy_resolver_finalize (GObject *object)
{
/* must chain up */
G_OBJECT_CLASS (g_dummy_proxy_resolver_parent_class)->finalize (object);
}
static void
g_dummy_proxy_resolver_init (GDummyProxyResolver *resolver)
{
}
static gboolean
g_dummy_proxy_resolver_is_supported (GProxyResolver *resolver)
{
return TRUE;
}
static gchar **
g_dummy_proxy_resolver_lookup (GProxyResolver *resolver,
const gchar *uri,
GCancellable *cancellable,
GError **error)
{
gchar **proxies;
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
proxies = g_new0 (gchar *, 2);
proxies[0] = g_strdup ("direct://");
return proxies;
}
static void
g_dummy_proxy_resolver_lookup_async (GProxyResolver *resolver,
const gchar *uri,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GError *error = NULL;
GSimpleAsyncResult *simple;
gchar **proxies;
proxies = g_dummy_proxy_resolver_lookup (resolver, uri, cancellable, &error);
simple = g_simple_async_result_new (G_OBJECT (resolver),
callback, user_data,
g_dummy_proxy_resolver_lookup_async);
if (proxies == NULL)
{
g_simple_async_result_set_from_error (simple, error);
g_error_free (error);
}
else
{
g_simple_async_result_set_op_res_gpointer (simple,
proxies,
NULL);
}
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
}
static gchar **
g_dummy_proxy_resolver_lookup_finish (GProxyResolver *resolver,
GAsyncResult *result,
GError **error)
{
if (G_IS_SIMPLE_ASYNC_RESULT (result))
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
if (g_simple_async_result_propagate_error (simple, error))
return NULL;
return g_simple_async_result_get_op_res_gpointer (simple);
}
return NULL;
}
static void
g_dummy_proxy_resolver_class_init (GDummyProxyResolverClass *resolver_class)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (resolver_class);
object_class->finalize = g_dummy_proxy_resolver_finalize;
}
static void
g_dummy_proxy_resolver_iface_init (GProxyResolverInterface *iface)
{
iface->is_supported = g_dummy_proxy_resolver_is_supported;
iface->lookup = g_dummy_proxy_resolver_lookup;
iface->lookup_async = g_dummy_proxy_resolver_lookup_async;
iface->lookup_finish = g_dummy_proxy_resolver_lookup_finish;
}

54
gio/gdummyproxyresolver.h Normal file
View File

@ -0,0 +1,54 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2010 Collabora, Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
*/
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
#error "Only <gio/gio.h> can be included directly."
#endif
#ifndef __G_DUMMY_PROXY_RESOLVER_H__
#define __G_DUMMY_PROXY_RESOLVER_H__
#include <gio/giotypes.h>
G_BEGIN_DECLS
#define G_TYPE_DUMMY_PROXY_RESOLVER (_g_dummy_proxy_resolver_get_type ())
#define G_DUMMY_PROXY_RESOLVER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_DUMMY_PROXY_RESOLVER, GDummyProxyResolver))
#define G_DUMMY_PROXY_RESOLVER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), G_TYPE_DUMMY_PROXY_RESOLVER, GDummyProxyResolverClass))
#define G_IS_DUMMY_PROXY_RESOLVER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_DUMMY_PROXY_RESOLVER))
#define G_IS_DUMMY_PROXY_RESOLVER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_DUMMY_PROXY_RESOLVER))
#define G_DUMMY_PROXY_RESOLVER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_DUMMY_PROXY_RESOLVER, GDummyProxyResolverClass))
typedef struct _GDummyProxyResolver GDummyProxyResolver;
typedef struct _GDummyProxyResolverClass GDummyProxyResolverClass;
struct _GDummyProxyResolverClass {
GObjectClass parent_class;
};
GType _g_dummy_proxy_resolver_get_type (void);
G_END_DECLS
#endif /* __G_DUMMY_PROXY_RESOLVER_H__ */

View File

@ -92,6 +92,7 @@
#include <gio/gnetworkservice.h>
#include <gio/goutputstream.h>
#include <gio/gpermission.h>
#include <gio/gproxyresolver.h>
#include <gio/gresolver.h>
#include <gio/gseekable.h>
#include <gio/gsettings.h>

View File

@ -1128,6 +1128,17 @@ g_socket_address_to_native
#endif
#endif
#if IN_HEADER(__G_PROXY_RESOLVER_H__)
#if IN_FILE(__G_PROXY_RESOLVER_C__)
g_proxy_resolver_get_default
g_proxy_resolver_get_type
g_proxy_resolver_is_supported
g_proxy_resolver_lookup
g_proxy_resolver_lookup_async
g_proxy_resolver_lookup_finish
#endif
#endif
#if IN_HEADER(__G_RESOLVER_H__)
#if IN_FILE(__G_RESOLVER_C__)
g_resolver_error_quark

View File

@ -30,6 +30,7 @@
#include "glocalfilemonitor.h"
#include "glocaldirectorymonitor.h"
#include "gnativevolumemonitor.h"
#include "gproxyresolver.h"
#include "gvfs.h"
#ifdef G_OS_UNIX
#include "gdesktopappinfo.h"
@ -467,6 +468,8 @@ extern GType _g_win32_volume_monitor_get_type (void);
extern GType g_win32_directory_monitor_get_type (void);
extern GType _g_winhttp_vfs_get_type (void);
extern GType _g_dummy_proxy_resolver_get_type (void);
#ifdef G_PLATFORM_WIN32
#include <windows.h>
@ -534,6 +537,9 @@ _g_io_modules_ensure_extension_points_registered (void)
ep = g_io_extension_point_register ("gsettings-backend");
g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
}
G_UNLOCK (registered_extensions);
@ -591,6 +597,7 @@ _g_io_modules_ensure_loaded (void)
_g_winhttp_vfs_get_type ();
#endif
_g_local_vfs_get_type ();
_g_dummy_proxy_resolver_get_type ();
}
G_UNLOCK (loaded_dirs);

View File

@ -198,6 +198,15 @@ typedef struct _GThreadedSocketService GThreadedSocketServi
typedef struct _GThemedIcon GThemedIcon;
typedef struct _GVfs GVfs; /* Dummy typedef */
/**
* GProxyResolver:
*
* A helper class to enumerate proxies base on URI.
*
* Since: 2.26
**/
typedef struct _GProxyResolver GProxyResolver;
/**
* GVolume:
*

241
gio/gproxyresolver.c Normal file
View File

@ -0,0 +1,241 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2010 Collabora, Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
*/
#include "config.h"
#include "gproxyresolver.h"
#include <glib.h>
#include "glibintl.h"
#include "gasyncresult.h"
#include "gcancellable.h"
#include "giomodule.h"
#include "giomodule-priv.h"
#include "gsimpleasyncresult.h"
/**
* SECTION:gproxyresolver
* @short_description: Asynchronous and cancellable network proxy resolver
* @include: gio/gio.h
*
* #GProxyResolver provides synchronous and asynchronous network proxy
* resolution. #GProxyResolver is used within #GClientSocket through
* the method g_socket_connectable_proxy_enumerate().
*/
G_DEFINE_INTERFACE (GProxyResolver, g_proxy_resolver, G_TYPE_OBJECT)
static void
g_proxy_resolver_default_init (GProxyResolverInterface *iface)
{
}
static gpointer
get_default_proxy_resolver (gpointer arg)
{
const gchar *use_this;
GProxyResolver *resolver;
GList *l;
GIOExtensionPoint *ep;
GIOExtension *extension;
use_this = g_getenv ("GIO_USE_PROXY_RESOLVER");
/* Ensure proxy-resolver modules loaded */
_g_io_modules_ensure_loaded ();
ep = g_io_extension_point_lookup (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
if (use_this)
{
extension = g_io_extension_point_get_extension_by_name (ep, use_this);
if (extension)
{
resolver = g_object_new (g_io_extension_get_type (extension), NULL);
if (g_proxy_resolver_is_supported (resolver))
return resolver;
g_object_unref (resolver);
}
}
for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
{
extension = l->data;
resolver = g_object_new (g_io_extension_get_type (extension), NULL);
if (g_proxy_resolver_is_supported (resolver))
return resolver;
g_object_unref (resolver);
}
return NULL;
}
/**
* g_proxy_resolver_get_default:
*
* Gets the default #GProxyResolver for the system.
*
* Return value: the default #GProxyResolver.
*
* Since: 2.26
*/
GProxyResolver *
g_proxy_resolver_get_default (void)
{
static GOnce once_init = G_ONCE_INIT;
return g_once (&once_init, get_default_proxy_resolver, NULL);
}
/**
* g_proxy_resolver_is_supported:
* @resolver: a #GProxyResolver
*
* Checks if @resolver can be used on this system. (This is used
* internally; g_proxy_resolver_get_default() will only return a proxy
* resolver that returns %TRUE for this method.)
*
* Return value: %TRUE if @resolver is supported.
*
* Since: 2.26
*/
gboolean
g_proxy_resolver_is_supported (GProxyResolver *resolver)
{
GProxyResolverInterface *iface;
g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), FALSE);
iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
return (* iface->is_supported) (resolver);
}
/**
* g_proxy_resolver_lookup:
* @resolver: a #GProxyResolver
* @uri: a URI representing the destination to connect to
* @cancellable: a #GCancellable, or %NULL
* @error: return location for a #GError, or %NULL
*
* Looks into the system proxy configuration to determine what proxy,
* if any, to use to connect to @uri. The returned proxy URIs are of the
* form <literal>&lt;protocol&gt;://[user[:password]@]host:port</literal>
* or <literal>direct://</literal>, where &lt;protocol&gt; could be
* http, rtsp, socks or other proxying protocol.
*
* If you don't know what network protocol is being used on the
* socket, you should use <literal>none</literal> as the URI protocol.
* In this case, the resolver might still return a generic proxy type
* (such as SOCKS), but would not return protocol-specific proxy types
* (such as http).
*
* <literal>direct://</literal> is used when no proxy is needed.
* Direct connection should not be attempted unless it is part of the
* returned array of proxies.
*
* Return value: A NULL-terminated array of proxy URIs. Must be freed with
* g_strfreev().
*
* Since: 2.26
*/
gchar **
g_proxy_resolver_lookup (GProxyResolver *resolver,
const gchar *uri,
GCancellable *cancellable,
GError **error)
{
GProxyResolverInterface *iface;
g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), NULL);
g_return_val_if_fail (uri != NULL, NULL);
iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
return (* iface->lookup) (resolver, uri, cancellable, error);
}
/**
* g_proxy_resolver_lookup_async:
* @resolver: a #GProxyResolver
* @uri: a URI representing the destination to connect to
* @cancellable: a #GCancellable, or %NULL
* @callback: callback to call after resolution completes
* @user_data: data for @callback
*
* Asynchronous lookup of proxy. See g_proxy_resolver_lookup() for more
* details.
*
* Since: 2.26
*/
void
g_proxy_resolver_lookup_async (GProxyResolver *resolver,
const gchar *uri,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GProxyResolverInterface *iface;
g_return_if_fail (G_IS_PROXY_RESOLVER (resolver));
g_return_if_fail (uri != NULL);
iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
(* iface->lookup_async) (resolver, uri, cancellable, callback, user_data);
}
/**
* g_proxy_resolver_lookup_finish:
* @resolver: a #GProxyResolver
* @result: the result passed to your #GAsyncReadyCallback
* @error: return location for a #GError, or %NULL
*
* Call this function to obtain the array of proxy URIs when
* g_proxy_resolver_lookup_async() is complete. See
* g_proxy_resolver_lookup() for more details.
*
* Return value: A NULL-terminated array of proxy URIs. Must be freed with
* g_strfreev().
*
* Since: 2.26
*/
gchar **
g_proxy_resolver_lookup_finish (GProxyResolver *resolver,
GAsyncResult *result,
GError **error)
{
GProxyResolverInterface *iface;
g_return_val_if_fail (G_IS_PROXY_RESOLVER (resolver), NULL);
iface = G_PROXY_RESOLVER_GET_IFACE (resolver);
return (* iface->lookup_finish) (resolver, result, error);
}

96
gio/gproxyresolver.h Normal file
View File

@ -0,0 +1,96 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2010 Collabora, Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
*/
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
#error "Only <gio/gio.h> can be included directly."
#endif
#ifndef __G_PROXY_RESOLVER_H__
#define __G_PROXY_RESOLVER_H__
#include <gio/giotypes.h>
G_BEGIN_DECLS
#define G_TYPE_PROXY_RESOLVER (g_proxy_resolver_get_type ())
#define G_PROXY_RESOLVER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_PROXY_RESOLVER, GProxyResolver))
#define G_IS_PROXY_RESOLVER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_PROXY_RESOLVER))
#define G_PROXY_RESOLVER_GET_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_PROXY_RESOLVER, GProxyResolverInterface))
/**
* G_PROXY_RESOLVER_EXTENSION_POINT_NAME:
*
* Extension point for proxy resolving functionality.
* See <link linkend="extending-gio">Extending GIO</link>.
*/
#define G_PROXY_RESOLVER_EXTENSION_POINT_NAME "gio-proxy-resolver"
/**
* GProxyResolver:
*
* Interface that can be used to resolve proxy address.
*/
typedef struct _GProxyResolverInterface GProxyResolverInterface;
struct _GProxyResolverInterface {
GTypeInterface g_iface;
/* Virtual Table */
gboolean (* is_supported) (GProxyResolver *resolver);
gchar ** (* lookup) (GProxyResolver *resolver,
const gchar *uri,
GCancellable *cancellable,
GError **error);
void (* lookup_async) (GProxyResolver *resolver,
const gchar *uri,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
gchar ** (* lookup_finish) (GProxyResolver *resolver,
GAsyncResult *result,
GError **error);
};
GType g_proxy_resolver_get_type (void) G_GNUC_CONST;
GProxyResolver *g_proxy_resolver_get_default (void);
gboolean g_proxy_resolver_is_supported (GProxyResolver *resolver);
gchar **g_proxy_resolver_lookup (GProxyResolver *resolver,
const gchar *uri,
GCancellable *cancellable,
GError **error);
void g_proxy_resolver_lookup_async (GProxyResolver *resolver,
const gchar *uri,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
gchar **g_proxy_resolver_lookup_finish (GProxyResolver *resolver,
GAsyncResult *result,
GError **error);
G_END_DECLS
#endif /* __G_PROXY_RESOLVER_H__ */