1 #include "git-compat-util.h"
5 #include "run-command.h"
8 #include "credential.h"
14 size_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
16 #if LIBCURL_VERSION_NUM >= 0x070a06
17 #define LIBCURL_CAN_HANDLE_AUTH_ANY
20 static int min_curl_sessions
= 1;
21 static int curl_session_count
;
23 static int max_requests
= -1;
26 #ifndef NO_CURL_EASY_DUPHANDLE
27 static CURL
*curl_default
;
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
;
41 #if LIBCURL_VERSION_NUM >= 0x070908
42 static const char *ssl_capath
;
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
60 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
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
;
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
);
90 curlioerr
ioctl_buffer(CURL
*handle
, int cmd
, void *clientp
)
92 struct buffer
*buffer
= clientp
;
98 case CURLIOCMD_RESTARTREAD
:
103 return CURLIOE_UNKNOWNCMD
;
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
);
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
)
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
);
144 slot
->results
->auth_avail
= 0;
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)
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
)
168 curl_multi_remove_handle(curlm
, slot
->curl
);
169 slot
->curl_result
= curl_result
;
170 finish_active_slot(slot
);
172 fprintf(stderr
, "Received DONE message for unknown request!\n");
175 fprintf(stderr
, "Unknown CURL message received: %d\n",
176 (int)curl_message
->msg
);
178 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
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
);
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
);
195 #if LIBCURL_VERSION_NUM >= 0x070908
196 if (!strcmp("http.sslcapath", var
))
197 return git_config_string(&ssl_capath
, var
, value
);
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
);
205 if (!strcmp("http.ssltry", var
)) {
206 curl_ssl_try
= git_config_bool(var
, value
);
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;
217 #ifdef USE_CURL_MULTI
218 if (!strcmp("http.maxrequests", var
)) {
219 max_requests
= git_config_int(var
, value
);
223 if (!strcmp("http.lowspeedlimit", var
)) {
224 curl_low_speed_limit
= (long)git_config_int(var
, value
);
227 if (!strcmp("http.lowspeedtime", var
)) {
228 curl_low_speed_time
= (long)git_config_int(var
, value
);
232 if (!strcmp("http.noepsv", var
)) {
233 curl_ftp_no_epsv
= git_config_bool(var
, value
);
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
);
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
;
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
)
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
);
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
280 strbuf_addf(&up
, "%s:%s",
281 http_auth
.username
, http_auth
.password
);
282 curl_easy_setopt(result
, CURLOPT_USERPWD
, up
.buf
);
287 static int has_cert_password(void)
289 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
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
);
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
)
311 socklen_t len
= (socklen_t
)sizeof(ka
);
313 if (type
!= CURLSOCKTYPE_IPCXN
)
316 rc
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&ka
, len
);
318 warning("unable to set SO_KEEPALIVE on socket %s",
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
);
330 static void set_curl_keepalive(CURL
*c
)
332 /* not supported on older curl versions */
336 static CURL
*get_curl_handle(void)
338 CURL
*result
= curl_easy_init();
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);
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
);
356 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
357 curl_easy_setopt(result
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
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
369 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
371 #if LIBCURL_VERSION_NUM >= 0x070908
372 if (ssl_capath
!= NULL
)
373 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
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);
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
403 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
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
);
416 static void set_from_env(const char **var
, const char *envname
)
418 const char *val
= getenv(envname
);
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";
432 config
.collect_fn
= http_options
;
433 config
.cascade_fn
= git_default_config
;
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();
462 die("curl_multi_init failed");
465 if (getenv("GIT_SSL_NO_VERIFY"))
468 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
469 #if LIBCURL_VERSION_NUM >= 0x070903
470 set_from_env(&ssl_key
, "GIT_SSL_KEY");
472 #if LIBCURL_VERSION_NUM >= 0x070908
473 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
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)
489 curl_session_count
= 0;
490 #ifdef USE_CURL_MULTI
491 if (max_requests
< 1)
492 max_requests
= DEFAULT_MAX_REQUESTS
;
495 if (getenv("GIT_CURL_FTP_NO_EPSV"))
496 curl_ftp_no_epsv
= 1;
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();
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
);
521 curl_easy_cleanup(slot
->curl
);
526 active_queue_head
= NULL
;
528 #ifndef NO_CURL_EASY_DUPHANDLE
529 curl_easy_cleanup(curl_default
);
532 #ifdef USE_CURL_MULTI
533 curl_multi_cleanup(curlm
);
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
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();
575 while (slot
!= NULL
&& slot
->in_use
)
579 newslot
= xmalloc(sizeof(*newslot
));
580 newslot
->curl
= NULL
;
582 newslot
->next
= NULL
;
584 slot
= active_queue_head
;
586 active_queue_head
= newslot
;
588 while (slot
->next
!= NULL
)
590 slot
->next
= newslot
;
595 if (slot
->curl
== NULL
) {
596 #ifdef NO_CURL_EASY_DUPHANDLE
597 slot
->curl
= get_curl_handle();
599 slot
->curl
= curl_easy_duphandle(curl_default
);
601 curl_session_count
++;
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
);
625 if (http_auth
.password
)
626 init_curl_http_auth(slot
->curl
);
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
);
637 if (curlm_result
!= CURLM_OK
&&
638 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
645 * We know there must be something to do, since we just added
648 curl_multi_perform(curlm
, &num_transfers
);
653 #ifdef USE_CURL_MULTI
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
;
670 linkp
= &(*linkp
)->next
;
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
))
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
);
693 curl_session_count
--;
699 void step_active_slots(void)
702 CURLMcode curlm_result
;
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();
714 void run_active_slot(struct active_request_slot
*slot
)
716 #ifdef USE_CURL_MULTI
721 struct timeval select_timeout
;
724 slot
->finished
= &finished
;
729 #if LIBCURL_VERSION_NUM >= 0x070f04
731 curl_multi_timeout(curlm
, &curl_timeout
);
732 if (curl_timeout
== 0) {
734 } else if (curl_timeout
== -1) {
735 select_timeout
.tv_sec
= 0;
736 select_timeout
.tv_usec
= 50000;
738 select_timeout
.tv_sec
= curl_timeout
/ 1000;
739 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
742 select_timeout
.tv_sec
= 0;
743 select_timeout
.tv_usec
= 50000;
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.
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
);
768 while (slot
->in_use
) {
769 slot
->curl_result
= curl_easy_perform(slot
->curl
);
770 finish_active_slot(slot
);
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
);
782 curl_easy_cleanup(slot
->curl
);
784 curl_session_count
--;
786 #ifdef USE_CURL_MULTI
791 void finish_all_active_slots(void)
793 struct active_request_slot
*slot
= active_queue_head
;
797 run_active_slot(slot
);
798 slot
= active_queue_head
;
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'))
817 static char *quote_ref_url(const char *base
, const char *ref
)
819 struct strbuf buf
= STRBUF_INIT
;
823 end_url_with_slash(&buf
, base
);
825 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
827 strbuf_addf(&buf
, "%%%02x", ch
);
829 strbuf_addch(&buf
, *cp
);
831 return strbuf_detach(&buf
, NULL
);
834 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
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
869 snprintf(curl_errorstr
, sizeof(curl_errorstr
),
870 "The requested URL returned error: %ld",
874 if (results
->curl_result
== CURLE_OK
) {
875 credential_approve(&http_auth
);
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
);
884 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
885 http_auth_methods
&= ~CURLAUTH_GSSNEGOTIATE
;
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
));
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
)
920 ret
= curl_easy_getinfo(curl
, info
, &ptr
);
922 strbuf_addstr(buf
, ptr
);
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
934 static int extract_param(const char *raw
, const char *name
,
937 size_t len
= strlen(name
);
939 if (strncasecmp(raw
, name
, len
))
947 while (*raw
&& !isspace(*raw
) && *raw
!= ';')
948 strbuf_addch(out
, *raw
++);
953 * Extract a normalized version of the content type, with any
954 * spaces suppressed, all letters lowercased, and no trailing ";"
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
962 * If the "charset" argument is not NULL, store the value of any
963 * charset parameter there.
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
)
975 strbuf_grow(type
, raw
->len
);
976 for (p
= raw
->buf
; *p
; p
++) {
983 strbuf_addch(type
, tolower(*p
));
989 strbuf_reset(charset
);
991 while (isspace(*p
) || *p
== ';')
993 if (!extract_param(p
, "charset", charset
))
995 while (*p
&& !isspace(*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)
1014 retval
= getenv("LANGUAGE");
1015 if (retval
&& *retval
)
1019 retval
= setlocale(LC_MESSAGES
, NULL
);
1020 if (retval
&& *retval
&&
1021 strcmp(retval
, "C") &&
1022 strcmp(retval
, "POSIX"))
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
;
1042 const char *s
= get_preferred_languages();
1044 struct strbuf tag
= STRBUF_INIT
;
1046 /* Don't add Accept-Language header if no language is preferred. */
1051 * Split the colon-separated string of preferred languages into
1052 * language_tags array.
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
!= ':')
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 '*' */
1072 /* write Accept-Language header into buf */
1074 int last_buf_len
= 0;
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
++) {
1095 strbuf_addstr(buf
, ", ");
1097 strbuf_addstr(buf
, language_tags
[i
]);
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
);
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.
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
);
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
;
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);
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
,
1169 strbuf_addf(&buf
, "Range: bytes=%ld-", posn
);
1170 headers
= curl_slist_append(headers
, buf
.buf
);
1174 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
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
,
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
);
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
1222 * The "got" parameter is the URL that curl reported to us as where we ended
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
,
1242 const struct strbuf
*got
)
1247 if (!strcmp(asked
, got
->buf
))
1250 if (!skip_prefix(asked
, base
->buf
, &tail
))
1251 die("BUG: update_url_from_redirect: %s is not a superset of %s",
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 */
1261 strbuf_add(base
, got
->buf
, got
->len
- tail_len
);
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
)
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
) {
1290 case HTTP_REQUEST_STRBUF
:
1291 strbuf_reset(result
);
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
)
1320 struct strbuf tmpfile
= STRBUF_INIT
;
1323 strbuf_addf(&tmpfile
, "%s.temp", filename
);
1324 result
= fopen(tmpfile
.buf
, "a");
1326 error("Unable to open local file %s", tmpfile
.buf
);
1331 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
1334 if (ret
== HTTP_OK
&& move_temp_to_file(tmpfile
.buf
, filename
))
1337 strbuf_release(&tmpfile
);
1341 int http_fetch_ref(const char *base
, struct ref
*ref
)
1343 struct http_get_options options
= {0};
1345 struct strbuf buffer
= STRBUF_INIT
;
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);
1361 strbuf_release(&buffer
);
1366 /* Helpers for fetching packs */
1367 static char *fetch_pack_index(unsigned char *sha1
, const char *base_url
)
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
);
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
;
1399 if (has_pack_index(sha1
)) {
1400 new_pack
= parse_pack_index(sha1
, sha1_pack_index_name(sha1
));
1402 return -1; /* parse_pack_index() already issued error message */
1406 tmp_idx
= fetch_pack_index(sha1
, base_url
);
1410 new_pack
= parse_pack_index(sha1
, tmp_idx
);
1415 return -1; /* parse_pack_index() already issued error message */
1418 ret
= verify_pack_index(new_pack
);
1420 close_pack_index(new_pack
);
1421 ret
= move_temp_to_file(tmp_idx
, sha1_pack_index_name(sha1
));
1428 new_pack
->next
= *packs_head
;
1429 *packs_head
= new_pack
;
1433 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
1435 struct http_get_options options
= {0};
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
);
1451 while (i
< buf
.len
) {
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
,
1465 while (i
< buf
.len
&& data
[i
] != '\n')
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
;
1490 int finish_http_pack_request(struct http_pack_request
*preq
)
1492 struct packed_git
**lst
;
1493 struct packed_git
*p
= preq
->target
;
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
;
1505 lst
= &((*lst
)->next
);
1506 *lst
= (*lst
)->next
;
1508 tmp_idx
= xstrdup(preq
->tmpfile
);
1509 strcpy(tmp_idx
+ strlen(tmp_idx
) - strlen(".pack.temp"),
1512 ip_argv
[0] = "index-pack";
1514 ip_argv
[2] = tmp_idx
;
1515 ip_argv
[3] = preq
->tmpfile
;
1523 if (run_command(&ip
)) {
1524 unlink(preq
->tmpfile
);
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
))) {
1538 install_packed_git(p
);
1543 struct http_pack_request
*new_http_pack_request(
1544 struct packed_git
*target
, const char *base_url
)
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",
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
,
1576 * If there is data present from a previous transfer attempt,
1577 * resume where it left off
1579 prev_posn
= ftell(preq
->packfile
);
1581 if (http_is_verbose
)
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
);
1599 /* Helpers for fetching objects (loose) */
1600 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
1603 unsigned char expn
[4096];
1604 size_t size
= eltsize
* nmemb
;
1606 struct http_object_request
*freq
=
1607 (struct http_object_request
*)data
;
1609 ssize_t retval
= xwrite(freq
->localfile
,
1610 (char *) ptr
+ posn
, size
- posn
);
1614 } while (posn
< size
);
1616 freq
->stream
.avail_in
= size
;
1617 freq
->stream
.next_in
= (void *)ptr
;
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
);
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
];
1635 char prev_buf
[PREV_BUF_SIZE
];
1636 ssize_t prev_read
= 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
, '/');
1667 mkdir(freq
->tmpfile
, 0777);
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
));
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
1690 prevlocal
= open(prevfile
, O_RDONLY
);
1691 if (prevlocal
!= -1) {
1693 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
1695 if (fwrite_sha1_file(prev_buf
,
1698 freq
) == prev_read
) {
1699 prev_posn
+= prev_read
;
1704 } while (prev_read
> 0);
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
);
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
));
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.
1741 if (http_is_verbose
)
1743 "Resuming fetch of object %s at byte %ld\n",
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
);
1759 void process_http_object_request(struct http_object_request
*freq
)
1761 if (freq
->slot
== NULL
)
1763 freq
->curl_result
= freq
->slot
->curl_result
;
1764 freq
->http_code
= freq
->slot
->http_code
;
1768 int finish_http_object_request(struct http_object_request
*freq
)
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
);
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
);
1792 if (hashcmp(freq
->sha1
, freq
->real_sha1
)) {
1793 unlink_or_warn(freq
->tmpfile
);
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
) {
1819 if (freq
->slot
!= NULL
) {
1820 freq
->slot
->callback_func
= NULL
;
1821 freq
->slot
->callback_data
= NULL
;
1822 release_active_slot(freq
->slot
);