4 #include "run-command.h"
6 #include "credential.h"
12 size_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
14 #if LIBCURL_VERSION_NUM >= 0x070a06
15 #define LIBCURL_CAN_HANDLE_AUTH_ANY
18 static int min_curl_sessions
= 1;
19 static int curl_session_count
;
21 static int max_requests
= -1;
24 #ifndef NO_CURL_EASY_DUPHANDLE
25 static CURL
*curl_default
;
28 #define PREV_BUF_SIZE 4096
29 #define RANGE_HEADER_SIZE 30
31 char curl_errorstr
[CURL_ERROR_SIZE
];
33 static int curl_ssl_verify
= -1;
34 static int curl_ssl_try
;
35 static const char *ssl_cert
;
36 #if LIBCURL_VERSION_NUM >= 0x070903
37 static const char *ssl_key
;
39 #if LIBCURL_VERSION_NUM >= 0x070908
40 static const char *ssl_capath
;
42 static const char *ssl_cainfo
;
43 static long curl_low_speed_limit
= -1;
44 static long curl_low_speed_time
= -1;
45 static int curl_ftp_no_epsv
;
46 static const char *curl_http_proxy
;
47 static const char *curl_cookie_file
;
48 static struct credential http_auth
= CREDENTIAL_INIT
;
49 static int http_proactive_auth
;
50 static const char *user_agent
;
52 #if LIBCURL_VERSION_NUM >= 0x071700
53 /* Use CURLOPT_KEYPASSWD as is */
54 #elif LIBCURL_VERSION_NUM >= 0x070903
55 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
57 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
60 static struct credential cert_auth
= CREDENTIAL_INIT
;
61 static int ssl_cert_password_required
;
63 static struct curl_slist
*pragma_header
;
64 static struct curl_slist
*no_pragma_header
;
66 static struct active_request_slot
*active_queue_head
;
68 size_t fread_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
70 size_t size
= eltsize
* nmemb
;
71 struct buffer
*buffer
= buffer_
;
73 if (size
> buffer
->buf
.len
- buffer
->posn
)
74 size
= buffer
->buf
.len
- buffer
->posn
;
75 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
82 curlioerr
ioctl_buffer(CURL
*handle
, int cmd
, void *clientp
)
84 struct buffer
*buffer
= clientp
;
90 case CURLIOCMD_RESTARTREAD
:
95 return CURLIOE_UNKNOWNCMD
;
100 size_t fwrite_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
102 size_t size
= eltsize
* nmemb
;
103 struct strbuf
*buffer
= buffer_
;
105 strbuf_add(buffer
, ptr
, size
);
109 size_t fwrite_null(char *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
111 return eltsize
* nmemb
;
114 #ifdef USE_CURL_MULTI
115 static void process_curl_messages(void)
118 struct active_request_slot
*slot
;
119 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
121 while (curl_message
!= NULL
) {
122 if (curl_message
->msg
== CURLMSG_DONE
) {
123 int curl_result
= curl_message
->data
.result
;
124 slot
= active_queue_head
;
125 while (slot
!= NULL
&&
126 slot
->curl
!= curl_message
->easy_handle
)
129 curl_multi_remove_handle(curlm
, slot
->curl
);
130 slot
->curl_result
= curl_result
;
131 finish_active_slot(slot
);
133 fprintf(stderr
, "Received DONE message for unknown request!\n");
136 fprintf(stderr
, "Unknown CURL message received: %d\n",
137 (int)curl_message
->msg
);
139 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
144 static int http_options(const char *var
, const char *value
, void *cb
)
146 if (!strcmp("http.sslverify", var
)) {
147 curl_ssl_verify
= git_config_bool(var
, value
);
150 if (!strcmp("http.sslcert", var
))
151 return git_config_string(&ssl_cert
, var
, value
);
152 #if LIBCURL_VERSION_NUM >= 0x070903
153 if (!strcmp("http.sslkey", var
))
154 return git_config_string(&ssl_key
, var
, value
);
156 #if LIBCURL_VERSION_NUM >= 0x070908
157 if (!strcmp("http.sslcapath", var
))
158 return git_config_string(&ssl_capath
, var
, value
);
160 if (!strcmp("http.sslcainfo", var
))
161 return git_config_string(&ssl_cainfo
, var
, value
);
162 if (!strcmp("http.sslcertpasswordprotected", var
)) {
163 if (git_config_bool(var
, value
))
164 ssl_cert_password_required
= 1;
167 if (!strcmp("http.ssltry", var
)) {
168 curl_ssl_try
= git_config_bool(var
, value
);
171 if (!strcmp("http.minsessions", var
)) {
172 min_curl_sessions
= git_config_int(var
, value
);
173 #ifndef USE_CURL_MULTI
174 if (min_curl_sessions
> 1)
175 min_curl_sessions
= 1;
179 #ifdef USE_CURL_MULTI
180 if (!strcmp("http.maxrequests", var
)) {
181 max_requests
= git_config_int(var
, value
);
185 if (!strcmp("http.lowspeedlimit", var
)) {
186 curl_low_speed_limit
= (long)git_config_int(var
, value
);
189 if (!strcmp("http.lowspeedtime", var
)) {
190 curl_low_speed_time
= (long)git_config_int(var
, value
);
194 if (!strcmp("http.noepsv", var
)) {
195 curl_ftp_no_epsv
= git_config_bool(var
, value
);
198 if (!strcmp("http.proxy", var
))
199 return git_config_string(&curl_http_proxy
, var
, value
);
201 if (!strcmp("http.cookiefile", var
))
202 return git_config_string(&curl_cookie_file
, var
, value
);
204 if (!strcmp("http.postbuffer", var
)) {
205 http_post_buffer
= git_config_int(var
, value
);
206 if (http_post_buffer
< LARGE_PACKET_MAX
)
207 http_post_buffer
= LARGE_PACKET_MAX
;
211 if (!strcmp("http.useragent", var
))
212 return git_config_string(&user_agent
, var
, value
);
214 /* Fall back on the default ones */
215 return git_default_config(var
, value
, cb
);
218 static void init_curl_http_auth(CURL
*result
)
220 if (!http_auth
.username
)
223 credential_fill(&http_auth
);
225 #if LIBCURL_VERSION_NUM >= 0x071301
226 curl_easy_setopt(result
, CURLOPT_USERNAME
, http_auth
.username
);
227 curl_easy_setopt(result
, CURLOPT_PASSWORD
, http_auth
.password
);
230 static struct strbuf up
= STRBUF_INIT
;
232 * Note that we assume we only ever have a single set of
233 * credentials in a given program run, so we do not have
234 * to worry about updating this buffer, only setting its
238 strbuf_addf(&up
, "%s:%s",
239 http_auth
.username
, http_auth
.password
);
240 curl_easy_setopt(result
, CURLOPT_USERPWD
, up
.buf
);
245 static int has_cert_password(void)
247 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
249 if (!cert_auth
.password
) {
250 cert_auth
.protocol
= xstrdup("cert");
251 cert_auth
.username
= xstrdup("");
252 cert_auth
.path
= xstrdup(ssl_cert
);
253 credential_fill(&cert_auth
);
258 static CURL
*get_curl_handle(void)
260 CURL
*result
= curl_easy_init();
262 if (!curl_ssl_verify
) {
263 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
264 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
266 /* Verify authenticity of the peer's certificate */
267 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
268 /* The name in the cert must match whom we tried to connect */
269 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
272 #if LIBCURL_VERSION_NUM >= 0x070907
273 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
275 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
276 curl_easy_setopt(result
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
279 if (http_proactive_auth
)
280 init_curl_http_auth(result
);
282 if (ssl_cert
!= NULL
)
283 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
284 if (has_cert_password())
285 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, cert_auth
.password
);
286 #if LIBCURL_VERSION_NUM >= 0x070903
288 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
290 #if LIBCURL_VERSION_NUM >= 0x070908
291 if (ssl_capath
!= NULL
)
292 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
294 if (ssl_cainfo
!= NULL
)
295 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
297 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
298 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
299 curl_low_speed_limit
);
300 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
301 curl_low_speed_time
);
304 curl_easy_setopt(result
, CURLOPT_FOLLOWLOCATION
, 1);
305 #if LIBCURL_VERSION_NUM >= 0x071301
306 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
307 #elif LIBCURL_VERSION_NUM >= 0x071101
308 curl_easy_setopt(result
, CURLOPT_POST301
, 1);
311 if (getenv("GIT_CURL_VERBOSE"))
312 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
314 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
315 user_agent
? user_agent
: git_user_agent());
317 if (curl_ftp_no_epsv
)
318 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
320 #ifdef CURLOPT_USE_SSL
322 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
325 if (curl_http_proxy
) {
326 curl_easy_setopt(result
, CURLOPT_PROXY
, curl_http_proxy
);
327 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
333 static void set_from_env(const char **var
, const char *envname
)
335 const char *val
= getenv(envname
);
340 void http_init(struct remote
*remote
, const char *url
, int proactive_auth
)
342 char *low_speed_limit
;
343 char *low_speed_time
;
347 git_config(http_options
, NULL
);
349 curl_global_init(CURL_GLOBAL_ALL
);
351 http_proactive_auth
= proactive_auth
;
353 if (remote
&& remote
->http_proxy
)
354 curl_http_proxy
= xstrdup(remote
->http_proxy
);
356 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
357 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
359 #ifdef USE_CURL_MULTI
361 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
362 if (http_max_requests
!= NULL
)
363 max_requests
= atoi(http_max_requests
);
366 curlm
= curl_multi_init();
368 fprintf(stderr
, "Error creating curl multi handle.\n");
373 if (getenv("GIT_SSL_NO_VERIFY"))
376 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
377 #if LIBCURL_VERSION_NUM >= 0x070903
378 set_from_env(&ssl_key
, "GIT_SSL_KEY");
380 #if LIBCURL_VERSION_NUM >= 0x070908
381 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
383 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
385 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
387 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
388 if (low_speed_limit
!= NULL
)
389 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
390 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
391 if (low_speed_time
!= NULL
)
392 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
394 if (curl_ssl_verify
== -1)
397 curl_session_count
= 0;
398 #ifdef USE_CURL_MULTI
399 if (max_requests
< 1)
400 max_requests
= DEFAULT_MAX_REQUESTS
;
403 if (getenv("GIT_CURL_FTP_NO_EPSV"))
404 curl_ftp_no_epsv
= 1;
407 credential_from_url(&http_auth
, url
);
408 if (!ssl_cert_password_required
&&
409 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
410 !prefixcmp(url
, "https://"))
411 ssl_cert_password_required
= 1;
414 #ifndef NO_CURL_EASY_DUPHANDLE
415 curl_default
= get_curl_handle();
419 void http_cleanup(void)
421 struct active_request_slot
*slot
= active_queue_head
;
423 while (slot
!= NULL
) {
424 struct active_request_slot
*next
= slot
->next
;
425 if (slot
->curl
!= NULL
) {
426 #ifdef USE_CURL_MULTI
427 curl_multi_remove_handle(curlm
, slot
->curl
);
429 curl_easy_cleanup(slot
->curl
);
434 active_queue_head
= NULL
;
436 #ifndef NO_CURL_EASY_DUPHANDLE
437 curl_easy_cleanup(curl_default
);
440 #ifdef USE_CURL_MULTI
441 curl_multi_cleanup(curlm
);
443 curl_global_cleanup();
445 curl_slist_free_all(pragma_header
);
446 pragma_header
= NULL
;
448 curl_slist_free_all(no_pragma_header
);
449 no_pragma_header
= NULL
;
451 if (curl_http_proxy
) {
452 free((void *)curl_http_proxy
);
453 curl_http_proxy
= NULL
;
456 if (cert_auth
.password
!= NULL
) {
457 memset(cert_auth
.password
, 0, strlen(cert_auth
.password
));
458 free(cert_auth
.password
);
459 cert_auth
.password
= NULL
;
461 ssl_cert_password_required
= 0;
464 struct active_request_slot
*get_active_slot(void)
466 struct active_request_slot
*slot
= active_queue_head
;
467 struct active_request_slot
*newslot
;
469 #ifdef USE_CURL_MULTI
472 /* Wait for a slot to open up if the queue is full */
473 while (active_requests
>= max_requests
) {
474 curl_multi_perform(curlm
, &num_transfers
);
475 if (num_transfers
< active_requests
)
476 process_curl_messages();
480 while (slot
!= NULL
&& slot
->in_use
)
484 newslot
= xmalloc(sizeof(*newslot
));
485 newslot
->curl
= NULL
;
487 newslot
->next
= NULL
;
489 slot
= active_queue_head
;
491 active_queue_head
= newslot
;
493 while (slot
->next
!= NULL
)
495 slot
->next
= newslot
;
500 if (slot
->curl
== NULL
) {
501 #ifdef NO_CURL_EASY_DUPHANDLE
502 slot
->curl
= get_curl_handle();
504 slot
->curl
= curl_easy_duphandle(curl_default
);
506 curl_session_count
++;
511 slot
->results
= NULL
;
512 slot
->finished
= NULL
;
513 slot
->callback_data
= NULL
;
514 slot
->callback_func
= NULL
;
515 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
516 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
517 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
518 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
519 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
520 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
521 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
522 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
523 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
524 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 1);
525 if (http_auth
.password
)
526 init_curl_http_auth(slot
->curl
);
531 int start_active_slot(struct active_request_slot
*slot
)
533 #ifdef USE_CURL_MULTI
534 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
537 if (curlm_result
!= CURLM_OK
&&
538 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
545 * We know there must be something to do, since we just added
548 curl_multi_perform(curlm
, &num_transfers
);
553 #ifdef USE_CURL_MULTI
557 struct fill_chain
*next
;
560 static struct fill_chain
*fill_cfg
;
562 void add_fill_function(void *data
, int (*fill
)(void *))
564 struct fill_chain
*new = xmalloc(sizeof(*new));
565 struct fill_chain
**linkp
= &fill_cfg
;
570 linkp
= &(*linkp
)->next
;
574 void fill_active_slots(void)
576 struct active_request_slot
*slot
= active_queue_head
;
578 while (active_requests
< max_requests
) {
579 struct fill_chain
*fill
;
580 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
581 if (fill
->fill(fill
->data
))
588 while (slot
!= NULL
) {
589 if (!slot
->in_use
&& slot
->curl
!= NULL
590 && curl_session_count
> min_curl_sessions
) {
591 curl_easy_cleanup(slot
->curl
);
593 curl_session_count
--;
599 void step_active_slots(void)
602 CURLMcode curlm_result
;
605 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
606 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
607 if (num_transfers
< active_requests
) {
608 process_curl_messages();
614 void run_active_slot(struct active_request_slot
*slot
)
616 #ifdef USE_CURL_MULTI
621 struct timeval select_timeout
;
624 slot
->finished
= &finished
;
629 #if LIBCURL_VERSION_NUM >= 0x070f04
631 curl_multi_timeout(curlm
, &curl_timeout
);
632 if (curl_timeout
== 0) {
634 } else if (curl_timeout
== -1) {
635 select_timeout
.tv_sec
= 0;
636 select_timeout
.tv_usec
= 50000;
638 select_timeout
.tv_sec
= curl_timeout
/ 1000;
639 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
642 select_timeout
.tv_sec
= 0;
643 select_timeout
.tv_usec
= 50000;
650 curl_multi_fdset(curlm
, &readfds
, &writefds
, &excfds
, &max_fd
);
653 * It can happen that curl_multi_timeout returns a pathologically
654 * long timeout when curl_multi_fdset returns no file descriptors
655 * to read. See commit message for more details.
658 (select_timeout
.tv_sec
> 0 ||
659 select_timeout
.tv_usec
> 50000)) {
660 select_timeout
.tv_sec
= 0;
661 select_timeout
.tv_usec
= 50000;
664 select(max_fd
+1, &readfds
, &writefds
, &excfds
, &select_timeout
);
668 while (slot
->in_use
) {
669 slot
->curl_result
= curl_easy_perform(slot
->curl
);
670 finish_active_slot(slot
);
675 static void closedown_active_slot(struct active_request_slot
*slot
)
681 static void release_active_slot(struct active_request_slot
*slot
)
683 closedown_active_slot(slot
);
684 if (slot
->curl
&& curl_session_count
> min_curl_sessions
) {
685 #ifdef USE_CURL_MULTI
686 curl_multi_remove_handle(curlm
, slot
->curl
);
688 curl_easy_cleanup(slot
->curl
);
690 curl_session_count
--;
692 #ifdef USE_CURL_MULTI
697 void finish_active_slot(struct active_request_slot
*slot
)
699 closedown_active_slot(slot
);
700 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
702 if (slot
->finished
!= NULL
)
703 (*slot
->finished
) = 1;
705 /* Store slot results so they can be read after the slot is reused */
706 if (slot
->results
!= NULL
) {
707 slot
->results
->curl_result
= slot
->curl_result
;
708 slot
->results
->http_code
= slot
->http_code
;
711 /* Run callback if appropriate */
712 if (slot
->callback_func
!= NULL
)
713 slot
->callback_func(slot
->callback_data
);
716 void finish_all_active_slots(void)
718 struct active_request_slot
*slot
= active_queue_head
;
722 run_active_slot(slot
);
723 slot
= active_queue_head
;
729 /* Helpers for modifying and creating URLs */
730 static inline int needs_quote(int ch
)
732 if (((ch
>= 'A') && (ch
<= 'Z'))
733 || ((ch
>= 'a') && (ch
<= 'z'))
734 || ((ch
>= '0') && (ch
<= '9'))
742 static char *quote_ref_url(const char *base
, const char *ref
)
744 struct strbuf buf
= STRBUF_INIT
;
748 end_url_with_slash(&buf
, base
);
750 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
752 strbuf_addf(&buf
, "%%%02x", ch
);
754 strbuf_addch(&buf
, *cp
);
756 return strbuf_detach(&buf
, NULL
);
759 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
761 int only_two_digit_prefix
)
763 end_url_with_slash(buf
, url
);
765 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
766 if (!only_two_digit_prefix
)
767 strbuf_addf(buf
, "%s", hex
+2);
770 char *get_remote_object_url(const char *url
, const char *hex
,
771 int only_two_digit_prefix
)
773 struct strbuf buf
= STRBUF_INIT
;
774 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
775 return strbuf_detach(&buf
, NULL
);
778 int handle_curl_result(struct slot_results
*results
)
781 * If we see a failing http code with CURLE_OK, we have turned off
782 * FAILONERROR (to keep the server's custom error response), and should
783 * translate the code into failure here.
785 if (results
->curl_result
== CURLE_OK
&&
786 results
->http_code
>= 400) {
787 results
->curl_result
= CURLE_HTTP_RETURNED_ERROR
;
789 * Normally curl will already have put the "reason phrase"
790 * from the server into curl_errorstr; unfortunately without
791 * FAILONERROR it is lost, so we can give only the numeric
794 snprintf(curl_errorstr
, sizeof(curl_errorstr
),
795 "The requested URL returned error: %ld",
799 if (results
->curl_result
== CURLE_OK
) {
800 credential_approve(&http_auth
);
802 } else if (missing_target(results
))
803 return HTTP_MISSING_TARGET
;
804 else if (results
->http_code
== 401) {
805 if (http_auth
.username
&& http_auth
.password
) {
806 credential_reject(&http_auth
);
809 credential_fill(&http_auth
);
813 #if LIBCURL_VERSION_NUM >= 0x070c00
814 if (!curl_errorstr
[0])
815 strlcpy(curl_errorstr
,
816 curl_easy_strerror(results
->curl_result
),
817 sizeof(curl_errorstr
));
823 /* http_request() targets */
824 #define HTTP_REQUEST_STRBUF 0
825 #define HTTP_REQUEST_FILE 1
827 static int http_request(const char *url
, struct strbuf
*type
,
828 void *result
, int target
, int options
)
830 struct active_request_slot
*slot
;
831 struct slot_results results
;
832 struct curl_slist
*headers
= NULL
;
833 struct strbuf buf
= STRBUF_INIT
;
836 slot
= get_active_slot();
837 slot
->results
= &results
;
838 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
840 if (result
== NULL
) {
841 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
843 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
844 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, result
);
846 if (target
== HTTP_REQUEST_FILE
) {
847 long posn
= ftell(result
);
848 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
851 strbuf_addf(&buf
, "Range: bytes=%ld-", posn
);
852 headers
= curl_slist_append(headers
, buf
.buf
);
856 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
860 strbuf_addstr(&buf
, "Pragma:");
861 if (options
& HTTP_NO_CACHE
)
862 strbuf_addstr(&buf
, " no-cache");
863 if (options
& HTTP_KEEP_ERROR
)
864 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
866 headers
= curl_slist_append(headers
, buf
.buf
);
868 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
869 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
870 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
872 if (start_active_slot(slot
)) {
873 run_active_slot(slot
);
874 ret
= handle_curl_result(&results
);
876 snprintf(curl_errorstr
, sizeof(curl_errorstr
),
877 "failed to start HTTP request");
878 ret
= HTTP_START_FAILED
;
884 curl_easy_getinfo(slot
->curl
, CURLINFO_CONTENT_TYPE
, &t
);
886 strbuf_addstr(type
, t
);
889 curl_slist_free_all(headers
);
890 strbuf_release(&buf
);
895 static int http_request_reauth(const char *url
,
897 void *result
, int target
,
900 int ret
= http_request(url
, type
, result
, target
, options
);
901 if (ret
!= HTTP_REAUTH
)
905 * If we are using KEEP_ERROR, the previous request may have
906 * put cruft into our output stream; we should clear it out before
907 * making our next request. We only know how to do this for
908 * the strbuf case, but that is enough to satisfy current callers.
910 if (options
& HTTP_KEEP_ERROR
) {
912 case HTTP_REQUEST_STRBUF
:
913 strbuf_reset(result
);
916 die("BUG: HTTP_KEEP_ERROR is only supported with strbufs");
919 return http_request(url
, type
, result
, target
, options
);
922 int http_get_strbuf(const char *url
,
924 struct strbuf
*result
, int options
)
926 return http_request_reauth(url
, type
, result
,
927 HTTP_REQUEST_STRBUF
, options
);
931 * Downloads a URL and stores the result in the given file.
933 * If a previous interrupted download is detected (i.e. a previous temporary
934 * file is still around) the download is resumed.
936 static int http_get_file(const char *url
, const char *filename
, int options
)
939 struct strbuf tmpfile
= STRBUF_INIT
;
942 strbuf_addf(&tmpfile
, "%s.temp", filename
);
943 result
= fopen(tmpfile
.buf
, "a");
945 error("Unable to open local file %s", tmpfile
.buf
);
950 ret
= http_request_reauth(url
, NULL
, result
, HTTP_REQUEST_FILE
, options
);
953 if ((ret
== HTTP_OK
) && move_temp_to_file(tmpfile
.buf
, filename
))
956 strbuf_release(&tmpfile
);
960 int http_fetch_ref(const char *base
, struct ref
*ref
)
963 struct strbuf buffer
= STRBUF_INIT
;
966 url
= quote_ref_url(base
, ref
->name
);
967 if (http_get_strbuf(url
, NULL
, &buffer
, HTTP_NO_CACHE
) == HTTP_OK
) {
968 strbuf_rtrim(&buffer
);
969 if (buffer
.len
== 40)
970 ret
= get_sha1_hex(buffer
.buf
, ref
->old_sha1
);
971 else if (!prefixcmp(buffer
.buf
, "ref: ")) {
972 ref
->symref
= xstrdup(buffer
.buf
+ 5);
977 strbuf_release(&buffer
);
982 /* Helpers for fetching packs */
983 static char *fetch_pack_index(unsigned char *sha1
, const char *base_url
)
986 struct strbuf buf
= STRBUF_INIT
;
989 fprintf(stderr
, "Getting index for pack %s\n", sha1_to_hex(sha1
));
991 end_url_with_slash(&buf
, base_url
);
992 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", sha1_to_hex(sha1
));
993 url
= strbuf_detach(&buf
, NULL
);
995 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(sha1
));
996 tmp
= strbuf_detach(&buf
, NULL
);
998 if (http_get_file(url
, tmp
, 0) != HTTP_OK
) {
999 error("Unable to get pack index %s", url
);
1008 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
1009 unsigned char *sha1
, const char *base_url
)
1011 struct packed_git
*new_pack
;
1012 char *tmp_idx
= NULL
;
1015 if (has_pack_index(sha1
)) {
1016 new_pack
= parse_pack_index(sha1
, NULL
);
1018 return -1; /* parse_pack_index() already issued error message */
1022 tmp_idx
= fetch_pack_index(sha1
, base_url
);
1026 new_pack
= parse_pack_index(sha1
, tmp_idx
);
1031 return -1; /* parse_pack_index() already issued error message */
1034 ret
= verify_pack_index(new_pack
);
1036 close_pack_index(new_pack
);
1037 ret
= move_temp_to_file(tmp_idx
, sha1_pack_index_name(sha1
));
1044 new_pack
->next
= *packs_head
;
1045 *packs_head
= new_pack
;
1049 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
1053 struct strbuf buf
= STRBUF_INIT
;
1054 unsigned char sha1
[20];
1056 end_url_with_slash(&buf
, base_url
);
1057 strbuf_addstr(&buf
, "objects/info/packs");
1058 url
= strbuf_detach(&buf
, NULL
);
1060 ret
= http_get_strbuf(url
, NULL
, &buf
, HTTP_NO_CACHE
);
1065 while (i
< buf
.len
) {
1069 if (i
+ 52 <= buf
.len
&&
1070 !prefixcmp(data
+ i
, " pack-") &&
1071 !prefixcmp(data
+ i
+ 46, ".pack\n")) {
1072 get_sha1_hex(data
+ i
+ 6, sha1
);
1073 fetch_and_setup_pack_index(packs_head
, sha1
,
1079 while (i
< buf
.len
&& data
[i
] != '\n')
1090 void release_http_pack_request(struct http_pack_request
*preq
)
1092 if (preq
->packfile
!= NULL
) {
1093 fclose(preq
->packfile
);
1094 preq
->packfile
= NULL
;
1096 if (preq
->range_header
!= NULL
) {
1097 curl_slist_free_all(preq
->range_header
);
1098 preq
->range_header
= NULL
;
1104 int finish_http_pack_request(struct http_pack_request
*preq
)
1106 struct packed_git
**lst
;
1107 struct packed_git
*p
= preq
->target
;
1109 struct child_process ip
;
1110 const char *ip_argv
[8];
1112 close_pack_index(p
);
1114 fclose(preq
->packfile
);
1115 preq
->packfile
= NULL
;
1119 lst
= &((*lst
)->next
);
1120 *lst
= (*lst
)->next
;
1122 tmp_idx
= xstrdup(preq
->tmpfile
);
1123 strcpy(tmp_idx
+ strlen(tmp_idx
) - strlen(".pack.temp"),
1126 ip_argv
[0] = "index-pack";
1128 ip_argv
[2] = tmp_idx
;
1129 ip_argv
[3] = preq
->tmpfile
;
1132 memset(&ip
, 0, sizeof(ip
));
1138 if (run_command(&ip
)) {
1139 unlink(preq
->tmpfile
);
1145 unlink(sha1_pack_index_name(p
->sha1
));
1147 if (move_temp_to_file(preq
->tmpfile
, sha1_pack_name(p
->sha1
))
1148 || move_temp_to_file(tmp_idx
, sha1_pack_index_name(p
->sha1
))) {
1153 install_packed_git(p
);
1158 struct http_pack_request
*new_http_pack_request(
1159 struct packed_git
*target
, const char *base_url
)
1162 char range
[RANGE_HEADER_SIZE
];
1163 struct strbuf buf
= STRBUF_INIT
;
1164 struct http_pack_request
*preq
;
1166 preq
= xcalloc(1, sizeof(*preq
));
1167 preq
->target
= target
;
1169 end_url_with_slash(&buf
, base_url
);
1170 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
1171 sha1_to_hex(target
->sha1
));
1172 preq
->url
= strbuf_detach(&buf
, NULL
);
1174 snprintf(preq
->tmpfile
, sizeof(preq
->tmpfile
), "%s.temp",
1175 sha1_pack_name(target
->sha1
));
1176 preq
->packfile
= fopen(preq
->tmpfile
, "a");
1177 if (!preq
->packfile
) {
1178 error("Unable to open local file %s for pack",
1183 preq
->slot
= get_active_slot();
1184 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_FILE
, preq
->packfile
);
1185 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
1186 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
1187 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1191 * If there is data present from a previous transfer attempt,
1192 * resume where it left off
1194 prev_posn
= ftell(preq
->packfile
);
1196 if (http_is_verbose
)
1198 "Resuming fetch of pack %s at byte %ld\n",
1199 sha1_to_hex(target
->sha1
), prev_posn
);
1200 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1201 preq
->range_header
= curl_slist_append(NULL
, range
);
1202 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1203 preq
->range_header
);
1214 /* Helpers for fetching objects (loose) */
1215 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
1218 unsigned char expn
[4096];
1219 size_t size
= eltsize
* nmemb
;
1221 struct http_object_request
*freq
=
1222 (struct http_object_request
*)data
;
1224 ssize_t retval
= xwrite(freq
->localfile
,
1225 (char *) ptr
+ posn
, size
- posn
);
1229 } while (posn
< size
);
1231 freq
->stream
.avail_in
= size
;
1232 freq
->stream
.next_in
= (void *)ptr
;
1234 freq
->stream
.next_out
= expn
;
1235 freq
->stream
.avail_out
= sizeof(expn
);
1236 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
1237 git_SHA1_Update(&freq
->c
, expn
,
1238 sizeof(expn
) - freq
->stream
.avail_out
);
1239 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
1243 struct http_object_request
*new_http_object_request(const char *base_url
,
1244 unsigned char *sha1
)
1246 char *hex
= sha1_to_hex(sha1
);
1248 char prevfile
[PATH_MAX
];
1250 char prev_buf
[PREV_BUF_SIZE
];
1251 ssize_t prev_read
= 0;
1253 char range
[RANGE_HEADER_SIZE
];
1254 struct curl_slist
*range_header
= NULL
;
1255 struct http_object_request
*freq
;
1257 freq
= xcalloc(1, sizeof(*freq
));
1258 hashcpy(freq
->sha1
, sha1
);
1259 freq
->localfile
= -1;
1261 filename
= sha1_file_name(sha1
);
1262 snprintf(freq
->tmpfile
, sizeof(freq
->tmpfile
),
1263 "%s.temp", filename
);
1265 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", filename
);
1266 unlink_or_warn(prevfile
);
1267 rename(freq
->tmpfile
, prevfile
);
1268 unlink_or_warn(freq
->tmpfile
);
1270 if (freq
->localfile
!= -1)
1271 error("fd leakage in start: %d", freq
->localfile
);
1272 freq
->localfile
= open(freq
->tmpfile
,
1273 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1275 * This could have failed due to the "lazy directory creation";
1276 * try to mkdir the last path component.
1278 if (freq
->localfile
< 0 && errno
== ENOENT
) {
1279 char *dir
= strrchr(freq
->tmpfile
, '/');
1282 mkdir(freq
->tmpfile
, 0777);
1285 freq
->localfile
= open(freq
->tmpfile
,
1286 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1289 if (freq
->localfile
< 0) {
1290 error("Couldn't create temporary file %s: %s",
1291 freq
->tmpfile
, strerror(errno
));
1295 git_inflate_init(&freq
->stream
);
1297 git_SHA1_Init(&freq
->c
);
1299 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
1302 * If a previous temp file is present, process what was already
1305 prevlocal
= open(prevfile
, O_RDONLY
);
1306 if (prevlocal
!= -1) {
1308 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
1310 if (fwrite_sha1_file(prev_buf
,
1313 freq
) == prev_read
) {
1314 prev_posn
+= prev_read
;
1319 } while (prev_read
> 0);
1322 unlink_or_warn(prevfile
);
1325 * Reset inflate/SHA1 if there was an error reading the previous temp
1326 * file; also rewind to the beginning of the local file.
1328 if (prev_read
== -1) {
1329 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
1330 git_inflate_init(&freq
->stream
);
1331 git_SHA1_Init(&freq
->c
);
1334 lseek(freq
->localfile
, 0, SEEK_SET
);
1335 if (ftruncate(freq
->localfile
, 0) < 0) {
1336 error("Couldn't truncate temporary file %s: %s",
1337 freq
->tmpfile
, strerror(errno
));
1343 freq
->slot
= get_active_slot();
1345 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FILE
, freq
);
1346 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
1347 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
1348 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
1349 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
1352 * If we have successfully processed data from a previous fetch
1353 * attempt, only fetch the data we don't already have.
1356 if (http_is_verbose
)
1358 "Resuming fetch of object %s at byte %ld\n",
1360 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1361 range_header
= curl_slist_append(range_header
, range
);
1362 curl_easy_setopt(freq
->slot
->curl
,
1363 CURLOPT_HTTPHEADER
, range_header
);
1374 void process_http_object_request(struct http_object_request
*freq
)
1376 if (freq
->slot
== NULL
)
1378 freq
->curl_result
= freq
->slot
->curl_result
;
1379 freq
->http_code
= freq
->slot
->http_code
;
1383 int finish_http_object_request(struct http_object_request
*freq
)
1387 close(freq
->localfile
);
1388 freq
->localfile
= -1;
1390 process_http_object_request(freq
);
1392 if (freq
->http_code
== 416) {
1393 warning("requested range invalid; we may already have all the data.");
1394 } else if (freq
->curl_result
!= CURLE_OK
) {
1395 if (stat(freq
->tmpfile
, &st
) == 0)
1396 if (st
.st_size
== 0)
1397 unlink_or_warn(freq
->tmpfile
);
1401 git_inflate_end(&freq
->stream
);
1402 git_SHA1_Final(freq
->real_sha1
, &freq
->c
);
1403 if (freq
->zret
!= Z_STREAM_END
) {
1404 unlink_or_warn(freq
->tmpfile
);
1407 if (hashcmp(freq
->sha1
, freq
->real_sha1
)) {
1408 unlink_or_warn(freq
->tmpfile
);
1412 move_temp_to_file(freq
->tmpfile
, sha1_file_name(freq
->sha1
));
1414 return freq
->rename
;
1417 void abort_http_object_request(struct http_object_request
*freq
)
1419 unlink_or_warn(freq
->tmpfile
);
1421 release_http_object_request(freq
);
1424 void release_http_object_request(struct http_object_request
*freq
)
1426 if (freq
->localfile
!= -1) {
1427 close(freq
->localfile
);
1428 freq
->localfile
= -1;
1430 if (freq
->url
!= NULL
) {
1434 if (freq
->slot
!= NULL
) {
1435 freq
->slot
->callback_func
= NULL
;
1436 freq
->slot
->callback_data
= NULL
;
1437 release_active_slot(freq
->slot
);