| Directory: | src/ |
|---|---|
| File: | src/backend/modules/config-env.c |
| Date: | 2023-01-04 17:35:37 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 39 | 44 | 88.6% |
| Branches: | 18 | 27 | 66.7% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* config-env.c | ||
| 2 | * | ||
| 3 | * Copyright 2022-2023 Jan-Michael Brummer | ||
| 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 <glib-object.h> | ||
| 23 | |||
| 24 | #include "config-env.h" | ||
| 25 | #include "px-module.h" | ||
| 26 | #include "px-config-module.h" | ||
| 27 | |||
| 28 | struct _PxEnvModule { | ||
| 29 | GObject parent_instance; | ||
| 30 | }; | ||
| 31 | |||
| 32 | static void module_interface_init (gpointer g_iface, | ||
| 33 | gpointer data); | ||
| 34 | |||
| 35 |
6/7✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 8 times.
|
28 | G_DEFINE_TYPE_WITH_CODE (PxEnvModule, px_env_module, |
| 36 | PX_TYPE_MODULE, | ||
| 37 | G_IMPLEMENT_INTERFACE (PX_TYPE_CONFIG_MODULE, module_interface_init); | ||
| 38 | ); | ||
| 39 | |||
| 40 | static void | ||
| 41 | 10 | px_env_module_init (PxEnvModule *self) | |
| 42 | { | ||
| 43 | 10 | } | |
| 44 | |||
| 45 | static void | ||
| 46 | 2 | px_env_module_class_init (PxEnvModuleClass *klass) | |
| 47 | { | ||
| 48 | 2 | } | |
| 49 | |||
| 50 | static gboolean | ||
| 51 | 9 | env_check_available (void) | |
| 52 | { | ||
| 53 | 9 | return TRUE; | |
| 54 | } | ||
| 55 | |||
| 56 | static GStrv | ||
| 57 | 9 | env_get_config (PxModule *px_module, | |
| 58 | GUri *uri, | ||
| 59 | GError **error) | ||
| 60 | { | ||
| 61 | 9 | g_auto (GStrv) ret = NULL; | |
| 62 | 9 | const char *proxy = NULL; | |
| 63 | 9 | const char *scheme = g_uri_get_scheme (uri); | |
| 64 | |||
| 65 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
|
9 | if (g_strcmp0 (scheme, "ftp") == 0) { |
| 66 | 3 | proxy = g_getenv ("ftp_proxy"); | |
| 67 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (!proxy) |
| 68 | 3 | proxy = g_getenv ("FTP_PROXY"); | |
| 69 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | } else if (g_strcmp0 (scheme, "https") == 0) { |
| 70 | 3 | proxy = g_getenv ("https_proxy"); | |
| 71 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (!proxy) |
| 72 | 3 | proxy = g_getenv ("HTTPS_PROXY"); | |
| 73 | } | ||
| 74 | |||
| 75 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
|
9 | if (!proxy) { |
| 76 | 7 | proxy = g_getenv ("http_proxy"); | |
| 77 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (!proxy) |
| 78 | 7 | proxy = g_getenv ("HTTP_PROXY"); | |
| 79 | } | ||
| 80 | |||
| 81 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
9 | if (!proxy && error) { |
| 82 | 4 | g_set_error (error, PX_MODULE_ERROR, PX_MODULE_ERROR_CONFIGURATION, "Unable to read environment configuration"); | |
| 83 | 4 | return NULL; | |
| 84 | } | ||
| 85 | |||
| 86 | 5 | ret = g_malloc0 (sizeof (char *) * 2); | |
| 87 | 5 | ret[0] = g_strdup (proxy); | |
| 88 | |||
| 89 | 5 | return g_steal_pointer (&ret); | |
| 90 | } | ||
| 91 | |||
| 92 | static char * | ||
| 93 | ✗ | env_get_ignore (PxModule *px_module, | |
| 94 | GUri *uri, | ||
| 95 | GError **error) | ||
| 96 | { | ||
| 97 | ✗ | const char *ignore = g_getenv ("no_proxy"); | |
| 98 | |||
| 99 | ✗ | if (!ignore) | |
| 100 | ✗ | ignore = g_getenv ("NO_PROXY"); | |
| 101 | |||
| 102 | ✗ | return g_strdup (ignore ? ignore : ""); | |
| 103 | } | ||
| 104 | |||
| 105 | static void | ||
| 106 | 2 | module_interface_init (gpointer g_iface, | |
| 107 | gpointer data) | ||
| 108 | { | ||
| 109 | 2 | PxConfigModuleInterface *iface = g_iface; | |
| 110 | |||
| 111 | 2 | iface->name = g_strdup ("Environment"); | |
| 112 | 2 | iface->version = 1; | |
| 113 | 2 | iface->check_available = env_check_available; | |
| 114 | 2 | iface->get_config = env_get_config; | |
| 115 | 2 | iface->get_ignore = env_get_ignore; | |
| 116 | 2 | } | |
| 117 | |||
| 118 | PxModule * | ||
| 119 | 10 | px_module_create (void) | |
| 120 | { | ||
| 121 | 10 | return g_object_new (PX_TYPE_ENV_MODULE, NULL); | |
| 122 | } | ||
| 123 |