http: honor no_http env variable to bypass proxy
[git] / http.c
1 #include "git-compat-util.h"
2 #include "http.h"
3 #include "pack.h"
4 #include "sideband.h"
5 #include "run-command.h"
6 #include "url.h"
7 #include "urlmatch.h"
8 #include "credential.h"
9 #include "version.h"
10 #include "pkt-line.h"
11 #include "gettext.h"
12 #include "transport.h"
13
14 int active_requests;
15 int http_is_verbose;
16 size_t http_post_buffer = 16 * LARGE_PACKET_MAX;
17
18 #if LIBCURL_VERSION_NUM >= 0x070a06
19 #define LIBCURL_CAN_HANDLE_AUTH_ANY
20 #endif
21
22 static int min_curl_sessions = 1;
23 static int curl_session_count;
24 #ifdef USE_CURL_MULTI
25 static int max_requests = -1;
26 static CURLM *curlm;
27 #endif
28 #ifndef NO_CURL_EASY_DUPHANDLE
29 static CURL *curl_default;
30 #endif
31
32 #define PREV_BUF_SIZE 4096
33
34 char curl_errorstr[CURL_ERROR_SIZE];
35
36 static int curl_ssl_verify = -1;
37 static int curl_ssl_try;
38 static const char *ssl_cert;
39 static const char *ssl_cipherlist;
40 static const char *ssl_version;
41 static struct {
42         const char *name;
43         long ssl_version;
44 } sslversions[] = {
45         { "sslv2", CURL_SSLVERSION_SSLv2 },
46         { "sslv3", CURL_SSLVERSION_SSLv3 },
47         { "tlsv1", CURL_SSLVERSION_TLSv1 },
48 #if LIBCURL_VERSION_NUM >= 0x072200
49         { "tlsv1.0", CURL_SSLVERSION_TLSv1_0 },
50         { "tlsv1.1", CURL_SSLVERSION_TLSv1_1 },
51         { "tlsv1.2", CURL_SSLVERSION_TLSv1_2 },
52 #endif
53 };
54 #if LIBCURL_VERSION_NUM >= 0x070903
55 static const char *ssl_key;
56 #endif
57 #if LIBCURL_VERSION_NUM >= 0x070908
58 static const char *ssl_capath;
59 #endif
60 static const char *ssl_cainfo;
61 static long curl_low_speed_limit = -1;
62 static long curl_low_speed_time = -1;
63 static int curl_ftp_no_epsv;
64 static const char *curl_http_proxy;
65 static const char *curl_no_proxy;
66 static const char *http_proxy_authmethod;
67 static struct {
68         const char *name;
69         long curlauth_param;
70 } proxy_authmethods[] = {
71         { "basic", CURLAUTH_BASIC },
72         { "digest", CURLAUTH_DIGEST },
73         { "negotiate", CURLAUTH_GSSNEGOTIATE },
74         { "ntlm", CURLAUTH_NTLM },
75 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
76         { "anyauth", CURLAUTH_ANY },
77 #endif
78         /*
79          * CURLAUTH_DIGEST_IE has no corresponding command-line option in
80          * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
81          * here, too
82          */
83 };
84 static struct credential proxy_auth = CREDENTIAL_INIT;
85 static const char *curl_proxyuserpwd;
86 static const char *curl_cookie_file;
87 static int curl_save_cookies;
88 struct credential http_auth = CREDENTIAL_INIT;
89 static int http_proactive_auth;
90 static const char *user_agent;
91
92 #if LIBCURL_VERSION_NUM >= 0x071700
93 /* Use CURLOPT_KEYPASSWD as is */
94 #elif LIBCURL_VERSION_NUM >= 0x070903
95 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
96 #else
97 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
98 #endif
99
100 static struct credential cert_auth = CREDENTIAL_INIT;
101 static int ssl_cert_password_required;
102 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
103 static unsigned long http_auth_methods = CURLAUTH_ANY;
104 #endif
105
106 static struct curl_slist *pragma_header;
107 static struct curl_slist *no_pragma_header;
108
109 static struct active_request_slot *active_queue_head;
110
111 static char *cached_accept_language;
112
113 size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
114 {
115         size_t size = eltsize * nmemb;
116         struct buffer *buffer = buffer_;
117
118         if (size > buffer->buf.len - buffer->posn)
119                 size = buffer->buf.len - buffer->posn;
120         memcpy(ptr, buffer->buf.buf + buffer->posn, size);
121         buffer->posn += size;
122
123         return size;
124 }
125
126 #ifndef NO_CURL_IOCTL
127 curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
128 {
129         struct buffer *buffer = clientp;
130
131         switch (cmd) {
132         case CURLIOCMD_NOP:
133                 return CURLIOE_OK;
134
135         case CURLIOCMD_RESTARTREAD:
136                 buffer->posn = 0;
137                 return CURLIOE_OK;
138
139         default:
140                 return CURLIOE_UNKNOWNCMD;
141         }
142 }
143 #endif
144
145 size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
146 {
147         size_t size = eltsize * nmemb;
148         struct strbuf *buffer = buffer_;
149
150         strbuf_add(buffer, ptr, size);
151         return size;
152 }
153
154 size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf)
155 {
156         return eltsize * nmemb;
157 }
158
159 static void closedown_active_slot(struct active_request_slot *slot)
160 {
161         active_requests--;
162         slot->in_use = 0;
163 }
164
165 static void finish_active_slot(struct active_request_slot *slot)
166 {
167         closedown_active_slot(slot);
168         curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
169
170         if (slot->finished != NULL)
171                 (*slot->finished) = 1;
172
173         /* Store slot results so they can be read after the slot is reused */
174         if (slot->results != NULL) {
175                 slot->results->curl_result = slot->curl_result;
176                 slot->results->http_code = slot->http_code;
177 #if LIBCURL_VERSION_NUM >= 0x070a08
178                 curl_easy_getinfo(slot->curl, CURLINFO_HTTPAUTH_AVAIL,
179                                   &slot->results->auth_avail);
180 #else
181                 slot->results->auth_avail = 0;
182 #endif
183
184                 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CONNECTCODE,
185                         &slot->results->http_connectcode);
186         }
187
188         /* Run callback if appropriate */
189         if (slot->callback_func != NULL)
190                 slot->callback_func(slot->callback_data);
191 }
192
193 #ifdef USE_CURL_MULTI
194 static void process_curl_messages(void)
195 {
196         int num_messages;
197         struct active_request_slot *slot;
198         CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
199
200         while (curl_message != NULL) {
201                 if (curl_message->msg == CURLMSG_DONE) {
202                         int curl_result = curl_message->data.result;
203                         slot = active_queue_head;
204                         while (slot != NULL &&
205                                slot->curl != curl_message->easy_handle)
206                                 slot = slot->next;
207                         if (slot != NULL) {
208                                 curl_multi_remove_handle(curlm, slot->curl);
209                                 slot->curl_result = curl_result;
210                                 finish_active_slot(slot);
211                         } else {
212                                 fprintf(stderr, "Received DONE message for unknown request!\n");
213                         }
214                 } else {
215                         fprintf(stderr, "Unknown CURL message received: %d\n",
216                                 (int)curl_message->msg);
217                 }
218                 curl_message = curl_multi_info_read(curlm, &num_messages);
219         }
220 }
221 #endif
222
223 static int http_options(const char *var, const char *value, void *cb)
224 {
225         if (!strcmp("http.sslverify", var)) {
226                 curl_ssl_verify = git_config_bool(var, value);
227                 return 0;
228         }
229         if (!strcmp("http.sslcipherlist", var))
230                 return git_config_string(&ssl_cipherlist, var, value);
231         if (!strcmp("http.sslversion", var))
232                 return git_config_string(&ssl_version, var, value);
233         if (!strcmp("http.sslcert", var))
234                 return git_config_string(&ssl_cert, var, value);
235 #if LIBCURL_VERSION_NUM >= 0x070903
236         if (!strcmp("http.sslkey", var))
237                 return git_config_string(&ssl_key, var, value);
238 #endif
239 #if LIBCURL_VERSION_NUM >= 0x070908
240         if (!strcmp("http.sslcapath", var))
241                 return git_config_pathname(&ssl_capath, var, value);
242 #endif
243         if (!strcmp("http.sslcainfo", var))
244                 return git_config_pathname(&ssl_cainfo, var, value);
245         if (!strcmp("http.sslcertpasswordprotected", var)) {
246                 ssl_cert_password_required = git_config_bool(var, value);
247                 return 0;
248         }
249         if (!strcmp("http.ssltry", var)) {
250                 curl_ssl_try = git_config_bool(var, value);
251                 return 0;
252         }
253         if (!strcmp("http.minsessions", var)) {
254                 min_curl_sessions = git_config_int(var, value);
255 #ifndef USE_CURL_MULTI
256                 if (min_curl_sessions > 1)
257                         min_curl_sessions = 1;
258 #endif
259                 return 0;
260         }
261 #ifdef USE_CURL_MULTI
262         if (!strcmp("http.maxrequests", var)) {
263                 max_requests = git_config_int(var, value);
264                 return 0;
265         }
266 #endif
267         if (!strcmp("http.lowspeedlimit", var)) {
268                 curl_low_speed_limit = (long)git_config_int(var, value);
269                 return 0;
270         }
271         if (!strcmp("http.lowspeedtime", var)) {
272                 curl_low_speed_time = (long)git_config_int(var, value);
273                 return 0;
274         }
275
276         if (!strcmp("http.noepsv", var)) {
277                 curl_ftp_no_epsv = git_config_bool(var, value);
278                 return 0;
279         }
280         if (!strcmp("http.proxy", var))
281                 return git_config_string(&curl_http_proxy, var, value);
282
283         if (!strcmp("http.proxyauthmethod", var))
284                 return git_config_string(&http_proxy_authmethod, var, value);
285
286         if (!strcmp("http.cookiefile", var))
287                 return git_config_string(&curl_cookie_file, var, value);
288         if (!strcmp("http.savecookies", var)) {
289                 curl_save_cookies = git_config_bool(var, value);
290                 return 0;
291         }
292
293         if (!strcmp("http.postbuffer", var)) {
294                 http_post_buffer = git_config_int(var, value);
295                 if (http_post_buffer < LARGE_PACKET_MAX)
296                         http_post_buffer = LARGE_PACKET_MAX;
297                 return 0;
298         }
299
300         if (!strcmp("http.useragent", var))
301                 return git_config_string(&user_agent, var, value);
302
303         /* Fall back on the default ones */
304         return git_default_config(var, value, cb);
305 }
306
307 static void init_curl_http_auth(CURL *result)
308 {
309         if (!http_auth.username)
310                 return;
311
312         credential_fill(&http_auth);
313
314 #if LIBCURL_VERSION_NUM >= 0x071301
315         curl_easy_setopt(result, CURLOPT_USERNAME, http_auth.username);
316         curl_easy_setopt(result, CURLOPT_PASSWORD, http_auth.password);
317 #else
318         {
319                 static struct strbuf up = STRBUF_INIT;
320                 /*
321                  * Note that we assume we only ever have a single set of
322                  * credentials in a given program run, so we do not have
323                  * to worry about updating this buffer, only setting its
324                  * initial value.
325                  */
326                 if (!up.len)
327                         strbuf_addf(&up, "%s:%s",
328                                 http_auth.username, http_auth.password);
329                 curl_easy_setopt(result, CURLOPT_USERPWD, up.buf);
330         }
331 #endif
332 }
333
334 /* *var must be free-able */
335 static void var_override(const char **var, char *value)
336 {
337         if (value) {
338                 free((void *)*var);
339                 *var = xstrdup(value);
340         }
341 }
342
343 static void set_proxyauth_name_password(CURL *result)
344 {
345 #if LIBCURL_VERSION_NUM >= 0x071301
346                 curl_easy_setopt(result, CURLOPT_PROXYUSERNAME,
347                         proxy_auth.username);
348                 curl_easy_setopt(result, CURLOPT_PROXYPASSWORD,
349                         proxy_auth.password);
350 #else
351                 struct strbuf s = STRBUF_INIT;
352
353                 strbuf_addstr_urlencode(&s, proxy_auth.username, 1);
354                 strbuf_addch(&s, ':');
355                 strbuf_addstr_urlencode(&s, proxy_auth.password, 1);
356                 curl_proxyuserpwd = strbuf_detach(&s, NULL);
357                 curl_easy_setopt(result, CURLOPT_PROXYUSERPWD, curl_proxyuserpwd);
358 #endif
359 }
360
361 static void init_curl_proxy_auth(CURL *result)
362 {
363         if (proxy_auth.username) {
364                 if (!proxy_auth.password)
365                         credential_fill(&proxy_auth);
366                 set_proxyauth_name_password(result);
367         }
368
369         var_override(&http_proxy_authmethod, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
370
371 #if LIBCURL_VERSION_NUM >= 0x070a07 /* CURLOPT_PROXYAUTH and CURLAUTH_ANY */
372         if (http_proxy_authmethod) {
373                 int i;
374                 for (i = 0; i < ARRAY_SIZE(proxy_authmethods); i++) {
375                         if (!strcmp(http_proxy_authmethod, proxy_authmethods[i].name)) {
376                                 curl_easy_setopt(result, CURLOPT_PROXYAUTH,
377                                                 proxy_authmethods[i].curlauth_param);
378                                 break;
379                         }
380                 }
381                 if (i == ARRAY_SIZE(proxy_authmethods)) {
382                         warning("unsupported proxy authentication method %s: using anyauth",
383                                         http_proxy_authmethod);
384                         curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
385                 }
386         }
387         else
388                 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
389 #endif
390 }
391
392 static int has_cert_password(void)
393 {
394         if (ssl_cert == NULL || ssl_cert_password_required != 1)
395                 return 0;
396         if (!cert_auth.password) {
397                 cert_auth.protocol = xstrdup("cert");
398                 cert_auth.username = xstrdup("");
399                 cert_auth.path = xstrdup(ssl_cert);
400                 credential_fill(&cert_auth);
401         }
402         return 1;
403 }
404
405 #if LIBCURL_VERSION_NUM >= 0x071900
406 static void set_curl_keepalive(CURL *c)
407 {
408         curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
409 }
410
411 #elif LIBCURL_VERSION_NUM >= 0x071000
412 static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
413 {
414         int ka = 1;
415         int rc;
416         socklen_t len = (socklen_t)sizeof(ka);
417
418         if (type != CURLSOCKTYPE_IPCXN)
419                 return 0;
420
421         rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
422         if (rc < 0)
423                 warning("unable to set SO_KEEPALIVE on socket %s",
424                         strerror(errno));
425
426         return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
427 }
428
429 static void set_curl_keepalive(CURL *c)
430 {
431         curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
432 }
433
434 #else
435 static void set_curl_keepalive(CURL *c)
436 {
437         /* not supported on older curl versions */
438 }
439 #endif
440
441 static CURL *get_curl_handle(void)
442 {
443         CURL *result = curl_easy_init();
444         long allowed_protocols = 0;
445
446         if (!result)
447                 die("curl_easy_init failed");
448
449         if (!curl_ssl_verify) {
450                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
451                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
452         } else {
453                 /* Verify authenticity of the peer's certificate */
454                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
455                 /* The name in the cert must match whom we tried to connect */
456                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
457         }
458
459 #if LIBCURL_VERSION_NUM >= 0x070907
460         curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
461 #endif
462 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
463         curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
464 #endif
465
466         if (http_proactive_auth)
467                 init_curl_http_auth(result);
468
469         if (getenv("GIT_SSL_VERSION"))
470                 ssl_version = getenv("GIT_SSL_VERSION");
471         if (ssl_version && *ssl_version) {
472                 int i;
473                 for (i = 0; i < ARRAY_SIZE(sslversions); i++) {
474                         if (!strcmp(ssl_version, sslversions[i].name)) {
475                                 curl_easy_setopt(result, CURLOPT_SSLVERSION,
476                                                  sslversions[i].ssl_version);
477                                 break;
478                         }
479                 }
480                 if (i == ARRAY_SIZE(sslversions))
481                         warning("unsupported ssl version %s: using default",
482                                 ssl_version);
483         }
484
485         if (getenv("GIT_SSL_CIPHER_LIST"))
486                 ssl_cipherlist = getenv("GIT_SSL_CIPHER_LIST");
487         if (ssl_cipherlist != NULL && *ssl_cipherlist)
488                 curl_easy_setopt(result, CURLOPT_SSL_CIPHER_LIST,
489                                 ssl_cipherlist);
490
491         if (ssl_cert != NULL)
492                 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
493         if (has_cert_password())
494                 curl_easy_setopt(result, CURLOPT_KEYPASSWD, cert_auth.password);
495 #if LIBCURL_VERSION_NUM >= 0x070903
496         if (ssl_key != NULL)
497                 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
498 #endif
499 #if LIBCURL_VERSION_NUM >= 0x070908
500         if (ssl_capath != NULL)
501                 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
502 #endif
503         if (ssl_cainfo != NULL)
504                 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
505
506         if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
507                 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
508                                  curl_low_speed_limit);
509                 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
510                                  curl_low_speed_time);
511         }
512
513         curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
514         curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
515 #if LIBCURL_VERSION_NUM >= 0x071301
516         curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
517 #elif LIBCURL_VERSION_NUM >= 0x071101
518         curl_easy_setopt(result, CURLOPT_POST301, 1);
519 #endif
520 #if LIBCURL_VERSION_NUM >= 0x071304
521         if (is_transport_allowed("http"))
522                 allowed_protocols |= CURLPROTO_HTTP;
523         if (is_transport_allowed("https"))
524                 allowed_protocols |= CURLPROTO_HTTPS;
525         if (is_transport_allowed("ftp"))
526                 allowed_protocols |= CURLPROTO_FTP;
527         if (is_transport_allowed("ftps"))
528                 allowed_protocols |= CURLPROTO_FTPS;
529         curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS, allowed_protocols);
530 #else
531         if (transport_restrict_protocols())
532                 warning("protocol restrictions not applied to curl redirects because\n"
533                         "your curl version is too old (>= 7.19.4)");
534 #endif
535
536         if (getenv("GIT_CURL_VERBOSE"))
537                 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
538
539         curl_easy_setopt(result, CURLOPT_USERAGENT,
540                 user_agent ? user_agent : git_user_agent());
541
542         if (curl_ftp_no_epsv)
543                 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
544
545 #ifdef CURLOPT_USE_SSL
546         if (curl_ssl_try)
547                 curl_easy_setopt(result, CURLOPT_USE_SSL, CURLUSESSL_TRY);
548 #endif
549
550         /*
551          * CURL also examines these variables as a fallback; but we need to query
552          * them here in order to decide whether to prompt for missing password (cf.
553          * init_curl_proxy_auth()).
554          *
555          * Unlike many other common environment variables, these are historically
556          * lowercase only. It appears that CURL did not know this and implemented
557          * only uppercase variants, which was later corrected to take both - with
558          * the exception of http_proxy, which is lowercase only also in CURL. As
559          * the lowercase versions are the historical quasi-standard, they take
560          * precedence here, as in CURL.
561          */
562         if (!curl_http_proxy) {
563                 if (!strcmp(http_auth.protocol, "https")) {
564                         var_override(&curl_http_proxy, getenv("HTTPS_PROXY"));
565                         var_override(&curl_http_proxy, getenv("https_proxy"));
566                 } else {
567                         var_override(&curl_http_proxy, getenv("http_proxy"));
568                 }
569                 if (!curl_http_proxy) {
570                         var_override(&curl_http_proxy, getenv("ALL_PROXY"));
571                         var_override(&curl_http_proxy, getenv("all_proxy"));
572                 }
573         }
574
575         if (curl_http_proxy) {
576                 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
577 #if LIBCURL_VERSION_NUM >= 0x071800
578                 if (starts_with(curl_http_proxy, "socks5"))
579                         curl_easy_setopt(result,
580                                 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
581                 else if (starts_with(curl_http_proxy, "socks4a"))
582                         curl_easy_setopt(result,
583                                 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
584                 else if (starts_with(curl_http_proxy, "socks"))
585                         curl_easy_setopt(result,
586                                 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
587 #endif
588                 if (strstr(curl_http_proxy, "://"))
589                         credential_from_url(&proxy_auth, curl_http_proxy);
590                 else {
591                         struct strbuf url = STRBUF_INIT;
592                         strbuf_addf(&url, "http://%s", curl_http_proxy);
593                         credential_from_url(&proxy_auth, url.buf);
594                         strbuf_release(&url);
595                 }
596
597                 curl_easy_setopt(result, CURLOPT_PROXY, proxy_auth.host);
598 #if LIBCURL_VERSION_NUM >= 0x071304
599                 var_override(&curl_no_proxy, getenv("NO_PROXY"));
600                 var_override(&curl_no_proxy, getenv("no_proxy"));
601                 curl_easy_setopt(result, CURLOPT_NOPROXY, curl_no_proxy);
602 #endif
603         }
604         init_curl_proxy_auth(result);
605
606         set_curl_keepalive(result);
607
608         return result;
609 }
610
611 static void set_from_env(const char **var, const char *envname)
612 {
613         const char *val = getenv(envname);
614         if (val)
615                 *var = val;
616 }
617
618 void http_init(struct remote *remote, const char *url, int proactive_auth)
619 {
620         char *low_speed_limit;
621         char *low_speed_time;
622         char *normalized_url;
623         struct urlmatch_config config = { STRING_LIST_INIT_DUP };
624
625         config.section = "http";
626         config.key = NULL;
627         config.collect_fn = http_options;
628         config.cascade_fn = git_default_config;
629         config.cb = NULL;
630
631         http_is_verbose = 0;
632         normalized_url = url_normalize(url, &config.url);
633
634         git_config(urlmatch_config_entry, &config);
635         free(normalized_url);
636
637         if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
638                 die("curl_global_init failed");
639
640         http_proactive_auth = proactive_auth;
641
642         if (remote && remote->http_proxy)
643                 curl_http_proxy = xstrdup(remote->http_proxy);
644
645         if (remote)
646                 var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
647
648         pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
649         no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
650
651 #ifdef USE_CURL_MULTI
652         {
653                 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
654                 if (http_max_requests != NULL)
655                         max_requests = atoi(http_max_requests);
656         }
657
658         curlm = curl_multi_init();
659         if (!curlm)
660                 die("curl_multi_init failed");
661 #endif
662
663         if (getenv("GIT_SSL_NO_VERIFY"))
664                 curl_ssl_verify = 0;
665
666         set_from_env(&ssl_cert, "GIT_SSL_CERT");
667 #if LIBCURL_VERSION_NUM >= 0x070903
668         set_from_env(&ssl_key, "GIT_SSL_KEY");
669 #endif
670 #if LIBCURL_VERSION_NUM >= 0x070908
671         set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
672 #endif
673         set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
674
675         set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
676
677         low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
678         if (low_speed_limit != NULL)
679                 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
680         low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
681         if (low_speed_time != NULL)
682                 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
683
684         if (curl_ssl_verify == -1)
685                 curl_ssl_verify = 1;
686
687         curl_session_count = 0;
688 #ifdef USE_CURL_MULTI
689         if (max_requests < 1)
690                 max_requests = DEFAULT_MAX_REQUESTS;
691 #endif
692
693         if (getenv("GIT_CURL_FTP_NO_EPSV"))
694                 curl_ftp_no_epsv = 1;
695
696         if (url) {
697                 credential_from_url(&http_auth, url);
698                 if (!ssl_cert_password_required &&
699                     getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
700                     starts_with(url, "https://"))
701                         ssl_cert_password_required = 1;
702         }
703
704 #ifndef NO_CURL_EASY_DUPHANDLE
705         curl_default = get_curl_handle();
706 #endif
707 }
708
709 void http_cleanup(void)
710 {
711         struct active_request_slot *slot = active_queue_head;
712
713         while (slot != NULL) {
714                 struct active_request_slot *next = slot->next;
715                 if (slot->curl != NULL) {
716 #ifdef USE_CURL_MULTI
717                         curl_multi_remove_handle(curlm, slot->curl);
718 #endif
719                         curl_easy_cleanup(slot->curl);
720                 }
721                 free(slot);
722                 slot = next;
723         }
724         active_queue_head = NULL;
725
726 #ifndef NO_CURL_EASY_DUPHANDLE
727         curl_easy_cleanup(curl_default);
728 #endif
729
730 #ifdef USE_CURL_MULTI
731         curl_multi_cleanup(curlm);
732 #endif
733         curl_global_cleanup();
734
735         curl_slist_free_all(pragma_header);
736         pragma_header = NULL;
737
738         curl_slist_free_all(no_pragma_header);
739         no_pragma_header = NULL;
740
741         if (curl_http_proxy) {
742                 free((void *)curl_http_proxy);
743                 curl_http_proxy = NULL;
744         }
745
746         if (proxy_auth.password) {
747                 memset(proxy_auth.password, 0, strlen(proxy_auth.password));
748                 free(proxy_auth.password);
749                 proxy_auth.password = NULL;
750         }
751
752         free((void *)curl_proxyuserpwd);
753         curl_proxyuserpwd = NULL;
754
755         free((void *)http_proxy_authmethod);
756         http_proxy_authmethod = NULL;
757
758         if (cert_auth.password != NULL) {
759                 memset(cert_auth.password, 0, strlen(cert_auth.password));
760                 free(cert_auth.password);
761                 cert_auth.password = NULL;
762         }
763         ssl_cert_password_required = 0;
764
765         free(cached_accept_language);
766         cached_accept_language = NULL;
767 }
768
769 struct active_request_slot *get_active_slot(void)
770 {
771         struct active_request_slot *slot = active_queue_head;
772         struct active_request_slot *newslot;
773
774 #ifdef USE_CURL_MULTI
775         int num_transfers;
776
777         /* Wait for a slot to open up if the queue is full */
778         while (active_requests >= max_requests) {
779                 curl_multi_perform(curlm, &num_transfers);
780                 if (num_transfers < active_requests)
781                         process_curl_messages();
782         }
783 #endif
784
785         while (slot != NULL && slot->in_use)
786                 slot = slot->next;
787
788         if (slot == NULL) {
789                 newslot = xmalloc(sizeof(*newslot));
790                 newslot->curl = NULL;
791                 newslot->in_use = 0;
792                 newslot->next = NULL;
793
794                 slot = active_queue_head;
795                 if (slot == NULL) {
796                         active_queue_head = newslot;
797                 } else {
798                         while (slot->next != NULL)
799                                 slot = slot->next;
800                         slot->next = newslot;
801                 }
802                 slot = newslot;
803         }
804
805         if (slot->curl == NULL) {
806 #ifdef NO_CURL_EASY_DUPHANDLE
807                 slot->curl = get_curl_handle();
808 #else
809                 slot->curl = curl_easy_duphandle(curl_default);
810 #endif
811                 curl_session_count++;
812         }
813
814         active_requests++;
815         slot->in_use = 1;
816         slot->results = NULL;
817         slot->finished = NULL;
818         slot->callback_data = NULL;
819         slot->callback_func = NULL;
820         curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
821         if (curl_save_cookies)
822                 curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
823         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
824         curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
825         curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
826         curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
827         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
828         curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
829         curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
830         curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
831         curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
832         curl_easy_setopt(slot->curl, CURLOPT_RANGE, NULL);
833 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
834         curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods);
835 #endif
836         if (http_auth.password)
837                 init_curl_http_auth(slot->curl);
838
839         return slot;
840 }
841
842 int start_active_slot(struct active_request_slot *slot)
843 {
844 #ifdef USE_CURL_MULTI
845         CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
846         int num_transfers;
847
848         if (curlm_result != CURLM_OK &&
849             curlm_result != CURLM_CALL_MULTI_PERFORM) {
850                 active_requests--;
851                 slot->in_use = 0;
852                 return 0;
853         }
854
855         /*
856          * We know there must be something to do, since we just added
857          * something.
858          */
859         curl_multi_perform(curlm, &num_transfers);
860 #endif
861         return 1;
862 }
863
864 #ifdef USE_CURL_MULTI
865 struct fill_chain {
866         void *data;
867         int (*fill)(void *);
868         struct fill_chain *next;
869 };
870
871 static struct fill_chain *fill_cfg;
872
873 void add_fill_function(void *data, int (*fill)(void *))
874 {
875         struct fill_chain *new = xmalloc(sizeof(*new));
876         struct fill_chain **linkp = &fill_cfg;
877         new->data = data;
878         new->fill = fill;
879         new->next = NULL;
880         while (*linkp)
881                 linkp = &(*linkp)->next;
882         *linkp = new;
883 }
884
885 void fill_active_slots(void)
886 {
887         struct active_request_slot *slot = active_queue_head;
888
889         while (active_requests < max_requests) {
890                 struct fill_chain *fill;
891                 for (fill = fill_cfg; fill; fill = fill->next)
892                         if (fill->fill(fill->data))
893                                 break;
894
895                 if (!fill)
896                         break;
897         }
898
899         while (slot != NULL) {
900                 if (!slot->in_use && slot->curl != NULL
901                         && curl_session_count > min_curl_sessions) {
902                         curl_easy_cleanup(slot->curl);
903                         slot->curl = NULL;
904                         curl_session_count--;
905                 }
906                 slot = slot->next;
907         }
908 }
909
910 void step_active_slots(void)
911 {
912         int num_transfers;
913         CURLMcode curlm_result;
914
915         do {
916                 curlm_result = curl_multi_perform(curlm, &num_transfers);
917         } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
918         if (num_transfers < active_requests) {
919                 process_curl_messages();
920                 fill_active_slots();
921         }
922 }
923 #endif
924
925 void run_active_slot(struct active_request_slot *slot)
926 {
927 #ifdef USE_CURL_MULTI
928         fd_set readfds;
929         fd_set writefds;
930         fd_set excfds;
931         int max_fd;
932         struct timeval select_timeout;
933         int finished = 0;
934
935         slot->finished = &finished;
936         while (!finished) {
937                 step_active_slots();
938
939                 if (slot->in_use) {
940 #if LIBCURL_VERSION_NUM >= 0x070f04
941                         long curl_timeout;
942                         curl_multi_timeout(curlm, &curl_timeout);
943                         if (curl_timeout == 0) {
944                                 continue;
945                         } else if (curl_timeout == -1) {
946                                 select_timeout.tv_sec  = 0;
947                                 select_timeout.tv_usec = 50000;
948                         } else {
949                                 select_timeout.tv_sec  =  curl_timeout / 1000;
950                                 select_timeout.tv_usec = (curl_timeout % 1000) * 1000;
951                         }
952 #else
953                         select_timeout.tv_sec  = 0;
954                         select_timeout.tv_usec = 50000;
955 #endif
956
957                         max_fd = -1;
958                         FD_ZERO(&readfds);
959                         FD_ZERO(&writefds);
960                         FD_ZERO(&excfds);
961                         curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
962
963                         /*
964                          * It can happen that curl_multi_timeout returns a pathologically
965                          * long timeout when curl_multi_fdset returns no file descriptors
966                          * to read.  See commit message for more details.
967                          */
968                         if (max_fd < 0 &&
969                             (select_timeout.tv_sec > 0 ||
970                              select_timeout.tv_usec > 50000)) {
971                                 select_timeout.tv_sec  = 0;
972                                 select_timeout.tv_usec = 50000;
973                         }
974
975                         select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
976                 }
977         }
978 #else
979         while (slot->in_use) {
980                 slot->curl_result = curl_easy_perform(slot->curl);
981                 finish_active_slot(slot);
982         }
983 #endif
984 }
985
986 static void release_active_slot(struct active_request_slot *slot)
987 {
988         closedown_active_slot(slot);
989         if (slot->curl && curl_session_count > min_curl_sessions) {
990 #ifdef USE_CURL_MULTI
991                 curl_multi_remove_handle(curlm, slot->curl);
992 #endif
993                 curl_easy_cleanup(slot->curl);
994                 slot->curl = NULL;
995                 curl_session_count--;
996         }
997 #ifdef USE_CURL_MULTI
998         fill_active_slots();
999 #endif
1000 }
1001
1002 void finish_all_active_slots(void)
1003 {
1004         struct active_request_slot *slot = active_queue_head;
1005
1006         while (slot != NULL)
1007                 if (slot->in_use) {
1008                         run_active_slot(slot);
1009                         slot = active_queue_head;
1010                 } else {
1011                         slot = slot->next;
1012                 }
1013 }
1014
1015 /* Helpers for modifying and creating URLs */
1016 static inline int needs_quote(int ch)
1017 {
1018         if (((ch >= 'A') && (ch <= 'Z'))
1019                         || ((ch >= 'a') && (ch <= 'z'))
1020                         || ((ch >= '0') && (ch <= '9'))
1021                         || (ch == '/')
1022                         || (ch == '-')
1023                         || (ch == '.'))
1024                 return 0;
1025         return 1;
1026 }
1027
1028 static char *quote_ref_url(const char *base, const char *ref)
1029 {
1030         struct strbuf buf = STRBUF_INIT;
1031         const char *cp;
1032         int ch;
1033
1034         end_url_with_slash(&buf, base);
1035
1036         for (cp = ref; (ch = *cp) != 0; cp++)
1037                 if (needs_quote(ch))
1038                         strbuf_addf(&buf, "%%%02x", ch);
1039                 else
1040                         strbuf_addch(&buf, *cp);
1041
1042         return strbuf_detach(&buf, NULL);
1043 }
1044
1045 void append_remote_object_url(struct strbuf *buf, const char *url,
1046                               const char *hex,
1047                               int only_two_digit_prefix)
1048 {
1049         end_url_with_slash(buf, url);
1050
1051         strbuf_addf(buf, "objects/%.*s/", 2, hex);
1052         if (!only_two_digit_prefix)
1053                 strbuf_addf(buf, "%s", hex+2);
1054 }
1055
1056 char *get_remote_object_url(const char *url, const char *hex,
1057                             int only_two_digit_prefix)
1058 {
1059         struct strbuf buf = STRBUF_INIT;
1060         append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
1061         return strbuf_detach(&buf, NULL);
1062 }
1063
1064 static int handle_curl_result(struct slot_results *results)
1065 {
1066         /*
1067          * If we see a failing http code with CURLE_OK, we have turned off
1068          * FAILONERROR (to keep the server's custom error response), and should
1069          * translate the code into failure here.
1070          */
1071         if (results->curl_result == CURLE_OK &&
1072             results->http_code >= 400) {
1073                 results->curl_result = CURLE_HTTP_RETURNED_ERROR;
1074                 /*
1075                  * Normally curl will already have put the "reason phrase"
1076                  * from the server into curl_errorstr; unfortunately without
1077                  * FAILONERROR it is lost, so we can give only the numeric
1078                  * status code.
1079                  */
1080                 snprintf(curl_errorstr, sizeof(curl_errorstr),
1081                          "The requested URL returned error: %ld",
1082                          results->http_code);
1083         }
1084
1085         if (results->curl_result == CURLE_OK) {
1086                 credential_approve(&http_auth);
1087                 if (proxy_auth.password)
1088                         credential_approve(&proxy_auth);
1089                 return HTTP_OK;
1090         } else if (missing_target(results))
1091                 return HTTP_MISSING_TARGET;
1092         else if (results->http_code == 401) {
1093                 if (http_auth.username && http_auth.password) {
1094                         credential_reject(&http_auth);
1095                         return HTTP_NOAUTH;
1096                 } else {
1097 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1098                         http_auth_methods &= ~CURLAUTH_GSSNEGOTIATE;
1099 #endif
1100                         return HTTP_REAUTH;
1101                 }
1102         } else {
1103                 if (results->http_connectcode == 407)
1104                         credential_reject(&proxy_auth);
1105 #if LIBCURL_VERSION_NUM >= 0x070c00
1106                 if (!curl_errorstr[0])
1107                         strlcpy(curl_errorstr,
1108                                 curl_easy_strerror(results->curl_result),
1109                                 sizeof(curl_errorstr));
1110 #endif
1111                 return HTTP_ERROR;
1112         }
1113 }
1114
1115 int run_one_slot(struct active_request_slot *slot,
1116                  struct slot_results *results)
1117 {
1118         slot->results = results;
1119         if (!start_active_slot(slot)) {
1120                 snprintf(curl_errorstr, sizeof(curl_errorstr),
1121                          "failed to start HTTP request");
1122                 return HTTP_START_FAILED;
1123         }
1124
1125         run_active_slot(slot);
1126         return handle_curl_result(results);
1127 }
1128
1129 static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
1130 {
1131         char *ptr;
1132         CURLcode ret;
1133
1134         strbuf_reset(buf);
1135         ret = curl_easy_getinfo(curl, info, &ptr);
1136         if (!ret && ptr)
1137                 strbuf_addstr(buf, ptr);
1138         return ret;
1139 }
1140
1141 /*
1142  * Check for and extract a content-type parameter. "raw"
1143  * should be positioned at the start of the potential
1144  * parameter, with any whitespace already removed.
1145  *
1146  * "name" is the name of the parameter. The value is appended
1147  * to "out".
1148  */
1149 static int extract_param(const char *raw, const char *name,
1150                          struct strbuf *out)
1151 {
1152         size_t len = strlen(name);
1153
1154         if (strncasecmp(raw, name, len))
1155                 return -1;
1156         raw += len;
1157
1158         if (*raw != '=')
1159                 return -1;
1160         raw++;
1161
1162         while (*raw && !isspace(*raw) && *raw != ';')
1163                 strbuf_addch(out, *raw++);
1164         return 0;
1165 }
1166
1167 /*
1168  * Extract a normalized version of the content type, with any
1169  * spaces suppressed, all letters lowercased, and no trailing ";"
1170  * or parameters.
1171  *
1172  * Note that we will silently remove even invalid whitespace. For
1173  * example, "text / plain" is specifically forbidden by RFC 2616,
1174  * but "text/plain" is the only reasonable output, and this keeps
1175  * our code simple.
1176  *
1177  * If the "charset" argument is not NULL, store the value of any
1178  * charset parameter there.
1179  *
1180  * Example:
1181  *   "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1182  *   "text / plain" -> "text/plain"
1183  */
1184 static void extract_content_type(struct strbuf *raw, struct strbuf *type,
1185                                  struct strbuf *charset)
1186 {
1187         const char *p;
1188
1189         strbuf_reset(type);
1190         strbuf_grow(type, raw->len);
1191         for (p = raw->buf; *p; p++) {
1192                 if (isspace(*p))
1193                         continue;
1194                 if (*p == ';') {
1195                         p++;
1196                         break;
1197                 }
1198                 strbuf_addch(type, tolower(*p));
1199         }
1200
1201         if (!charset)
1202                 return;
1203
1204         strbuf_reset(charset);
1205         while (*p) {
1206                 while (isspace(*p) || *p == ';')
1207                         p++;
1208                 if (!extract_param(p, "charset", charset))
1209                         return;
1210                 while (*p && !isspace(*p))
1211                         p++;
1212         }
1213
1214         if (!charset->len && starts_with(type->buf, "text/"))
1215                 strbuf_addstr(charset, "ISO-8859-1");
1216 }
1217
1218 static void write_accept_language(struct strbuf *buf)
1219 {
1220         /*
1221          * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1222          * that, q-value will be smaller than 0.001, the minimum q-value the
1223          * HTTP specification allows. See
1224          * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1225          */
1226         const int MAX_DECIMAL_PLACES = 3;
1227         const int MAX_LANGUAGE_TAGS = 1000;
1228         const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE = 4000;
1229         char **language_tags = NULL;
1230         int num_langs = 0;
1231         const char *s = get_preferred_languages();
1232         int i;
1233         struct strbuf tag = STRBUF_INIT;
1234
1235         /* Don't add Accept-Language header if no language is preferred. */
1236         if (!s)
1237                 return;
1238
1239         /*
1240          * Split the colon-separated string of preferred languages into
1241          * language_tags array.
1242          */
1243         do {
1244                 /* collect language tag */
1245                 for (; *s && (isalnum(*s) || *s == '_'); s++)
1246                         strbuf_addch(&tag, *s == '_' ? '-' : *s);
1247
1248                 /* skip .codeset, @modifier and any other unnecessary parts */
1249                 while (*s && *s != ':')
1250                         s++;
1251
1252                 if (tag.len) {
1253                         num_langs++;
1254                         REALLOC_ARRAY(language_tags, num_langs);
1255                         language_tags[num_langs - 1] = strbuf_detach(&tag, NULL);
1256                         if (num_langs >= MAX_LANGUAGE_TAGS - 1) /* -1 for '*' */
1257                                 break;
1258                 }
1259         } while (*s++);
1260
1261         /* write Accept-Language header into buf */
1262         if (num_langs) {
1263                 int last_buf_len = 0;
1264                 int max_q;
1265                 int decimal_places;
1266                 char q_format[32];
1267
1268                 /* add '*' */
1269                 REALLOC_ARRAY(language_tags, num_langs + 1);
1270                 language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */
1271
1272                 /* compute decimal_places */
1273                 for (max_q = 1, decimal_places = 0;
1274                      max_q < num_langs && decimal_places <= MAX_DECIMAL_PLACES;
1275                      decimal_places++, max_q *= 10)
1276                         ;
1277
1278                 xsnprintf(q_format, sizeof(q_format), ";q=0.%%0%dd", decimal_places);
1279
1280                 strbuf_addstr(buf, "Accept-Language: ");
1281
1282                 for (i = 0; i < num_langs; i++) {
1283                         if (i > 0)
1284                                 strbuf_addstr(buf, ", ");
1285
1286                         strbuf_addstr(buf, language_tags[i]);
1287
1288                         if (i > 0)
1289                                 strbuf_addf(buf, q_format, max_q - i);
1290
1291                         if (buf->len > MAX_ACCEPT_LANGUAGE_HEADER_SIZE) {
1292                                 strbuf_remove(buf, last_buf_len, buf->len - last_buf_len);
1293                                 break;
1294                         }
1295
1296                         last_buf_len = buf->len;
1297                 }
1298         }
1299
1300         /* free language tags -- last one is a static '*' */
1301         for (i = 0; i < num_langs - 1; i++)
1302                 free(language_tags[i]);
1303         free(language_tags);
1304 }
1305
1306 /*
1307  * Get an Accept-Language header which indicates user's preferred languages.
1308  *
1309  * Examples:
1310  *   LANGUAGE= -> ""
1311  *   LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1312  *   LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1313  *   LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1314  *   LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1315  *   LANGUAGE= LANG=C -> ""
1316  */
1317 static const char *get_accept_language(void)
1318 {
1319         if (!cached_accept_language) {
1320                 struct strbuf buf = STRBUF_INIT;
1321                 write_accept_language(&buf);
1322                 if (buf.len > 0)
1323                         cached_accept_language = strbuf_detach(&buf, NULL);
1324         }
1325
1326         return cached_accept_language;
1327 }
1328
1329 static void http_opt_request_remainder(CURL *curl, off_t pos)
1330 {
1331         char buf[128];
1332         xsnprintf(buf, sizeof(buf), "%"PRIuMAX"-", (uintmax_t)pos);
1333         curl_easy_setopt(curl, CURLOPT_RANGE, buf);
1334 }
1335
1336 /* http_request() targets */
1337 #define HTTP_REQUEST_STRBUF     0
1338 #define HTTP_REQUEST_FILE       1
1339
1340 static int http_request(const char *url,
1341                         void *result, int target,
1342                         const struct http_get_options *options)
1343 {
1344         struct active_request_slot *slot;
1345         struct slot_results results;
1346         struct curl_slist *headers = NULL;
1347         struct strbuf buf = STRBUF_INIT;
1348         const char *accept_language;
1349         int ret;
1350
1351         slot = get_active_slot();
1352         curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1353
1354         if (result == NULL) {
1355                 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
1356         } else {
1357                 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1358                 curl_easy_setopt(slot->curl, CURLOPT_FILE, result);
1359
1360                 if (target == HTTP_REQUEST_FILE) {
1361                         off_t posn = ftello(result);
1362                         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1363                                          fwrite);
1364                         if (posn > 0)
1365                                 http_opt_request_remainder(slot->curl, posn);
1366                 } else
1367                         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1368                                          fwrite_buffer);
1369         }
1370
1371         accept_language = get_accept_language();
1372
1373         if (accept_language)
1374                 headers = curl_slist_append(headers, accept_language);
1375
1376         strbuf_addstr(&buf, "Pragma:");
1377         if (options && options->no_cache)
1378                 strbuf_addstr(&buf, " no-cache");
1379         if (options && options->keep_error)
1380                 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1381
1382         headers = curl_slist_append(headers, buf.buf);
1383
1384         curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1385         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1386         curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
1387
1388         ret = run_one_slot(slot, &results);
1389
1390         if (options && options->content_type) {
1391                 struct strbuf raw = STRBUF_INIT;
1392                 curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, &raw);
1393                 extract_content_type(&raw, options->content_type,
1394                                      options->charset);
1395                 strbuf_release(&raw);
1396         }
1397
1398         if (options && options->effective_url)
1399                 curlinfo_strbuf(slot->curl, CURLINFO_EFFECTIVE_URL,
1400                                 options->effective_url);
1401
1402         curl_slist_free_all(headers);
1403         strbuf_release(&buf);
1404
1405         return ret;
1406 }
1407
1408 /*
1409  * Update the "base" url to a more appropriate value, as deduced by
1410  * redirects seen when requesting a URL starting with "url".
1411  *
1412  * The "asked" parameter is a URL that we asked curl to access, and must begin
1413  * with "base".
1414  *
1415  * The "got" parameter is the URL that curl reported to us as where we ended
1416  * up.
1417  *
1418  * Returns 1 if we updated the base url, 0 otherwise.
1419  *
1420  * Our basic strategy is to compare "base" and "asked" to find the bits
1421  * specific to our request. We then strip those bits off of "got" to yield the
1422  * new base. So for example, if our base is "http://example.com/foo.git",
1423  * and we ask for "http://example.com/foo.git/info/refs", we might end up
1424  * with "https://other.example.com/foo.git/info/refs". We would want the
1425  * new URL to become "https://other.example.com/foo.git".
1426  *
1427  * Note that this assumes a sane redirect scheme. It's entirely possible
1428  * in the example above to end up at a URL that does not even end in
1429  * "info/refs".  In such a case we simply punt, as there is not much we can
1430  * do (and such a scheme is unlikely to represent a real git repository,
1431  * which means we are likely about to abort anyway).
1432  */
1433 static int update_url_from_redirect(struct strbuf *base,
1434                                     const char *asked,
1435                                     const struct strbuf *got)
1436 {
1437         const char *tail;
1438         size_t tail_len;
1439
1440         if (!strcmp(asked, got->buf))
1441                 return 0;
1442
1443         if (!skip_prefix(asked, base->buf, &tail))
1444                 die("BUG: update_url_from_redirect: %s is not a superset of %s",
1445                     asked, base->buf);
1446
1447         tail_len = strlen(tail);
1448
1449         if (got->len < tail_len ||
1450             strcmp(tail, got->buf + got->len - tail_len))
1451                 return 0; /* insane redirect scheme */
1452
1453         strbuf_reset(base);
1454         strbuf_add(base, got->buf, got->len - tail_len);
1455         return 1;
1456 }
1457
1458 static int http_request_reauth(const char *url,
1459                                void *result, int target,
1460                                struct http_get_options *options)
1461 {
1462         int ret = http_request(url, result, target, options);
1463
1464         if (options && options->effective_url && options->base_url) {
1465                 if (update_url_from_redirect(options->base_url,
1466                                              url, options->effective_url)) {
1467                         credential_from_url(&http_auth, options->base_url->buf);
1468                         url = options->effective_url->buf;
1469                 }
1470         }
1471
1472         if (ret != HTTP_REAUTH)
1473                 return ret;
1474
1475         /*
1476          * If we are using KEEP_ERROR, the previous request may have
1477          * put cruft into our output stream; we should clear it out before
1478          * making our next request. We only know how to do this for
1479          * the strbuf case, but that is enough to satisfy current callers.
1480          */
1481         if (options && options->keep_error) {
1482                 switch (target) {
1483                 case HTTP_REQUEST_STRBUF:
1484                         strbuf_reset(result);
1485                         break;
1486                 default:
1487                         die("BUG: HTTP_KEEP_ERROR is only supported with strbufs");
1488                 }
1489         }
1490
1491         credential_fill(&http_auth);
1492
1493         return http_request(url, result, target, options);
1494 }
1495
1496 int http_get_strbuf(const char *url,
1497                     struct strbuf *result,
1498                     struct http_get_options *options)
1499 {
1500         return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
1501 }
1502
1503 /*
1504  * Downloads a URL and stores the result in the given file.
1505  *
1506  * If a previous interrupted download is detected (i.e. a previous temporary
1507  * file is still around) the download is resumed.
1508  */
1509 static int http_get_file(const char *url, const char *filename,
1510                          struct http_get_options *options)
1511 {
1512         int ret;
1513         struct strbuf tmpfile = STRBUF_INIT;
1514         FILE *result;
1515
1516         strbuf_addf(&tmpfile, "%s.temp", filename);
1517         result = fopen(tmpfile.buf, "a");
1518         if (!result) {
1519                 error("Unable to open local file %s", tmpfile.buf);
1520                 ret = HTTP_ERROR;
1521                 goto cleanup;
1522         }
1523
1524         ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
1525         fclose(result);
1526
1527         if (ret == HTTP_OK && finalize_object_file(tmpfile.buf, filename))
1528                 ret = HTTP_ERROR;
1529 cleanup:
1530         strbuf_release(&tmpfile);
1531         return ret;
1532 }
1533
1534 int http_fetch_ref(const char *base, struct ref *ref)
1535 {
1536         struct http_get_options options = {0};
1537         char *url;
1538         struct strbuf buffer = STRBUF_INIT;
1539         int ret = -1;
1540
1541         options.no_cache = 1;
1542
1543         url = quote_ref_url(base, ref->name);
1544         if (http_get_strbuf(url, &buffer, &options) == HTTP_OK) {
1545                 strbuf_rtrim(&buffer);
1546                 if (buffer.len == 40)
1547                         ret = get_oid_hex(buffer.buf, &ref->old_oid);
1548                 else if (starts_with(buffer.buf, "ref: ")) {
1549                         ref->symref = xstrdup(buffer.buf + 5);
1550                         ret = 0;
1551                 }
1552         }
1553
1554         strbuf_release(&buffer);
1555         free(url);
1556         return ret;
1557 }
1558
1559 /* Helpers for fetching packs */
1560 static char *fetch_pack_index(unsigned char *sha1, const char *base_url)
1561 {
1562         char *url, *tmp;
1563         struct strbuf buf = STRBUF_INIT;
1564
1565         if (http_is_verbose)
1566                 fprintf(stderr, "Getting index for pack %s\n", sha1_to_hex(sha1));
1567
1568         end_url_with_slash(&buf, base_url);
1569         strbuf_addf(&buf, "objects/pack/pack-%s.idx", sha1_to_hex(sha1));
1570         url = strbuf_detach(&buf, NULL);
1571
1572         strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(sha1));
1573         tmp = strbuf_detach(&buf, NULL);
1574
1575         if (http_get_file(url, tmp, NULL) != HTTP_OK) {
1576                 error("Unable to get pack index %s", url);
1577                 free(tmp);
1578                 tmp = NULL;
1579         }
1580
1581         free(url);
1582         return tmp;
1583 }
1584
1585 static int fetch_and_setup_pack_index(struct packed_git **packs_head,
1586         unsigned char *sha1, const char *base_url)
1587 {
1588         struct packed_git *new_pack;
1589         char *tmp_idx = NULL;
1590         int ret;
1591
1592         if (has_pack_index(sha1)) {
1593                 new_pack = parse_pack_index(sha1, sha1_pack_index_name(sha1));
1594                 if (!new_pack)
1595                         return -1; /* parse_pack_index() already issued error message */
1596                 goto add_pack;
1597         }
1598
1599         tmp_idx = fetch_pack_index(sha1, base_url);
1600         if (!tmp_idx)
1601                 return -1;
1602
1603         new_pack = parse_pack_index(sha1, tmp_idx);
1604         if (!new_pack) {
1605                 unlink(tmp_idx);
1606                 free(tmp_idx);
1607
1608                 return -1; /* parse_pack_index() already issued error message */
1609         }
1610
1611         ret = verify_pack_index(new_pack);
1612         if (!ret) {
1613                 close_pack_index(new_pack);
1614                 ret = finalize_object_file(tmp_idx, sha1_pack_index_name(sha1));
1615         }
1616         free(tmp_idx);
1617         if (ret)
1618                 return -1;
1619
1620 add_pack:
1621         new_pack->next = *packs_head;
1622         *packs_head = new_pack;
1623         return 0;
1624 }
1625
1626 int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
1627 {
1628         struct http_get_options options = {0};
1629         int ret = 0, i = 0;
1630         char *url, *data;
1631         struct strbuf buf = STRBUF_INIT;
1632         unsigned char sha1[20];
1633
1634         end_url_with_slash(&buf, base_url);
1635         strbuf_addstr(&buf, "objects/info/packs");
1636         url = strbuf_detach(&buf, NULL);
1637
1638         options.no_cache = 1;
1639         ret = http_get_strbuf(url, &buf, &options);
1640         if (ret != HTTP_OK)
1641                 goto cleanup;
1642
1643         data = buf.buf;
1644         while (i < buf.len) {
1645                 switch (data[i]) {
1646                 case 'P':
1647                         i++;
1648                         if (i + 52 <= buf.len &&
1649                             starts_with(data + i, " pack-") &&
1650                             starts_with(data + i + 46, ".pack\n")) {
1651                                 get_sha1_hex(data + i + 6, sha1);
1652                                 fetch_and_setup_pack_index(packs_head, sha1,
1653                                                       base_url);
1654                                 i += 51;
1655                                 break;
1656                         }
1657                 default:
1658                         while (i < buf.len && data[i] != '\n')
1659                                 i++;
1660                 }
1661                 i++;
1662         }
1663
1664 cleanup:
1665         free(url);
1666         return ret;
1667 }
1668
1669 void release_http_pack_request(struct http_pack_request *preq)
1670 {
1671         if (preq->packfile != NULL) {
1672                 fclose(preq->packfile);
1673                 preq->packfile = NULL;
1674         }
1675         preq->slot = NULL;
1676         free(preq->url);
1677         free(preq);
1678 }
1679
1680 int finish_http_pack_request(struct http_pack_request *preq)
1681 {
1682         struct packed_git **lst;
1683         struct packed_git *p = preq->target;
1684         char *tmp_idx;
1685         size_t len;
1686         struct child_process ip = CHILD_PROCESS_INIT;
1687         const char *ip_argv[8];
1688
1689         close_pack_index(p);
1690
1691         fclose(preq->packfile);
1692         preq->packfile = NULL;
1693
1694         lst = preq->lst;
1695         while (*lst != p)
1696                 lst = &((*lst)->next);
1697         *lst = (*lst)->next;
1698
1699         if (!strip_suffix(preq->tmpfile, ".pack.temp", &len))
1700                 die("BUG: pack tmpfile does not end in .pack.temp?");
1701         tmp_idx = xstrfmt("%.*s.idx.temp", (int)len, preq->tmpfile);
1702
1703         ip_argv[0] = "index-pack";
1704         ip_argv[1] = "-o";
1705         ip_argv[2] = tmp_idx;
1706         ip_argv[3] = preq->tmpfile;
1707         ip_argv[4] = NULL;
1708
1709         ip.argv = ip_argv;
1710         ip.git_cmd = 1;
1711         ip.no_stdin = 1;
1712         ip.no_stdout = 1;
1713
1714         if (run_command(&ip)) {
1715                 unlink(preq->tmpfile);
1716                 unlink(tmp_idx);
1717                 free(tmp_idx);
1718                 return -1;
1719         }
1720
1721         unlink(sha1_pack_index_name(p->sha1));
1722
1723         if (finalize_object_file(preq->tmpfile, sha1_pack_name(p->sha1))
1724          || finalize_object_file(tmp_idx, sha1_pack_index_name(p->sha1))) {
1725                 free(tmp_idx);
1726                 return -1;
1727         }
1728
1729         install_packed_git(p);
1730         free(tmp_idx);
1731         return 0;
1732 }
1733
1734 struct http_pack_request *new_http_pack_request(
1735         struct packed_git *target, const char *base_url)
1736 {
1737         off_t prev_posn = 0;
1738         struct strbuf buf = STRBUF_INIT;
1739         struct http_pack_request *preq;
1740
1741         preq = xcalloc(1, sizeof(*preq));
1742         preq->target = target;
1743
1744         end_url_with_slash(&buf, base_url);
1745         strbuf_addf(&buf, "objects/pack/pack-%s.pack",
1746                 sha1_to_hex(target->sha1));
1747         preq->url = strbuf_detach(&buf, NULL);
1748
1749         snprintf(preq->tmpfile, sizeof(preq->tmpfile), "%s.temp",
1750                 sha1_pack_name(target->sha1));
1751         preq->packfile = fopen(preq->tmpfile, "a");
1752         if (!preq->packfile) {
1753                 error("Unable to open local file %s for pack",
1754                       preq->tmpfile);
1755                 goto abort;
1756         }
1757
1758         preq->slot = get_active_slot();
1759         curl_easy_setopt(preq->slot->curl, CURLOPT_FILE, preq->packfile);
1760         curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
1761         curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
1762         curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
1763                 no_pragma_header);
1764
1765         /*
1766          * If there is data present from a previous transfer attempt,
1767          * resume where it left off
1768          */
1769         prev_posn = ftello(preq->packfile);
1770         if (prev_posn>0) {
1771                 if (http_is_verbose)
1772                         fprintf(stderr,
1773                                 "Resuming fetch of pack %s at byte %"PRIuMAX"\n",
1774                                 sha1_to_hex(target->sha1), (uintmax_t)prev_posn);
1775                 http_opt_request_remainder(preq->slot->curl, prev_posn);
1776         }
1777
1778         return preq;
1779
1780 abort:
1781         free(preq->url);
1782         free(preq);
1783         return NULL;
1784 }
1785
1786 /* Helpers for fetching objects (loose) */
1787 static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
1788                                void *data)
1789 {
1790         unsigned char expn[4096];
1791         size_t size = eltsize * nmemb;
1792         int posn = 0;
1793         struct http_object_request *freq =
1794                 (struct http_object_request *)data;
1795         do {
1796                 ssize_t retval = xwrite(freq->localfile,
1797                                         (char *) ptr + posn, size - posn);
1798                 if (retval < 0)
1799                         return posn;
1800                 posn += retval;
1801         } while (posn < size);
1802
1803         freq->stream.avail_in = size;
1804         freq->stream.next_in = (void *)ptr;
1805         do {
1806                 freq->stream.next_out = expn;
1807                 freq->stream.avail_out = sizeof(expn);
1808                 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
1809                 git_SHA1_Update(&freq->c, expn,
1810                                 sizeof(expn) - freq->stream.avail_out);
1811         } while (freq->stream.avail_in && freq->zret == Z_OK);
1812         return size;
1813 }
1814
1815 struct http_object_request *new_http_object_request(const char *base_url,
1816         unsigned char *sha1)
1817 {
1818         char *hex = sha1_to_hex(sha1);
1819         const char *filename;
1820         char prevfile[PATH_MAX];
1821         int prevlocal;
1822         char prev_buf[PREV_BUF_SIZE];
1823         ssize_t prev_read = 0;
1824         off_t prev_posn = 0;
1825         struct http_object_request *freq;
1826
1827         freq = xcalloc(1, sizeof(*freq));
1828         hashcpy(freq->sha1, sha1);
1829         freq->localfile = -1;
1830
1831         filename = sha1_file_name(sha1);
1832         snprintf(freq->tmpfile, sizeof(freq->tmpfile),
1833                  "%s.temp", filename);
1834
1835         snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
1836         unlink_or_warn(prevfile);
1837         rename(freq->tmpfile, prevfile);
1838         unlink_or_warn(freq->tmpfile);
1839
1840         if (freq->localfile != -1)
1841                 error("fd leakage in start: %d", freq->localfile);
1842         freq->localfile = open(freq->tmpfile,
1843                                O_WRONLY | O_CREAT | O_EXCL, 0666);
1844         /*
1845          * This could have failed due to the "lazy directory creation";
1846          * try to mkdir the last path component.
1847          */
1848         if (freq->localfile < 0 && errno == ENOENT) {
1849                 char *dir = strrchr(freq->tmpfile, '/');
1850                 if (dir) {
1851                         *dir = 0;
1852                         mkdir(freq->tmpfile, 0777);
1853                         *dir = '/';
1854                 }
1855                 freq->localfile = open(freq->tmpfile,
1856                                        O_WRONLY | O_CREAT | O_EXCL, 0666);
1857         }
1858
1859         if (freq->localfile < 0) {
1860                 error("Couldn't create temporary file %s: %s",
1861                       freq->tmpfile, strerror(errno));
1862                 goto abort;
1863         }
1864
1865         git_inflate_init(&freq->stream);
1866
1867         git_SHA1_Init(&freq->c);
1868
1869         freq->url = get_remote_object_url(base_url, hex, 0);
1870
1871         /*
1872          * If a previous temp file is present, process what was already
1873          * fetched.
1874          */
1875         prevlocal = open(prevfile, O_RDONLY);
1876         if (prevlocal != -1) {
1877                 do {
1878                         prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
1879                         if (prev_read>0) {
1880                                 if (fwrite_sha1_file(prev_buf,
1881                                                      1,
1882                                                      prev_read,
1883                                                      freq) == prev_read) {
1884                                         prev_posn += prev_read;
1885                                 } else {
1886                                         prev_read = -1;
1887                                 }
1888                         }
1889                 } while (prev_read > 0);
1890                 close(prevlocal);
1891         }
1892         unlink_or_warn(prevfile);
1893
1894         /*
1895          * Reset inflate/SHA1 if there was an error reading the previous temp
1896          * file; also rewind to the beginning of the local file.
1897          */
1898         if (prev_read == -1) {
1899                 memset(&freq->stream, 0, sizeof(freq->stream));
1900                 git_inflate_init(&freq->stream);
1901                 git_SHA1_Init(&freq->c);
1902                 if (prev_posn>0) {
1903                         prev_posn = 0;
1904                         lseek(freq->localfile, 0, SEEK_SET);
1905                         if (ftruncate(freq->localfile, 0) < 0) {
1906                                 error("Couldn't truncate temporary file %s: %s",
1907                                           freq->tmpfile, strerror(errno));
1908                                 goto abort;
1909                         }
1910                 }
1911         }
1912
1913         freq->slot = get_active_slot();
1914
1915         curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq);
1916         curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
1917         curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
1918         curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
1919         curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1920
1921         /*
1922          * If we have successfully processed data from a previous fetch
1923          * attempt, only fetch the data we don't already have.
1924          */
1925         if (prev_posn>0) {
1926                 if (http_is_verbose)
1927                         fprintf(stderr,
1928                                 "Resuming fetch of object %s at byte %"PRIuMAX"\n",
1929                                 hex, (uintmax_t)prev_posn);
1930                 http_opt_request_remainder(freq->slot->curl, prev_posn);
1931         }
1932
1933         return freq;
1934
1935 abort:
1936         free(freq->url);
1937         free(freq);
1938         return NULL;
1939 }
1940
1941 void process_http_object_request(struct http_object_request *freq)
1942 {
1943         if (freq->slot == NULL)
1944                 return;
1945         freq->curl_result = freq->slot->curl_result;
1946         freq->http_code = freq->slot->http_code;
1947         freq->slot = NULL;
1948 }
1949
1950 int finish_http_object_request(struct http_object_request *freq)
1951 {
1952         struct stat st;
1953
1954         close(freq->localfile);
1955         freq->localfile = -1;
1956
1957         process_http_object_request(freq);
1958
1959         if (freq->http_code == 416) {
1960                 warning("requested range invalid; we may already have all the data.");
1961         } else if (freq->curl_result != CURLE_OK) {
1962                 if (stat(freq->tmpfile, &st) == 0)
1963                         if (st.st_size == 0)
1964                                 unlink_or_warn(freq->tmpfile);
1965                 return -1;
1966         }
1967
1968         git_inflate_end(&freq->stream);
1969         git_SHA1_Final(freq->real_sha1, &freq->c);
1970         if (freq->zret != Z_STREAM_END) {
1971                 unlink_or_warn(freq->tmpfile);
1972                 return -1;
1973         }
1974         if (hashcmp(freq->sha1, freq->real_sha1)) {
1975                 unlink_or_warn(freq->tmpfile);
1976                 return -1;
1977         }
1978         freq->rename =
1979                 finalize_object_file(freq->tmpfile, sha1_file_name(freq->sha1));
1980
1981         return freq->rename;
1982 }
1983
1984 void abort_http_object_request(struct http_object_request *freq)
1985 {
1986         unlink_or_warn(freq->tmpfile);
1987
1988         release_http_object_request(freq);
1989 }
1990
1991 void release_http_object_request(struct http_object_request *freq)
1992 {
1993         if (freq->localfile != -1) {
1994                 close(freq->localfile);
1995                 freq->localfile = -1;
1996         }
1997         if (freq->url != NULL) {
1998                 free(freq->url);
1999                 freq->url = NULL;
2000         }
2001         if (freq->slot != NULL) {
2002                 freq->slot->callback_func = NULL;
2003                 freq->slot->callback_data = NULL;
2004                 release_active_slot(freq->slot);
2005                 freq->slot = NULL;
2006         }
2007 }