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