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