summaryrefslogtreecommitdiff
path: root/libsoup/soup-proxy-resolver-wrapper.c
blob: e07664ca9d222e92c302b114580b02db3f70e111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * soup-proxy-resolver-wrapper.c: SoupProxyURIResolver -> GProxyResolver wrapper
 *
 * Copyright 2013 Red Hat, Inc.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "soup-proxy-resolver-wrapper.h"
#include "soup.h"

static void soup_proxy_resolver_wrapper_interface_init (GProxyResolverInterface *proxy_resolver_interface);

G_DEFINE_TYPE_WITH_CODE (SoupProxyResolverWrapper, soup_proxy_resolver_wrapper, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER, soup_proxy_resolver_wrapper_interface_init);
			 )

static void
soup_proxy_resolver_wrapper_init (SoupProxyResolverWrapper *resolver_wrapper)
{
}

static void
soup_proxy_resolver_wrapper_finalize (GObject *object)
{
	SoupProxyResolverWrapper *wrapper =
		SOUP_PROXY_RESOLVER_WRAPPER (object);

	g_clear_object (&wrapper->soup_resolver);

	G_OBJECT_CLASS (soup_proxy_resolver_wrapper_parent_class)->finalize (object);
}

static char **
convert_response (SoupURI *source_uri, guint status,
		  SoupURI *proxy_uri, GError **error)
{
	char **proxies = NULL;

	if (status == SOUP_STATUS_CANT_RESOLVE_PROXY) {
		g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
			     "%s (%s)", soup_status_get_phrase (status),
			     source_uri->host);
	} else if (status == SOUP_STATUS_CANT_CONNECT_PROXY) {
		g_set_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED,
			     "%s (%s)", soup_status_get_phrase (status),
			     source_uri->host);
	} else {
		g_return_val_if_fail (status == SOUP_STATUS_OK, NULL);

		proxies = g_new (char *, 2);
		proxies[0] = soup_uri_to_string (proxy_uri, FALSE);
		proxies[1] = NULL;

		soup_uri_free (proxy_uri);
	}

	return proxies;
}

static void
wrapper_lookup_async_complete (SoupProxyURIResolver *resolver,
			       guint status, SoupURI *proxy_uri,
			       gpointer user_data)
{
	GTask *task = user_data;
	SoupURI *source_uri = g_task_get_task_data (task);
	char **proxies;
	GError *error = NULL;

	proxies = convert_response (source_uri, status, proxy_uri, &error);
	if (error)
		g_task_return_error (task, error);
	else
		g_task_return_pointer (task, proxies, (GDestroyNotify) g_strfreev);
	g_object_unref (task);
}

static void
soup_proxy_resolver_wrapper_lookup_async (GProxyResolver       *resolver,
					  const gchar          *uri,
					  GCancellable         *cancellable,
					  GAsyncReadyCallback   callback,
					  gpointer              user_data)
{
	SoupProxyResolverWrapper *wrapper =
		SOUP_PROXY_RESOLVER_WRAPPER (resolver);
	GTask *task;
	SoupURI *source_uri;

	task = g_task_new (resolver, cancellable, callback, user_data);
	source_uri = soup_uri_new (uri);
	g_task_set_task_data (task, source_uri, (GDestroyNotify) soup_uri_free);

	G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
	soup_proxy_uri_resolver_get_proxy_uri_async (wrapper->soup_resolver,
						     source_uri,
						     g_main_context_get_thread_default (),
						     cancellable,
						     wrapper_lookup_async_complete,
						     task);
	G_GNUC_END_IGNORE_DEPRECATIONS;
}

static char **
soup_proxy_resolver_wrapper_lookup_finish (GProxyResolver       *resolver,
					   GAsyncResult         *result,
					   GError              **error)
{
	return g_task_propagate_pointer (G_TASK (result), error);
}

static gchar **
soup_proxy_resolver_wrapper_lookup (GProxyResolver  *resolver,
				    const gchar     *uri,
				    GCancellable    *cancellable,
				    GError         **error)
{
	SoupProxyResolverWrapper *wrapper =
		SOUP_PROXY_RESOLVER_WRAPPER (resolver);
	SoupURI *source_uri, *proxy_uri;
	guint status;
	gchar **proxies;

	source_uri = soup_uri_new (uri);
	G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
	status = soup_proxy_uri_resolver_get_proxy_uri_sync (wrapper->soup_resolver,
							     source_uri,
							     cancellable,
							     &proxy_uri);
	G_GNUC_END_IGNORE_DEPRECATIONS;
	proxies = convert_response (source_uri, status, proxy_uri, error);
	soup_uri_free (source_uri);
	return proxies;
}

static void
soup_proxy_resolver_wrapper_class_init (SoupProxyResolverWrapperClass *static_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (static_class);

	object_class->finalize = soup_proxy_resolver_wrapper_finalize;
}

static void
soup_proxy_resolver_wrapper_interface_init (GProxyResolverInterface *proxy_resolver_interface)
{
	proxy_resolver_interface->lookup =
		soup_proxy_resolver_wrapper_lookup;
	proxy_resolver_interface->lookup_async =
		soup_proxy_resolver_wrapper_lookup_async;
	proxy_resolver_interface->lookup_finish =
		soup_proxy_resolver_wrapper_lookup_finish;
}

GProxyResolver *
soup_proxy_resolver_wrapper_new (SoupProxyURIResolver *soup_resolver)
{
	SoupProxyResolverWrapper *wrapper;

	wrapper = g_object_new (SOUP_TYPE_PROXY_RESOLVER_WRAPPER, NULL);
	wrapper->soup_resolver = g_object_ref (soup_resolver);
	return (GProxyResolver *)wrapper;
}