Skip to content

Commit 7059cd9

Browse files
committed
http_init(): Fix config file parsing
We honor the command line options, environment variables, variables in repository configuration file, variables in user's global configuration file, variables in the system configuration file, and then finally use built-in default. To implement this semantics, the code should: - start from built-in default values; - call git_config() with the configuration parser callback, which implements "later definition overrides earlier ones" logic (git_config() reads the system's, user's and then repository's configuration file in this order); - override the result from the above with environment variables if set; - override the result from the above with command line options. The initialization code http_init() for http transfer got this wrong, and implemented a "first one wins, ignoring the later ones" in http_options(), to compensate this mistake, read environment variables before calling git_config(). This is all wrong. As a second class citizen, the http codepath hasn't been audited as closely as other parts of the system, but we should try to bring sanity to it, before inviting contributors to improve on it. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4251ccb commit 7059cd9

File tree

1 file changed

+27
-42
lines changed

1 file changed

+27
-42
lines changed

http.c

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -94,64 +94,42 @@ static void process_curl_messages(void)
9494
static int http_options(const char *var, const char *value, void *cb)
9595
{
9696
if (!strcmp("http.sslverify", var)) {
97-
if (curl_ssl_verify == -1)
98-
curl_ssl_verify = git_config_bool(var, value);
99-
return 0;
100-
}
101-
102-
if (!strcmp("http.sslcert", var)) {
103-
if (ssl_cert == NULL)
104-
return git_config_string(&ssl_cert, var, value);
97+
curl_ssl_verify = git_config_bool(var, value);
10598
return 0;
10699
}
100+
if (!strcmp("http.sslcert", var))
101+
return git_config_string(&ssl_cert, var, value);
107102
#if LIBCURL_VERSION_NUM >= 0x070902
108-
if (!strcmp("http.sslkey", var)) {
109-
if (ssl_key == NULL)
110-
return git_config_string(&ssl_key, var, value);
111-
return 0;
112-
}
103+
if (!strcmp("http.sslkey", var))
104+
return git_config_string(&ssl_key, var, value);
113105
#endif
114106
#if LIBCURL_VERSION_NUM >= 0x070908
115-
if (!strcmp("http.sslcapath", var)) {
116-
if (ssl_capath == NULL)
117-
return git_config_string(&ssl_capath, var, value);
118-
return 0;
119-
}
107+
if (!strcmp("http.sslcapath", var))
108+
return git_config_string(&ssl_capath, var, value);
120109
#endif
121-
if (!strcmp("http.sslcainfo", var)) {
122-
if (ssl_cainfo == NULL)
123-
return git_config_string(&ssl_cainfo, var, value);
124-
return 0;
125-
}
126-
110+
if (!strcmp("http.sslcainfo", var))
111+
return git_config_string(&ssl_cainfo, var, value);
127112
#ifdef USE_CURL_MULTI
128113
if (!strcmp("http.maxrequests", var)) {
129-
if (max_requests == -1)
130-
max_requests = git_config_int(var, value);
114+
max_requests = git_config_int(var, value);
131115
return 0;
132116
}
133117
#endif
134-
135118
if (!strcmp("http.lowspeedlimit", var)) {
136-
if (curl_low_speed_limit == -1)
137-
curl_low_speed_limit = (long)git_config_int(var, value);
119+
curl_low_speed_limit = (long)git_config_int(var, value);
138120
return 0;
139121
}
140122
if (!strcmp("http.lowspeedtime", var)) {
141-
if (curl_low_speed_time == -1)
142-
curl_low_speed_time = (long)git_config_int(var, value);
123+
curl_low_speed_time = (long)git_config_int(var, value);
143124
return 0;
144125
}
145126

146127
if (!strcmp("http.noepsv", var)) {
147128
curl_ftp_no_epsv = git_config_bool(var, value);
148129
return 0;
149130
}
150-
if (!strcmp("http.proxy", var)) {
151-
if (curl_http_proxy == NULL)
152-
return git_config_string(&curl_http_proxy, var, value);
153-
return 0;
154-
}
131+
if (!strcmp("http.proxy", var))
132+
return git_config_string(&curl_http_proxy, var, value);
155133

156134
/* Fall back on the default ones */
157135
return git_default_config(var, value, cb);
@@ -212,11 +190,20 @@ static CURL *get_curl_handle(void)
212190
return result;
213191
}
214192

193+
static void set_from_env(const char **var, const char *envname)
194+
{
195+
const char *val = getenv(envname);
196+
if (val)
197+
*var = val;
198+
}
199+
215200
void http_init(struct remote *remote)
216201
{
217202
char *low_speed_limit;
218203
char *low_speed_time;
219204

205+
git_config(http_options, NULL);
206+
220207
curl_global_init(CURL_GLOBAL_ALL);
221208

222209
if (remote && remote->http_proxy)
@@ -241,14 +228,14 @@ void http_init(struct remote *remote)
241228
if (getenv("GIT_SSL_NO_VERIFY"))
242229
curl_ssl_verify = 0;
243230

244-
ssl_cert = getenv("GIT_SSL_CERT");
231+
set_from_env(&ssl_cert, "GIT_SSL_CERT");
245232
#if LIBCURL_VERSION_NUM >= 0x070902
246-
ssl_key = getenv("GIT_SSL_KEY");
233+
set_from_env(&ssl_key, "GIT_SSL_KEY");
247234
#endif
248235
#if LIBCURL_VERSION_NUM >= 0x070908
249-
ssl_capath = getenv("GIT_SSL_CAPATH");
236+
set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
250237
#endif
251-
ssl_cainfo = getenv("GIT_SSL_CAINFO");
238+
set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
252239

253240
low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
254241
if (low_speed_limit != NULL)
@@ -257,8 +244,6 @@ void http_init(struct remote *remote)
257244
if (low_speed_time != NULL)
258245
curl_low_speed_time = strtol(low_speed_time, NULL, 10);
259246

260-
git_config(http_options, NULL);
261-
262247
if (curl_ssl_verify == -1)
263248
curl_ssl_verify = 1;
264249

0 commit comments

Comments
 (0)