| 1 |
/* config-env.c |
| 2 |
* |
| 3 |
* Copyright 2022-2023 The Libproxy Team |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <gio/gio.h> |
| 23 |
|
| 24 |
#include "config-env.h" |
| 25 |
|
| 26 |
#include "px-manager.h" |
| 27 |
#include "px-plugin-config.h" |
| 28 |
|
| 29 |
static void px_config_iface_init (PxConfigInterface *iface); |
| 30 |
|
| 31 |
struct _PxConfigEnv { |
| 32 |
GObject parent_instance; |
| 33 |
}; |
| 34 |
|
| 35 |
G_DEFINE_FINAL_TYPE_WITH_CODE (PxConfigEnv, |
| 36 |
px_config_env, |
| 37 |
G_TYPE_OBJECT, |
| 38 |
G_IMPLEMENT_INTERFACE (PX_TYPE_CONFIG, px_config_iface_init)) |
| 39 |
|
| 40 |
enum { |
| 41 |
PROP_0, |
| 42 |
PROP_CONFIG_OPTION |
| 43 |
}; |
| 44 |
|
| 45 |
static void |
| 46 |
px_config_env_init (PxConfigEnv *self) |
| 47 |
{ |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
px_config_env_set_property (GObject *object, |
| 52 |
guint prop_id, |
| 53 |
const GValue *value, |
| 54 |
GParamSpec *pspec) |
| 55 |
{ |
| 56 |
switch (prop_id) { |
| 57 |
case PROP_CONFIG_OPTION: |
| 58 |
break; |
| 59 |
|
| 60 |
default: |
| 61 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 62 |
break; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static void |
| 67 |
px_config_env_get_property (GObject *object, |
| 68 |
guint prop_id, |
| 69 |
GValue *value, |
| 70 |
GParamSpec *pspec) |
| 71 |
{ |
| 72 |
switch (prop_id) { |
| 73 |
case PROP_CONFIG_OPTION: |
| 74 |
break; |
| 75 |
|
| 76 |
default: |
| 77 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 78 |
break; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
static void |
| 83 |
px_config_env_class_init (PxConfigEnvClass *klass) |
| 84 |
{ |
| 85 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 86 |
|
| 87 |
object_class->set_property = px_config_env_set_property; |
| 88 |
object_class->get_property = px_config_env_get_property; |
| 89 |
|
| 90 |
g_object_class_override_property (object_class, PROP_CONFIG_OPTION, "config-option"); |
| 91 |
} |
| 92 |
|
| 93 |
static void |
| 94 |
px_config_env_get_config (PxConfig *config, |
| 95 |
GUri *uri, |
| 96 |
GStrvBuilder *builder) |
| 97 |
{ |
| 98 |
const char *proxy = NULL; |
| 99 |
const char *scheme = g_uri_get_scheme (uri); |
| 100 |
|
| 101 |
proxy = g_getenv ("no_proxy"); |
| 102 |
if (!proxy) |
| 103 |
proxy = g_getenv ("NO_PROXY"); |
| 104 |
|
| 105 |
if (proxy) { |
| 106 |
g_auto (GStrv) no_proxy = g_strsplit (proxy, ",", -1); |
| 107 |
|
| 108 |
if (px_manager_is_ignore (uri, no_proxy)) |
| 109 |
return; |
| 110 |
|
| 111 |
proxy = NULL; |
| 112 |
} |
| 113 |
|
| 114 |
if (g_strcmp0 (scheme, "ftp") == 0) { |
| 115 |
proxy = g_getenv ("ftp_proxy"); |
| 116 |
if (!proxy) |
| 117 |
proxy = g_getenv ("FTP_PROXY"); |
| 118 |
} else if (g_strcmp0 (scheme, "https") == 0) { |
| 119 |
proxy = g_getenv ("https_proxy"); |
| 120 |
if (!proxy) |
| 121 |
proxy = g_getenv ("HTTPS_PROXY"); |
| 122 |
} |
| 123 |
|
| 124 |
if (!proxy) |
| 125 |
proxy = g_getenv ("http_proxy"); |
| 126 |
|
| 127 |
if (!proxy) |
| 128 |
proxy = g_getenv ("HTTP_PROXY"); |
| 129 |
|
| 130 |
if (proxy) |
| 131 |
px_strv_builder_add_proxy (builder, proxy); |
| 132 |
} |
| 133 |
|
| 134 |
static void |
| 135 |
px_config_iface_init (PxConfigInterface *iface) |
| 136 |
{ |
| 137 |
iface->name = "config-env"; |
| 138 |
iface->priority = PX_CONFIG_PRIORITY_FIRST; |
| 139 |
iface->get_config = px_config_env_get_config; |
| 140 |
} |