| 1 |
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* |
| 3 |
* GIO - GLib Input, Output and Streaming Library |
| 4 |
* |
| 5 |
* Copyright © 2022 Red Hat Inc. |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General |
| 18 |
* Public License along with this library; if not, see |
| 19 |
* <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include "genvironmentproxyresolver.h" |
| 25 |
|
| 26 |
#include <glib.h> |
| 27 |
#include <glib/gi18n-lib.h> |
| 28 |
|
| 29 |
struct _GEnvironmentProxyResolver { |
| 30 |
GObject parent_instance; |
| 31 |
|
| 32 |
GProxyResolver *base_resolver; |
| 33 |
}; |
| 34 |
|
| 35 |
static void g_environment_proxy_resolver_iface_init (GProxyResolverInterface *iface); |
| 36 |
|
| 37 |
#ifdef GENVIRONMENTPROXY_MODULE |
| 38 |
static void |
| 39 |
g_environment_proxy_resolver_class_finalize (GEnvironmentProxyResolverClass *klass) |
| 40 |
{ |
| 41 |
} |
| 42 |
|
| 43 |
G_DEFINE_DYNAMIC_TYPE_EXTENDED (GEnvironmentProxyResolver, |
| 44 |
g_environment_proxy_resolver, |
| 45 |
G_TYPE_OBJECT, G_TYPE_FLAG_FINAL, |
| 46 |
G_IMPLEMENT_INTERFACE_DYNAMIC (G_TYPE_PROXY_RESOLVER, |
| 47 |
g_environment_proxy_resolver_iface_init)) |
| 48 |
#else |
| 49 |
G_DEFINE_FINAL_TYPE_WITH_CODE (GEnvironmentProxyResolver, |
| 50 |
g_environment_proxy_resolver, |
| 51 |
G_TYPE_OBJECT, |
| 52 |
G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER, |
| 53 |
g_environment_proxy_resolver_iface_init)) |
| 54 |
#endif |
| 55 |
|
| 56 |
static gboolean |
| 57 |
g_environment_proxy_resolver_is_supported (GProxyResolver *object) |
| 58 |
{ |
| 59 |
return (g_getenv ("ftp_proxy") || g_getenv ("FTP_PROXY") || |
| 60 |
g_getenv ("https_proxy") || g_getenv ("HTTPS_PROXY") || |
| 61 |
g_getenv ("http_proxy") || g_getenv ("HTTP_PROXY") || |
| 62 |
g_getenv ("no_proxy") || g_getenv ("NO_PROXY")); |
| 63 |
} |
| 64 |
|
| 65 |
static GProxyResolver * |
| 66 |
get_base_resolver (GProxyResolver *resolver) |
| 67 |
{ |
| 68 |
return G_PROXY_RESOLVER (G_ENVIRONMENT_PROXY_RESOLVER (resolver)->base_resolver); |
| 69 |
} |
| 70 |
|
| 71 |
static gchar ** |
| 72 |
g_environment_proxy_resolver_lookup (GProxyResolver *resolver, |
| 73 |
const gchar *uri, |
| 74 |
GCancellable *cancellable, |
| 75 |
GError **error) |
| 76 |
{ |
| 77 |
return g_proxy_resolver_lookup (get_base_resolver (resolver), uri, cancellable, error); |
| 78 |
} |
| 79 |
|
| 80 |
static void |
| 81 |
g_environment_proxy_resolver_lookup_async (GProxyResolver *resolver, |
| 82 |
const gchar *uri, |
| 83 |
GCancellable *cancellable, |
| 84 |
GAsyncReadyCallback callback, |
| 85 |
gpointer user_data) |
| 86 |
{ |
| 87 |
g_proxy_resolver_lookup_async (get_base_resolver (resolver), uri, cancellable, callback, user_data); |
| 88 |
} |
| 89 |
|
| 90 |
static gchar ** |
| 91 |
g_environment_proxy_resolver_lookup_finish (GProxyResolver *resolver, |
| 92 |
GAsyncResult *result, |
| 93 |
GError **error) |
| 94 |
{ |
| 95 |
return g_proxy_resolver_lookup_finish (resolver, result, error); |
| 96 |
} |
| 97 |
|
| 98 |
static void |
| 99 |
g_environment_proxy_resolver_finalize (GObject *object) |
| 100 |
{ |
| 101 |
GEnvironmentProxyResolver *resolver = G_ENVIRONMENT_PROXY_RESOLVER (object); |
| 102 |
|
| 103 |
g_object_unref (resolver->base_resolver); |
| 104 |
|
| 105 |
G_OBJECT_CLASS (g_environment_proxy_resolver_parent_class)->finalize (object); |
| 106 |
} |
| 107 |
|
| 108 |
static const char * |
| 109 |
validate_proxy_envvar (const char *var) |
| 110 |
{ |
| 111 |
const char *url; |
| 112 |
GError *error = NULL; |
| 113 |
|
| 114 |
if ((url = g_getenv (var))) |
| 115 |
{ |
| 116 |
/* Empty strings mean no proxy. */ |
| 117 |
if (*url == '\0') |
| 118 |
return NULL; |
| 119 |
|
| 120 |
if (g_uri_is_valid (url, G_URI_FLAGS_NONE, &error)) |
| 121 |
return url; |
| 122 |
|
| 123 |
g_warning ("Environment variable %s specifies invalid proxy URL %s: %s", var, url, error->message); |
| 124 |
g_error_free (error); |
| 125 |
} |
| 126 |
|
| 127 |
return NULL; |
| 128 |
} |
| 129 |
|
| 130 |
static void |
| 131 |
g_environment_proxy_resolver_init (GEnvironmentProxyResolver *resolver) |
| 132 |
{ |
| 133 |
char **ignore_hosts = NULL; |
| 134 |
const char *default_proxy = NULL; |
| 135 |
const char *url; |
| 136 |
|
| 137 |
if (g_getenv ("no_proxy")) |
| 138 |
ignore_hosts = g_strsplit (g_getenv ("no_proxy"), ",", -1); |
| 139 |
else if (g_getenv ("NO_PROXY")) |
| 140 |
ignore_hosts = g_strsplit (g_getenv ("NO_PROXY"), ",", -1); |
| 141 |
|
| 142 |
/* The http_proxy/HTTP_PROXY is used for *all* protocols (except FTP or HTTPS, |
| 143 |
* if more specific environment variables are set). It is not just for HTTP. |
| 144 |
* This matches the behavior of libproxy's environment variable module, or |
| 145 |
* GNOME's use-same-proxy setting. |
| 146 |
*/ |
| 147 |
if ((url = validate_proxy_envvar ("http_proxy"))) |
| 148 |
default_proxy = url; |
| 149 |
else if ((url = validate_proxy_envvar ("HTTP_PROXY"))) |
| 150 |
default_proxy = url; |
| 151 |
|
| 152 |
resolver->base_resolver = g_simple_proxy_resolver_new (default_proxy, ignore_hosts); |
| 153 |
g_strfreev (ignore_hosts); |
| 154 |
|
| 155 |
if ((url = validate_proxy_envvar ("ftp_proxy"))) |
| 156 |
g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver->base_resolver), "ftp", url); |
| 157 |
else if ((url = validate_proxy_envvar ("FTP_PROXY"))) |
| 158 |
g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver->base_resolver), "ftp", url); |
| 159 |
|
| 160 |
if ((url = validate_proxy_envvar ("https_proxy"))) |
| 161 |
g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver->base_resolver), "https", url); |
| 162 |
else if ((url = validate_proxy_envvar ("HTTPS_PROXY"))) |
| 163 |
g_simple_proxy_resolver_set_uri_proxy (G_SIMPLE_PROXY_RESOLVER (resolver->base_resolver), "https", url); |
| 164 |
} |
| 165 |
|
| 166 |
static void |
| 167 |
g_environment_proxy_resolver_class_init (GEnvironmentProxyResolverClass *resolver_class) |
| 168 |
{ |
| 169 |
GObjectClass *object_class; |
| 170 |
|
| 171 |
object_class = G_OBJECT_CLASS (resolver_class); |
| 172 |
object_class->finalize = g_environment_proxy_resolver_finalize; |
| 173 |
} |
| 174 |
|
| 175 |
static void |
| 176 |
g_environment_proxy_resolver_iface_init (GProxyResolverInterface *iface) |
| 177 |
{ |
| 178 |
iface->is_supported = g_environment_proxy_resolver_is_supported; |
| 179 |
iface->lookup = g_environment_proxy_resolver_lookup; |
| 180 |
iface->lookup_async = g_environment_proxy_resolver_lookup_async; |
| 181 |
iface->lookup_finish = g_environment_proxy_resolver_lookup_finish; |
| 182 |
} |
| 183 |
|
| 184 |
#ifdef GENVIRONMENTPROXY_MODULE |
| 185 |
void |
| 186 |
g_environment_proxy_resolver_register (GIOModule *module) |
| 187 |
{ |
| 188 |
g_environment_proxy_resolver_register_type (G_TYPE_MODULE (module)); |
| 189 |
if (!module) |
| 190 |
g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME); |
| 191 |
g_io_extension_point_implement (G_PROXY_RESOLVER_EXTENSION_POINT_NAME, |
| 192 |
g_environment_proxy_resolver_get_type(), |
| 193 |
"environment", |
| 194 |
0); |
| 195 |
} |
| 196 |
#endif |