4 #include "run-command.h"
7 #include "credential.h"
13 size_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
15 #if LIBCURL_VERSION_NUM >= 0x070a06
16 #define LIBCURL_CAN_HANDLE_AUTH_ANY
19 static int min_curl_sessions
= 1;
20 static int curl_session_count
;
22 static int max_requests
= -1;
25 #ifndef NO_CURL_EASY_DUPHANDLE
26 static CURL
*curl_default
;
29 #define PREV_BUF_SIZE 4096
30 #define RANGE_HEADER_SIZE 30
32 char curl_errorstr
[CURL_ERROR_SIZE
];
34 static int curl_ssl_verify
= -1;
35 static int curl_ssl_try
;
36 static const char *ssl_cert
;
37 #if LIBCURL_VERSION_NUM >= 0x070903
38 static const char *ssl_key
;
40 #if LIBCURL_VERSION_NUM >= 0x070908
41 static const char *ssl_capath
;
43 static const char *ssl_cainfo
;
44 static long curl_low_speed_limit
= -1;
45 static long curl_low_speed_time
= -1;
46 static int curl_ftp_no_epsv
;
47 static const char *curl_http_proxy
;
48 static const char *curl_cookie_file
;
49 static int curl_save_cookies
;
50 struct credential http_auth
= CREDENTIAL_INIT
;
51 static int http_proactive_auth
;
52 static const char *user_agent
;
54 #if LIBCURL_VERSION_NUM >= 0x071700
55 /* Use CURLOPT_KEYPASSWD as is */
56 #elif LIBCURL_VERSION_NUM >= 0x070903
57 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
59 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
62 static struct credential cert_auth
= CREDENTIAL_INIT
;
63 static int ssl_cert_password_required
;
65 static struct curl_slist
*pragma_header
;
66 static struct curl_slist
*no_pragma_header
;
68 static struct active_request_slot
*active_queue_head
;
70 size_t fread_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
72 size_t size
= eltsize
* nmemb
;
73 struct buffer
*buffer
= buffer_
;
75 if (size
> buffer
->buf
.len
- buffer
->posn
)
76 size
= buffer
->buf
.len
- buffer
->posn
;
77 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
84 curlioerr
ioctl_buffer(CURL
*handle
, int cmd
, void *clientp
)
86 struct buffer
*buffer
= clientp
;
92 case CURLIOCMD_RESTARTREAD
:
97 return CURLIOE_UNKNOWNCMD
;
102 size_t fwrite_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
104 size_t size
= eltsize
* nmemb
;
105 struct strbuf
*buffer
= buffer_
;
107 strbuf_add(buffer
, ptr
, size
);
111 size_t fwrite_null(char *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
113 return eltsize
* nmemb
;
116 #ifdef USE_CURL_MULTI
117 static void process_curl_messages(void)
120 struct active_request_slot
*slot
;
121 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
123 while (curl_message
!= NULL
) {
124 if (curl_message
->msg
== CURLMSG_DONE
) {
125 int curl_result
= curl_message
->data
.result
;
126 slot
= active_queue_head
;
127 while (slot
!= NULL
&&
128 slot
->curl
!= curl_message
->easy_handle
)
131 curl_multi_remove_handle(curlm
, slot
->curl
);
132 slot
->curl_result
= curl_result
;
133 finish_active_slot(slot
);
135 fprintf(stderr
, "Received DONE message for unknown request!\n");
138 fprintf(stderr
, "Unknown CURL message received: %d\n",
139 (int)curl_message
->msg
);
141 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
146 static int http_options(const char *var
, const char *value
, void *cb
)
148 if (!strcmp("http.sslverify", var
)) {
149 curl_ssl_verify
= git_config_bool(var
, value
);
152 if (!strcmp("http.sslcert", var
))
153 return git_config_string(&ssl_cert
, var
, value
);
154 #if LIBCURL_VERSION_NUM >= 0x070903
155 if (!strcmp("http.sslkey", var
))
156 return git_config_string(&ssl_key
, var
, value
);
158 #if LIBCURL_VERSION_NUM >= 0x070908
159 if (!strcmp("http.sslcapath", var
))
160 return git_config_string(&ssl_capath
, var
, value
);
162 if (!strcmp("http.sslcainfo", var
))
163 return git_config_string(&ssl_cainfo
, var
, value
);
164 if (!strcmp("http.sslcertpasswordprotected", var
)) {
165 ssl_cert_password_required
= git_config_bool(var
, value
);
168 if (!strcmp("http.ssltry", var
)) {
169 curl_ssl_try
= git_config_bool(var
, value
);
172 if (!strcmp("http.minsessions", var
)) {
173 min_curl_sessions
= git_config_int(var
, value
);
174 #ifndef USE_CURL_MULTI
175 if (min_curl_sessions
> 1)
176 min_curl_sessions
= 1;
180 #ifdef USE_CURL_MULTI
181 if (!strcmp("http.maxrequests", var
)) {
182 max_requests
= git_config_int(var
, value
);
186 if (!strcmp("http.lowspeedlimit", var
)) {
187 curl_low_speed_limit
= (long)git_config_int(var
, value
);
190 if (!strcmp("http.lowspeedtime", var
)) {
191 curl_low_speed_time
= (long)git_config_int(var
, value
);
195 if (!strcmp("http.noepsv", var
)) {
196 curl_ftp_no_epsv
= git_config_bool(var
, value
);
199 if (!strcmp("http.proxy", var
))
200 return git_config_string(&curl_http_proxy
, var
, value
);
202 if (!strcmp("http.cookiefile", var
))
203 return git_config_string(&curl_cookie_file
, var
, value
);
204 if (!strcmp("http.savecookies", var
)) {
205 curl_save_cookies
= git_config_bool(var
, value
);
209 if (!strcmp("http.postbuffer", var
)) {
210 http_post_buffer
= git_config_int(var
, value
);
211 if (http_post_buffer
< LARGE_PACKET_MAX
)
212 http_post_buffer
= LARGE_PACKET_MAX
;
216 if (!strcmp("http.useragent", var
))
217 return git_config_string(&user_agent
, var
, value
);
219 /* Fall back on the default ones */
220 return git_default_config(var
, value
, cb
);
223 static void init_curl_http_auth(CURL
*result
)
225 if (!http_auth
.username
)
228 credential_fill(&http_auth
);
230 #if LIBCURL_VERSION_NUM >= 0x071301
231 curl_easy_setopt(result
, CURLOPT_USERNAME
, http_auth
.username
);
232 curl_easy_setopt(result
, CURLOPT_PASSWORD
, http_auth
.password
);
235 static struct strbuf up
= STRBUF_INIT
;
237 * Note that we assume we only ever have a single set of
238 * credentials in a given program run, so we do not have
239 * to worry about updating this buffer, only setting its
243 strbuf_addf(&up
, "%s:%s",
244 http_auth
.username
, http_auth
.password
);
245 curl_easy_setopt(result
, CURLOPT_USERPWD
, up
.buf
);
250 static int has_cert_password(void)
252 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
254 if (!cert_auth
.password
) {
255 cert_auth
.protocol
= xstrdup("cert");
256 cert_auth
.username
= xstrdup("");
257 cert_auth
.path
= xstrdup(ssl_cert
);
258 credential_fill(&cert_auth
);
263 #if LIBCURL_VERSION_NUM >= 0x071900
264 static void set_curl_keepalive(CURL
*c
)
266 curl_easy_setopt(c
, CURLOPT_TCP_KEEPALIVE
, 1);
269 #elif LIBCURL_VERSION_NUM >= 0x071000
270 static int sockopt_callback(void *client
, curl_socket_t fd
, curlsocktype type
)
274 socklen_t len
= (socklen_t
)sizeof(ka
);
276 if (type
!= CURLSOCKTYPE_IPCXN
)
279 rc
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&ka
, len
);
281 warning("unable to set SO_KEEPALIVE on socket %s",
284 return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
287 static void set_curl_keepalive(CURL
*c
)
289 curl_easy_setopt(c
, CURLOPT_SOCKOPTFUNCTION
, sockopt_callback
);
293 static void set_curl_keepalive(CURL
*c
)
295 /* not supported on older curl versions */
299 static CURL
*get_curl_handle(void)
301 CURL
*result
= curl_easy_init();
304 die("curl_easy_init failed");
306 if (!curl_ssl_verify
) {
307 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
308 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
310 /* Verify authenticity of the peer's certificate */
311 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
312 /* The name in the cert must match whom we tried to connect */
313 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
316 #if LIBCURL_VERSION_NUM >= 0x070907
317 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
319 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
320 curl_easy_setopt(result
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
323 if (http_proactive_auth
)
324 init_curl_http_auth(result
);
326 if (ssl_cert
!= NULL
)
327 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
328 if (has_cert_password())
329 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, cert_auth
.password
);
330 #if LIBCURL_VERSION_NUM >= 0x070903
332 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
334 #if LIBCURL_VERSION_NUM >= 0x070908
335 if (ssl_capath
!= NULL
)
336 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
338 if (ssl_cainfo
!= NULL
)
339 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
341 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
342 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
343 curl_low_speed_limit
);
344 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
345 curl_low_speed_time
);
348 curl_easy_setopt(result
, CURLOPT_FOLLOWLOCATION
, 1);
349 #if LIBCURL_VERSION_NUM >= 0x071301
350 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
351 #elif LIBCURL_VERSION_NUM >= 0x071101
352 curl_easy_setopt(result
, CURLOPT_POST301
, 1);
355 if (getenv("GIT_CURL_VERBOSE"))
356 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
358 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
359 user_agent
? user_agent
: git_user_agent());
361 if (curl_ftp_no_epsv
)
362 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
364 #ifdef CURLOPT_USE_SSL
366 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
369 if (curl_http_proxy
) {
370 curl_easy_setopt(result
, CURLOPT_PROXY
, curl_http_proxy
);
371 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
374 set_curl_keepalive(result
);
379 static void set_from_env(const char **var
, const char *envname
)
381 const char *val
= getenv(envname
);
386 void http_init(struct remote
*remote
, const char *url
, int proactive_auth
)
388 char *low_speed_limit
;
389 char *low_speed_time
;
390 char *normalized_url
;
391 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
393 config
.section
= "http";
395 config
.collect_fn
= http_options
;
396 config
.cascade_fn
= git_default_config
;
400 normalized_url
= url_normalize(url
, &config
.url
);
402 git_config(urlmatch_config_entry
, &config
);
403 free(normalized_url
);
405 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
)
406 die("curl_global_init failed");
408 http_proactive_auth
= proactive_auth
;
410 if (remote
&& remote
->http_proxy
)
411 curl_http_proxy
= xstrdup(remote
->http_proxy
);
413 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
414 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
416 #ifdef USE_CURL_MULTI
418 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
419 if (http_max_requests
!= NULL
)
420 max_requests
= atoi(http_max_requests
);
423 curlm
= curl_multi_init();
425 fprintf(stderr
, "Error creating curl multi handle.\n");
430 if (getenv("GIT_SSL_NO_VERIFY"))
433 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
434 #if LIBCURL_VERSION_NUM >= 0x070903
435 set_from_env(&ssl_key
, "GIT_SSL_KEY");
437 #if LIBCURL_VERSION_NUM >= 0x070908
438 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
440 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
442 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
444 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
445 if (low_speed_limit
!= NULL
)
446 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
447 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
448 if (low_speed_time
!= NULL
)
449 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
451 if (curl_ssl_verify
== -1)
454 curl_session_count
= 0;
455 #ifdef USE_CURL_MULTI
456 if (max_requests
< 1)
457 max_requests
= DEFAULT_MAX_REQUESTS
;
460 if (getenv("GIT_CURL_FTP_NO_EPSV"))
461 curl_ftp_no_epsv
= 1;
464 credential_from_url(&http_auth
, url
);
465 if (!ssl_cert_password_required
&&
466 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
467 starts_with(url
, "https://"))
468 ssl_cert_password_required
= 1;
471 #ifndef NO_CURL_EASY_DUPHANDLE
472 curl_default
= get_curl_handle();
476 void http_cleanup(void)
478 struct active_request_slot
*slot
= active_queue_head
;
480 while (slot
!= NULL
) {
481 struct active_request_slot
*next
= slot
->next
;
482 if (slot
->curl
!= NULL
) {
483 #ifdef USE_CURL_MULTI
484 curl_multi_remove_handle(curlm
, slot
->curl
);
486 curl_easy_cleanup(slot
->curl
);
491 active_queue_head
= NULL
;
493 #ifndef NO_CURL_EASY_DUPHANDLE
494 curl_easy_cleanup(curl_default
);
497 #ifdef USE_CURL_MULTI
498 curl_multi_cleanup(curlm
);
500 curl_global_cleanup();
502 curl_slist_free_all(pragma_header
);
503 pragma_header
= NULL
;
505 curl_slist_free_all(no_pragma_header
);
506 no_pragma_header
= NULL
;
508 if (curl_http_proxy
) {
509 free((void *)curl_http_proxy
);
510 curl_http_proxy
= NULL
;
513 if (cert_auth
.password
!= NULL
) {
514 memset(cert_auth
.password
, 0, strlen(cert_auth
.password
));
515 free(cert_auth
.password
);
516 cert_auth
.password
= NULL
;
518 ssl_cert_password_required
= 0;
521 struct active_request_slot
*get_active_slot(void)
523 struct active_request_slot
*slot
= active_queue_head
;
524 struct active_request_slot
*newslot
;
526 #ifdef USE_CURL_MULTI
529 /* Wait for a slot to open up if the queue is full */
530 while (active_requests
>= max_requests
) {
531 curl_multi_perform(curlm
, &num_transfers
);
532 if (num_transfers
< active_requests
)
533 process_curl_messages();
537 while (slot
!= NULL
&& slot
->in_use
)
541 newslot
= xmalloc(sizeof(*newslot
));
542 newslot
->curl
= NULL
;
544 newslot
->next
= NULL
;
546 slot
= active_queue_head
;
548 active_queue_head
= newslot
;
550 while (slot
->next
!= NULL
)
552 slot
->next
= newslot
;
557 if (slot
->curl
== NULL
) {
558 #ifdef NO_CURL_EASY_DUPHANDLE
559 slot
->curl
= get_curl_handle();
561 slot
->curl
= curl_easy_duphandle(curl_default
);
563 curl_session_count
++;
568 slot
->results
= NULL
;
569 slot
->finished
= NULL
;
570 slot
->callback_data
= NULL
;
571 slot
->callback_func
= NULL
;
572 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
573 if (curl_save_cookies
)
574 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEJAR
, curl_cookie_file
);
575 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
576 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
577 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
578 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
579 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
580 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
581 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
582 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
583 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 1);
584 if (http_auth
.password
)
585 init_curl_http_auth(slot
->curl
);
590 int start_active_slot(struct active_request_slot
*slot
)
592 #ifdef USE_CURL_MULTI
593 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
596 if (curlm_result
!= CURLM_OK
&&
597 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
604 * We know there must be something to do, since we just added
607 curl_multi_perform(curlm
, &num_transfers
);
612 #ifdef USE_CURL_MULTI
616 struct fill_chain
*next
;
619 static struct fill_chain
*fill_cfg
;
621 void add_fill_function(void *data
, int (*fill
)(void *))
623 struct fill_chain
*new = xmalloc(sizeof(*new));
624 struct fill_chain
**linkp
= &fill_cfg
;
629 linkp
= &(*linkp
)->next
;
633 void fill_active_slots(void)
635 struct active_request_slot
*slot
= active_queue_head
;
637 while (active_requests
< max_requests
) {
638 struct fill_chain
*fill
;
639 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
640 if (fill
->fill(fill
->data
))
647 while (slot
!= NULL
) {
648 if (!slot
->in_use
&& slot
->curl
!= NULL
649 && curl_session_count
> min_curl_sessions
) {
650 curl_easy_cleanup(slot
->curl
);
652 curl_session_count
--;
658 void step_active_slots(void)
661 CURLMcode curlm_result
;
664 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
665 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
666 if (num_transfers
< active_requests
) {
667 process_curl_messages();
673 void run_active_slot(struct active_request_slot
*slot
)
675 #ifdef USE_CURL_MULTI
680 struct timeval select_timeout
;
683 slot
->finished
= &finished
;
688 #if LIBCURL_VERSION_NUM >= 0x070f04
690 curl_multi_timeout(curlm
, &curl_timeout
);
691 if (curl_timeout
== 0) {
693 } else if (curl_timeout
== -1) {
694 select_timeout
.tv_sec
= 0;
695 select_timeout
.tv_usec
= 50000;
697 select_timeout
.tv_sec
= curl_timeout
/ 1000;
698 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
701 select_timeout
.tv_sec
= 0;
702 select_timeout
.tv_usec
= 50000;
709 curl_multi_fdset(curlm
, &readfds
, &writefds
, &excfds
, &max_fd
);
712 * It can happen that curl_multi_timeout returns a pathologically
713 * long timeout when curl_multi_fdset returns no file descriptors
714 * to read. See commit message for more details.
717 (select_timeout
.tv_sec
> 0 ||
718 select_timeout
.tv_usec
> 50000)) {
719 select_timeout
.tv_sec
= 0;
720 select_timeout
.tv_usec
= 50000;
723 select(max_fd
+1, &readfds
, &writefds
, &excfds
, &select_timeout
);
727 while (slot
->in_use
) {
728 slot
->curl_result
= curl_easy_perform(slot
->curl
);
729 finish_active_slot(slot
);
734 static void closedown_active_slot(struct active_request_slot
*slot
)
740 static void release_active_slot(struct active_request_slot
*slot
)
742 closedown_active_slot(slot
);
743 if (slot
->curl
&& curl_session_count
> min_curl_sessions
) {
744 #ifdef USE_CURL_MULTI
745 curl_multi_remove_handle(curlm
, slot
->curl
);
747 curl_easy_cleanup(slot
->curl
);
749 curl_session_count
--;
751 #ifdef USE_CURL_MULTI
756 void finish_active_slot(struct active_request_slot
*slot
)
758 closedown_active_slot(slot
);
759 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
761 if (slot
->finished
!= NULL
)
762 (*slot
->finished
) = 1;
764 /* Store slot results so they can be read after the slot is reused */
765 if (slot
->results
!= NULL
) {
766 slot
->results
->curl_result
= slot
->curl_result
;
767 slot
->results
->http_code
= slot
->http_code
;
768 #if LIBCURL_VERSION_NUM >= 0x070a08
769 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTPAUTH_AVAIL
,
770 &slot
->results
->auth_avail
);
772 slot
->results
->auth_avail
= 0;
776 /* Run callback if appropriate */
777 if (slot
->callback_func
!= NULL
)
778 slot
->callback_func(slot
->callback_data
);
781 void finish_all_active_slots(void)
783 struct active_request_slot
*slot
= active_queue_head
;
787 run_active_slot(slot
);
788 slot
= active_queue_head
;
794 /* Helpers for modifying and creating URLs */
795 static inline int needs_quote(int ch
)
797 if (((ch
>= 'A') && (ch
<= 'Z'))
798 || ((ch
>= 'a') && (ch
<= 'z'))
799 || ((ch
>= '0') && (ch
<= '9'))
807 static char *quote_ref_url(const char *base
, const char *ref
)
809 struct strbuf buf
= STRBUF_INIT
;
813 end_url_with_slash(&buf
, base
);
815 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
817 strbuf_addf(&buf
, "%%%02x", ch
);
819 strbuf_addch(&buf
, *cp
);
821 return strbuf_detach(&buf
, NULL
);
824 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
826 int only_two_digit_prefix
)
828 end_url_with_slash(buf
, url
);
830 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
831 if (!only_two_digit_prefix
)
832 strbuf_addf(buf
, "%s", hex
+2);
835 char *get_remote_object_url(const char *url
, const char *hex
,
836 int only_two_digit_prefix
)
838 struct strbuf buf
= STRBUF_INIT
;
839 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
840 return strbuf_detach(&buf
, NULL
);
843 int handle_curl_result(struct slot_results
*results
)
846 * If we see a failing http code with CURLE_OK, we have turned off
847 * FAILONERROR (to keep the server's custom error response), and should
848 * translate the code into failure here.
850 if (results
->curl_result
== CURLE_OK
&&
851 results
->http_code
>= 400) {
852 results
->curl_result
= CURLE_HTTP_RETURNED_ERROR
;
854 * Normally curl will already have put the "reason phrase"
855 * from the server into curl_errorstr; unfortunately without
856 * FAILONERROR it is lost, so we can give only the numeric
859 snprintf(curl_errorstr
, sizeof(curl_errorstr
),
860 "The requested URL returned error: %ld",
864 if (results
->curl_result
== CURLE_OK
) {
865 credential_approve(&http_auth
);
867 } else if (missing_target(results
))
868 return HTTP_MISSING_TARGET
;
869 else if (results
->http_code
== 401) {
870 if (http_auth
.username
&& http_auth
.password
) {
871 credential_reject(&http_auth
);
877 #if LIBCURL_VERSION_NUM >= 0x070c00
878 if (!curl_errorstr
[0])
879 strlcpy(curl_errorstr
,
880 curl_easy_strerror(results
->curl_result
),
881 sizeof(curl_errorstr
));
887 int run_one_slot(struct active_request_slot
*slot
,
888 struct slot_results
*results
)
890 slot
->results
= results
;
891 if (!start_active_slot(slot
)) {
892 snprintf(curl_errorstr
, sizeof(curl_errorstr
),
893 "failed to start HTTP request");
894 return HTTP_START_FAILED
;
897 run_active_slot(slot
);
898 return handle_curl_result(results
);
901 static CURLcode
curlinfo_strbuf(CURL
*curl
, CURLINFO info
, struct strbuf
*buf
)
907 ret
= curl_easy_getinfo(curl
, info
, &ptr
);
909 strbuf_addstr(buf
, ptr
);
913 /* http_request() targets */
914 #define HTTP_REQUEST_STRBUF 0
915 #define HTTP_REQUEST_FILE 1
917 static int http_request(const char *url
,
918 void *result
, int target
,
919 const struct http_get_options
*options
)
921 struct active_request_slot
*slot
;
922 struct slot_results results
;
923 struct curl_slist
*headers
= NULL
;
924 struct strbuf buf
= STRBUF_INIT
;
927 slot
= get_active_slot();
928 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
930 if (result
== NULL
) {
931 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
933 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
934 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, result
);
936 if (target
== HTTP_REQUEST_FILE
) {
937 long posn
= ftell(result
);
938 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
941 strbuf_addf(&buf
, "Range: bytes=%ld-", posn
);
942 headers
= curl_slist_append(headers
, buf
.buf
);
946 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
950 strbuf_addstr(&buf
, "Pragma:");
951 if (options
&& options
->no_cache
)
952 strbuf_addstr(&buf
, " no-cache");
953 if (options
&& options
->keep_error
)
954 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
956 headers
= curl_slist_append(headers
, buf
.buf
);
958 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
959 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
960 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
962 ret
= run_one_slot(slot
, &results
);
964 if (options
&& options
->content_type
)
965 curlinfo_strbuf(slot
->curl
, CURLINFO_CONTENT_TYPE
,
966 options
->content_type
);
968 if (options
&& options
->effective_url
)
969 curlinfo_strbuf(slot
->curl
, CURLINFO_EFFECTIVE_URL
,
970 options
->effective_url
);
972 curl_slist_free_all(headers
);
973 strbuf_release(&buf
);
979 * Update the "base" url to a more appropriate value, as deduced by
980 * redirects seen when requesting a URL starting with "url".
982 * The "asked" parameter is a URL that we asked curl to access, and must begin
985 * The "got" parameter is the URL that curl reported to us as where we ended
988 * Returns 1 if we updated the base url, 0 otherwise.
990 * Our basic strategy is to compare "base" and "asked" to find the bits
991 * specific to our request. We then strip those bits off of "got" to yield the
992 * new base. So for example, if our base is "http://example.com/foo.git",
993 * and we ask for "http://example.com/foo.git/info/refs", we might end up
994 * with "https://other.example.com/foo.git/info/refs". We would want the
995 * new URL to become "https://other.example.com/foo.git".
997 * Note that this assumes a sane redirect scheme. It's entirely possible
998 * in the example above to end up at a URL that does not even end in
999 * "info/refs". In such a case we simply punt, as there is not much we can
1000 * do (and such a scheme is unlikely to represent a real git repository,
1001 * which means we are likely about to abort anyway).
1003 static int update_url_from_redirect(struct strbuf
*base
,
1005 const struct strbuf
*got
)
1010 if (!strcmp(asked
, got
->buf
))
1013 if (!starts_with(asked
, base
->buf
))
1014 die("BUG: update_url_from_redirect: %s is not a superset of %s",
1017 tail
= asked
+ base
->len
;
1018 tail_len
= strlen(tail
);
1020 if (got
->len
< tail_len
||
1021 strcmp(tail
, got
->buf
+ got
->len
- tail_len
))
1022 return 0; /* insane redirect scheme */
1025 strbuf_add(base
, got
->buf
, got
->len
- tail_len
);
1029 static int http_request_reauth(const char *url
,
1030 void *result
, int target
,
1031 struct http_get_options
*options
)
1033 int ret
= http_request(url
, result
, target
, options
);
1035 if (options
&& options
->effective_url
&& options
->base_url
) {
1036 if (update_url_from_redirect(options
->base_url
,
1037 url
, options
->effective_url
)) {
1038 credential_from_url(&http_auth
, options
->base_url
->buf
);
1039 url
= options
->effective_url
->buf
;
1043 if (ret
!= HTTP_REAUTH
)
1047 * If we are using KEEP_ERROR, the previous request may have
1048 * put cruft into our output stream; we should clear it out before
1049 * making our next request. We only know how to do this for
1050 * the strbuf case, but that is enough to satisfy current callers.
1052 if (options
&& options
->keep_error
) {
1054 case HTTP_REQUEST_STRBUF
:
1055 strbuf_reset(result
);
1058 die("BUG: HTTP_KEEP_ERROR is only supported with strbufs");
1062 credential_fill(&http_auth
);
1064 return http_request(url
, result
, target
, options
);
1067 int http_get_strbuf(const char *url
,
1068 struct strbuf
*result
,
1069 struct http_get_options
*options
)
1071 return http_request_reauth(url
, result
, HTTP_REQUEST_STRBUF
, options
);
1075 * Downloads a URL and stores the result in the given file.
1077 * If a previous interrupted download is detected (i.e. a previous temporary
1078 * file is still around) the download is resumed.
1080 static int http_get_file(const char *url
, const char *filename
,
1081 struct http_get_options
*options
)
1084 struct strbuf tmpfile
= STRBUF_INIT
;
1087 strbuf_addf(&tmpfile
, "%s.temp", filename
);
1088 result
= fopen(tmpfile
.buf
, "a");
1090 error("Unable to open local file %s", tmpfile
.buf
);
1095 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
1098 if (ret
== HTTP_OK
&& move_temp_to_file(tmpfile
.buf
, filename
))
1101 strbuf_release(&tmpfile
);
1105 int http_fetch_ref(const char *base
, struct ref
*ref
)
1107 struct http_get_options options
= {0};
1109 struct strbuf buffer
= STRBUF_INIT
;
1112 options
.no_cache
= 1;
1114 url
= quote_ref_url(base
, ref
->name
);
1115 if (http_get_strbuf(url
, &buffer
, &options
) == HTTP_OK
) {
1116 strbuf_rtrim(&buffer
);
1117 if (buffer
.len
== 40)
1118 ret
= get_sha1_hex(buffer
.buf
, ref
->old_sha1
);
1119 else if (starts_with(buffer
.buf
, "ref: ")) {
1120 ref
->symref
= xstrdup(buffer
.buf
+ 5);
1125 strbuf_release(&buffer
);
1130 /* Helpers for fetching packs */
1131 static char *fetch_pack_index(unsigned char *sha1
, const char *base_url
)
1134 struct strbuf buf
= STRBUF_INIT
;
1136 if (http_is_verbose
)
1137 fprintf(stderr
, "Getting index for pack %s\n", sha1_to_hex(sha1
));
1139 end_url_with_slash(&buf
, base_url
);
1140 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", sha1_to_hex(sha1
));
1141 url
= strbuf_detach(&buf
, NULL
);
1143 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(sha1
));
1144 tmp
= strbuf_detach(&buf
, NULL
);
1146 if (http_get_file(url
, tmp
, NULL
) != HTTP_OK
) {
1147 error("Unable to get pack index %s", url
);
1156 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
1157 unsigned char *sha1
, const char *base_url
)
1159 struct packed_git
*new_pack
;
1160 char *tmp_idx
= NULL
;
1163 if (has_pack_index(sha1
)) {
1164 new_pack
= parse_pack_index(sha1
, NULL
);
1166 return -1; /* parse_pack_index() already issued error message */
1170 tmp_idx
= fetch_pack_index(sha1
, base_url
);
1174 new_pack
= parse_pack_index(sha1
, tmp_idx
);
1179 return -1; /* parse_pack_index() already issued error message */
1182 ret
= verify_pack_index(new_pack
);
1184 close_pack_index(new_pack
);
1185 ret
= move_temp_to_file(tmp_idx
, sha1_pack_index_name(sha1
));
1192 new_pack
->next
= *packs_head
;
1193 *packs_head
= new_pack
;
1197 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
1199 struct http_get_options options
= {0};
1202 struct strbuf buf
= STRBUF_INIT
;
1203 unsigned char sha1
[20];
1205 end_url_with_slash(&buf
, base_url
);
1206 strbuf_addstr(&buf
, "objects/info/packs");
1207 url
= strbuf_detach(&buf
, NULL
);
1209 options
.no_cache
= 1;
1210 ret
= http_get_strbuf(url
, &buf
, &options
);
1215 while (i
< buf
.len
) {
1219 if (i
+ 52 <= buf
.len
&&
1220 starts_with(data
+ i
, " pack-") &&
1221 starts_with(data
+ i
+ 46, ".pack\n")) {
1222 get_sha1_hex(data
+ i
+ 6, sha1
);
1223 fetch_and_setup_pack_index(packs_head
, sha1
,
1229 while (i
< buf
.len
&& data
[i
] != '\n')
1240 void release_http_pack_request(struct http_pack_request
*preq
)
1242 if (preq
->packfile
!= NULL
) {
1243 fclose(preq
->packfile
);
1244 preq
->packfile
= NULL
;
1246 if (preq
->range_header
!= NULL
) {
1247 curl_slist_free_all(preq
->range_header
);
1248 preq
->range_header
= NULL
;
1254 int finish_http_pack_request(struct http_pack_request
*preq
)
1256 struct packed_git
**lst
;
1257 struct packed_git
*p
= preq
->target
;
1259 struct child_process ip
;
1260 const char *ip_argv
[8];
1262 close_pack_index(p
);
1264 fclose(preq
->packfile
);
1265 preq
->packfile
= NULL
;
1269 lst
= &((*lst
)->next
);
1270 *lst
= (*lst
)->next
;
1272 tmp_idx
= xstrdup(preq
->tmpfile
);
1273 strcpy(tmp_idx
+ strlen(tmp_idx
) - strlen(".pack.temp"),
1276 ip_argv
[0] = "index-pack";
1278 ip_argv
[2] = tmp_idx
;
1279 ip_argv
[3] = preq
->tmpfile
;
1282 memset(&ip
, 0, sizeof(ip
));
1288 if (run_command(&ip
)) {
1289 unlink(preq
->tmpfile
);
1295 unlink(sha1_pack_index_name(p
->sha1
));
1297 if (move_temp_to_file(preq
->tmpfile
, sha1_pack_name(p
->sha1
))
1298 || move_temp_to_file(tmp_idx
, sha1_pack_index_name(p
->sha1
))) {
1303 install_packed_git(p
);
1308 struct http_pack_request
*new_http_pack_request(
1309 struct packed_git
*target
, const char *base_url
)
1312 char range
[RANGE_HEADER_SIZE
];
1313 struct strbuf buf
= STRBUF_INIT
;
1314 struct http_pack_request
*preq
;
1316 preq
= xcalloc(1, sizeof(*preq
));
1317 preq
->target
= target
;
1319 end_url_with_slash(&buf
, base_url
);
1320 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
1321 sha1_to_hex(target
->sha1
));
1322 preq
->url
= strbuf_detach(&buf
, NULL
);
1324 snprintf(preq
->tmpfile
, sizeof(preq
->tmpfile
), "%s.temp",
1325 sha1_pack_name(target
->sha1
));
1326 preq
->packfile
= fopen(preq
->tmpfile
, "a");
1327 if (!preq
->packfile
) {
1328 error("Unable to open local file %s for pack",
1333 preq
->slot
= get_active_slot();
1334 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_FILE
, preq
->packfile
);
1335 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
1336 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
1337 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1341 * If there is data present from a previous transfer attempt,
1342 * resume where it left off
1344 prev_posn
= ftell(preq
->packfile
);
1346 if (http_is_verbose
)
1348 "Resuming fetch of pack %s at byte %ld\n",
1349 sha1_to_hex(target
->sha1
), prev_posn
);
1350 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1351 preq
->range_header
= curl_slist_append(NULL
, range
);
1352 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1353 preq
->range_header
);
1364 /* Helpers for fetching objects (loose) */
1365 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
1368 unsigned char expn
[4096];
1369 size_t size
= eltsize
* nmemb
;
1371 struct http_object_request
*freq
=
1372 (struct http_object_request
*)data
;
1374 ssize_t retval
= xwrite(freq
->localfile
,
1375 (char *) ptr
+ posn
, size
- posn
);
1379 } while (posn
< size
);
1381 freq
->stream
.avail_in
= size
;
1382 freq
->stream
.next_in
= (void *)ptr
;
1384 freq
->stream
.next_out
= expn
;
1385 freq
->stream
.avail_out
= sizeof(expn
);
1386 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
1387 git_SHA1_Update(&freq
->c
, expn
,
1388 sizeof(expn
) - freq
->stream
.avail_out
);
1389 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
1393 struct http_object_request
*new_http_object_request(const char *base_url
,
1394 unsigned char *sha1
)
1396 char *hex
= sha1_to_hex(sha1
);
1397 const char *filename
;
1398 char prevfile
[PATH_MAX
];
1400 char prev_buf
[PREV_BUF_SIZE
];
1401 ssize_t prev_read
= 0;
1403 char range
[RANGE_HEADER_SIZE
];
1404 struct curl_slist
*range_header
= NULL
;
1405 struct http_object_request
*freq
;
1407 freq
= xcalloc(1, sizeof(*freq
));
1408 hashcpy(freq
->sha1
, sha1
);
1409 freq
->localfile
= -1;
1411 filename
= sha1_file_name(sha1
);
1412 snprintf(freq
->tmpfile
, sizeof(freq
->tmpfile
),
1413 "%s.temp", filename
);
1415 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", filename
);
1416 unlink_or_warn(prevfile
);
1417 rename(freq
->tmpfile
, prevfile
);
1418 unlink_or_warn(freq
->tmpfile
);
1420 if (freq
->localfile
!= -1)
1421 error("fd leakage in start: %d", freq
->localfile
);
1422 freq
->localfile
= open(freq
->tmpfile
,
1423 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1425 * This could have failed due to the "lazy directory creation";
1426 * try to mkdir the last path component.
1428 if (freq
->localfile
< 0 && errno
== ENOENT
) {
1429 char *dir
= strrchr(freq
->tmpfile
, '/');
1432 mkdir(freq
->tmpfile
, 0777);
1435 freq
->localfile
= open(freq
->tmpfile
,
1436 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1439 if (freq
->localfile
< 0) {
1440 error("Couldn't create temporary file %s: %s",
1441 freq
->tmpfile
, strerror(errno
));
1445 git_inflate_init(&freq
->stream
);
1447 git_SHA1_Init(&freq
->c
);
1449 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
1452 * If a previous temp file is present, process what was already
1455 prevlocal
= open(prevfile
, O_RDONLY
);
1456 if (prevlocal
!= -1) {
1458 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
1460 if (fwrite_sha1_file(prev_buf
,
1463 freq
) == prev_read
) {
1464 prev_posn
+= prev_read
;
1469 } while (prev_read
> 0);
1472 unlink_or_warn(prevfile
);
1475 * Reset inflate/SHA1 if there was an error reading the previous temp
1476 * file; also rewind to the beginning of the local file.
1478 if (prev_read
== -1) {
1479 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
1480 git_inflate_init(&freq
->stream
);
1481 git_SHA1_Init(&freq
->c
);
1484 lseek(freq
->localfile
, 0, SEEK_SET
);
1485 if (ftruncate(freq
->localfile
, 0) < 0) {
1486 error("Couldn't truncate temporary file %s: %s",
1487 freq
->tmpfile
, strerror(errno
));
1493 freq
->slot
= get_active_slot();
1495 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FILE
, freq
);
1496 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
1497 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
1498 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
1499 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
1502 * If we have successfully processed data from a previous fetch
1503 * attempt, only fetch the data we don't already have.
1506 if (http_is_verbose
)
1508 "Resuming fetch of object %s at byte %ld\n",
1510 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1511 range_header
= curl_slist_append(range_header
, range
);
1512 curl_easy_setopt(freq
->slot
->curl
,
1513 CURLOPT_HTTPHEADER
, range_header
);
1524 void process_http_object_request(struct http_object_request
*freq
)
1526 if (freq
->slot
== NULL
)
1528 freq
->curl_result
= freq
->slot
->curl_result
;
1529 freq
->http_code
= freq
->slot
->http_code
;
1533 int finish_http_object_request(struct http_object_request
*freq
)
1537 close(freq
->localfile
);
1538 freq
->localfile
= -1;
1540 process_http_object_request(freq
);
1542 if (freq
->http_code
== 416) {
1543 warning("requested range invalid; we may already have all the data.");
1544 } else if (freq
->curl_result
!= CURLE_OK
) {
1545 if (stat(freq
->tmpfile
, &st
) == 0)
1546 if (st
.st_size
== 0)
1547 unlink_or_warn(freq
->tmpfile
);
1551 git_inflate_end(&freq
->stream
);
1552 git_SHA1_Final(freq
->real_sha1
, &freq
->c
);
1553 if (freq
->zret
!= Z_STREAM_END
) {
1554 unlink_or_warn(freq
->tmpfile
);
1557 if (hashcmp(freq
->sha1
, freq
->real_sha1
)) {
1558 unlink_or_warn(freq
->tmpfile
);
1562 move_temp_to_file(freq
->tmpfile
, sha1_file_name(freq
->sha1
));
1564 return freq
->rename
;
1567 void abort_http_object_request(struct http_object_request
*freq
)
1569 unlink_or_warn(freq
->tmpfile
);
1571 release_http_object_request(freq
);
1574 void release_http_object_request(struct http_object_request
*freq
)
1576 if (freq
->localfile
!= -1) {
1577 close(freq
->localfile
);
1578 freq
->localfile
= -1;
1580 if (freq
->url
!= NULL
) {
1584 if (freq
->slot
!= NULL
) {
1585 freq
->slot
->callback_func
= NULL
;
1586 freq
->slot
->callback_data
= NULL
;
1587 release_active_slot(freq
->slot
);