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