Merge branch 'es/blame-commit-info-fix'
[git.git] / http.c
blobefdab0979692153f016bdf1fdb1a2e782738f1bc
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"
12 int active_requests;
13 int http_is_verbose;
14 size_t http_post_buffer = 16 * LARGE_PACKET_MAX;
16 #if LIBCURL_VERSION_NUM >= 0x070a06
17 #define LIBCURL_CAN_HANDLE_AUTH_ANY
18 #endif
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
30 #define PREV_BUF_SIZE 4096
31 #define RANGE_HEADER_SIZE 30
33 char curl_errorstr[CURL_ERROR_SIZE];
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;
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
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
69 static struct curl_slist *pragma_header;
70 static struct curl_slist *no_pragma_header;
72 static struct active_request_slot *active_queue_head;
74 static char *cached_accept_language;
76 size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
78 size_t size = eltsize * nmemb;
79 struct buffer *buffer = buffer_;
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;
86 return size;
89 #ifndef NO_CURL_IOCTL
90 curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
92 struct buffer *buffer = clientp;
94 switch (cmd) {
95 case CURLIOCMD_NOP:
96 return CURLIOE_OK;
98 case CURLIOCMD_RESTARTREAD:
99 buffer->posn = 0;
100 return CURLIOE_OK;
102 default:
103 return CURLIOE_UNKNOWNCMD;
106 #endif
108 size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
110 size_t size = eltsize * nmemb;
111 struct strbuf *buffer = buffer_;
113 strbuf_add(buffer, ptr, size);
114 return size;
117 size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf)
119 return eltsize * nmemb;
122 static void closedown_active_slot(struct active_request_slot *slot)
124 active_requests--;
125 slot->in_use = 0;
128 static void finish_active_slot(struct active_request_slot *slot)
130 closedown_active_slot(slot);
131 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
133 if (slot->finished != NULL)
134 (*slot->finished) = 1;
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
148 /* Run callback if appropriate */
149 if (slot->callback_func != NULL)
150 slot->callback_func(slot->callback_data);
153 #ifdef USE_CURL_MULTI
154 static void process_curl_messages(void)
156 int num_messages;
157 struct active_request_slot *slot;
158 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
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");
174 } else {
175 fprintf(stderr, "Unknown CURL message received: %d\n",
176 (int)curl_message->msg);
178 curl_message = curl_multi_info_read(curlm, &num_messages);
181 #endif
183 static int http_options(const char *var, const char *value, void *cb)
185 if (!strcmp("http.sslverify", var)) {
186 curl_ssl_verify = git_config_bool(var, value);
187 return 0;
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;
205 if (!strcmp("http.ssltry", var)) {
206 curl_ssl_try = git_config_bool(var, value);
207 return 0;
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;
217 #ifdef USE_CURL_MULTI
218 if (!strcmp("http.maxrequests", var)) {
219 max_requests = git_config_int(var, value);
220 return 0;
222 #endif
223 if (!strcmp("http.lowspeedlimit", var)) {
224 curl_low_speed_limit = (long)git_config_int(var, value);
225 return 0;
227 if (!strcmp("http.lowspeedtime", var)) {
228 curl_low_speed_time = (long)git_config_int(var, value);
229 return 0;
232 if (!strcmp("http.noepsv", var)) {
233 curl_ftp_no_epsv = git_config_bool(var, value);
234 return 0;
236 if (!strcmp("http.proxy", var))
237 return git_config_string(&curl_http_proxy, var, value);
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;
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;
253 if (!strcmp("http.useragent", var))
254 return git_config_string(&user_agent, var, value);
256 /* Fall back on the default ones */
257 return git_default_config(var, value, cb);
260 static void init_curl_http_auth(CURL *result)
262 if (!http_auth.username)
263 return;
265 credential_fill(&http_auth);
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
272 static struct strbuf up = STRBUF_INIT;
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.
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);
284 #endif
287 static int has_cert_password(void)
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);
297 return 1;
300 #if LIBCURL_VERSION_NUM >= 0x071900
301 static void set_curl_keepalive(CURL *c)
303 curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
306 #elif LIBCURL_VERSION_NUM >= 0x071000
307 static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
309 int ka = 1;
310 int rc;
311 socklen_t len = (socklen_t)sizeof(ka);
313 if (type != CURLSOCKTYPE_IPCXN)
314 return 0;
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));
321 return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
324 static void set_curl_keepalive(CURL *c)
326 curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
329 #else
330 static void set_curl_keepalive(CURL *c)
332 /* not supported on older curl versions */
334 #endif
336 static CURL *get_curl_handle(void)
338 CURL *result = curl_easy_init();
340 if (!result)
341 die("curl_easy_init failed");
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);
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
360 if (http_proactive_auth)
361 init_curl_http_auth(result);
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);
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);
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
392 if (getenv("GIT_CURL_VERBOSE"))
393 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
395 curl_easy_setopt(result, CURLOPT_USERAGENT,
396 user_agent ? user_agent : git_user_agent());
398 if (curl_ftp_no_epsv)
399 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
401 #ifdef CURLOPT_USE_SSL
402 if (curl_ssl_try)
403 curl_easy_setopt(result, CURLOPT_USE_SSL, CURLUSESSL_TRY);
404 #endif
406 if (curl_http_proxy) {
407 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
408 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
411 set_curl_keepalive(result);
413 return result;
416 static void set_from_env(const char **var, const char *envname)
418 const char *val = getenv(envname);
419 if (val)
420 *var = val;
423 void http_init(struct remote *remote, const char *url, int proactive_auth)
425 char *low_speed_limit;
426 char *low_speed_time;
427 char *normalized_url;
428 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
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;
436 http_is_verbose = 0;
437 normalized_url = url_normalize(url, &config.url);
439 git_config(urlmatch_config_entry, &config);
440 free(normalized_url);
442 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
443 die("curl_global_init failed");
445 http_proactive_auth = proactive_auth;
447 if (remote && remote->http_proxy)
448 curl_http_proxy = xstrdup(remote->http_proxy);
450 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
451 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
453 #ifdef USE_CURL_MULTI
455 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
456 if (http_max_requests != NULL)
457 max_requests = atoi(http_max_requests);
460 curlm = curl_multi_init();
461 if (!curlm)
462 die("curl_multi_init failed");
463 #endif
465 if (getenv("GIT_SSL_NO_VERIFY"))
466 curl_ssl_verify = 0;
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");
477 set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
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);
486 if (curl_ssl_verify == -1)
487 curl_ssl_verify = 1;
489 curl_session_count = 0;
490 #ifdef USE_CURL_MULTI
491 if (max_requests < 1)
492 max_requests = DEFAULT_MAX_REQUESTS;
493 #endif
495 if (getenv("GIT_CURL_FTP_NO_EPSV"))
496 curl_ftp_no_epsv = 1;
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;
506 #ifndef NO_CURL_EASY_DUPHANDLE
507 curl_default = get_curl_handle();
508 #endif
511 void http_cleanup(void)
513 struct active_request_slot *slot = active_queue_head;
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);
523 free(slot);
524 slot = next;
526 active_queue_head = NULL;
528 #ifndef NO_CURL_EASY_DUPHANDLE
529 curl_easy_cleanup(curl_default);
530 #endif
532 #ifdef USE_CURL_MULTI
533 curl_multi_cleanup(curlm);
534 #endif
535 curl_global_cleanup();
537 curl_slist_free_all(pragma_header);
538 pragma_header = NULL;
540 curl_slist_free_all(no_pragma_header);
541 no_pragma_header = NULL;
543 if (curl_http_proxy) {
544 free((void *)curl_http_proxy);
545 curl_http_proxy = NULL;
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;
553 ssl_cert_password_required = 0;
555 free(cached_accept_language);
556 cached_accept_language = NULL;
559 struct active_request_slot *get_active_slot(void)
561 struct active_request_slot *slot = active_queue_head;
562 struct active_request_slot *newslot;
564 #ifdef USE_CURL_MULTI
565 int num_transfers;
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();
573 #endif
575 while (slot != NULL && slot->in_use)
576 slot = slot->next;
578 if (slot == NULL) {
579 newslot = xmalloc(sizeof(*newslot));
580 newslot->curl = NULL;
581 newslot->in_use = 0;
582 newslot->next = NULL;
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;
592 slot = newslot;
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++;
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);
628 return slot;
631 int start_active_slot(struct active_request_slot *slot)
633 #ifdef USE_CURL_MULTI
634 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
635 int num_transfers;
637 if (curlm_result != CURLM_OK &&
638 curlm_result != CURLM_CALL_MULTI_PERFORM) {
639 active_requests--;
640 slot->in_use = 0;
641 return 0;
645 * We know there must be something to do, since we just added
646 * something.
648 curl_multi_perform(curlm, &num_transfers);
649 #endif
650 return 1;
653 #ifdef USE_CURL_MULTI
654 struct fill_chain {
655 void *data;
656 int (*fill)(void *);
657 struct fill_chain *next;
660 static struct fill_chain *fill_cfg;
662 void add_fill_function(void *data, int (*fill)(void *))
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;
674 void fill_active_slots(void)
676 struct active_request_slot *slot = active_queue_head;
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;
684 if (!fill)
685 break;
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--;
695 slot = slot->next;
699 void step_active_slots(void)
701 int num_transfers;
702 CURLMcode curlm_result;
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();
712 #endif
714 void run_active_slot(struct active_request_slot *slot)
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;
724 slot->finished = &finished;
725 while (!finished) {
726 step_active_slots();
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;
741 #else
742 select_timeout.tv_sec = 0;
743 select_timeout.tv_usec = 50000;
744 #endif
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);
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.
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;
764 select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
767 #else
768 while (slot->in_use) {
769 slot->curl_result = curl_easy_perform(slot->curl);
770 finish_active_slot(slot);
772 #endif
775 static void release_active_slot(struct active_request_slot *slot)
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--;
786 #ifdef USE_CURL_MULTI
787 fill_active_slots();
788 #endif
791 void finish_all_active_slots(void)
793 struct active_request_slot *slot = active_queue_head;
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;
804 /* Helpers for modifying and creating URLs */
805 static inline int needs_quote(int ch)
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;
817 static char *quote_ref_url(const char *base, const char *ref)
819 struct strbuf buf = STRBUF_INIT;
820 const char *cp;
821 int ch;
823 end_url_with_slash(&buf, base);
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);
831 return strbuf_detach(&buf, NULL);
834 void append_remote_object_url(struct strbuf *buf, const char *url,
835 const char *hex,
836 int only_two_digit_prefix)
838 end_url_with_slash(buf, url);
840 strbuf_addf(buf, "objects/%.*s/", 2, hex);
841 if (!only_two_digit_prefix)
842 strbuf_addf(buf, "%s", hex+2);
845 char *get_remote_object_url(const char *url, const char *hex,
846 int only_two_digit_prefix)
848 struct strbuf buf = STRBUF_INIT;
849 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
850 return strbuf_detach(&buf, NULL);
853 static int handle_curl_result(struct slot_results *results)
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.
860 if (results->curl_result == CURLE_OK &&
861 results->http_code >= 400) {
862 results->curl_result = CURLE_HTTP_RETURNED_ERROR;
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.
869 snprintf(curl_errorstr, sizeof(curl_errorstr),
870 "The requested URL returned error: %ld",
871 results->http_code);
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;
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;
900 int run_one_slot(struct active_request_slot *slot,
901 struct slot_results *results)
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;
910 run_active_slot(slot);
911 return handle_curl_result(results);
914 static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
916 char *ptr;
917 CURLcode ret;
919 strbuf_reset(buf);
920 ret = curl_easy_getinfo(curl, info, &ptr);
921 if (!ret && ptr)
922 strbuf_addstr(buf, ptr);
923 return ret;
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.
931 * "name" is the name of the parameter. The value is appended
932 * to "out".
934 static int extract_param(const char *raw, const char *name,
935 struct strbuf *out)
937 size_t len = strlen(name);
939 if (strncasecmp(raw, name, len))
940 return -1;
941 raw += len;
943 if (*raw != '=')
944 return -1;
945 raw++;
947 while (*raw && !isspace(*raw) && *raw != ';')
948 strbuf_addch(out, *raw++);
949 return 0;
953 * Extract a normalized version of the content type, with any
954 * spaces suppressed, all letters lowercased, and no trailing ";"
955 * or parameters.
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.
962 * If the "charset" argument is not NULL, store the value of any
963 * charset parameter there.
965 * Example:
966 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
967 * "text / plain" -> "text/plain"
969 static void extract_content_type(struct strbuf *raw, struct strbuf *type,
970 struct strbuf *charset)
972 const char *p;
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;
983 strbuf_addch(type, tolower(*p));
986 if (!charset)
987 return;
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++;
999 if (!charset->len && starts_with(type->buf, "text/"))
1000 strbuf_addstr(charset, "ISO-8859-1");
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.
1008 * The result can be a colon-separated list like "ko:ja:en".
1010 static const char *get_preferred_languages(void)
1012 const char *retval;
1014 retval = getenv("LANGUAGE");
1015 if (retval && *retval)
1016 return retval;
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
1026 return NULL;
1029 static void write_accept_language(struct strbuf *buf)
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.
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;
1046 /* Don't add Accept-Language header if no language is preferred. */
1047 if (!s)
1048 return;
1051 * Split the colon-separated string of preferred languages into
1052 * language_tags array.
1054 do {
1055 /* collect language tag */
1056 for (; *s && (isalnum(*s) || *s == '_'); s++)
1057 strbuf_addch(&tag, *s == '_' ? '-' : *s);
1059 /* skip .codeset, @modifier and any other unnecessary parts */
1060 while (*s && *s != ':')
1061 s++;
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;
1070 } while (*s++);
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];
1079 /* add '*' */
1080 REALLOC_ARRAY(language_tags, num_langs + 1);
1081 language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */
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)
1089 sprintf(q_format, ";q=0.%%0%dd", decimal_places);
1091 strbuf_addstr(buf, "Accept-Language: ");
1093 for (i = 0; i < num_langs; i++) {
1094 if (i > 0)
1095 strbuf_addstr(buf, ", ");
1097 strbuf_addstr(buf, language_tags[i]);
1099 if (i > 0)
1100 strbuf_addf(buf, q_format, max_q - i);
1102 if (buf->len > MAX_ACCEPT_LANGUAGE_HEADER_SIZE) {
1103 strbuf_remove(buf, last_buf_len, buf->len - last_buf_len);
1104 break;
1107 last_buf_len = buf->len;
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);
1118 * Get an Accept-Language header which indicates user's preferred languages.
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 -> ""
1128 static const char *get_accept_language(void)
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);
1137 return cached_accept_language;
1140 /* http_request() targets */
1141 #define HTTP_REQUEST_STRBUF 0
1142 #define HTTP_REQUEST_FILE 1
1144 static int http_request(const char *url,
1145 void *result, int target,
1146 const struct http_get_options *options)
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;
1155 slot = get_active_slot();
1156 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
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);
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);
1173 } else
1174 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1175 fwrite_buffer);
1178 accept_language = get_accept_language();
1180 if (accept_language)
1181 headers = curl_slist_append(headers, accept_language);
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);
1189 headers = curl_slist_append(headers, buf.buf);
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");
1195 ret = run_one_slot(slot, &results);
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);
1205 if (options && options->effective_url)
1206 curlinfo_strbuf(slot->curl, CURLINFO_EFFECTIVE_URL,
1207 options->effective_url);
1209 curl_slist_free_all(headers);
1210 strbuf_release(&buf);
1212 return ret;
1216 * Update the "base" url to a more appropriate value, as deduced by
1217 * redirects seen when requesting a URL starting with "url".
1219 * The "asked" parameter is a URL that we asked curl to access, and must begin
1220 * with "base".
1222 * The "got" parameter is the URL that curl reported to us as where we ended
1223 * up.
1225 * Returns 1 if we updated the base url, 0 otherwise.
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".
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).
1240 static int update_url_from_redirect(struct strbuf *base,
1241 const char *asked,
1242 const struct strbuf *got)
1244 const char *tail;
1245 size_t tail_len;
1247 if (!strcmp(asked, got->buf))
1248 return 0;
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);
1254 tail_len = strlen(tail);
1256 if (got->len < tail_len ||
1257 strcmp(tail, got->buf + got->len - tail_len))
1258 return 0; /* insane redirect scheme */
1260 strbuf_reset(base);
1261 strbuf_add(base, got->buf, got->len - tail_len);
1262 return 1;
1265 static int http_request_reauth(const char *url,
1266 void *result, int target,
1267 struct http_get_options *options)
1269 int ret = http_request(url, result, target, options);
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;
1279 if (ret != HTTP_REAUTH)
1280 return ret;
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.
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");
1298 credential_fill(&http_auth);
1300 return http_request(url, result, target, options);
1303 int http_get_strbuf(const char *url,
1304 struct strbuf *result,
1305 struct http_get_options *options)
1307 return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
1311 * Downloads a URL and stores the result in the given file.
1313 * If a previous interrupted download is detected (i.e. a previous temporary
1314 * file is still around) the download is resumed.
1316 static int http_get_file(const char *url, const char *filename,
1317 struct http_get_options *options)
1319 int ret;
1320 struct strbuf tmpfile = STRBUF_INIT;
1321 FILE *result;
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;
1331 ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
1332 fclose(result);
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;
1341 int http_fetch_ref(const char *base, struct ref *ref)
1343 struct http_get_options options = {0};
1344 char *url;
1345 struct strbuf buffer = STRBUF_INIT;
1346 int ret = -1;
1348 options.no_cache = 1;
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;
1361 strbuf_release(&buffer);
1362 free(url);
1363 return ret;
1366 /* Helpers for fetching packs */
1367 static char *fetch_pack_index(unsigned char *sha1, const char *base_url)
1369 char *url, *tmp;
1370 struct strbuf buf = STRBUF_INIT;
1372 if (http_is_verbose)
1373 fprintf(stderr, "Getting index for pack %s\n", sha1_to_hex(sha1));
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);
1379 strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(sha1));
1380 tmp = strbuf_detach(&buf, NULL);
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;
1388 free(url);
1389 return tmp;
1392 static int fetch_and_setup_pack_index(struct packed_git **packs_head,
1393 unsigned char *sha1, const char *base_url)
1395 struct packed_git *new_pack;
1396 char *tmp_idx = NULL;
1397 int ret;
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;
1406 tmp_idx = fetch_pack_index(sha1, base_url);
1407 if (!tmp_idx)
1408 return -1;
1410 new_pack = parse_pack_index(sha1, tmp_idx);
1411 if (!new_pack) {
1412 unlink(tmp_idx);
1413 free(tmp_idx);
1415 return -1; /* parse_pack_index() already issued error message */
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));
1423 free(tmp_idx);
1424 if (ret)
1425 return -1;
1427 add_pack:
1428 new_pack->next = *packs_head;
1429 *packs_head = new_pack;
1430 return 0;
1433 int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
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];
1441 end_url_with_slash(&buf, base_url);
1442 strbuf_addstr(&buf, "objects/info/packs");
1443 url = strbuf_detach(&buf, NULL);
1445 options.no_cache = 1;
1446 ret = http_get_strbuf(url, &buf, &options);
1447 if (ret != HTTP_OK)
1448 goto cleanup;
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;
1464 default:
1465 while (i < buf.len && data[i] != '\n')
1466 i++;
1468 i++;
1471 cleanup:
1472 free(url);
1473 return ret;
1476 void release_http_pack_request(struct http_pack_request *preq)
1478 if (preq->packfile != NULL) {
1479 fclose(preq->packfile);
1480 preq->packfile = NULL;
1482 if (preq->range_header != NULL) {
1483 curl_slist_free_all(preq->range_header);
1484 preq->range_header = NULL;
1486 preq->slot = NULL;
1487 free(preq->url);
1490 int finish_http_pack_request(struct http_pack_request *preq)
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];
1498 close_pack_index(p);
1500 fclose(preq->packfile);
1501 preq->packfile = NULL;
1503 lst = preq->lst;
1504 while (*lst != p)
1505 lst = &((*lst)->next);
1506 *lst = (*lst)->next;
1508 tmp_idx = xstrdup(preq->tmpfile);
1509 strcpy(tmp_idx + strlen(tmp_idx) - strlen(".pack.temp"),
1510 ".idx.temp");
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;
1518 ip.argv = ip_argv;
1519 ip.git_cmd = 1;
1520 ip.no_stdin = 1;
1521 ip.no_stdout = 1;
1523 if (run_command(&ip)) {
1524 unlink(preq->tmpfile);
1525 unlink(tmp_idx);
1526 free(tmp_idx);
1527 return -1;
1530 unlink(sha1_pack_index_name(p->sha1));
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;
1538 install_packed_git(p);
1539 free(tmp_idx);
1540 return 0;
1543 struct http_pack_request *new_http_pack_request(
1544 struct packed_git *target, const char *base_url)
1546 long prev_posn = 0;
1547 char range[RANGE_HEADER_SIZE];
1548 struct strbuf buf = STRBUF_INIT;
1549 struct http_pack_request *preq;
1551 preq = xcalloc(1, sizeof(*preq));
1552 preq->target = target;
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);
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;
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);
1576 * If there is data present from a previous transfer attempt,
1577 * resume where it left off
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);
1591 return preq;
1593 abort:
1594 free(preq->url);
1595 free(preq);
1596 return NULL;
1599 /* Helpers for fetching objects (loose) */
1600 static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
1601 void *data)
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);
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;
1628 struct http_object_request *new_http_object_request(const char *base_url,
1629 unsigned char *sha1)
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;
1642 freq = xcalloc(1, sizeof(*freq));
1643 hashcpy(freq->sha1, sha1);
1644 freq->localfile = -1;
1646 filename = sha1_file_name(sha1);
1647 snprintf(freq->tmpfile, sizeof(freq->tmpfile),
1648 "%s.temp", filename);
1650 snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
1651 unlink_or_warn(prevfile);
1652 rename(freq->tmpfile, prevfile);
1653 unlink_or_warn(freq->tmpfile);
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);
1660 * This could have failed due to the "lazy directory creation";
1661 * try to mkdir the last path component.
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 = '/';
1670 freq->localfile = open(freq->tmpfile,
1671 O_WRONLY | O_CREAT | O_EXCL, 0666);
1674 if (freq->localfile < 0) {
1675 error("Couldn't create temporary file %s: %s",
1676 freq->tmpfile, strerror(errno));
1677 goto abort;
1680 git_inflate_init(&freq->stream);
1682 git_SHA1_Init(&freq->c);
1684 freq->url = get_remote_object_url(base_url, hex, 0);
1687 * If a previous temp file is present, process what was already
1688 * fetched.
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,
1697 prev_read,
1698 freq) == prev_read) {
1699 prev_posn += prev_read;
1700 } else {
1701 prev_read = -1;
1704 } while (prev_read > 0);
1705 close(prevlocal);
1707 unlink_or_warn(prevfile);
1710 * Reset inflate/SHA1 if there was an error reading the previous temp
1711 * file; also rewind to the beginning of the local file.
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;
1728 freq->slot = get_active_slot();
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);
1737 * If we have successfully processed data from a previous fetch
1738 * attempt, only fetch the data we don't already have.
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);
1751 return freq;
1753 abort:
1754 free(freq->url);
1755 free(freq);
1756 return NULL;
1759 void process_http_object_request(struct http_object_request *freq)
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;
1768 int finish_http_object_request(struct http_object_request *freq)
1770 struct stat st;
1772 close(freq->localfile);
1773 freq->localfile = -1;
1775 process_http_object_request(freq);
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;
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;
1792 if (hashcmp(freq->sha1, freq->real_sha1)) {
1793 unlink_or_warn(freq->tmpfile);
1794 return -1;
1796 freq->rename =
1797 move_temp_to_file(freq->tmpfile, sha1_file_name(freq->sha1));
1799 return freq->rename;
1802 void abort_http_object_request(struct http_object_request *freq)
1804 unlink_or_warn(freq->tmpfile);
1806 release_http_object_request(freq);
1809 void release_http_object_request(struct http_object_request *freq)
1811 if (freq->localfile != -1) {
1812 close(freq->localfile);
1813 freq->localfile = -1;
1815 if (freq->url != NULL) {
1816 free(freq->url);
1817 freq->url = NULL;
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;