@matheusbsbs / matheus-bagliano / commits / 7059cd99fc
Fork 0

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 <gitster@pobox.com>

Junio C Hamano committed Mar 9, 2009 at 19:00 UTC 7059cd99fc671f9594b61cee7d10d69704f3ebe2
1 file changed +27 -42
http.c
+27 -42
@@ -94,52 +94,33 @@ static void process_curl_messages(void)
94 static int http_options(const char *var, const char *value, void *cb)
95 {
96 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);
98 return 0;
99 }
100 + if (!strcmp("http.sslcert", var))
101 + return git_config_string(&ssl_cert, var, value);
102 #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);
105 #endif
106 #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);
109 #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);
112 #ifdef USE_CURL_MULTI
113 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);
115 return 0;
116 }
117 #endif
134 -
118 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);
120 return 0;
121 }
122 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);
124 return 0;
125 }
126
@@ -147,11 +128,8 @@ static int http_options(const char *var, const char *value, void *cb)
128 curl_ftp_no_epsv = git_config_bool(var, value);
129 return 0;
130 }
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);
133
134 /* Fall back on the default ones */
135 return git_default_config(var, value, cb);
@@ -212,11 +190,20 @@ static CURL *get_curl_handle(void)
190 return result;
191 }
192
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 +
200 void http_init(struct remote *remote)
201 {
202 char *low_speed_limit;
203 char *low_speed_time;
204
205 + git_config(http_options, NULL);
206 +
207 curl_global_init(CURL_GLOBAL_ALL);
208
209 if (remote && remote->http_proxy)
@@ -241,14 +228,14 @@ void http_init(struct remote *remote)
228 if (getenv("GIT_SSL_NO_VERIFY"))
229 curl_ssl_verify = 0;
230
244 - ssl_cert = getenv("GIT_SSL_CERT");
231 + set_from_env(&ssl_cert, "GIT_SSL_CERT");
232 #if LIBCURL_VERSION_NUM >= 0x070902
246 - ssl_key = getenv("GIT_SSL_KEY");
233 + set_from_env(&ssl_key, "GIT_SSL_KEY");
234 #endif
235 #if LIBCURL_VERSION_NUM >= 0x070908
249 - ssl_capath = getenv("GIT_SSL_CAPATH");
236 + set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
237 #endif
251 - ssl_cainfo = getenv("GIT_SSL_CAINFO");
238 + set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
239
240 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
241 if (low_speed_limit != NULL)
@@ -257,8 +244,6 @@ void http_init(struct remote *remote)
244 if (low_speed_time != NULL)
245 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
246
260 - git_config(http_options, NULL);
261 -
247 if (curl_ssl_verify == -1)
248 curl_ssl_verify = 1;
249