-
Notifications
You must be signed in to change notification settings - Fork 2
/
config_env.c
109 lines (89 loc) · 2.78 KB
/
config_env.c
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
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
#include "config_i.h"
#include "config_env.h"
#include "util.h"
// Get the environment variable using all lowercase name and then all uppercase name
// https://unix.stackexchange.com/questions/212894
static char *get_proxy_env_var(const char *name, bool check_uppercase) {
// Check original lowercase environment variable name passed in
char *proxy = getenv(name);
if (proxy) {
if (*proxy)
return proxy;
free(proxy);
}
if (check_uppercase) {
// Allocate a copy of the environment variable name
char *upper_name = strdup(name);
if (!upper_name)
return NULL;
// Convert string to all uppercase
for (size_t i = 0; i < strlen(upper_name); i++)
upper_name[i] = toupper(upper_name[i]);
// Check uppercase environment variable name
proxy = getenv(upper_name);
free(upper_name);
if (proxy && !*proxy) {
free(proxy);
proxy = NULL;
}
return proxy;
}
return NULL;
}
bool proxy_config_env_get_auto_discover(void) {
return false;
}
char *proxy_config_env_get_auto_config_url(void) {
return NULL;
}
char *proxy_config_env_get_proxy(const char *scheme) {
if (!scheme)
return NULL;
// Construct name of environment variable based on proxy scheme
size_t name_len = strlen(scheme) + 8;
char *name = (char *)malloc(name_len);
if (!name)
return NULL;
snprintf(name, name_len, "%s_proxy", scheme);
// Don't check HTTP_PROXY due to CGI environment variable creation
// https://everything.curl.dev/usingcurl/proxies/env
bool check_uppercase = strcmp(scheme, "http") != 0;
char *proxy = get_proxy_env_var(name, check_uppercase);
free(name);
if (!proxy)
return NULL;
return strdup(proxy);
}
char *proxy_config_env_get_bypass_list(void) {
const char *no_proxy = get_proxy_env_var("no_proxy", true);
if (!no_proxy)
return NULL;
char *bypass_list = strdup(no_proxy);
if (!bypass_list)
return NULL;
// Remove last separator
str_trim_end(bypass_list, ',');
return bypass_list;
}
bool proxy_config_env_global_init(void) {
return true;
}
bool proxy_config_env_global_cleanup(void) {
return true;
}
proxy_config_i_s *proxy_config_env_get_interface(void) {
static proxy_config_i_s proxy_config_env_i = {
proxy_config_env_get_auto_discover, proxy_config_env_get_auto_config_url, proxy_config_env_get_proxy,
proxy_config_env_get_bypass_list, proxy_config_env_global_init, proxy_config_env_global_cleanup};
return &proxy_config_env_i;
}
#ifdef __cplusplus
}
#endif