1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * soup-proxy-resolver-wrapper.c: SoupProxyURIResolver -> GProxyResolver wrapper
4 *
5 * Copyright 2013 Red Hat, Inc.
6 */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include "soup-proxy-resolver-wrapper.h"
13 #include "soup.h"
14
15 static void soup_proxy_resolver_wrapper_interface_init (GProxyResolverInterface *proxy_resolver_interface);
16
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);)17 G_DEFINE_TYPE_WITH_CODE (SoupProxyResolverWrapper, soup_proxy_resolver_wrapper, G_TYPE_OBJECT,
18 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER, soup_proxy_resolver_wrapper_interface_init);
19 )
20
21 static void
22 soup_proxy_resolver_wrapper_init (SoupProxyResolverWrapper *resolver_wrapper)
23 {
24 }
25
26 static void
soup_proxy_resolver_wrapper_finalize(GObject * object)27 soup_proxy_resolver_wrapper_finalize (GObject *object)
28 {
29 SoupProxyResolverWrapper *wrapper =
30 SOUP_PROXY_RESOLVER_WRAPPER (object);
31
32 g_clear_object (&wrapper->soup_resolver);
33
34 G_OBJECT_CLASS (soup_proxy_resolver_wrapper_parent_class)->finalize (object);
35 }
36
37 static char **
convert_response(SoupURI * source_uri,guint status,SoupURI * proxy_uri,GError ** error)38 convert_response (SoupURI *source_uri, guint status,
39 SoupURI *proxy_uri, GError **error)
40 {
41 char **proxies = NULL;
42
43 if (status == SOUP_STATUS_CANT_RESOLVE_PROXY) {
44 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
45 "%s (%s)", soup_status_get_phrase (status),
46 source_uri->host);
47 } else if (status == SOUP_STATUS_CANT_CONNECT_PROXY) {
48 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED,
49 "%s (%s)", soup_status_get_phrase (status),
50 source_uri->host);
51 } else {
52 g_return_val_if_fail (status == SOUP_STATUS_OK, NULL);
53
54 proxies = g_new (char *, 2);
55 proxies[0] = soup_uri_to_string (proxy_uri, FALSE);
56 proxies[1] = NULL;
57
58 soup_uri_free (proxy_uri);
59 }
60
61 return proxies;
62 }
63
64 static void
wrapper_lookup_async_complete(SoupProxyURIResolver * resolver,guint status,SoupURI * proxy_uri,gpointer user_data)65 wrapper_lookup_async_complete (SoupProxyURIResolver *resolver,
66 guint status, SoupURI *proxy_uri,
67 gpointer user_data)
68 {
69 GTask *task = user_data;
70 SoupURI *source_uri = g_task_get_task_data (task);
71 char **proxies;
72 GError *error = NULL;
73
74 proxies = convert_response (source_uri, status, proxy_uri, &error);
75 if (error)
76 g_task_return_error (task, error);
77 else
78 g_task_return_pointer (task, proxies, (GDestroyNotify) g_strfreev);
79 g_object_unref (task);
80 }
81
82 static void
soup_proxy_resolver_wrapper_lookup_async(GProxyResolver * resolver,const gchar * uri,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)83 soup_proxy_resolver_wrapper_lookup_async (GProxyResolver *resolver,
84 const gchar *uri,
85 GCancellable *cancellable,
86 GAsyncReadyCallback callback,
87 gpointer user_data)
88 {
89 SoupProxyResolverWrapper *wrapper =
90 SOUP_PROXY_RESOLVER_WRAPPER (resolver);
91 GTask *task;
92 SoupURI *source_uri;
93
94 task = g_task_new (resolver, cancellable, callback, user_data);
95 source_uri = soup_uri_new (uri);
96 g_task_set_task_data (task, source_uri, (GDestroyNotify) soup_uri_free);
97
98 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
99 soup_proxy_uri_resolver_get_proxy_uri_async (wrapper->soup_resolver,
100 source_uri,
101 g_main_context_get_thread_default (),
102 cancellable,
103 wrapper_lookup_async_complete,
104 task);
105 G_GNUC_END_IGNORE_DEPRECATIONS;
106 }
107
108 static char **
soup_proxy_resolver_wrapper_lookup_finish(GProxyResolver * resolver,GAsyncResult * result,GError ** error)109 soup_proxy_resolver_wrapper_lookup_finish (GProxyResolver *resolver,
110 GAsyncResult *result,
111 GError **error)
112 {
113 return g_task_propagate_pointer (G_TASK (result), error);
114 }
115
116 static gchar **
soup_proxy_resolver_wrapper_lookup(GProxyResolver * resolver,const gchar * uri,GCancellable * cancellable,GError ** error)117 soup_proxy_resolver_wrapper_lookup (GProxyResolver *resolver,
118 const gchar *uri,
119 GCancellable *cancellable,
120 GError **error)
121 {
122 SoupProxyResolverWrapper *wrapper =
123 SOUP_PROXY_RESOLVER_WRAPPER (resolver);
124 SoupURI *source_uri, *proxy_uri;
125 guint status;
126 gchar **proxies;
127
128 source_uri = soup_uri_new (uri);
129 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
130 status = soup_proxy_uri_resolver_get_proxy_uri_sync (wrapper->soup_resolver,
131 source_uri,
132 cancellable,
133 &proxy_uri);
134 G_GNUC_END_IGNORE_DEPRECATIONS;
135 proxies = convert_response (source_uri, status, proxy_uri, error);
136 soup_uri_free (source_uri);
137 return proxies;
138 }
139
140 static void
soup_proxy_resolver_wrapper_class_init(SoupProxyResolverWrapperClass * static_class)141 soup_proxy_resolver_wrapper_class_init (SoupProxyResolverWrapperClass *static_class)
142 {
143 GObjectClass *object_class = G_OBJECT_CLASS (static_class);
144
145 object_class->finalize = soup_proxy_resolver_wrapper_finalize;
146 }
147
148 static void
soup_proxy_resolver_wrapper_interface_init(GProxyResolverInterface * proxy_resolver_interface)149 soup_proxy_resolver_wrapper_interface_init (GProxyResolverInterface *proxy_resolver_interface)
150 {
151 proxy_resolver_interface->lookup =
152 soup_proxy_resolver_wrapper_lookup;
153 proxy_resolver_interface->lookup_async =
154 soup_proxy_resolver_wrapper_lookup_async;
155 proxy_resolver_interface->lookup_finish =
156 soup_proxy_resolver_wrapper_lookup_finish;
157 }
158
159 GProxyResolver *
soup_proxy_resolver_wrapper_new(SoupProxyURIResolver * soup_resolver)160 soup_proxy_resolver_wrapper_new (SoupProxyURIResolver *soup_resolver)
161 {
162 SoupProxyResolverWrapper *wrapper;
163
164 wrapper = g_object_new (SOUP_TYPE_PROXY_RESOLVER_WRAPPER, NULL);
165 wrapper->soup_resolver = g_object_ref (soup_resolver);
166 return (GProxyResolver *)wrapper;
167 }
168