list-objects-filter: delay parsing of sparse oid
[git] / http.c
1 #include "git-compat-util.h"
2 #include "http.h"
3 #include "config.h"
4 #include "pack.h"
5 #include "sideband.h"
6 #include "run-command.h"
7 #include "url.h"
8 #include "urlmatch.h"
9 #include "credential.h"
10 #include "version.h"
11 #include "pkt-line.h"
12 #include "gettext.h"
13 #include "transport.h"
14 #include "packfile.h"
15 #include "protocol.h"
16 #include "string-list.h"
17 #include "object-store.h"
18
19 static struct trace_key trace_curl = TRACE_KEY_INIT(CURL);
20 static int trace_curl_data = 1;
21 static struct string_list cookies_to_redact = STRING_LIST_INIT_DUP;
22 #if LIBCURL_VERSION_NUM >= 0x070a08
23 long int git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
24 #else
25 long int git_curl_ipresolve;
26 #endif
27 int active_requests;
28 int http_is_verbose;
29 ssize_t http_post_buffer = 16 * LARGE_PACKET_MAX;
30
31 #if LIBCURL_VERSION_NUM >= 0x070a06
32 #define LIBCURL_CAN_HANDLE_AUTH_ANY
33 #endif
34
35 static int min_curl_sessions = 1;
36 static int curl_session_count;
37 #ifdef USE_CURL_MULTI
38 static int max_requests = -1;
39 static CURLM *curlm;
40 #endif
41 #ifndef NO_CURL_EASY_DUPHANDLE
42 static CURL *curl_default;
43 #endif
44
45 #define PREV_BUF_SIZE 4096
46
47 char curl_errorstr[CURL_ERROR_SIZE];
48
49 static int curl_ssl_verify = -1;
50 static int curl_ssl_try;
51 static const char *curl_http_version = NULL;
52 static const char *ssl_cert;
53 static const char *ssl_cipherlist;
54 static const char *ssl_version;
55 static struct {
56         const char *name;
57         long ssl_version;
58 } sslversions[] = {
59         { "sslv2", CURL_SSLVERSION_SSLv2 },
60         { "sslv3", CURL_SSLVERSION_SSLv3 },
61         { "tlsv1", CURL_SSLVERSION_TLSv1 },
62 #if LIBCURL_VERSION_NUM >= 0x072200
63         { "tlsv1.0", CURL_SSLVERSION_TLSv1_0 },
64         { "tlsv1.1", CURL_SSLVERSION_TLSv1_1 },
65         { "tlsv1.2", CURL_SSLVERSION_TLSv1_2 },
66 #endif
67 #if LIBCURL_VERSION_NUM >= 0x073400
68         { "tlsv1.3", CURL_SSLVERSION_TLSv1_3 },
69 #endif
70 };
71 #if LIBCURL_VERSION_NUM >= 0x070903
72 static const char *ssl_key;
73 #endif
74 #if LIBCURL_VERSION_NUM >= 0x070908
75 static const char *ssl_capath;
76 #endif
77 #if LIBCURL_VERSION_NUM >= 0x071304
78 static const char *curl_no_proxy;
79 #endif
80 #if LIBCURL_VERSION_NUM >= 0x072c00
81 static const char *ssl_pinnedkey;
82 #endif
83 static const char *ssl_cainfo;
84 static long curl_low_speed_limit = -1;
85 static long curl_low_speed_time = -1;
86 static int curl_ftp_no_epsv;
87 static const char *curl_http_proxy;
88 static const char *http_proxy_authmethod;
89 static struct {
90         const char *name;
91         long curlauth_param;
92 } proxy_authmethods[] = {
93         { "basic", CURLAUTH_BASIC },
94         { "digest", CURLAUTH_DIGEST },
95         { "negotiate", CURLAUTH_GSSNEGOTIATE },
96         { "ntlm", CURLAUTH_NTLM },
97 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
98         { "anyauth", CURLAUTH_ANY },
99 #endif
100         /*
101          * CURLAUTH_DIGEST_IE has no corresponding command-line option in
102          * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
103          * here, too
104          */
105 };
106 #ifdef CURLGSSAPI_DELEGATION_FLAG
107 static const char *curl_deleg;
108 static struct {
109         const char *name;
110         long curl_deleg_param;
111 } curl_deleg_levels[] = {
112         { "none", CURLGSSAPI_DELEGATION_NONE },
113         { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG },
114         { "always", CURLGSSAPI_DELEGATION_FLAG },
115 };
116 #endif
117
118 static struct credential proxy_auth = CREDENTIAL_INIT;
119 static const char *curl_proxyuserpwd;
120 static const char *curl_cookie_file;
121 static int curl_save_cookies;
122 struct credential http_auth = CREDENTIAL_INIT;
123 static int http_proactive_auth;
124 static const char *user_agent;
125 static int curl_empty_auth = -1;
126
127 enum http_follow_config http_follow_config = HTTP_FOLLOW_INITIAL;
128
129 #if LIBCURL_VERSION_NUM >= 0x071700
130 /* Use CURLOPT_KEYPASSWD as is */
131 #elif LIBCURL_VERSION_NUM >= 0x070903
132 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
133 #else
134 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
135 #endif
136
137 static struct credential cert_auth = CREDENTIAL_INIT;
138 static int ssl_cert_password_required;
139 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
140 static unsigned long http_auth_methods = CURLAUTH_ANY;
141 static int http_auth_methods_restricted;
142 /* Modes for which empty_auth cannot actually help us. */
143 static unsigned long empty_auth_useless =
144         CURLAUTH_BASIC
145 #ifdef CURLAUTH_DIGEST_IE
146         | CURLAUTH_DIGEST_IE
147 #endif
148         | CURLAUTH_DIGEST;
149 #endif
150
151 static struct curl_slist *pragma_header;
152 static struct curl_slist *no_pragma_header;
153 static struct curl_slist *extra_http_headers;
154
155 static struct active_request_slot *active_queue_head;
156
157 static char *cached_accept_language;
158
159 static char *http_ssl_backend;
160
161 static int http_schannel_check_revoke = 1;
162 /*
163  * With the backend being set to `schannel`, setting sslCAinfo would override
164  * the Certificate Store in cURL v7.60.0 and later, which is not what we want
165  * by default.
166  */
167 static int http_schannel_use_ssl_cainfo;
168
169 size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
170 {
171         size_t size = eltsize * nmemb;
172         struct buffer *buffer = buffer_;
173
174         if (size > buffer->buf.len - buffer->posn)
175                 size = buffer->buf.len - buffer->posn;
176         memcpy(ptr, buffer->buf.buf + buffer->posn, size);
177         buffer->posn += size;
178
179         return size / eltsize;
180 }
181
182 #ifndef NO_CURL_IOCTL
183 curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
184 {
185         struct buffer *buffer = clientp;
186
187         switch (cmd) {
188         case CURLIOCMD_NOP:
189                 return CURLIOE_OK;
190
191         case CURLIOCMD_RESTARTREAD:
192                 buffer->posn = 0;
193                 return CURLIOE_OK;
194
195         default:
196                 return CURLIOE_UNKNOWNCMD;
197         }
198 }
199 #endif
200
201 size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
202 {
203         size_t size = eltsize * nmemb;
204         struct strbuf *buffer = buffer_;
205
206         strbuf_add(buffer, ptr, size);
207         return nmemb;
208 }
209
210 size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf)
211 {
212         return nmemb;
213 }
214
215 static void closedown_active_slot(struct active_request_slot *slot)
216 {
217         active_requests--;
218         slot->in_use = 0;
219 }
220
221 static void finish_active_slot(struct active_request_slot *slot)
222 {
223         closedown_active_slot(slot);
224         curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
225
226         if (slot->finished != NULL)
227                 (*slot->finished) = 1;
228
229         /* Store slot results so they can be read after the slot is reused */
230         if (slot->results != NULL) {
231                 slot->results->curl_result = slot->curl_result;
232                 slot->results->http_code = slot->http_code;
233 #if LIBCURL_VERSION_NUM >= 0x070a08
234                 curl_easy_getinfo(slot->curl, CURLINFO_HTTPAUTH_AVAIL,
235                                   &slot->results->auth_avail);
236 #else
237                 slot->results->auth_avail = 0;
238 #endif
239
240                 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CONNECTCODE,
241                         &slot->results->http_connectcode);
242         }
243
244         /* Run callback if appropriate */
245         if (slot->callback_func != NULL)
246                 slot->callback_func(slot->callback_data);
247 }
248
249 static void xmulti_remove_handle(struct active_request_slot *slot)
250 {
251 #ifdef USE_CURL_MULTI
252         curl_multi_remove_handle(curlm, slot->curl);
253 #endif
254 }
255
256 #ifdef USE_CURL_MULTI
257 static void process_curl_messages(void)
258 {
259         int num_messages;
260         struct active_request_slot *slot;
261         CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
262
263         while (curl_message != NULL) {
264                 if (curl_message->msg == CURLMSG_DONE) {
265                         int curl_result = curl_message->data.result;
266                         slot = active_queue_head;
267                         while (slot != NULL &&
268                                slot->curl != curl_message->easy_handle)
269                                 slot = slot->next;
270                         if (slot != NULL) {
271                                 xmulti_remove_handle(slot);
272                                 slot->curl_result = curl_result;
273                                 finish_active_slot(slot);
274                         } else {
275                                 fprintf(stderr, "Received DONE message for unknown request!\n");
276                         }
277                 } else {
278                         fprintf(stderr, "Unknown CURL message received: %d\n",
279                                 (int)curl_message->msg);
280                 }
281                 curl_message = curl_multi_info_read(curlm, &num_messages);
282         }
283 }
284 #endif
285
286 static int http_options(const char *var, const char *value, void *cb)
287 {
288         if (!strcmp("http.version", var)) {
289                 return git_config_string(&curl_http_version, var, value);
290         }
291         if (!strcmp("http.sslverify", var)) {
292                 curl_ssl_verify = git_config_bool(var, value);
293                 return 0;
294         }
295         if (!strcmp("http.sslcipherlist", var))
296                 return git_config_string(&ssl_cipherlist, var, value);
297         if (!strcmp("http.sslversion", var))
298                 return git_config_string(&ssl_version, var, value);
299         if (!strcmp("http.sslcert", var))
300                 return git_config_pathname(&ssl_cert, var, value);
301 #if LIBCURL_VERSION_NUM >= 0x070903
302         if (!strcmp("http.sslkey", var))
303                 return git_config_pathname(&ssl_key, var, value);
304 #endif
305 #if LIBCURL_VERSION_NUM >= 0x070908
306         if (!strcmp("http.sslcapath", var))
307                 return git_config_pathname(&ssl_capath, var, value);
308 #endif
309         if (!strcmp("http.sslcainfo", var))
310                 return git_config_pathname(&ssl_cainfo, var, value);
311         if (!strcmp("http.sslcertpasswordprotected", var)) {
312                 ssl_cert_password_required = git_config_bool(var, value);
313                 return 0;
314         }
315         if (!strcmp("http.ssltry", var)) {
316                 curl_ssl_try = git_config_bool(var, value);
317                 return 0;
318         }
319         if (!strcmp("http.sslbackend", var)) {
320                 free(http_ssl_backend);
321                 http_ssl_backend = xstrdup_or_null(value);
322                 return 0;
323         }
324
325         if (!strcmp("http.schannelcheckrevoke", var)) {
326                 http_schannel_check_revoke = git_config_bool(var, value);
327                 return 0;
328         }
329
330         if (!strcmp("http.schannelusesslcainfo", var)) {
331                 http_schannel_use_ssl_cainfo = git_config_bool(var, value);
332                 return 0;
333         }
334
335         if (!strcmp("http.minsessions", var)) {
336                 min_curl_sessions = git_config_int(var, value);
337 #ifndef USE_CURL_MULTI
338                 if (min_curl_sessions > 1)
339                         min_curl_sessions = 1;
340 #endif
341                 return 0;
342         }
343 #ifdef USE_CURL_MULTI
344         if (!strcmp("http.maxrequests", var)) {
345                 max_requests = git_config_int(var, value);
346                 return 0;
347         }
348 #endif
349         if (!strcmp("http.lowspeedlimit", var)) {
350                 curl_low_speed_limit = (long)git_config_int(var, value);
351                 return 0;
352         }
353         if (!strcmp("http.lowspeedtime", var)) {
354                 curl_low_speed_time = (long)git_config_int(var, value);
355                 return 0;
356         }
357
358         if (!strcmp("http.noepsv", var)) {
359                 curl_ftp_no_epsv = git_config_bool(var, value);
360                 return 0;
361         }
362         if (!strcmp("http.proxy", var))
363                 return git_config_string(&curl_http_proxy, var, value);
364
365         if (!strcmp("http.proxyauthmethod", var))
366                 return git_config_string(&http_proxy_authmethod, var, value);
367
368         if (!strcmp("http.cookiefile", var))
369                 return git_config_pathname(&curl_cookie_file, var, value);
370         if (!strcmp("http.savecookies", var)) {
371                 curl_save_cookies = git_config_bool(var, value);
372                 return 0;
373         }
374
375         if (!strcmp("http.postbuffer", var)) {
376                 http_post_buffer = git_config_ssize_t(var, value);
377                 if (http_post_buffer < 0)
378                         warning(_("negative value for http.postbuffer; defaulting to %d"), LARGE_PACKET_MAX);
379                 if (http_post_buffer < LARGE_PACKET_MAX)
380                         http_post_buffer = LARGE_PACKET_MAX;
381                 return 0;
382         }
383
384         if (!strcmp("http.useragent", var))
385                 return git_config_string(&user_agent, var, value);
386
387         if (!strcmp("http.emptyauth", var)) {
388                 if (value && !strcmp("auto", value))
389                         curl_empty_auth = -1;
390                 else
391                         curl_empty_auth = git_config_bool(var, value);
392                 return 0;
393         }
394
395         if (!strcmp("http.delegation", var)) {
396 #ifdef CURLGSSAPI_DELEGATION_FLAG
397                 return git_config_string(&curl_deleg, var, value);
398 #else
399                 warning(_("Delegation control is not supported with cURL < 7.22.0"));
400                 return 0;
401 #endif
402         }
403
404         if (!strcmp("http.pinnedpubkey", var)) {
405 #if LIBCURL_VERSION_NUM >= 0x072c00
406                 return git_config_pathname(&ssl_pinnedkey, var, value);
407 #else
408                 warning(_("Public key pinning not supported with cURL < 7.44.0"));
409                 return 0;
410 #endif
411         }
412
413         if (!strcmp("http.extraheader", var)) {
414                 if (!value) {
415                         return config_error_nonbool(var);
416                 } else if (!*value) {
417                         curl_slist_free_all(extra_http_headers);
418                         extra_http_headers = NULL;
419                 } else {
420                         extra_http_headers =
421                                 curl_slist_append(extra_http_headers, value);
422                 }
423                 return 0;
424         }
425
426         if (!strcmp("http.followredirects", var)) {
427                 if (value && !strcmp(value, "initial"))
428                         http_follow_config = HTTP_FOLLOW_INITIAL;
429                 else if (git_config_bool(var, value))
430                         http_follow_config = HTTP_FOLLOW_ALWAYS;
431                 else
432                         http_follow_config = HTTP_FOLLOW_NONE;
433                 return 0;
434         }
435
436         /* Fall back on the default ones */
437         return git_default_config(var, value, cb);
438 }
439
440 static int curl_empty_auth_enabled(void)
441 {
442         if (curl_empty_auth >= 0)
443                 return curl_empty_auth;
444
445 #ifndef LIBCURL_CAN_HANDLE_AUTH_ANY
446         /*
447          * Our libcurl is too old to do AUTH_ANY in the first place;
448          * just default to turning the feature off.
449          */
450 #else
451         /*
452          * In the automatic case, kick in the empty-auth
453          * hack as long as we would potentially try some
454          * method more exotic than "Basic" or "Digest".
455          *
456          * But only do this when this is our second or
457          * subsequent request, as by then we know what
458          * methods are available.
459          */
460         if (http_auth_methods_restricted &&
461             (http_auth_methods & ~empty_auth_useless))
462                 return 1;
463 #endif
464         return 0;
465 }
466
467 static void init_curl_http_auth(CURL *result)
468 {
469         if (!http_auth.username || !*http_auth.username) {
470                 if (curl_empty_auth_enabled())
471                         curl_easy_setopt(result, CURLOPT_USERPWD, ":");
472                 return;
473         }
474
475         credential_fill(&http_auth);
476
477 #if LIBCURL_VERSION_NUM >= 0x071301
478         curl_easy_setopt(result, CURLOPT_USERNAME, http_auth.username);
479         curl_easy_setopt(result, CURLOPT_PASSWORD, http_auth.password);
480 #else
481         {
482                 static struct strbuf up = STRBUF_INIT;
483                 /*
484                  * Note that we assume we only ever have a single set of
485                  * credentials in a given program run, so we do not have
486                  * to worry about updating this buffer, only setting its
487                  * initial value.
488                  */
489                 if (!up.len)
490                         strbuf_addf(&up, "%s:%s",
491                                 http_auth.username, http_auth.password);
492                 curl_easy_setopt(result, CURLOPT_USERPWD, up.buf);
493         }
494 #endif
495 }
496
497 /* *var must be free-able */
498 static void var_override(const char **var, char *value)
499 {
500         if (value) {
501                 free((void *)*var);
502                 *var = xstrdup(value);
503         }
504 }
505
506 static void set_proxyauth_name_password(CURL *result)
507 {
508 #if LIBCURL_VERSION_NUM >= 0x071301
509                 curl_easy_setopt(result, CURLOPT_PROXYUSERNAME,
510                         proxy_auth.username);
511                 curl_easy_setopt(result, CURLOPT_PROXYPASSWORD,
512                         proxy_auth.password);
513 #else
514                 struct strbuf s = STRBUF_INIT;
515
516                 strbuf_addstr_urlencode(&s, proxy_auth.username, 1);
517                 strbuf_addch(&s, ':');
518                 strbuf_addstr_urlencode(&s, proxy_auth.password, 1);
519                 curl_proxyuserpwd = strbuf_detach(&s, NULL);
520                 curl_easy_setopt(result, CURLOPT_PROXYUSERPWD, curl_proxyuserpwd);
521 #endif
522 }
523
524 static void init_curl_proxy_auth(CURL *result)
525 {
526         if (proxy_auth.username) {
527                 if (!proxy_auth.password)
528                         credential_fill(&proxy_auth);
529                 set_proxyauth_name_password(result);
530         }
531
532         var_override(&http_proxy_authmethod, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
533
534 #if LIBCURL_VERSION_NUM >= 0x070a07 /* CURLOPT_PROXYAUTH and CURLAUTH_ANY */
535         if (http_proxy_authmethod) {
536                 int i;
537                 for (i = 0; i < ARRAY_SIZE(proxy_authmethods); i++) {
538                         if (!strcmp(http_proxy_authmethod, proxy_authmethods[i].name)) {
539                                 curl_easy_setopt(result, CURLOPT_PROXYAUTH,
540                                                 proxy_authmethods[i].curlauth_param);
541                                 break;
542                         }
543                 }
544                 if (i == ARRAY_SIZE(proxy_authmethods)) {
545                         warning("unsupported proxy authentication method %s: using anyauth",
546                                         http_proxy_authmethod);
547                         curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
548                 }
549         }
550         else
551                 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
552 #endif
553 }
554
555 static int has_cert_password(void)
556 {
557         if (ssl_cert == NULL || ssl_cert_password_required != 1)
558                 return 0;
559         if (!cert_auth.password) {
560                 cert_auth.protocol = xstrdup("cert");
561                 cert_auth.username = xstrdup("");
562                 cert_auth.path = xstrdup(ssl_cert);
563                 credential_fill(&cert_auth);
564         }
565         return 1;
566 }
567
568 #if LIBCURL_VERSION_NUM >= 0x071900
569 static void set_curl_keepalive(CURL *c)
570 {
571         curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
572 }
573
574 #elif LIBCURL_VERSION_NUM >= 0x071000
575 static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
576 {
577         int ka = 1;
578         int rc;
579         socklen_t len = (socklen_t)sizeof(ka);
580
581         if (type != CURLSOCKTYPE_IPCXN)
582                 return 0;
583
584         rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
585         if (rc < 0)
586                 warning_errno("unable to set SO_KEEPALIVE on socket");
587
588         return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
589 }
590
591 static void set_curl_keepalive(CURL *c)
592 {
593         curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
594 }
595
596 #else
597 static void set_curl_keepalive(CURL *c)
598 {
599         /* not supported on older curl versions */
600 }
601 #endif
602
603 static void redact_sensitive_header(struct strbuf *header)
604 {
605         const char *sensitive_header;
606
607         if (skip_prefix(header->buf, "Authorization:", &sensitive_header) ||
608             skip_prefix(header->buf, "Proxy-Authorization:", &sensitive_header)) {
609                 /* The first token is the type, which is OK to log */
610                 while (isspace(*sensitive_header))
611                         sensitive_header++;
612                 while (*sensitive_header && !isspace(*sensitive_header))
613                         sensitive_header++;
614                 /* Everything else is opaque and possibly sensitive */
615                 strbuf_setlen(header,  sensitive_header - header->buf);
616                 strbuf_addstr(header, " <redacted>");
617         } else if (cookies_to_redact.nr &&
618                    skip_prefix(header->buf, "Cookie:", &sensitive_header)) {
619                 struct strbuf redacted_header = STRBUF_INIT;
620                 char *cookie;
621
622                 while (isspace(*sensitive_header))
623                         sensitive_header++;
624
625                 /*
626                  * The contents of header starting from sensitive_header will
627                  * subsequently be overridden, so it is fine to mutate this
628                  * string (hence the assignment to "char *").
629                  */
630                 cookie = (char *) sensitive_header;
631
632                 while (cookie) {
633                         char *equals;
634                         char *semicolon = strstr(cookie, "; ");
635                         if (semicolon)
636                                 *semicolon = 0;
637                         equals = strchrnul(cookie, '=');
638                         if (!equals) {
639                                 /* invalid cookie, just append and continue */
640                                 strbuf_addstr(&redacted_header, cookie);
641                                 continue;
642                         }
643                         *equals = 0; /* temporarily set to NUL for lookup */
644                         if (string_list_lookup(&cookies_to_redact, cookie)) {
645                                 strbuf_addstr(&redacted_header, cookie);
646                                 strbuf_addstr(&redacted_header, "=<redacted>");
647                         } else {
648                                 *equals = '=';
649                                 strbuf_addstr(&redacted_header, cookie);
650                         }
651                         if (semicolon) {
652                                 /*
653                                  * There are more cookies. (Or, for some
654                                  * reason, the input string ends in "; ".)
655                                  */
656                                 strbuf_addstr(&redacted_header, "; ");
657                                 cookie = semicolon + strlen("; ");
658                         } else {
659                                 cookie = NULL;
660                         }
661                 }
662
663                 strbuf_setlen(header, sensitive_header - header->buf);
664                 strbuf_addbuf(header, &redacted_header);
665         }
666 }
667
668 static void curl_dump_header(const char *text, unsigned char *ptr, size_t size, int hide_sensitive_header)
669 {
670         struct strbuf out = STRBUF_INIT;
671         struct strbuf **headers, **header;
672
673         strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
674                 text, (long)size, (long)size);
675         trace_strbuf(&trace_curl, &out);
676         strbuf_reset(&out);
677         strbuf_add(&out, ptr, size);
678         headers = strbuf_split_max(&out, '\n', 0);
679
680         for (header = headers; *header; header++) {
681                 if (hide_sensitive_header)
682                         redact_sensitive_header(*header);
683                 strbuf_insert((*header), 0, text, strlen(text));
684                 strbuf_insert((*header), strlen(text), ": ", 2);
685                 strbuf_rtrim((*header));
686                 strbuf_addch((*header), '\n');
687                 trace_strbuf(&trace_curl, (*header));
688         }
689         strbuf_list_free(headers);
690         strbuf_release(&out);
691 }
692
693 static void curl_dump_data(const char *text, unsigned char *ptr, size_t size)
694 {
695         size_t i;
696         struct strbuf out = STRBUF_INIT;
697         unsigned int width = 60;
698
699         strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
700                 text, (long)size, (long)size);
701         trace_strbuf(&trace_curl, &out);
702
703         for (i = 0; i < size; i += width) {
704                 size_t w;
705
706                 strbuf_reset(&out);
707                 strbuf_addf(&out, "%s: ", text);
708                 for (w = 0; (w < width) && (i + w < size); w++) {
709                         unsigned char ch = ptr[i + w];
710
711                         strbuf_addch(&out,
712                                        (ch >= 0x20) && (ch < 0x80)
713                                        ? ch : '.');
714                 }
715                 strbuf_addch(&out, '\n');
716                 trace_strbuf(&trace_curl, &out);
717         }
718         strbuf_release(&out);
719 }
720
721 static int curl_trace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp)
722 {
723         const char *text;
724         enum { NO_FILTER = 0, DO_FILTER = 1 };
725
726         switch (type) {
727         case CURLINFO_TEXT:
728                 trace_printf_key(&trace_curl, "== Info: %s", data);
729                 break;
730         case CURLINFO_HEADER_OUT:
731                 text = "=> Send header";
732                 curl_dump_header(text, (unsigned char *)data, size, DO_FILTER);
733                 break;
734         case CURLINFO_DATA_OUT:
735                 if (trace_curl_data) {
736                         text = "=> Send data";
737                         curl_dump_data(text, (unsigned char *)data, size);
738                 }
739                 break;
740         case CURLINFO_SSL_DATA_OUT:
741                 if (trace_curl_data) {
742                         text = "=> Send SSL data";
743                         curl_dump_data(text, (unsigned char *)data, size);
744                 }
745                 break;
746         case CURLINFO_HEADER_IN:
747                 text = "<= Recv header";
748                 curl_dump_header(text, (unsigned char *)data, size, NO_FILTER);
749                 break;
750         case CURLINFO_DATA_IN:
751                 if (trace_curl_data) {
752                         text = "<= Recv data";
753                         curl_dump_data(text, (unsigned char *)data, size);
754                 }
755                 break;
756         case CURLINFO_SSL_DATA_IN:
757                 if (trace_curl_data) {
758                         text = "<= Recv SSL data";
759                         curl_dump_data(text, (unsigned char *)data, size);
760                 }
761                 break;
762
763         default:                /* we ignore unknown types by default */
764                 return 0;
765         }
766         return 0;
767 }
768
769 void setup_curl_trace(CURL *handle)
770 {
771         if (!trace_want(&trace_curl))
772                 return;
773         curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L);
774         curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, curl_trace);
775         curl_easy_setopt(handle, CURLOPT_DEBUGDATA, NULL);
776 }
777
778 #ifdef CURLPROTO_HTTP
779 static long get_curl_allowed_protocols(int from_user)
780 {
781         long allowed_protocols = 0;
782
783         if (is_transport_allowed("http", from_user))
784                 allowed_protocols |= CURLPROTO_HTTP;
785         if (is_transport_allowed("https", from_user))
786                 allowed_protocols |= CURLPROTO_HTTPS;
787         if (is_transport_allowed("ftp", from_user))
788                 allowed_protocols |= CURLPROTO_FTP;
789         if (is_transport_allowed("ftps", from_user))
790                 allowed_protocols |= CURLPROTO_FTPS;
791
792         return allowed_protocols;
793 }
794 #endif
795
796 #if LIBCURL_VERSION_NUM >=0x072f00
797 static int get_curl_http_version_opt(const char *version_string, long *opt)
798 {
799         int i;
800         static struct {
801                 const char *name;
802                 long opt_token;
803         } choice[] = {
804                 { "HTTP/1.1", CURL_HTTP_VERSION_1_1 },
805                 { "HTTP/2", CURL_HTTP_VERSION_2 }
806         };
807
808         for (i = 0; i < ARRAY_SIZE(choice); i++) {
809                 if (!strcmp(version_string, choice[i].name)) {
810                         *opt = choice[i].opt_token;
811                         return 0;
812                 }
813         }
814
815         warning("unknown value given to http.version: '%s'", version_string);
816         return -1; /* not found */
817 }
818
819 #endif
820
821 static CURL *get_curl_handle(void)
822 {
823         CURL *result = curl_easy_init();
824
825         if (!result)
826                 die("curl_easy_init failed");
827
828         if (!curl_ssl_verify) {
829                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
830                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
831         } else {
832                 /* Verify authenticity of the peer's certificate */
833                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
834                 /* The name in the cert must match whom we tried to connect */
835                 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
836         }
837
838 #if LIBCURL_VERSION_NUM >= 0x072f00 // 7.47.0
839     if (curl_http_version) {
840                 long opt;
841                 if (!get_curl_http_version_opt(curl_http_version, &opt)) {
842                         /* Set request use http version */
843                         curl_easy_setopt(result, CURLOPT_HTTP_VERSION, opt);
844                 }
845     }
846 #endif
847
848 #if LIBCURL_VERSION_NUM >= 0x070907
849         curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
850 #endif
851 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
852         curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
853 #endif
854
855 #ifdef CURLGSSAPI_DELEGATION_FLAG
856         if (curl_deleg) {
857                 int i;
858                 for (i = 0; i < ARRAY_SIZE(curl_deleg_levels); i++) {
859                         if (!strcmp(curl_deleg, curl_deleg_levels[i].name)) {
860                                 curl_easy_setopt(result, CURLOPT_GSSAPI_DELEGATION,
861                                                 curl_deleg_levels[i].curl_deleg_param);
862                                 break;
863                         }
864                 }
865                 if (i == ARRAY_SIZE(curl_deleg_levels))
866                         warning("Unknown delegation method '%s': using default",
867                                 curl_deleg);
868         }
869 #endif
870
871         if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) &&
872             !http_schannel_check_revoke) {
873 #if LIBCURL_VERSION_NUM >= 0x072c00
874                 curl_easy_setopt(result, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
875 #else
876                 warning(_("CURLSSLOPT_NO_REVOKE not supported with cURL < 7.44.0"));
877 #endif
878         }
879
880         if (http_proactive_auth)
881                 init_curl_http_auth(result);
882
883         if (getenv("GIT_SSL_VERSION"))
884                 ssl_version = getenv("GIT_SSL_VERSION");
885         if (ssl_version && *ssl_version) {
886                 int i;
887                 for (i = 0; i < ARRAY_SIZE(sslversions); i++) {
888                         if (!strcmp(ssl_version, sslversions[i].name)) {
889                                 curl_easy_setopt(result, CURLOPT_SSLVERSION,
890                                                  sslversions[i].ssl_version);
891                                 break;
892                         }
893                 }
894                 if (i == ARRAY_SIZE(sslversions))
895                         warning("unsupported ssl version %s: using default",
896                                 ssl_version);
897         }
898
899         if (getenv("GIT_SSL_CIPHER_LIST"))
900                 ssl_cipherlist = getenv("GIT_SSL_CIPHER_LIST");
901         if (ssl_cipherlist != NULL && *ssl_cipherlist)
902                 curl_easy_setopt(result, CURLOPT_SSL_CIPHER_LIST,
903                                 ssl_cipherlist);
904
905         if (ssl_cert != NULL)
906                 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
907         if (has_cert_password())
908                 curl_easy_setopt(result, CURLOPT_KEYPASSWD, cert_auth.password);
909 #if LIBCURL_VERSION_NUM >= 0x070903
910         if (ssl_key != NULL)
911                 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
912 #endif
913 #if LIBCURL_VERSION_NUM >= 0x070908
914         if (ssl_capath != NULL)
915                 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
916 #endif
917 #if LIBCURL_VERSION_NUM >= 0x072c00
918         if (ssl_pinnedkey != NULL)
919                 curl_easy_setopt(result, CURLOPT_PINNEDPUBLICKEY, ssl_pinnedkey);
920 #endif
921         if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) &&
922             !http_schannel_use_ssl_cainfo) {
923                 curl_easy_setopt(result, CURLOPT_CAINFO, NULL);
924 #if LIBCURL_VERSION_NUM >= 0x073400
925                 curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, NULL);
926 #endif
927         } else if (ssl_cainfo != NULL)
928                 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
929
930         if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
931                 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
932                                  curl_low_speed_limit);
933                 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
934                                  curl_low_speed_time);
935         }
936
937         curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
938 #if LIBCURL_VERSION_NUM >= 0x071301
939         curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
940 #elif LIBCURL_VERSION_NUM >= 0x071101
941         curl_easy_setopt(result, CURLOPT_POST301, 1);
942 #endif
943 #ifdef CURLPROTO_HTTP
944         curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS,
945                          get_curl_allowed_protocols(0));
946         curl_easy_setopt(result, CURLOPT_PROTOCOLS,
947                          get_curl_allowed_protocols(-1));
948 #else
949         warning(_("Protocol restrictions not supported with cURL < 7.19.4"));
950 #endif
951         if (getenv("GIT_CURL_VERBOSE"))
952                 curl_easy_setopt(result, CURLOPT_VERBOSE, 1L);
953         setup_curl_trace(result);
954         if (getenv("GIT_TRACE_CURL_NO_DATA"))
955                 trace_curl_data = 0;
956         if (getenv("GIT_REDACT_COOKIES")) {
957                 string_list_split(&cookies_to_redact,
958                                   getenv("GIT_REDACT_COOKIES"), ',', -1);
959                 string_list_sort(&cookies_to_redact);
960         }
961
962         curl_easy_setopt(result, CURLOPT_USERAGENT,
963                 user_agent ? user_agent : git_user_agent());
964
965         if (curl_ftp_no_epsv)
966                 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
967
968 #ifdef CURLOPT_USE_SSL
969         if (curl_ssl_try)
970                 curl_easy_setopt(result, CURLOPT_USE_SSL, CURLUSESSL_TRY);
971 #endif
972
973         /*
974          * CURL also examines these variables as a fallback; but we need to query
975          * them here in order to decide whether to prompt for missing password (cf.
976          * init_curl_proxy_auth()).
977          *
978          * Unlike many other common environment variables, these are historically
979          * lowercase only. It appears that CURL did not know this and implemented
980          * only uppercase variants, which was later corrected to take both - with
981          * the exception of http_proxy, which is lowercase only also in CURL. As
982          * the lowercase versions are the historical quasi-standard, they take
983          * precedence here, as in CURL.
984          */
985         if (!curl_http_proxy) {
986                 if (http_auth.protocol && !strcmp(http_auth.protocol, "https")) {
987                         var_override(&curl_http_proxy, getenv("HTTPS_PROXY"));
988                         var_override(&curl_http_proxy, getenv("https_proxy"));
989                 } else {
990                         var_override(&curl_http_proxy, getenv("http_proxy"));
991                 }
992                 if (!curl_http_proxy) {
993                         var_override(&curl_http_proxy, getenv("ALL_PROXY"));
994                         var_override(&curl_http_proxy, getenv("all_proxy"));
995                 }
996         }
997
998         if (curl_http_proxy && curl_http_proxy[0] == '\0') {
999                 /*
1000                  * Handle case with the empty http.proxy value here to keep
1001                  * common code clean.
1002                  * NB: empty option disables proxying at all.
1003                  */
1004                 curl_easy_setopt(result, CURLOPT_PROXY, "");
1005         } else if (curl_http_proxy) {
1006 #if LIBCURL_VERSION_NUM >= 0x071800
1007                 if (starts_with(curl_http_proxy, "socks5h"))
1008                         curl_easy_setopt(result,
1009                                 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
1010                 else if (starts_with(curl_http_proxy, "socks5"))
1011                         curl_easy_setopt(result,
1012                                 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
1013                 else if (starts_with(curl_http_proxy, "socks4a"))
1014                         curl_easy_setopt(result,
1015                                 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
1016                 else if (starts_with(curl_http_proxy, "socks"))
1017                         curl_easy_setopt(result,
1018                                 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
1019 #endif
1020 #if LIBCURL_VERSION_NUM >= 0x073400
1021                 else if (starts_with(curl_http_proxy, "https"))
1022                         curl_easy_setopt(result,
1023                                 CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
1024 #endif
1025                 if (strstr(curl_http_proxy, "://"))
1026                         credential_from_url(&proxy_auth, curl_http_proxy);
1027                 else {
1028                         struct strbuf url = STRBUF_INIT;
1029                         strbuf_addf(&url, "http://%s", curl_http_proxy);
1030                         credential_from_url(&proxy_auth, url.buf);
1031                         strbuf_release(&url);
1032                 }
1033
1034                 if (!proxy_auth.host)
1035                         die("Invalid proxy URL '%s'", curl_http_proxy);
1036
1037                 curl_easy_setopt(result, CURLOPT_PROXY, proxy_auth.host);
1038 #if LIBCURL_VERSION_NUM >= 0x071304
1039                 var_override(&curl_no_proxy, getenv("NO_PROXY"));
1040                 var_override(&curl_no_proxy, getenv("no_proxy"));
1041                 curl_easy_setopt(result, CURLOPT_NOPROXY, curl_no_proxy);
1042 #endif
1043         }
1044         init_curl_proxy_auth(result);
1045
1046         set_curl_keepalive(result);
1047
1048         return result;
1049 }
1050
1051 static void set_from_env(const char **var, const char *envname)
1052 {
1053         const char *val = getenv(envname);
1054         if (val)
1055                 *var = val;
1056 }
1057
1058 void http_init(struct remote *remote, const char *url, int proactive_auth)
1059 {
1060         char *low_speed_limit;
1061         char *low_speed_time;
1062         char *normalized_url;
1063         struct urlmatch_config config = { STRING_LIST_INIT_DUP };
1064
1065         config.section = "http";
1066         config.key = NULL;
1067         config.collect_fn = http_options;
1068         config.cascade_fn = git_default_config;
1069         config.cb = NULL;
1070
1071         http_is_verbose = 0;
1072         normalized_url = url_normalize(url, &config.url);
1073
1074         git_config(urlmatch_config_entry, &config);
1075         free(normalized_url);
1076
1077 #if LIBCURL_VERSION_NUM >= 0x073800
1078         if (http_ssl_backend) {
1079                 const curl_ssl_backend **backends;
1080                 struct strbuf buf = STRBUF_INIT;
1081                 int i;
1082
1083                 switch (curl_global_sslset(-1, http_ssl_backend, &backends)) {
1084                 case CURLSSLSET_UNKNOWN_BACKEND:
1085                         strbuf_addf(&buf, _("Unsupported SSL backend '%s'. "
1086                                             "Supported SSL backends:"),
1087                                             http_ssl_backend);
1088                         for (i = 0; backends[i]; i++)
1089                                 strbuf_addf(&buf, "\n\t%s", backends[i]->name);
1090                         die("%s", buf.buf);
1091                 case CURLSSLSET_NO_BACKENDS:
1092                         die(_("Could not set SSL backend to '%s': "
1093                               "cURL was built without SSL backends"),
1094                             http_ssl_backend);
1095                 case CURLSSLSET_TOO_LATE:
1096                         die(_("Could not set SSL backend to '%s': already set"),
1097                             http_ssl_backend);
1098                 case CURLSSLSET_OK:
1099                         break; /* Okay! */
1100                 }
1101         }
1102 #endif
1103
1104         if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
1105                 die("curl_global_init failed");
1106
1107         http_proactive_auth = proactive_auth;
1108
1109         if (remote && remote->http_proxy)
1110                 curl_http_proxy = xstrdup(remote->http_proxy);
1111
1112         if (remote)
1113                 var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
1114
1115         pragma_header = curl_slist_append(http_copy_default_headers(),
1116                 "Pragma: no-cache");
1117         no_pragma_header = curl_slist_append(http_copy_default_headers(),
1118                 "Pragma:");
1119
1120 #ifdef USE_CURL_MULTI
1121         {
1122                 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1123                 if (http_max_requests != NULL)
1124                         max_requests = atoi(http_max_requests);
1125         }
1126
1127         curlm = curl_multi_init();
1128         if (!curlm)
1129                 die("curl_multi_init failed");
1130 #endif
1131
1132         if (getenv("GIT_SSL_NO_VERIFY"))
1133                 curl_ssl_verify = 0;
1134
1135         set_from_env(&ssl_cert, "GIT_SSL_CERT");
1136 #if LIBCURL_VERSION_NUM >= 0x070903
1137         set_from_env(&ssl_key, "GIT_SSL_KEY");
1138 #endif
1139 #if LIBCURL_VERSION_NUM >= 0x070908
1140         set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
1141 #endif
1142         set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
1143
1144         set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
1145
1146         low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1147         if (low_speed_limit != NULL)
1148                 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
1149         low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
1150         if (low_speed_time != NULL)
1151                 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
1152
1153         if (curl_ssl_verify == -1)
1154                 curl_ssl_verify = 1;
1155
1156         curl_session_count = 0;
1157 #ifdef USE_CURL_MULTI
1158         if (max_requests < 1)
1159                 max_requests = DEFAULT_MAX_REQUESTS;
1160 #endif
1161
1162         if (getenv("GIT_CURL_FTP_NO_EPSV"))
1163                 curl_ftp_no_epsv = 1;
1164
1165         if (url) {
1166                 credential_from_url(&http_auth, url);
1167                 if (!ssl_cert_password_required &&
1168                     getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
1169                     starts_with(url, "https://"))
1170                         ssl_cert_password_required = 1;
1171         }
1172
1173 #ifndef NO_CURL_EASY_DUPHANDLE
1174         curl_default = get_curl_handle();
1175 #endif
1176 }
1177
1178 void http_cleanup(void)
1179 {
1180         struct active_request_slot *slot = active_queue_head;
1181
1182         while (slot != NULL) {
1183                 struct active_request_slot *next = slot->next;
1184                 if (slot->curl != NULL) {
1185                         xmulti_remove_handle(slot);
1186                         curl_easy_cleanup(slot->curl);
1187                 }
1188                 free(slot);
1189                 slot = next;
1190         }
1191         active_queue_head = NULL;
1192
1193 #ifndef NO_CURL_EASY_DUPHANDLE
1194         curl_easy_cleanup(curl_default);
1195 #endif
1196
1197 #ifdef USE_CURL_MULTI
1198         curl_multi_cleanup(curlm);
1199 #endif
1200         curl_global_cleanup();
1201
1202         curl_slist_free_all(extra_http_headers);
1203         extra_http_headers = NULL;
1204
1205         curl_slist_free_all(pragma_header);
1206         pragma_header = NULL;
1207
1208         curl_slist_free_all(no_pragma_header);
1209         no_pragma_header = NULL;
1210
1211         if (curl_http_proxy) {
1212                 free((void *)curl_http_proxy);
1213                 curl_http_proxy = NULL;
1214         }
1215
1216         if (proxy_auth.password) {
1217                 memset(proxy_auth.password, 0, strlen(proxy_auth.password));
1218                 FREE_AND_NULL(proxy_auth.password);
1219         }
1220
1221         free((void *)curl_proxyuserpwd);
1222         curl_proxyuserpwd = NULL;
1223
1224         free((void *)http_proxy_authmethod);
1225         http_proxy_authmethod = NULL;
1226
1227         if (cert_auth.password != NULL) {
1228                 memset(cert_auth.password, 0, strlen(cert_auth.password));
1229                 FREE_AND_NULL(cert_auth.password);
1230         }
1231         ssl_cert_password_required = 0;
1232
1233         FREE_AND_NULL(cached_accept_language);
1234 }
1235
1236 struct active_request_slot *get_active_slot(void)
1237 {
1238         struct active_request_slot *slot = active_queue_head;
1239         struct active_request_slot *newslot;
1240
1241 #ifdef USE_CURL_MULTI
1242         int num_transfers;
1243
1244         /* Wait for a slot to open up if the queue is full */
1245         while (active_requests >= max_requests) {
1246                 curl_multi_perform(curlm, &num_transfers);
1247                 if (num_transfers < active_requests)
1248                         process_curl_messages();
1249         }
1250 #endif
1251
1252         while (slot != NULL && slot->in_use)
1253                 slot = slot->next;
1254
1255         if (slot == NULL) {
1256                 newslot = xmalloc(sizeof(*newslot));
1257                 newslot->curl = NULL;
1258                 newslot->in_use = 0;
1259                 newslot->next = NULL;
1260
1261                 slot = active_queue_head;
1262                 if (slot == NULL) {
1263                         active_queue_head = newslot;
1264                 } else {
1265                         while (slot->next != NULL)
1266                                 slot = slot->next;
1267                         slot->next = newslot;
1268                 }
1269                 slot = newslot;
1270         }
1271
1272         if (slot->curl == NULL) {
1273 #ifdef NO_CURL_EASY_DUPHANDLE
1274                 slot->curl = get_curl_handle();
1275 #else
1276                 slot->curl = curl_easy_duphandle(curl_default);
1277 #endif
1278                 curl_session_count++;
1279         }
1280
1281         active_requests++;
1282         slot->in_use = 1;
1283         slot->results = NULL;
1284         slot->finished = NULL;
1285         slot->callback_data = NULL;
1286         slot->callback_func = NULL;
1287         curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
1288         if (curl_save_cookies)
1289                 curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
1290         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
1291         curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
1292         curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
1293         curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
1294         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
1295         curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
1296         curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
1297         curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1298         curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
1299         curl_easy_setopt(slot->curl, CURLOPT_RANGE, NULL);
1300
1301         /*
1302          * Default following to off unless "ALWAYS" is configured; this gives
1303          * callers a sane starting point, and they can tweak for individual
1304          * HTTP_FOLLOW_* cases themselves.
1305          */
1306         if (http_follow_config == HTTP_FOLLOW_ALWAYS)
1307                 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
1308         else
1309                 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 0);
1310
1311 #if LIBCURL_VERSION_NUM >= 0x070a08
1312         curl_easy_setopt(slot->curl, CURLOPT_IPRESOLVE, git_curl_ipresolve);
1313 #endif
1314 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1315         curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods);
1316 #endif
1317         if (http_auth.password || curl_empty_auth_enabled())
1318                 init_curl_http_auth(slot->curl);
1319
1320         return slot;
1321 }
1322
1323 int start_active_slot(struct active_request_slot *slot)
1324 {
1325 #ifdef USE_CURL_MULTI
1326         CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
1327         int num_transfers;
1328
1329         if (curlm_result != CURLM_OK &&
1330             curlm_result != CURLM_CALL_MULTI_PERFORM) {
1331                 warning("curl_multi_add_handle failed: %s",
1332                         curl_multi_strerror(curlm_result));
1333                 active_requests--;
1334                 slot->in_use = 0;
1335                 return 0;
1336         }
1337
1338         /*
1339          * We know there must be something to do, since we just added
1340          * something.
1341          */
1342         curl_multi_perform(curlm, &num_transfers);
1343 #endif
1344         return 1;
1345 }
1346
1347 #ifdef USE_CURL_MULTI
1348 struct fill_chain {
1349         void *data;
1350         int (*fill)(void *);
1351         struct fill_chain *next;
1352 };
1353
1354 static struct fill_chain *fill_cfg;
1355
1356 void add_fill_function(void *data, int (*fill)(void *))
1357 {
1358         struct fill_chain *new_fill = xmalloc(sizeof(*new_fill));
1359         struct fill_chain **linkp = &fill_cfg;
1360         new_fill->data = data;
1361         new_fill->fill = fill;
1362         new_fill->next = NULL;
1363         while (*linkp)
1364                 linkp = &(*linkp)->next;
1365         *linkp = new_fill;
1366 }
1367
1368 void fill_active_slots(void)
1369 {
1370         struct active_request_slot *slot = active_queue_head;
1371
1372         while (active_requests < max_requests) {
1373                 struct fill_chain *fill;
1374                 for (fill = fill_cfg; fill; fill = fill->next)
1375                         if (fill->fill(fill->data))
1376                                 break;
1377
1378                 if (!fill)
1379                         break;
1380         }
1381
1382         while (slot != NULL) {
1383                 if (!slot->in_use && slot->curl != NULL
1384                         && curl_session_count > min_curl_sessions) {
1385                         curl_easy_cleanup(slot->curl);
1386                         slot->curl = NULL;
1387                         curl_session_count--;
1388                 }
1389                 slot = slot->next;
1390         }
1391 }
1392
1393 void step_active_slots(void)
1394 {
1395         int num_transfers;
1396         CURLMcode curlm_result;
1397
1398         do {
1399                 curlm_result = curl_multi_perform(curlm, &num_transfers);
1400         } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
1401         if (num_transfers < active_requests) {
1402                 process_curl_messages();
1403                 fill_active_slots();
1404         }
1405 }
1406 #endif
1407
1408 void run_active_slot(struct active_request_slot *slot)
1409 {
1410 #ifdef USE_CURL_MULTI
1411         fd_set readfds;
1412         fd_set writefds;
1413         fd_set excfds;
1414         int max_fd;
1415         struct timeval select_timeout;
1416         int finished = 0;
1417
1418         slot->finished = &finished;
1419         while (!finished) {
1420                 step_active_slots();
1421
1422                 if (slot->in_use) {
1423 #if LIBCURL_VERSION_NUM >= 0x070f04
1424                         long curl_timeout;
1425                         curl_multi_timeout(curlm, &curl_timeout);
1426                         if (curl_timeout == 0) {
1427                                 continue;
1428                         } else if (curl_timeout == -1) {
1429                                 select_timeout.tv_sec  = 0;
1430                                 select_timeout.tv_usec = 50000;
1431                         } else {
1432                                 select_timeout.tv_sec  =  curl_timeout / 1000;
1433                                 select_timeout.tv_usec = (curl_timeout % 1000) * 1000;
1434                         }
1435 #else
1436                         select_timeout.tv_sec  = 0;
1437                         select_timeout.tv_usec = 50000;
1438 #endif
1439
1440                         max_fd = -1;
1441                         FD_ZERO(&readfds);
1442                         FD_ZERO(&writefds);
1443                         FD_ZERO(&excfds);
1444                         curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
1445
1446                         /*
1447                          * It can happen that curl_multi_timeout returns a pathologically
1448                          * long timeout when curl_multi_fdset returns no file descriptors
1449                          * to read.  See commit message for more details.
1450                          */
1451                         if (max_fd < 0 &&
1452                             (select_timeout.tv_sec > 0 ||
1453                              select_timeout.tv_usec > 50000)) {
1454                                 select_timeout.tv_sec  = 0;
1455                                 select_timeout.tv_usec = 50000;
1456                         }
1457
1458                         select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
1459                 }
1460         }
1461 #else
1462         while (slot->in_use) {
1463                 slot->curl_result = curl_easy_perform(slot->curl);
1464                 finish_active_slot(slot);
1465         }
1466 #endif
1467 }
1468
1469 static void release_active_slot(struct active_request_slot *slot)
1470 {
1471         closedown_active_slot(slot);
1472         if (slot->curl) {
1473                 xmulti_remove_handle(slot);
1474                 if (curl_session_count > min_curl_sessions) {
1475                         curl_easy_cleanup(slot->curl);
1476                         slot->curl = NULL;
1477                         curl_session_count--;
1478                 }
1479         }
1480 #ifdef USE_CURL_MULTI
1481         fill_active_slots();
1482 #endif
1483 }
1484
1485 void finish_all_active_slots(void)
1486 {
1487         struct active_request_slot *slot = active_queue_head;
1488
1489         while (slot != NULL)
1490                 if (slot->in_use) {
1491                         run_active_slot(slot);
1492                         slot = active_queue_head;
1493                 } else {
1494                         slot = slot->next;
1495                 }
1496 }
1497
1498 /* Helpers for modifying and creating URLs */
1499 static inline int needs_quote(int ch)
1500 {
1501         if (((ch >= 'A') && (ch <= 'Z'))
1502                         || ((ch >= 'a') && (ch <= 'z'))
1503                         || ((ch >= '0') && (ch <= '9'))
1504                         || (ch == '/')
1505                         || (ch == '-')
1506                         || (ch == '.'))
1507                 return 0;
1508         return 1;
1509 }
1510
1511 static char *quote_ref_url(const char *base, const char *ref)
1512 {
1513         struct strbuf buf = STRBUF_INIT;
1514         const char *cp;
1515         int ch;
1516
1517         end_url_with_slash(&buf, base);
1518
1519         for (cp = ref; (ch = *cp) != 0; cp++)
1520                 if (needs_quote(ch))
1521                         strbuf_addf(&buf, "%%%02x", ch);
1522                 else
1523                         strbuf_addch(&buf, *cp);
1524
1525         return strbuf_detach(&buf, NULL);
1526 }
1527
1528 void append_remote_object_url(struct strbuf *buf, const char *url,
1529                               const char *hex,
1530                               int only_two_digit_prefix)
1531 {
1532         end_url_with_slash(buf, url);
1533
1534         strbuf_addf(buf, "objects/%.*s/", 2, hex);
1535         if (!only_two_digit_prefix)
1536                 strbuf_addstr(buf, hex + 2);
1537 }
1538
1539 char *get_remote_object_url(const char *url, const char *hex,
1540                             int only_two_digit_prefix)
1541 {
1542         struct strbuf buf = STRBUF_INIT;
1543         append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
1544         return strbuf_detach(&buf, NULL);
1545 }
1546
1547 void normalize_curl_result(CURLcode *result, long http_code,
1548                            char *errorstr, size_t errorlen)
1549 {
1550         /*
1551          * If we see a failing http code with CURLE_OK, we have turned off
1552          * FAILONERROR (to keep the server's custom error response), and should
1553          * translate the code into failure here.
1554          *
1555          * Likewise, if we see a redirect (30x code), that means we turned off
1556          * redirect-following, and we should treat the result as an error.
1557          */
1558         if (*result == CURLE_OK && http_code >= 300) {
1559                 *result = CURLE_HTTP_RETURNED_ERROR;
1560                 /*
1561                  * Normally curl will already have put the "reason phrase"
1562                  * from the server into curl_errorstr; unfortunately without
1563                  * FAILONERROR it is lost, so we can give only the numeric
1564                  * status code.
1565                  */
1566                 xsnprintf(errorstr, errorlen,
1567                           "The requested URL returned error: %ld",
1568                           http_code);
1569         }
1570 }
1571
1572 static int handle_curl_result(struct slot_results *results)
1573 {
1574         normalize_curl_result(&results->curl_result, results->http_code,
1575                               curl_errorstr, sizeof(curl_errorstr));
1576
1577         if (results->curl_result == CURLE_OK) {
1578                 credential_approve(&http_auth);
1579                 if (proxy_auth.password)
1580                         credential_approve(&proxy_auth);
1581                 return HTTP_OK;
1582         } else if (missing_target(results))
1583                 return HTTP_MISSING_TARGET;
1584         else if (results->http_code == 401) {
1585                 if (http_auth.username && http_auth.password) {
1586                         credential_reject(&http_auth);
1587                         return HTTP_NOAUTH;
1588                 } else {
1589 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1590                         http_auth_methods &= ~CURLAUTH_GSSNEGOTIATE;
1591                         if (results->auth_avail) {
1592                                 http_auth_methods &= results->auth_avail;
1593                                 http_auth_methods_restricted = 1;
1594                         }
1595 #endif
1596                         return HTTP_REAUTH;
1597                 }
1598         } else {
1599                 if (results->http_connectcode == 407)
1600                         credential_reject(&proxy_auth);
1601 #if LIBCURL_VERSION_NUM >= 0x070c00
1602                 if (!curl_errorstr[0])
1603                         strlcpy(curl_errorstr,
1604                                 curl_easy_strerror(results->curl_result),
1605                                 sizeof(curl_errorstr));
1606 #endif
1607                 return HTTP_ERROR;
1608         }
1609 }
1610
1611 int run_one_slot(struct active_request_slot *slot,
1612                  struct slot_results *results)
1613 {
1614         slot->results = results;
1615         if (!start_active_slot(slot)) {
1616                 xsnprintf(curl_errorstr, sizeof(curl_errorstr),
1617                           "failed to start HTTP request");
1618                 return HTTP_START_FAILED;
1619         }
1620
1621         run_active_slot(slot);
1622         return handle_curl_result(results);
1623 }
1624
1625 struct curl_slist *http_copy_default_headers(void)
1626 {
1627         struct curl_slist *headers = NULL, *h;
1628
1629         for (h = extra_http_headers; h; h = h->next)
1630                 headers = curl_slist_append(headers, h->data);
1631
1632         return headers;
1633 }
1634
1635 static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
1636 {
1637         char *ptr;
1638         CURLcode ret;
1639
1640         strbuf_reset(buf);
1641         ret = curl_easy_getinfo(curl, info, &ptr);
1642         if (!ret && ptr)
1643                 strbuf_addstr(buf, ptr);
1644         return ret;
1645 }
1646
1647 /*
1648  * Check for and extract a content-type parameter. "raw"
1649  * should be positioned at the start of the potential
1650  * parameter, with any whitespace already removed.
1651  *
1652  * "name" is the name of the parameter. The value is appended
1653  * to "out".
1654  */
1655 static int extract_param(const char *raw, const char *name,
1656                          struct strbuf *out)
1657 {
1658         size_t len = strlen(name);
1659
1660         if (strncasecmp(raw, name, len))
1661                 return -1;
1662         raw += len;
1663
1664         if (*raw != '=')
1665                 return -1;
1666         raw++;
1667
1668         while (*raw && !isspace(*raw) && *raw != ';')
1669                 strbuf_addch(out, *raw++);
1670         return 0;
1671 }
1672
1673 /*
1674  * Extract a normalized version of the content type, with any
1675  * spaces suppressed, all letters lowercased, and no trailing ";"
1676  * or parameters.
1677  *
1678  * Note that we will silently remove even invalid whitespace. For
1679  * example, "text / plain" is specifically forbidden by RFC 2616,
1680  * but "text/plain" is the only reasonable output, and this keeps
1681  * our code simple.
1682  *
1683  * If the "charset" argument is not NULL, store the value of any
1684  * charset parameter there.
1685  *
1686  * Example:
1687  *   "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1688  *   "text / plain" -> "text/plain"
1689  */
1690 static void extract_content_type(struct strbuf *raw, struct strbuf *type,
1691                                  struct strbuf *charset)
1692 {
1693         const char *p;
1694
1695         strbuf_reset(type);
1696         strbuf_grow(type, raw->len);
1697         for (p = raw->buf; *p; p++) {
1698                 if (isspace(*p))
1699                         continue;
1700                 if (*p == ';') {
1701                         p++;
1702                         break;
1703                 }
1704                 strbuf_addch(type, tolower(*p));
1705         }
1706
1707         if (!charset)
1708                 return;
1709
1710         strbuf_reset(charset);
1711         while (*p) {
1712                 while (isspace(*p) || *p == ';')
1713                         p++;
1714                 if (!extract_param(p, "charset", charset))
1715                         return;
1716                 while (*p && !isspace(*p))
1717                         p++;
1718         }
1719
1720         if (!charset->len && starts_with(type->buf, "text/"))
1721                 strbuf_addstr(charset, "ISO-8859-1");
1722 }
1723
1724 static void write_accept_language(struct strbuf *buf)
1725 {
1726         /*
1727          * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1728          * that, q-value will be smaller than 0.001, the minimum q-value the
1729          * HTTP specification allows. See
1730          * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1731          */
1732         const int MAX_DECIMAL_PLACES = 3;
1733         const int MAX_LANGUAGE_TAGS = 1000;
1734         const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE = 4000;
1735         char **language_tags = NULL;
1736         int num_langs = 0;
1737         const char *s = get_preferred_languages();
1738         int i;
1739         struct strbuf tag = STRBUF_INIT;
1740
1741         /* Don't add Accept-Language header if no language is preferred. */
1742         if (!s)
1743                 return;
1744
1745         /*
1746          * Split the colon-separated string of preferred languages into
1747          * language_tags array.
1748          */
1749         do {
1750                 /* collect language tag */
1751                 for (; *s && (isalnum(*s) || *s == '_'); s++)
1752                         strbuf_addch(&tag, *s == '_' ? '-' : *s);
1753
1754                 /* skip .codeset, @modifier and any other unnecessary parts */
1755                 while (*s && *s != ':')
1756                         s++;
1757
1758                 if (tag.len) {
1759                         num_langs++;
1760                         REALLOC_ARRAY(language_tags, num_langs);
1761                         language_tags[num_langs - 1] = strbuf_detach(&tag, NULL);
1762                         if (num_langs >= MAX_LANGUAGE_TAGS - 1) /* -1 for '*' */
1763                                 break;
1764                 }
1765         } while (*s++);
1766
1767         /* write Accept-Language header into buf */
1768         if (num_langs) {
1769                 int last_buf_len = 0;
1770                 int max_q;
1771                 int decimal_places;
1772                 char q_format[32];
1773
1774                 /* add '*' */
1775                 REALLOC_ARRAY(language_tags, num_langs + 1);
1776                 language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */
1777
1778                 /* compute decimal_places */
1779                 for (max_q = 1, decimal_places = 0;
1780                      max_q < num_langs && decimal_places <= MAX_DECIMAL_PLACES;
1781                      decimal_places++, max_q *= 10)
1782                         ;
1783
1784                 xsnprintf(q_format, sizeof(q_format), ";q=0.%%0%dd", decimal_places);
1785
1786                 strbuf_addstr(buf, "Accept-Language: ");
1787
1788                 for (i = 0; i < num_langs; i++) {
1789                         if (i > 0)
1790                                 strbuf_addstr(buf, ", ");
1791
1792                         strbuf_addstr(buf, language_tags[i]);
1793
1794                         if (i > 0)
1795                                 strbuf_addf(buf, q_format, max_q - i);
1796
1797                         if (buf->len > MAX_ACCEPT_LANGUAGE_HEADER_SIZE) {
1798                                 strbuf_remove(buf, last_buf_len, buf->len - last_buf_len);
1799                                 break;
1800                         }
1801
1802                         last_buf_len = buf->len;
1803                 }
1804         }
1805
1806         /* free language tags -- last one is a static '*' */
1807         for (i = 0; i < num_langs - 1; i++)
1808                 free(language_tags[i]);
1809         free(language_tags);
1810 }
1811
1812 /*
1813  * Get an Accept-Language header which indicates user's preferred languages.
1814  *
1815  * Examples:
1816  *   LANGUAGE= -> ""
1817  *   LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1818  *   LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1819  *   LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1820  *   LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1821  *   LANGUAGE= LANG=C -> ""
1822  */
1823 static const char *get_accept_language(void)
1824 {
1825         if (!cached_accept_language) {
1826                 struct strbuf buf = STRBUF_INIT;
1827                 write_accept_language(&buf);
1828                 if (buf.len > 0)
1829                         cached_accept_language = strbuf_detach(&buf, NULL);
1830         }
1831
1832         return cached_accept_language;
1833 }
1834
1835 static void http_opt_request_remainder(CURL *curl, off_t pos)
1836 {
1837         char buf[128];
1838         xsnprintf(buf, sizeof(buf), "%"PRIuMAX"-", (uintmax_t)pos);
1839         curl_easy_setopt(curl, CURLOPT_RANGE, buf);
1840 }
1841
1842 /* http_request() targets */
1843 #define HTTP_REQUEST_STRBUF     0
1844 #define HTTP_REQUEST_FILE       1
1845
1846 static int http_request(const char *url,
1847                         void *result, int target,
1848                         const struct http_get_options *options)
1849 {
1850         struct active_request_slot *slot;
1851         struct slot_results results;
1852         struct curl_slist *headers = http_copy_default_headers();
1853         struct strbuf buf = STRBUF_INIT;
1854         const char *accept_language;
1855         int ret;
1856
1857         slot = get_active_slot();
1858         curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1859
1860         if (result == NULL) {
1861                 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
1862         } else {
1863                 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1864                 curl_easy_setopt(slot->curl, CURLOPT_FILE, result);
1865
1866                 if (target == HTTP_REQUEST_FILE) {
1867                         off_t posn = ftello(result);
1868                         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1869                                          fwrite);
1870                         if (posn > 0)
1871                                 http_opt_request_remainder(slot->curl, posn);
1872                 } else
1873                         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1874                                          fwrite_buffer);
1875         }
1876
1877         accept_language = get_accept_language();
1878
1879         if (accept_language)
1880                 headers = curl_slist_append(headers, accept_language);
1881
1882         strbuf_addstr(&buf, "Pragma:");
1883         if (options && options->no_cache)
1884                 strbuf_addstr(&buf, " no-cache");
1885         if (options && options->initial_request &&
1886             http_follow_config == HTTP_FOLLOW_INITIAL)
1887                 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
1888
1889         headers = curl_slist_append(headers, buf.buf);
1890
1891         /* Add additional headers here */
1892         if (options && options->extra_headers) {
1893                 const struct string_list_item *item;
1894                 for_each_string_list_item(item, options->extra_headers) {
1895                         headers = curl_slist_append(headers, item->string);
1896                 }
1897         }
1898
1899         curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1900         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1901         curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
1902         curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1903
1904         ret = run_one_slot(slot, &results);
1905
1906         if (options && options->content_type) {
1907                 struct strbuf raw = STRBUF_INIT;
1908                 curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, &raw);
1909                 extract_content_type(&raw, options->content_type,
1910                                      options->charset);
1911                 strbuf_release(&raw);
1912         }
1913
1914         if (options && options->effective_url)
1915                 curlinfo_strbuf(slot->curl, CURLINFO_EFFECTIVE_URL,
1916                                 options->effective_url);
1917
1918         curl_slist_free_all(headers);
1919         strbuf_release(&buf);
1920
1921         return ret;
1922 }
1923
1924 /*
1925  * Update the "base" url to a more appropriate value, as deduced by
1926  * redirects seen when requesting a URL starting with "url".
1927  *
1928  * The "asked" parameter is a URL that we asked curl to access, and must begin
1929  * with "base".
1930  *
1931  * The "got" parameter is the URL that curl reported to us as where we ended
1932  * up.
1933  *
1934  * Returns 1 if we updated the base url, 0 otherwise.
1935  *
1936  * Our basic strategy is to compare "base" and "asked" to find the bits
1937  * specific to our request. We then strip those bits off of "got" to yield the
1938  * new base. So for example, if our base is "http://example.com/foo.git",
1939  * and we ask for "http://example.com/foo.git/info/refs", we might end up
1940  * with "https://other.example.com/foo.git/info/refs". We would want the
1941  * new URL to become "https://other.example.com/foo.git".
1942  *
1943  * Note that this assumes a sane redirect scheme. It's entirely possible
1944  * in the example above to end up at a URL that does not even end in
1945  * "info/refs".  In such a case we die. There's not much we can do, such a
1946  * scheme is unlikely to represent a real git repository, and failing to
1947  * rewrite the base opens options for malicious redirects to do funny things.
1948  */
1949 static int update_url_from_redirect(struct strbuf *base,
1950                                     const char *asked,
1951                                     const struct strbuf *got)
1952 {
1953         const char *tail;
1954         size_t new_len;
1955
1956         if (!strcmp(asked, got->buf))
1957                 return 0;
1958
1959         if (!skip_prefix(asked, base->buf, &tail))
1960                 BUG("update_url_from_redirect: %s is not a superset of %s",
1961                     asked, base->buf);
1962
1963         new_len = got->len;
1964         if (!strip_suffix_mem(got->buf, &new_len, tail))
1965                 die(_("unable to update url base from redirection:\n"
1966                       "  asked for: %s\n"
1967                       "   redirect: %s"),
1968                     asked, got->buf);
1969
1970         strbuf_reset(base);
1971         strbuf_add(base, got->buf, new_len);
1972
1973         return 1;
1974 }
1975
1976 static int http_request_reauth(const char *url,
1977                                void *result, int target,
1978                                struct http_get_options *options)
1979 {
1980         int ret = http_request(url, result, target, options);
1981
1982         if (ret != HTTP_OK && ret != HTTP_REAUTH)
1983                 return ret;
1984
1985         if (options && options->effective_url && options->base_url) {
1986                 if (update_url_from_redirect(options->base_url,
1987                                              url, options->effective_url)) {
1988                         credential_from_url(&http_auth, options->base_url->buf);
1989                         url = options->effective_url->buf;
1990                 }
1991         }
1992
1993         if (ret != HTTP_REAUTH)
1994                 return ret;
1995
1996         /*
1997          * The previous request may have put cruft into our output stream; we
1998          * should clear it out before making our next request.
1999          */
2000         switch (target) {
2001         case HTTP_REQUEST_STRBUF:
2002                 strbuf_reset(result);
2003                 break;
2004         case HTTP_REQUEST_FILE:
2005                 if (fflush(result)) {
2006                         error_errno("unable to flush a file");
2007                         return HTTP_START_FAILED;
2008                 }
2009                 rewind(result);
2010                 if (ftruncate(fileno(result), 0) < 0) {
2011                         error_errno("unable to truncate a file");
2012                         return HTTP_START_FAILED;
2013                 }
2014                 break;
2015         default:
2016                 BUG("Unknown http_request target");
2017         }
2018
2019         credential_fill(&http_auth);
2020
2021         return http_request(url, result, target, options);
2022 }
2023
2024 int http_get_strbuf(const char *url,
2025                     struct strbuf *result,
2026                     struct http_get_options *options)
2027 {
2028         return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
2029 }
2030
2031 /*
2032  * Downloads a URL and stores the result in the given file.
2033  *
2034  * If a previous interrupted download is detected (i.e. a previous temporary
2035  * file is still around) the download is resumed.
2036  */
2037 static int http_get_file(const char *url, const char *filename,
2038                          struct http_get_options *options)
2039 {
2040         int ret;
2041         struct strbuf tmpfile = STRBUF_INIT;
2042         FILE *result;
2043
2044         strbuf_addf(&tmpfile, "%s.temp", filename);
2045         result = fopen(tmpfile.buf, "a");
2046         if (!result) {
2047                 error("Unable to open local file %s", tmpfile.buf);
2048                 ret = HTTP_ERROR;
2049                 goto cleanup;
2050         }
2051
2052         ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
2053         fclose(result);
2054
2055         if (ret == HTTP_OK && finalize_object_file(tmpfile.buf, filename))
2056                 ret = HTTP_ERROR;
2057 cleanup:
2058         strbuf_release(&tmpfile);
2059         return ret;
2060 }
2061
2062 int http_fetch_ref(const char *base, struct ref *ref)
2063 {
2064         struct http_get_options options = {0};
2065         char *url;
2066         struct strbuf buffer = STRBUF_INIT;
2067         int ret = -1;
2068
2069         options.no_cache = 1;
2070
2071         url = quote_ref_url(base, ref->name);
2072         if (http_get_strbuf(url, &buffer, &options) == HTTP_OK) {
2073                 strbuf_rtrim(&buffer);
2074                 if (buffer.len == the_hash_algo->hexsz)
2075                         ret = get_oid_hex(buffer.buf, &ref->old_oid);
2076                 else if (starts_with(buffer.buf, "ref: ")) {
2077                         ref->symref = xstrdup(buffer.buf + 5);
2078                         ret = 0;
2079                 }
2080         }
2081
2082         strbuf_release(&buffer);
2083         free(url);
2084         return ret;
2085 }
2086
2087 /* Helpers for fetching packs */
2088 static char *fetch_pack_index(unsigned char *hash, const char *base_url)
2089 {
2090         char *url, *tmp;
2091         struct strbuf buf = STRBUF_INIT;
2092
2093         if (http_is_verbose)
2094                 fprintf(stderr, "Getting index for pack %s\n", hash_to_hex(hash));
2095
2096         end_url_with_slash(&buf, base_url);
2097         strbuf_addf(&buf, "objects/pack/pack-%s.idx", hash_to_hex(hash));
2098         url = strbuf_detach(&buf, NULL);
2099
2100         strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(hash));
2101         tmp = strbuf_detach(&buf, NULL);
2102
2103         if (http_get_file(url, tmp, NULL) != HTTP_OK) {
2104                 error("Unable to get pack index %s", url);
2105                 FREE_AND_NULL(tmp);
2106         }
2107
2108         free(url);
2109         return tmp;
2110 }
2111
2112 static int fetch_and_setup_pack_index(struct packed_git **packs_head,
2113         unsigned char *sha1, const char *base_url)
2114 {
2115         struct packed_git *new_pack;
2116         char *tmp_idx = NULL;
2117         int ret;
2118
2119         if (has_pack_index(sha1)) {
2120                 new_pack = parse_pack_index(sha1, sha1_pack_index_name(sha1));
2121                 if (!new_pack)
2122                         return -1; /* parse_pack_index() already issued error message */
2123                 goto add_pack;
2124         }
2125
2126         tmp_idx = fetch_pack_index(sha1, base_url);
2127         if (!tmp_idx)
2128                 return -1;
2129
2130         new_pack = parse_pack_index(sha1, tmp_idx);
2131         if (!new_pack) {
2132                 unlink(tmp_idx);
2133                 free(tmp_idx);
2134
2135                 return -1; /* parse_pack_index() already issued error message */
2136         }
2137
2138         ret = verify_pack_index(new_pack);
2139         if (!ret) {
2140                 close_pack_index(new_pack);
2141                 ret = finalize_object_file(tmp_idx, sha1_pack_index_name(sha1));
2142         }
2143         free(tmp_idx);
2144         if (ret)
2145                 return -1;
2146
2147 add_pack:
2148         new_pack->next = *packs_head;
2149         *packs_head = new_pack;
2150         return 0;
2151 }
2152
2153 int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
2154 {
2155         struct http_get_options options = {0};
2156         int ret = 0;
2157         char *url;
2158         const char *data;
2159         struct strbuf buf = STRBUF_INIT;
2160         struct object_id oid;
2161
2162         end_url_with_slash(&buf, base_url);
2163         strbuf_addstr(&buf, "objects/info/packs");
2164         url = strbuf_detach(&buf, NULL);
2165
2166         options.no_cache = 1;
2167         ret = http_get_strbuf(url, &buf, &options);
2168         if (ret != HTTP_OK)
2169                 goto cleanup;
2170
2171         data = buf.buf;
2172         while (*data) {
2173                 if (skip_prefix(data, "P pack-", &data) &&
2174                     !parse_oid_hex(data, &oid, &data) &&
2175                     skip_prefix(data, ".pack", &data) &&
2176                     (*data == '\n' || *data == '\0')) {
2177                         fetch_and_setup_pack_index(packs_head, oid.hash, base_url);
2178                 } else {
2179                         data = strchrnul(data, '\n');
2180                 }
2181                 if (*data)
2182                         data++; /* skip past newline */
2183         }
2184
2185 cleanup:
2186         free(url);
2187         return ret;
2188 }
2189
2190 void release_http_pack_request(struct http_pack_request *preq)
2191 {
2192         if (preq->packfile != NULL) {
2193                 fclose(preq->packfile);
2194                 preq->packfile = NULL;
2195         }
2196         preq->slot = NULL;
2197         strbuf_release(&preq->tmpfile);
2198         free(preq->url);
2199         free(preq);
2200 }
2201
2202 int finish_http_pack_request(struct http_pack_request *preq)
2203 {
2204         struct packed_git **lst;
2205         struct packed_git *p = preq->target;
2206         char *tmp_idx;
2207         size_t len;
2208         struct child_process ip = CHILD_PROCESS_INIT;
2209
2210         close_pack_index(p);
2211
2212         fclose(preq->packfile);
2213         preq->packfile = NULL;
2214
2215         lst = preq->lst;
2216         while (*lst != p)
2217                 lst = &((*lst)->next);
2218         *lst = (*lst)->next;
2219
2220         if (!strip_suffix(preq->tmpfile.buf, ".pack.temp", &len))
2221                 BUG("pack tmpfile does not end in .pack.temp?");
2222         tmp_idx = xstrfmt("%.*s.idx.temp", (int)len, preq->tmpfile.buf);
2223
2224         argv_array_push(&ip.args, "index-pack");
2225         argv_array_pushl(&ip.args, "-o", tmp_idx, NULL);
2226         argv_array_push(&ip.args, preq->tmpfile.buf);
2227         ip.git_cmd = 1;
2228         ip.no_stdin = 1;
2229         ip.no_stdout = 1;
2230
2231         if (run_command(&ip)) {
2232                 unlink(preq->tmpfile.buf);
2233                 unlink(tmp_idx);
2234                 free(tmp_idx);
2235                 return -1;
2236         }
2237
2238         unlink(sha1_pack_index_name(p->hash));
2239
2240         if (finalize_object_file(preq->tmpfile.buf, sha1_pack_name(p->hash))
2241          || finalize_object_file(tmp_idx, sha1_pack_index_name(p->hash))) {
2242                 free(tmp_idx);
2243                 return -1;
2244         }
2245
2246         install_packed_git(the_repository, p);
2247         free(tmp_idx);
2248         return 0;
2249 }
2250
2251 struct http_pack_request *new_http_pack_request(
2252         struct packed_git *target, const char *base_url)
2253 {
2254         off_t prev_posn = 0;
2255         struct strbuf buf = STRBUF_INIT;
2256         struct http_pack_request *preq;
2257
2258         preq = xcalloc(1, sizeof(*preq));
2259         strbuf_init(&preq->tmpfile, 0);
2260         preq->target = target;
2261
2262         end_url_with_slash(&buf, base_url);
2263         strbuf_addf(&buf, "objects/pack/pack-%s.pack",
2264                 hash_to_hex(target->hash));
2265         preq->url = strbuf_detach(&buf, NULL);
2266
2267         strbuf_addf(&preq->tmpfile, "%s.temp", sha1_pack_name(target->hash));
2268         preq->packfile = fopen(preq->tmpfile.buf, "a");
2269         if (!preq->packfile) {
2270                 error("Unable to open local file %s for pack",
2271                       preq->tmpfile.buf);
2272                 goto abort;
2273         }
2274
2275         preq->slot = get_active_slot();
2276         curl_easy_setopt(preq->slot->curl, CURLOPT_FILE, preq->packfile);
2277         curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
2278         curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
2279         curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
2280                 no_pragma_header);
2281
2282         /*
2283          * If there is data present from a previous transfer attempt,
2284          * resume where it left off
2285          */
2286         prev_posn = ftello(preq->packfile);
2287         if (prev_posn>0) {
2288                 if (http_is_verbose)
2289                         fprintf(stderr,
2290                                 "Resuming fetch of pack %s at byte %"PRIuMAX"\n",
2291                                 hash_to_hex(target->hash),
2292                                 (uintmax_t)prev_posn);
2293                 http_opt_request_remainder(preq->slot->curl, prev_posn);
2294         }
2295
2296         return preq;
2297
2298 abort:
2299         strbuf_release(&preq->tmpfile);
2300         free(preq->url);
2301         free(preq);
2302         return NULL;
2303 }
2304
2305 /* Helpers for fetching objects (loose) */
2306 static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
2307                                void *data)
2308 {
2309         unsigned char expn[4096];
2310         size_t size = eltsize * nmemb;
2311         int posn = 0;
2312         struct http_object_request *freq = data;
2313         struct active_request_slot *slot = freq->slot;
2314
2315         if (slot) {
2316                 CURLcode c = curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE,
2317                                                 &slot->http_code);
2318                 if (c != CURLE_OK)
2319                         BUG("curl_easy_getinfo for HTTP code failed: %s",
2320                                 curl_easy_strerror(c));
2321                 if (slot->http_code >= 300)
2322                         return nmemb;
2323         }
2324
2325         do {
2326                 ssize_t retval = xwrite(freq->localfile,
2327                                         (char *) ptr + posn, size - posn);
2328                 if (retval < 0)
2329                         return posn / eltsize;
2330                 posn += retval;
2331         } while (posn < size);
2332
2333         freq->stream.avail_in = size;
2334         freq->stream.next_in = (void *)ptr;
2335         do {
2336                 freq->stream.next_out = expn;
2337                 freq->stream.avail_out = sizeof(expn);
2338                 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
2339                 the_hash_algo->update_fn(&freq->c, expn,
2340                                          sizeof(expn) - freq->stream.avail_out);
2341         } while (freq->stream.avail_in && freq->zret == Z_OK);
2342         return nmemb;
2343 }
2344
2345 struct http_object_request *new_http_object_request(const char *base_url,
2346                                                     const struct object_id *oid)
2347 {
2348         char *hex = oid_to_hex(oid);
2349         struct strbuf filename = STRBUF_INIT;
2350         struct strbuf prevfile = STRBUF_INIT;
2351         int prevlocal;
2352         char prev_buf[PREV_BUF_SIZE];
2353         ssize_t prev_read = 0;
2354         off_t prev_posn = 0;
2355         struct http_object_request *freq;
2356
2357         freq = xcalloc(1, sizeof(*freq));
2358         strbuf_init(&freq->tmpfile, 0);
2359         oidcpy(&freq->oid, oid);
2360         freq->localfile = -1;
2361
2362         loose_object_path(the_repository, &filename, oid);
2363         strbuf_addf(&freq->tmpfile, "%s.temp", filename.buf);
2364
2365         strbuf_addf(&prevfile, "%s.prev", filename.buf);
2366         unlink_or_warn(prevfile.buf);
2367         rename(freq->tmpfile.buf, prevfile.buf);
2368         unlink_or_warn(freq->tmpfile.buf);
2369         strbuf_release(&filename);
2370
2371         if (freq->localfile != -1)
2372                 error("fd leakage in start: %d", freq->localfile);
2373         freq->localfile = open(freq->tmpfile.buf,
2374                                O_WRONLY | O_CREAT | O_EXCL, 0666);
2375         /*
2376          * This could have failed due to the "lazy directory creation";
2377          * try to mkdir the last path component.
2378          */
2379         if (freq->localfile < 0 && errno == ENOENT) {
2380                 char *dir = strrchr(freq->tmpfile.buf, '/');
2381                 if (dir) {
2382                         *dir = 0;
2383                         mkdir(freq->tmpfile.buf, 0777);
2384                         *dir = '/';
2385                 }
2386                 freq->localfile = open(freq->tmpfile.buf,
2387                                        O_WRONLY | O_CREAT | O_EXCL, 0666);
2388         }
2389
2390         if (freq->localfile < 0) {
2391                 error_errno("Couldn't create temporary file %s",
2392                             freq->tmpfile.buf);
2393                 goto abort;
2394         }
2395
2396         git_inflate_init(&freq->stream);
2397
2398         the_hash_algo->init_fn(&freq->c);
2399
2400         freq->url = get_remote_object_url(base_url, hex, 0);
2401
2402         /*
2403          * If a previous temp file is present, process what was already
2404          * fetched.
2405          */
2406         prevlocal = open(prevfile.buf, O_RDONLY);
2407         if (prevlocal != -1) {
2408                 do {
2409                         prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
2410                         if (prev_read>0) {
2411                                 if (fwrite_sha1_file(prev_buf,
2412                                                      1,
2413                                                      prev_read,
2414                                                      freq) == prev_read) {
2415                                         prev_posn += prev_read;
2416                                 } else {
2417                                         prev_read = -1;
2418                                 }
2419                         }
2420                 } while (prev_read > 0);
2421                 close(prevlocal);
2422         }
2423         unlink_or_warn(prevfile.buf);
2424         strbuf_release(&prevfile);
2425
2426         /*
2427          * Reset inflate/SHA1 if there was an error reading the previous temp
2428          * file; also rewind to the beginning of the local file.
2429          */
2430         if (prev_read == -1) {
2431                 memset(&freq->stream, 0, sizeof(freq->stream));
2432                 git_inflate_init(&freq->stream);
2433                 the_hash_algo->init_fn(&freq->c);
2434                 if (prev_posn>0) {
2435                         prev_posn = 0;
2436                         lseek(freq->localfile, 0, SEEK_SET);
2437                         if (ftruncate(freq->localfile, 0) < 0) {
2438                                 error_errno("Couldn't truncate temporary file %s",
2439                                             freq->tmpfile.buf);
2440                                 goto abort;
2441                         }
2442                 }
2443         }
2444
2445         freq->slot = get_active_slot();
2446
2447         curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq);
2448         curl_easy_setopt(freq->slot->curl, CURLOPT_FAILONERROR, 0);
2449         curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
2450         curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
2451         curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
2452         curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
2453
2454         /*
2455          * If we have successfully processed data from a previous fetch
2456          * attempt, only fetch the data we don't already have.
2457          */
2458         if (prev_posn>0) {
2459                 if (http_is_verbose)
2460                         fprintf(stderr,
2461                                 "Resuming fetch of object %s at byte %"PRIuMAX"\n",
2462                                 hex, (uintmax_t)prev_posn);
2463                 http_opt_request_remainder(freq->slot->curl, prev_posn);
2464         }
2465
2466         return freq;
2467
2468 abort:
2469         strbuf_release(&prevfile);
2470         free(freq->url);
2471         free(freq);
2472         return NULL;
2473 }
2474
2475 void process_http_object_request(struct http_object_request *freq)
2476 {
2477         if (freq->slot == NULL)
2478                 return;
2479         freq->curl_result = freq->slot->curl_result;
2480         freq->http_code = freq->slot->http_code;
2481         freq->slot = NULL;
2482 }
2483
2484 int finish_http_object_request(struct http_object_request *freq)
2485 {
2486         struct stat st;
2487         struct strbuf filename = STRBUF_INIT;
2488
2489         close(freq->localfile);
2490         freq->localfile = -1;
2491
2492         process_http_object_request(freq);
2493
2494         if (freq->http_code == 416) {
2495                 warning("requested range invalid; we may already have all the data.");
2496         } else if (freq->curl_result != CURLE_OK) {
2497                 if (stat(freq->tmpfile.buf, &st) == 0)
2498                         if (st.st_size == 0)
2499                                 unlink_or_warn(freq->tmpfile.buf);
2500                 return -1;
2501         }
2502
2503         git_inflate_end(&freq->stream);
2504         the_hash_algo->final_fn(freq->real_oid.hash, &freq->c);
2505         if (freq->zret != Z_STREAM_END) {
2506                 unlink_or_warn(freq->tmpfile.buf);
2507                 return -1;
2508         }
2509         if (!oideq(&freq->oid, &freq->real_oid)) {
2510                 unlink_or_warn(freq->tmpfile.buf);
2511                 return -1;
2512         }
2513         loose_object_path(the_repository, &filename, &freq->oid);
2514         freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
2515         strbuf_release(&filename);
2516
2517         return freq->rename;
2518 }
2519
2520 void abort_http_object_request(struct http_object_request *freq)
2521 {
2522         unlink_or_warn(freq->tmpfile.buf);
2523
2524         release_http_object_request(freq);
2525 }
2526
2527 void release_http_object_request(struct http_object_request *freq)
2528 {
2529         if (freq->localfile != -1) {
2530                 close(freq->localfile);
2531                 freq->localfile = -1;
2532         }
2533         FREE_AND_NULL(freq->url);
2534         if (freq->slot != NULL) {
2535                 freq->slot->callback_func = NULL;
2536                 freq->slot->callback_data = NULL;
2537                 release_active_slot(freq->slot);
2538                 freq->slot = NULL;
2539         }
2540         strbuf_release(&freq->tmpfile);
2541 }