summaryrefslogtreecommitdiff
path: root/src/backend/plugins/config-env/config-env.c
blob: 6a326e09c744639694182590d27fd022561d96e9 (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
/* config-env.c
 *
 * Copyright 2022-2023 The Libproxy Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <gio/gio.h>

#include "config-env.h"

#include "px-manager.h"
#include "px-plugin-config.h"

static void px_config_iface_init (PxConfigInterface *iface);

struct _PxConfigEnv {
  GObject parent_instance;

  GStrv no_proxy;
  const char *ftp_proxy;
  const char *http_proxy;
  const char *https_proxy;
};

G_DEFINE_FINAL_TYPE_WITH_CODE (PxConfigEnv,
                               px_config_env,
                               G_TYPE_OBJECT,
                               G_IMPLEMENT_INTERFACE (PX_TYPE_CONFIG, px_config_iface_init))

enum {
  PROP_0,
  PROP_CONFIG_OPTION
};

static void
px_config_env_init (PxConfigEnv *self)
{
  const char *no_proxy;

  /* Collect data in init() to speed up get_config() calls */
  no_proxy = g_getenv ("no_proxy");
  if (!no_proxy)
    no_proxy = g_getenv ("NO_PROXY");

  if (no_proxy)
    self->no_proxy = g_strsplit (no_proxy, ",", -1);

  self->ftp_proxy = g_getenv ("ftp_proxy");
  if (!self->ftp_proxy)
    self->ftp_proxy = g_getenv ("FTP_PROXY");

  self->https_proxy = g_getenv ("https_proxy");
  if (!self->https_proxy)
    self->https_proxy = g_getenv ("HTTPS_PROXY");

  self->http_proxy = g_getenv ("http_proxy");
  if (!self->http_proxy)
    self->http_proxy = g_getenv ("HTTP_PROXY");
}

static void
px_config_env_dispose (GObject *object)
{
  PxConfigEnv *self = PX_CONFIG_ENV (object);

  g_clear_pointer (&self->no_proxy, g_strfreev);

  G_OBJECT_CLASS (px_config_env_parent_class)->dispose (object);
}

static void
px_config_env_set_property (GObject      *object,
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec)
{
  switch (prop_id) {
    case PROP_CONFIG_OPTION:
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
px_config_env_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
  switch (prop_id) {
    case PROP_CONFIG_OPTION:
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
px_config_env_class_init (PxConfigEnvClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = px_config_env_dispose;
  object_class->set_property = px_config_env_set_property;
  object_class->get_property = px_config_env_get_property;

  g_object_class_override_property (object_class, PROP_CONFIG_OPTION, "config-option");
}

static gboolean
px_config_env_is_available (PxConfig *self)
{
  return TRUE;
}

static void
px_config_env_get_config (PxConfig     *config,
                          GUri         *uri,
                          GStrvBuilder *builder)
{
  PxConfigEnv *self = PX_CONFIG_ENV (config);
  const char *proxy = NULL;
  const char *scheme = g_uri_get_scheme (uri);

  if (px_manager_is_ignore (uri, self->no_proxy))
    return;

  if (g_strcmp0 (scheme, "ftp") == 0)
    proxy = self->ftp_proxy;
  else if (g_strcmp0 (scheme, "https") == 0)
    proxy = self->https_proxy;

  if (!proxy)
    proxy = self->http_proxy;

  if (proxy)
    px_strv_builder_add_proxy (builder, proxy);
}

static void
px_config_iface_init (PxConfigInterface *iface)
{
  iface->name = "config-env";
  iface->priority = PX_CONFIG_PRIORITY_FIRST;
  iface->is_available = px_config_env_is_available;
  iface->get_config = px_config_env_get_config;
}