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