GProxyAddressEnumerator: add default-port property

Although none of the in-tree GSocketConnectable types need it, other
types (like SoupAddress) may find it useful to be able to pass a URI
and a default-port to GProxyAddressEnumerator separately (the same way
you can with GNetworkAddress). So add a default-port property.

https://bugzilla.gnome.org/show_bug.cgi?id=698877
This commit is contained in:
Dan Winship 2013-05-05 16:50:43 -04:00
parent 6104230bce
commit f4a1882341
2 changed files with 190 additions and 88 deletions

View File

@ -47,6 +47,7 @@ enum
{
PROP_0,
PROP_URI,
PROP_DEFAULT_PORT,
PROP_CONNECTABLE,
PROP_PROXY_RESOLVER
};
@ -55,23 +56,24 @@ struct _GProxyAddressEnumeratorPrivate
{
/* Destination address */
GSocketConnectable *connectable;
gchar *dest_uri;
gchar *dest_hostname;
guint16 dest_port;
gchar *dest_uri;
guint16 default_port;
gchar *dest_hostname;
guint16 dest_port;
GList *dest_ips;
/* Proxy enumeration */
GProxyResolver *proxy_resolver;
gchar **proxies;
gchar **next_proxy;
gchar **proxies;
gchar **next_proxy;
GSocketAddressEnumerator *addr_enum;
GSocketAddress *proxy_address;
const gchar *proxy_uri;
gchar *proxy_type;
gchar *proxy_username;
gchar *proxy_password;
gchar *proxy_type;
gchar *proxy_username;
gchar *proxy_password;
gboolean supports_hostname;
GList *next_dest_ip;
GList *next_dest_ip;
GError *last_error;
};
@ -569,95 +571,95 @@ g_proxy_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator,
return g_task_propagate_pointer (G_TASK (result), error);
}
static void
g_proxy_address_enumerator_constructed (GObject *object)
{
GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (object);
GSocketConnectable *conn;
guint port;
if (priv->dest_uri)
{
conn = g_network_address_parse_uri (priv->dest_uri, priv->default_port, NULL);
if (conn)
{
g_object_get (conn,
"hostname", &priv->dest_hostname,
"port", &port,
NULL);
priv->dest_port = port;
g_object_unref (conn);
}
else
g_warning ("Invalid URI '%s'", priv->dest_uri);
}
G_OBJECT_CLASS (g_proxy_address_enumerator_parent_class)->constructed (object);
}
static void
g_proxy_address_enumerator_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (object);
switch (property_id)
{
case PROP_URI:
g_value_set_string (value, priv->dest_uri);
break;
case PROP_URI:
g_value_set_string (value, priv->dest_uri);
break;
case PROP_CONNECTABLE:
g_value_set_object (value, priv->connectable);
break;
case PROP_DEFAULT_PORT:
g_value_set_uint (value, priv->default_port);
break;
case PROP_PROXY_RESOLVER:
g_value_set_object (value, priv->proxy_resolver);
break;
case PROP_CONNECTABLE:
g_value_set_object (value, priv->connectable);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
case PROP_PROXY_RESOLVER:
g_value_set_object (value, priv->proxy_resolver);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
g_proxy_address_enumerator_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (object);
switch (property_id)
{
case PROP_URI:
{
const gchar *uri;
case PROP_URI:
priv->dest_uri = g_value_dup_string (value);
break;
g_free (priv->dest_hostname);
priv->dest_hostname = NULL;
priv->dest_port = 0;
case PROP_DEFAULT_PORT:
priv->default_port = g_value_get_uint (value);
break;
g_free (priv->dest_uri);
priv->dest_uri = NULL;
case PROP_CONNECTABLE:
priv->connectable = g_value_dup_object (value);
break;
uri = g_value_get_string (value);
case PROP_PROXY_RESOLVER:
if (priv->proxy_resolver)
g_object_unref (priv->proxy_resolver);
priv->proxy_resolver = g_value_get_object (value);
if (!priv->proxy_resolver)
priv->proxy_resolver = g_proxy_resolver_get_default ();
g_object_ref (priv->proxy_resolver);
break;
if (uri)
{
GSocketConnectable *conn;
conn = g_network_address_parse_uri (uri, 0, NULL);
if (conn)
{
guint port;
priv->dest_uri = g_strdup (uri);
g_object_get (conn,
"hostname", &priv->dest_hostname,
"port", &port,
NULL);
priv->dest_port = port;
g_object_unref (conn);
}
else
g_warning ("Invalid URI '%s'", uri);
}
break;
}
case PROP_CONNECTABLE:
priv->connectable = g_value_dup_object (value);
break;
case PROP_PROXY_RESOLVER:
if (priv->proxy_resolver)
g_object_unref (priv->proxy_resolver);
priv->proxy_resolver = g_value_get_object (value);
if (!priv->proxy_resolver)
priv->proxy_resolver = g_proxy_resolver_get_default ();
g_object_ref (priv->proxy_resolver);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
@ -709,6 +711,7 @@ g_proxy_address_enumerator_class_init (GProxyAddressEnumeratorClass *proxy_enume
g_type_class_add_private (enumerator_class,
sizeof (GProxyAddressEnumeratorPrivate));
object_class->constructed = g_proxy_address_enumerator_constructed;
object_class->set_property = g_proxy_address_enumerator_set_property;
object_class->get_property = g_proxy_address_enumerator_get_property;
object_class->finalize = g_proxy_address_enumerator_finalize;
@ -727,6 +730,24 @@ g_proxy_address_enumerator_class_init (GProxyAddressEnumeratorClass *proxy_enume
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
/**
* GProxyAddressEnumerator:default-port:
*
* The default port to use if #GProxyAddressEnumerator:uri does not
* specify one.
*
* Since: 2.38
*/
g_object_class_install_property (object_class,
PROP_DEFAULT_PORT,
g_param_spec_uint ("default-port",
P_("Default port"),
P_("The default port to use if uri does not specify one"),
0, 65535, 0,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class,
PROP_CONNECTABLE,
g_param_spec_object ("connectable",

View File

@ -734,14 +734,17 @@ g_fake_resolver_lookup_by_name (GResolver *resolver,
GCancellable *cancellable,
GError **error)
{
/* This is only ever called with lookups that are expected to
* fail.
*/
g_set_error (error,
G_RESOLVER_ERROR,
G_RESOLVER_ERROR_NOT_FOUND,
"Not found");
return NULL;
if (!strcmp (hostname, "example.com"))
return g_list_prepend (NULL, g_inet_address_new_from_string ("127.0.0.1"));
else
{
/* Anything else is expected to fail. */
g_set_error (error,
G_RESOLVER_ERROR,
G_RESOLVER_ERROR_NOT_FOUND,
"Not found");
return NULL;
}
}
static void
@ -751,10 +754,24 @@ g_fake_resolver_lookup_by_name_async (GResolver *resolver,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_task_report_new_error (resolver, callback, user_data,
g_fake_resolver_lookup_by_name_async,
G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
"Not found");
GTask *task;
task = g_task_new (resolver, cancellable, callback, user_data);
if (!strcmp (hostname, "example.com"))
{
GList *result;
result = g_list_prepend (NULL, g_inet_address_new_from_string ("127.0.0.1"));
g_task_return_pointer (task, result, (GDestroyNotify) g_resolver_free_addresses);
}
else
{
g_task_return_new_error (task,
G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
"Not found");
}
g_object_unref (task);
}
static GList *
@ -1236,6 +1253,69 @@ test_override (gpointer fixture,
g_object_unref (alt_resolver);
}
static void
assert_destination_port (GSocketAddressEnumerator *etor,
guint16 port)
{
GSocketAddress *addr;
GProxyAddress *paddr;
GError *error = NULL;
while ((addr = g_socket_address_enumerator_next (etor, NULL, &error)))
{
g_assert_no_error (error);
g_assert (G_IS_PROXY_ADDRESS (addr));
paddr = G_PROXY_ADDRESS (addr);
g_assert_cmpint (g_proxy_address_get_destination_port (paddr), ==, port);
g_object_unref (addr);
}
g_assert_no_error (error);
}
static void
test_proxy_enumerator_ports (void)
{
GSocketAddressEnumerator *etor;
etor = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
"uri", "http://example.com/",
NULL);
assert_destination_port (etor, 0);
g_object_unref (etor);
/* Have to call this to clear last_proxies so the next call to
* g_test_proxy_resolver_lookup() won't assert.
*/
teardown_test (NULL, NULL);
etor = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
"uri", "http://example.com:8080/",
NULL);
assert_destination_port (etor, 8080);
g_object_unref (etor);
teardown_test (NULL, NULL);
etor = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
"uri", "http://example.com/",
"default-port", 80,
NULL);
assert_destination_port (etor, 80);
g_object_unref (etor);
teardown_test (NULL, NULL);
etor = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
"uri", "http://example.com:8080/",
"default-port", 80,
NULL);
assert_destination_port (etor, 8080);
g_object_unref (etor);
teardown_test (NULL, NULL);
}
int
main (int argc,
char *argv[])
@ -1276,6 +1356,7 @@ main (int argc,
g_test_add_vtable ("/proxy/multiple_async", 0, NULL, setup_test, test_multiple_async, teardown_test);
g_test_add_vtable ("/proxy/dns", 0, NULL, setup_test, test_dns, teardown_test);
g_test_add_vtable ("/proxy/override", 0, NULL, setup_test, test_override, teardown_test);
g_test_add_func ("/proxy/enumerator-ports", test_proxy_enumerator_ports);
result = g_test_run();