summaryrefslogtreecommitdiff
path: root/src/backend/plugins/config-env/config-env.c
blob: cbc4de591ef1d3a79d6e9fefd9dc12c976d235bd (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
/* config-env.c
 *
 * Copyright 2022-2023 Jan-Michael Brummer
 *
 * 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 <libpeas/peas.h>

#include "config-env.h"

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

static void px_config_iface_init (PxConfigInterface *iface);
G_MODULE_EXPORT void peas_register_types (PeasObjectModule *module);

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))

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_class_init (PxConfigEnvClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = px_config_env_dispose;
}

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

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

  /* TODO:
   * - Are host names resolved to IPs??
   * - case insensitive check?
   */
  if (self->no_proxy && (g_strv_contains ((const char * const *)self->no_proxy, g_uri_get_host (uri)) || g_strv_contains ((const char * const *)self->no_proxy, "*"))) {
    return TRUE;
  }

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

  /* TODO: Is this what we want as a fallback? What about ALL_PROXY? */
  if (!proxy)
    proxy = self->http_proxy;

  /* TODO: Where should we add proxy url validation ? */
  if (proxy)
    g_strv_builder_add (builder, proxy);

  return TRUE;
}

static void
px_config_iface_init (PxConfigInterface *iface)
{
  iface->is_available = px_config_env_is_available;
  iface->get_config = px_config_env_get_config;
}

G_MODULE_EXPORT void
peas_register_types (PeasObjectModule *module)
{
  peas_object_module_register_extension_type (module,
                                              PX_TYPE_CONFIG,
                                              PX_CONFIG_TYPE_ENV);
}