1 #include "git-compat-util.h"
5 #include "run-command.h"
8 #include "credential.h"
11 #include "transport.h"
15 size_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
17 #if LIBCURL_VERSION_NUM >= 0x070a06
18 #define LIBCURL_CAN_HANDLE_AUTH_ANY
21 static int min_curl_sessions
= 1;
22 static int curl_session_count
;
24 static int max_requests
= -1;
27 #ifndef NO_CURL_EASY_DUPHANDLE
28 static CURL
*curl_default
;
31 #define PREV_BUF_SIZE 4096
32 #define RANGE_HEADER_SIZE 30
34 char curl_errorstr
[CURL_ERROR_SIZE
];
36 static int curl_ssl_verify
= -1;
37 static int curl_ssl_try
;
38 static const char *ssl_cert
;
39 #if LIBCURL_VERSION_NUM >= 0x070903
40 static const char *ssl_key
;
42 #if LIBCURL_VERSION_NUM >= 0x070908
43 static const char *ssl_capath
;
45 static const char *ssl_cainfo
;
46 static long curl_low_speed_limit
= -1;
47 static long curl_low_speed_time
= -1;
48 static int curl_ftp_no_epsv
;
49 static const char *curl_http_proxy
;
50 static const char *curl_cookie_file
;
51 static int curl_save_cookies
;
52 struct credential http_auth
= CREDENTIAL_INIT
;
53 static int http_proactive_auth
;
54 static const char *user_agent
;
56 #if LIBCURL_VERSION_NUM >= 0x071700
57 /* Use CURLOPT_KEYPASSWD as is */
58 #elif LIBCURL_VERSION_NUM >= 0x070903
59 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
61 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
64 static struct credential cert_auth
= CREDENTIAL_INIT
;
65 static int ssl_cert_password_required
;
66 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
67 static unsigned long http_auth_methods
= CURLAUTH_ANY
;
70 static struct curl_slist
*pragma_header
;
71 static struct curl_slist
*no_pragma_header
;
73 static struct active_request_slot
*active_queue_head
;
75 size_t fread_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
77 size_t size
= eltsize
* nmemb
;
78 struct buffer
*buffer
= buffer_
;
80 if (size
> buffer
->buf
.len
- buffer
->posn
)
81 size
= buffer
->buf
.len
- buffer
->posn
;
82 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
89 curlioerr
ioctl_buffer(CURL
*handle
, int cmd
, void *clientp
)
91 struct buffer
*buffer
= clientp
;
97 case CURLIOCMD_RESTARTREAD
:
102 return CURLIOE_UNKNOWNCMD
;
107 size_t fwrite_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
109 size_t size
= eltsize
* nmemb
;
110 struct strbuf
*buffer
= buffer_
;
112 strbuf_add(buffer
, ptr
, size
);
116 size_t fwrite_null(char *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
118 return eltsize
* nmemb
;
121 #ifdef USE_CURL_MULTI
122 static void process_curl_messages(void)
125 struct active_request_slot
*slot
;
126 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
128 while (curl_message
!= NULL
) {
129 if (curl_message
->msg
== CURLMSG_DONE
) {
130 int curl_result
= curl_message
->data
.result
;
131 slot
= active_queue_head
;
132 while (slot
!= NULL
&&
133 slot
->curl
!= curl_message
->easy_handle
)
136 curl_multi_remove_handle(curlm
, slot
->curl
);
137 slot
->curl_result
= curl_result
;
138 finish_active_slot(slot
);
140 fprintf(stderr
, "Received DONE message for unknown request!\n");
143 fprintf(stderr
, "Unknown CURL message received: %d\n",
144 (int)curl_message
->msg
);
146 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
151 static int http_options(const char *var
, const char *value
, void *cb
)
153 if (!strcmp("http.sslverify", var
)) {
154 curl_ssl_verify
= git_config_bool(var
, value
);
157 if (!strcmp("http.sslcert", var
))
158 return git_config_string(&ssl_cert
, var
, value
);
159 #if LIBCURL_VERSION_NUM >= 0x070903
160 if (!strcmp("http.sslkey", var
))
161 return git_config_string(&ssl_key
, var
, value
);
163 #if LIBCURL_VERSION_NUM >= 0x070908
164 if (!strcmp("http.sslcapath", var
))
165 return git_config_string(&ssl_capath
, var
, value
);
167 if (!strcmp("http.sslcainfo", var
))
168 return git_config_string(&ssl_cainfo
, var
, value
);
169 if (!strcmp("http.sslcertpasswordprotected", var
)) {
170 ssl_cert_password_required
= git_config_bool(var
, value
);
173 if (!strcmp("http.ssltry", var
)) {
174 curl_ssl_try
= git_config_bool(var
, value
);
177 if (!strcmp("http.minsessions", var
)) {
178 min_curl_sessions
= git_config_int(var
, value
);
179 #ifndef USE_CURL_MULTI
180 if (min_curl_sessions
> 1)
181 min_curl_sessions
= 1;
185 #ifdef USE_CURL_MULTI
186 if (!strcmp("http.maxrequests", var
)) {
187 max_requests
= git_config_int(var
, value
);
191 if (!strcmp("http.lowspeedlimit", var
)) {
192 curl_low_speed_limit
= (long)git_config_int(var
, value
);
195 if (!strcmp("http.lowspeedtime", var
)) {
196 curl_low_speed_time
= (long)git_config_int(var
, value
);
200 if (!strcmp("http.noepsv", var
)) {
201 curl_ftp_no_epsv
= git_config_bool(var
, value
);
204 if (!strcmp("http.proxy", var
))
205 return git_config_string(&curl_http_proxy
, var
, value
);
207 if (!strcmp("http.cookiefile", var
))
208 return git_config_string(&curl_cookie_file
, var
, value
);
209 if (!strcmp("http.savecookies", var
)) {
210 curl_save_cookies
= git_config_bool(var
, value
);
214 if (!strcmp("http.postbuffer", var
)) {
215 http_post_buffer
= git_config_int(var
, value
);
216 if (http_post_buffer
< LARGE_PACKET_MAX
)
217 http_post_buffer
= LARGE_PACKET_MAX
;
221 if (!strcmp("http.useragent", var
))
222 return git_config_string(&user_agent
, var
, value
);
224 /* Fall back on the default ones */
225 return git_default_config(var
, value
, cb
);
228 static void init_curl_http_auth(CURL
*result
)
230 if (!http_auth
.username
)
233 credential_fill(&http_auth
);
235 #if LIBCURL_VERSION_NUM >= 0x071301
236 curl_easy_setopt(result
, CURLOPT_USERNAME
, http_auth
.username
);
237 curl_easy_setopt(result
, CURLOPT_PASSWORD
, http_auth
.password
);
240 static struct strbuf up
= STRBUF_INIT
;
242 * Note that we assume we only ever have a single set of
243 * credentials in a given program run, so we do not have
244 * to worry about updating this buffer, only setting its
248 strbuf_addf(&up
, "%s:%s",
249 http_auth
.username
, http_auth
.password
);
250 curl_easy_setopt(result
, CURLOPT_USERPWD
, up
.buf
);
255 static int has_cert_password(void)
257 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
259 if (!cert_auth
.password
) {
260 cert_auth
.protocol
= xstrdup("cert");
261 cert_auth
.username
= xstrdup("");
262 cert_auth
.path
= xstrdup(ssl_cert
);
263 credential_fill(&cert_auth
);
268 #if LIBCURL_VERSION_NUM >= 0x071900
269 static void set_curl_keepalive(CURL
*c
)
271 curl_easy_setopt(c
, CURLOPT_TCP_KEEPALIVE
, 1);
274 #elif LIBCURL_VERSION_NUM >= 0x071000
275 static int sockopt_callback(void *client
, curl_socket_t fd
, curlsocktype type
)
279 socklen_t len
= (socklen_t
)sizeof(ka
);
281 if (type
!= CURLSOCKTYPE_IPCXN
)
284 rc
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&ka
, len
);
286 warning("unable to set SO_KEEPALIVE on socket %s",
289 return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
292 static void set_curl_keepalive(CURL
*c
)
294 curl_easy_setopt(c
, CURLOPT_SOCKOPTFUNCTION
, sockopt_callback
);
298 static void set_curl_keepalive(CURL
*c
)
300 /* not supported on older curl versions */
304 static CURL
*get_curl_handle(void)
306 CURL
*result
= curl_easy_init();
307 long allowed_protocols
= 0;
310 die("curl_easy_init failed");
312 if (!curl_ssl_verify
) {
313 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
314 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
316 /* Verify authenticity of the peer's certificate */
317 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
318 /* The name in the cert must match whom we tried to connect */
319 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
322 #if LIBCURL_VERSION_NUM >= 0x070907
323 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
325 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
326 curl_easy_setopt(result
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
329 if (http_proactive_auth
)
330 init_curl_http_auth(result
);
332 if (ssl_cert
!= NULL
)
333 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
334 if (has_cert_password())
335 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, cert_auth
.password
);
336 #if LIBCURL_VERSION_NUM >= 0x070903
338 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
340 #if LIBCURL_VERSION_NUM >= 0x070908
341 if (ssl_capath
!= NULL
)
342 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
344 if (ssl_cainfo
!= NULL
)
345 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
347 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
348 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
349 curl_low_speed_limit
);
350 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
351 curl_low_speed_time
);
354 curl_easy_setopt(result
, CURLOPT_FOLLOWLOCATION
, 1);
355 #if LIBCURL_VERSION_NUM >= 0x071301
356 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
357 #elif LIBCURL_VERSION_NUM >= 0x071101
358 curl_easy_setopt(result
, CURLOPT_POST301
, 1);
360 #if LIBCURL_VERSION_NUM >= 0x071304
361 if (is_transport_allowed("http"))
362 allowed_protocols
|= CURLPROTO_HTTP
;
363 if (is_transport_allowed("https"))
364 allowed_protocols
|= CURLPROTO_HTTPS
;
365 if (is_transport_allowed("ftp"))
366 allowed_protocols
|= CURLPROTO_FTP
;
367 if (is_transport_allowed("ftps"))
368 allowed_protocols
|= CURLPROTO_FTPS
;
369 curl_easy_setopt(result
, CURLOPT_REDIR_PROTOCOLS
, allowed_protocols
);
371 if (transport_restrict_protocols())
372 warning("protocol restrictions not applied to curl redirects because\n"
373 "your curl version is too old (>= 7.19.4)");
376 if (getenv("GIT_CURL_VERBOSE"))
377 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
379 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
380 user_agent
? user_agent
: git_user_agent());
382 if (curl_ftp_no_epsv
)
383 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
385 #ifdef CURLOPT_USE_SSL
387 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
390 if (curl_http_proxy
) {
391 curl_easy_setopt(result
, CURLOPT_PROXY
, curl_http_proxy
);
392 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
395 set_curl_keepalive(result
);
400 static void set_from_env(const char **var
, const char *envname
)
402 const char *val
= getenv(envname
);
407 void http_init(struct remote
*remote
, const char *url
, int proactive_auth
)
409 char *low_speed_limit
;
410 char *low_speed_time
;
411 char *normalized_url
;
412 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
414 config
.section
= "http";
416 config
.collect_fn
= http_options
;
417 config
.cascade_fn
= git_default_config
;
421 normalized_url
= url_normalize(url
, &config
.url
);
423 git_config(urlmatch_config_entry
, &config
);
424 free(normalized_url
);
426 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
)
427 die("curl_global_init failed");
429 http_proactive_auth
= proactive_auth
;
431 if (remote
&& remote
->http_proxy
)
432 curl_http_proxy
= xstrdup(remote
->http_proxy
);
434 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
435 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
437 #ifdef USE_CURL_MULTI
439 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
440 if (http_max_requests
!= NULL
)
441 max_requests
= atoi(http_max_requests
);
444 curlm
= curl_multi_init();
446 die("curl_multi_init failed");
449 if (getenv("GIT_SSL_NO_VERIFY"))
452 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
453 #if LIBCURL_VERSION_NUM >= 0x070903
454 set_from_env(&ssl_key
, "GIT_SSL_KEY");
456 #if LIBCURL_VERSION_NUM >= 0x070908
457 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
459 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
461 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
463 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
464 if (low_speed_limit
!= NULL
)
465 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
466 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
467 if (low_speed_time
!= NULL
)
468 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
470 if (curl_ssl_verify
== -1)
473 curl_session_count
= 0;
474 #ifdef USE_CURL_MULTI
475 if (max_requests
< 1)
476 max_requests
= DEFAULT_MAX_REQUESTS
;
479 if (getenv("GIT_CURL_FTP_NO_EPSV"))
480 curl_ftp_no_epsv
= 1;
483 credential_from_url(&http_auth
, url
);
484 if (!ssl_cert_password_required
&&
485 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
486 starts_with(url
, "https://"))
487 ssl_cert_password_required
= 1;
490 #ifndef NO_CURL_EASY_DUPHANDLE
491 curl_default
= get_curl_handle();
495 void http_cleanup(void)
497 struct active_request_slot
*slot
= active_queue_head
;
499 while (slot
!= NULL
) {
500 struct active_request_slot
*next
= slot
->next
;
501 if (slot
->curl
!= NULL
) {
502 #ifdef USE_CURL_MULTI
503 curl_multi_remove_handle(curlm
, slot
->curl
);
505 curl_easy_cleanup(slot
->curl
);
510 active_queue_head
= NULL
;
512 #ifndef NO_CURL_EASY_DUPHANDLE
513 curl_easy_cleanup(curl_default
);
516 #ifdef USE_CURL_MULTI
517 curl_multi_cleanup(curlm
);
519 curl_global_cleanup();
521 curl_slist_free_all(pragma_header
);
522 pragma_header
= NULL
;
524 curl_slist_free_all(no_pragma_header
);
525 no_pragma_header
= NULL
;
527 if (curl_http_proxy
) {
528 free((void *)curl_http_proxy
);
529 curl_http_proxy
= NULL
;
532 if (cert_auth
.password
!= NULL
) {
533 memset(cert_auth
.password
, 0, strlen(cert_auth
.password
));
534 free(cert_auth
.password
);
535 cert_auth
.password
= NULL
;
537 ssl_cert_password_required
= 0;
540 struct active_request_slot
*get_active_slot(void)
542 struct active_request_slot
*slot
= active_queue_head
;
543 struct active_request_slot
*newslot
;
545 #ifdef USE_CURL_MULTI
548 /* Wait for a slot to open up if the queue is full */
549 while (active_requests
>= max_requests
) {
550 curl_multi_perform(curlm
, &num_transfers
);
551 if (num_transfers
< active_requests
)
552 process_curl_messages();
556 while (slot
!= NULL
&& slot
->in_use
)
560 newslot
= xmalloc(sizeof(*newslot
));
561 newslot
->curl
= NULL
;
563 newslot
->next
= NULL
;
565 slot
= active_queue_head
;
567 active_queue_head
= newslot
;
569 while (slot
->next
!= NULL
)
571 slot
->next
= newslot
;
576 if (slot
->curl
== NULL
) {
577 #ifdef NO_CURL_EASY_DUPHANDLE
578 slot
->curl
= get_curl_handle();
580 slot
->curl
= curl_easy_duphandle(curl_default
);
582 curl_session_count
++;
587 slot
->results
= NULL
;
588 slot
->finished
= NULL
;
589 slot
->callback_data
= NULL
;
590 slot
->callback_func
= NULL
;
591 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
592 if (curl_save_cookies
)
593 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEJAR
, curl_cookie_file
);
594 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
595 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
596 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
597 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
598 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
599 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
600 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
601 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
602 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 1);
603 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
604 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPAUTH
, http_auth_methods
);
606 if (http_auth
.password
)
607 init_curl_http_auth(slot
->curl
);
612 int start_active_slot(struct active_request_slot
*slot
)
614 #ifdef USE_CURL_MULTI
615 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
618 if (curlm_result
!= CURLM_OK
&&
619 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
626 * We know there must be something to do, since we just added
629 curl_multi_perform(curlm
, &num_transfers
);
634 #ifdef USE_CURL_MULTI
638 struct fill_chain
*next
;
641 static struct fill_chain
*fill_cfg
;
643 void add_fill_function(void *data
, int (*fill
)(void *))
645 struct fill_chain
*new = xmalloc(sizeof(*new));
646 struct fill_chain
**linkp
= &fill_cfg
;
651 linkp
= &(*linkp
)->next
;
655 void fill_active_slots(void)
657 struct active_request_slot
*slot
= active_queue_head
;
659 while (active_requests
< max_requests
) {
660 struct fill_chain
*fill
;
661 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
662 if (fill
->fill(fill
->data
))
669 while (slot
!= NULL
) {
670 if (!slot
->in_use
&& slot
->curl
!= NULL
671 && curl_session_count
> min_curl_sessions
) {
672 curl_easy_cleanup(slot
->curl
);
674 curl_session_count
--;
680 void step_active_slots(void)
683 CURLMcode curlm_result
;
686 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
687 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
688 if (num_transfers
< active_requests
) {
689 process_curl_messages();
695 void run_active_slot(struct active_request_slot
*slot
)
697 #ifdef USE_CURL_MULTI
702 struct timeval select_timeout
;
705 slot
->finished
= &finished
;
710 #if LIBCURL_VERSION_NUM >= 0x070f04
712 curl_multi_timeout(curlm
, &curl_timeout
);
713 if (curl_timeout
== 0) {
715 } else if (curl_timeout
== -1) {
716 select_timeout
.tv_sec
= 0;
717 select_timeout
.tv_usec
= 50000;
719 select_timeout
.tv_sec
= curl_timeout
/ 1000;
720 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
723 select_timeout
.tv_sec
= 0;
724 select_timeout
.tv_usec
= 50000;
731 curl_multi_fdset(curlm
, &readfds
, &writefds
, &excfds
, &max_fd
);
734 * It can happen that curl_multi_timeout returns a pathologically
735 * long timeout when curl_multi_fdset returns no file descriptors
736 * to read. See commit message for more details.
739 (select_timeout
.tv_sec
> 0 ||
740 select_timeout
.tv_usec
> 50000)) {
741 select_timeout
.tv_sec
= 0;
742 select_timeout
.tv_usec
= 50000;
745 select(max_fd
+1, &readfds
, &writefds
, &excfds
, &select_timeout
);
749 while (slot
->in_use
) {
750 slot
->curl_result
= curl_easy_perform(slot
->curl
);
751 finish_active_slot(slot
);
756 static void closedown_active_slot(struct active_request_slot
*slot
)
762 static void release_active_slot(struct active_request_slot
*slot
)
764 closedown_active_slot(slot
);
765 if (slot
->curl
&& curl_session_count
> min_curl_sessions
) {
766 #ifdef USE_CURL_MULTI
767 curl_multi_remove_handle(curlm
, slot
->curl
);
769 curl_easy_cleanup(slot
->curl
);
771 curl_session_count
--;
773 #ifdef USE_CURL_MULTI
778 void finish_active_slot(struct active_request_slot
*slot
)
780 closedown_active_slot(slot
);
781 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
783 if (slot
->finished
!= NULL
)
784 (*slot
->finished
) = 1;
786 /* Store slot results so they can be read after the slot is reused */
787 if (slot
->results
!= NULL
) {
788 slot
->results
->curl_result
= slot
->curl_result
;
789 slot
->results
->http_code
= slot
->http_code
;
790 #if LIBCURL_VERSION_NUM >= 0x070a08
791 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTPAUTH_AVAIL
,
792 &slot
->results
->auth_avail
);
794 slot
->results
->auth_avail
= 0;
798 /* Run callback if appropriate */
799 if (slot
->callback_func
!= NULL
)
800 slot
->callback_func(slot
->callback_data
);
803 void finish_all_active_slots(void)
805 struct active_request_slot
*slot
= active_queue_head
;
809 run_active_slot(slot
);
810 slot
= active_queue_head
;
816 /* Helpers for modifying and creating URLs */
817 static inline int needs_quote(int ch
)
819 if (((ch
>= 'A') && (ch
<= 'Z'))
820 || ((ch
>= 'a') && (ch
<= 'z'))
821 || ((ch
>= '0') && (ch
<= '9'))
829 static char *quote_ref_url(const char *base
, const char *ref
)
831 struct strbuf buf
= STRBUF_INIT
;
835 end_url_with_slash(&buf
, base
);
837 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
839 strbuf_addf(&buf
, "%%%02x", ch
);
841 strbuf_addch(&buf
, *cp
);
843 return strbuf_detach(&buf
, NULL
);
846 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
848 int only_two_digit_prefix
)
850 end_url_with_slash(buf
, url
);
852 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
853 if (!only_two_digit_prefix
)
854 strbuf_addf(buf
, "%s", hex
+2);
857 char *get_remote_object_url(const char *url
, const char *hex
,
858 int only_two_digit_prefix
)
860 struct strbuf buf
= STRBUF_INIT
;
861 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
862 return strbuf_detach(&buf
, NULL
);
865 int handle_curl_result(struct slot_results
*results
)
868 * If we see a failing http code with CURLE_OK, we have turned off
869 * FAILONERROR (to keep the server's custom error response), and should
870 * translate the code into failure here.
872 if (results
->curl_result
== CURLE_OK
&&
873 results
->http_code
>= 400) {
874 results
->curl_result
= CURLE_HTTP_RETURNED_ERROR
;
876 * Normally curl will already have put the "reason phrase"
877 * from the server into curl_errorstr; unfortunately without
878 * FAILONERROR it is lost, so we can give only the numeric
881 snprintf(curl_errorstr
, sizeof(curl_errorstr
),
882 "The requested URL returned error: %ld",
886 if (results
->curl_result
== CURLE_OK
) {
887 credential_approve(&http_auth
);
889 } else if (missing_target(results
))
890 return HTTP_MISSING_TARGET
;
891 else if (results
->http_code
== 401) {
892 if (http_auth
.username
&& http_auth
.password
) {
893 credential_reject(&http_auth
);
896 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
897 http_auth_methods
&= ~CURLAUTH_GSSNEGOTIATE
;
902 #if LIBCURL_VERSION_NUM >= 0x070c00
903 if (!curl_errorstr
[0])
904 strlcpy(curl_errorstr
,
905 curl_easy_strerror(results
->curl_result
),
906 sizeof(curl_errorstr
));
912 int run_one_slot(struct active_request_slot
*slot
,
913 struct slot_results
*results
)
915 slot
->results
= results
;
916 if (!start_active_slot(slot
)) {
917 snprintf(curl_errorstr
, sizeof(curl_errorstr
),
918 "failed to start HTTP request");
919 return HTTP_START_FAILED
;
922 run_active_slot(slot
);
923 return handle_curl_result(results
);
926 static CURLcode
curlinfo_strbuf(CURL
*curl
, CURLINFO info
, struct strbuf
*buf
)
932 ret
= curl_easy_getinfo(curl
, info
, &ptr
);
934 strbuf_addstr(buf
, ptr
);
939 * Check for and extract a content-type parameter. "raw"
940 * should be positioned at the start of the potential
941 * parameter, with any whitespace already removed.
943 * "name" is the name of the parameter. The value is appended
946 static int extract_param(const char *raw
, const char *name
,
949 size_t len
= strlen(name
);
951 if (strncasecmp(raw
, name
, len
))
959 while (*raw
&& !isspace(*raw
) && *raw
!= ';')
960 strbuf_addch(out
, *raw
++);
965 * Extract a normalized version of the content type, with any
966 * spaces suppressed, all letters lowercased, and no trailing ";"
969 * Note that we will silently remove even invalid whitespace. For
970 * example, "text / plain" is specifically forbidden by RFC 2616,
971 * but "text/plain" is the only reasonable output, and this keeps
974 * If the "charset" argument is not NULL, store the value of any
975 * charset parameter there.
978 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
979 * "text / plain" -> "text/plain"
981 static void extract_content_type(struct strbuf
*raw
, struct strbuf
*type
,
982 struct strbuf
*charset
)
987 strbuf_grow(type
, raw
->len
);
988 for (p
= raw
->buf
; *p
; p
++) {
995 strbuf_addch(type
, tolower(*p
));
1001 strbuf_reset(charset
);
1003 while (isspace(*p
) || *p
== ';')
1005 if (!extract_param(p
, "charset", charset
))
1007 while (*p
&& !isspace(*p
))
1011 if (!charset
->len
&& starts_with(type
->buf
, "text/"))
1012 strbuf_addstr(charset
, "ISO-8859-1");
1016 /* http_request() targets */
1017 #define HTTP_REQUEST_STRBUF 0
1018 #define HTTP_REQUEST_FILE 1
1020 static int http_request(const char *url
,
1021 void *result
, int target
,
1022 const struct http_get_options
*options
)
1024 struct active_request_slot
*slot
;
1025 struct slot_results results
;
1026 struct curl_slist
*headers
= NULL
;
1027 struct strbuf buf
= STRBUF_INIT
;
1030 slot
= get_active_slot();
1031 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1033 if (result
== NULL
) {
1034 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
1036 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1037 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, result
);
1039 if (target
== HTTP_REQUEST_FILE
) {
1040 long posn
= ftell(result
);
1041 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1044 strbuf_addf(&buf
, "Range: bytes=%ld-", posn
);
1045 headers
= curl_slist_append(headers
, buf
.buf
);
1049 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1053 strbuf_addstr(&buf
, "Pragma:");
1054 if (options
&& options
->no_cache
)
1055 strbuf_addstr(&buf
, " no-cache");
1056 if (options
&& options
->keep_error
)
1057 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1059 headers
= curl_slist_append(headers
, buf
.buf
);
1061 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1062 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1063 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
1065 ret
= run_one_slot(slot
, &results
);
1067 if (options
&& options
->content_type
) {
1068 struct strbuf raw
= STRBUF_INIT
;
1069 curlinfo_strbuf(slot
->curl
, CURLINFO_CONTENT_TYPE
, &raw
);
1070 extract_content_type(&raw
, options
->content_type
,
1072 strbuf_release(&raw
);
1075 if (options
&& options
->effective_url
)
1076 curlinfo_strbuf(slot
->curl
, CURLINFO_EFFECTIVE_URL
,
1077 options
->effective_url
);
1079 curl_slist_free_all(headers
);
1080 strbuf_release(&buf
);
1086 * Update the "base" url to a more appropriate value, as deduced by
1087 * redirects seen when requesting a URL starting with "url".
1089 * The "asked" parameter is a URL that we asked curl to access, and must begin
1092 * The "got" parameter is the URL that curl reported to us as where we ended
1095 * Returns 1 if we updated the base url, 0 otherwise.
1097 * Our basic strategy is to compare "base" and "asked" to find the bits
1098 * specific to our request. We then strip those bits off of "got" to yield the
1099 * new base. So for example, if our base is "http://example.com/foo.git",
1100 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1101 * with "https://other.example.com/foo.git/info/refs". We would want the
1102 * new URL to become "https://other.example.com/foo.git".
1104 * Note that this assumes a sane redirect scheme. It's entirely possible
1105 * in the example above to end up at a URL that does not even end in
1106 * "info/refs". In such a case we simply punt, as there is not much we can
1107 * do (and such a scheme is unlikely to represent a real git repository,
1108 * which means we are likely about to abort anyway).
1110 static int update_url_from_redirect(struct strbuf
*base
,
1112 const struct strbuf
*got
)
1117 if (!strcmp(asked
, got
->buf
))
1120 if (!skip_prefix(asked
, base
->buf
, &tail
))
1121 die("BUG: update_url_from_redirect: %s is not a superset of %s",
1124 tail_len
= strlen(tail
);
1126 if (got
->len
< tail_len
||
1127 strcmp(tail
, got
->buf
+ got
->len
- tail_len
))
1128 return 0; /* insane redirect scheme */
1131 strbuf_add(base
, got
->buf
, got
->len
- tail_len
);
1135 static int http_request_reauth(const char *url
,
1136 void *result
, int target
,
1137 struct http_get_options
*options
)
1139 int ret
= http_request(url
, result
, target
, options
);
1141 if (options
&& options
->effective_url
&& options
->base_url
) {
1142 if (update_url_from_redirect(options
->base_url
,
1143 url
, options
->effective_url
)) {
1144 credential_from_url(&http_auth
, options
->base_url
->buf
);
1145 url
= options
->effective_url
->buf
;
1149 if (ret
!= HTTP_REAUTH
)
1153 * If we are using KEEP_ERROR, the previous request may have
1154 * put cruft into our output stream; we should clear it out before
1155 * making our next request. We only know how to do this for
1156 * the strbuf case, but that is enough to satisfy current callers.
1158 if (options
&& options
->keep_error
) {
1160 case HTTP_REQUEST_STRBUF
:
1161 strbuf_reset(result
);
1164 die("BUG: HTTP_KEEP_ERROR is only supported with strbufs");
1168 credential_fill(&http_auth
);
1170 return http_request(url
, result
, target
, options
);
1173 int http_get_strbuf(const char *url
,
1174 struct strbuf
*result
,
1175 struct http_get_options
*options
)
1177 return http_request_reauth(url
, result
, HTTP_REQUEST_STRBUF
, options
);
1181 * Downloads a URL and stores the result in the given file.
1183 * If a previous interrupted download is detected (i.e. a previous temporary
1184 * file is still around) the download is resumed.
1186 static int http_get_file(const char *url
, const char *filename
,
1187 struct http_get_options
*options
)
1190 struct strbuf tmpfile
= STRBUF_INIT
;
1193 strbuf_addf(&tmpfile
, "%s.temp", filename
);
1194 result
= fopen(tmpfile
.buf
, "a");
1196 error("Unable to open local file %s", tmpfile
.buf
);
1201 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
1204 if (ret
== HTTP_OK
&& move_temp_to_file(tmpfile
.buf
, filename
))
1207 strbuf_release(&tmpfile
);
1211 int http_fetch_ref(const char *base
, struct ref
*ref
)
1213 struct http_get_options options
= {0};
1215 struct strbuf buffer
= STRBUF_INIT
;
1218 options
.no_cache
= 1;
1220 url
= quote_ref_url(base
, ref
->name
);
1221 if (http_get_strbuf(url
, &buffer
, &options
) == HTTP_OK
) {
1222 strbuf_rtrim(&buffer
);
1223 if (buffer
.len
== 40)
1224 ret
= get_sha1_hex(buffer
.buf
, ref
->old_sha1
);
1225 else if (starts_with(buffer
.buf
, "ref: ")) {
1226 ref
->symref
= xstrdup(buffer
.buf
+ 5);
1231 strbuf_release(&buffer
);
1236 /* Helpers for fetching packs */
1237 static char *fetch_pack_index(unsigned char *sha1
, const char *base_url
)
1240 struct strbuf buf
= STRBUF_INIT
;
1242 if (http_is_verbose
)
1243 fprintf(stderr
, "Getting index for pack %s\n", sha1_to_hex(sha1
));
1245 end_url_with_slash(&buf
, base_url
);
1246 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", sha1_to_hex(sha1
));
1247 url
= strbuf_detach(&buf
, NULL
);
1249 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(sha1
));
1250 tmp
= strbuf_detach(&buf
, NULL
);
1252 if (http_get_file(url
, tmp
, NULL
) != HTTP_OK
) {
1253 error("Unable to get pack index %s", url
);
1262 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
1263 unsigned char *sha1
, const char *base_url
)
1265 struct packed_git
*new_pack
;
1266 char *tmp_idx
= NULL
;
1269 if (has_pack_index(sha1
)) {
1270 new_pack
= parse_pack_index(sha1
, sha1_pack_index_name(sha1
));
1272 return -1; /* parse_pack_index() already issued error message */
1276 tmp_idx
= fetch_pack_index(sha1
, base_url
);
1280 new_pack
= parse_pack_index(sha1
, tmp_idx
);
1285 return -1; /* parse_pack_index() already issued error message */
1288 ret
= verify_pack_index(new_pack
);
1290 close_pack_index(new_pack
);
1291 ret
= move_temp_to_file(tmp_idx
, sha1_pack_index_name(sha1
));
1298 new_pack
->next
= *packs_head
;
1299 *packs_head
= new_pack
;
1303 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
1305 struct http_get_options options
= {0};
1308 struct strbuf buf
= STRBUF_INIT
;
1309 unsigned char sha1
[20];
1311 end_url_with_slash(&buf
, base_url
);
1312 strbuf_addstr(&buf
, "objects/info/packs");
1313 url
= strbuf_detach(&buf
, NULL
);
1315 options
.no_cache
= 1;
1316 ret
= http_get_strbuf(url
, &buf
, &options
);
1321 while (i
< buf
.len
) {
1325 if (i
+ 52 <= buf
.len
&&
1326 starts_with(data
+ i
, " pack-") &&
1327 starts_with(data
+ i
+ 46, ".pack\n")) {
1328 get_sha1_hex(data
+ i
+ 6, sha1
);
1329 fetch_and_setup_pack_index(packs_head
, sha1
,
1335 while (i
< buf
.len
&& data
[i
] != '\n')
1346 void release_http_pack_request(struct http_pack_request
*preq
)
1348 if (preq
->packfile
!= NULL
) {
1349 fclose(preq
->packfile
);
1350 preq
->packfile
= NULL
;
1352 if (preq
->range_header
!= NULL
) {
1353 curl_slist_free_all(preq
->range_header
);
1354 preq
->range_header
= NULL
;
1360 int finish_http_pack_request(struct http_pack_request
*preq
)
1362 struct packed_git
**lst
;
1363 struct packed_git
*p
= preq
->target
;
1365 struct child_process ip
= CHILD_PROCESS_INIT
;
1366 const char *ip_argv
[8];
1368 close_pack_index(p
);
1370 fclose(preq
->packfile
);
1371 preq
->packfile
= NULL
;
1375 lst
= &((*lst
)->next
);
1376 *lst
= (*lst
)->next
;
1378 tmp_idx
= xstrdup(preq
->tmpfile
);
1379 strcpy(tmp_idx
+ strlen(tmp_idx
) - strlen(".pack.temp"),
1382 ip_argv
[0] = "index-pack";
1384 ip_argv
[2] = tmp_idx
;
1385 ip_argv
[3] = preq
->tmpfile
;
1393 if (run_command(&ip
)) {
1394 unlink(preq
->tmpfile
);
1400 unlink(sha1_pack_index_name(p
->sha1
));
1402 if (move_temp_to_file(preq
->tmpfile
, sha1_pack_name(p
->sha1
))
1403 || move_temp_to_file(tmp_idx
, sha1_pack_index_name(p
->sha1
))) {
1408 install_packed_git(p
);
1413 struct http_pack_request
*new_http_pack_request(
1414 struct packed_git
*target
, const char *base_url
)
1417 char range
[RANGE_HEADER_SIZE
];
1418 struct strbuf buf
= STRBUF_INIT
;
1419 struct http_pack_request
*preq
;
1421 preq
= xcalloc(1, sizeof(*preq
));
1422 preq
->target
= target
;
1424 end_url_with_slash(&buf
, base_url
);
1425 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
1426 sha1_to_hex(target
->sha1
));
1427 preq
->url
= strbuf_detach(&buf
, NULL
);
1429 snprintf(preq
->tmpfile
, sizeof(preq
->tmpfile
), "%s.temp",
1430 sha1_pack_name(target
->sha1
));
1431 preq
->packfile
= fopen(preq
->tmpfile
, "a");
1432 if (!preq
->packfile
) {
1433 error("Unable to open local file %s for pack",
1438 preq
->slot
= get_active_slot();
1439 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_FILE
, preq
->packfile
);
1440 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
1441 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
1442 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1446 * If there is data present from a previous transfer attempt,
1447 * resume where it left off
1449 prev_posn
= ftell(preq
->packfile
);
1451 if (http_is_verbose
)
1453 "Resuming fetch of pack %s at byte %ld\n",
1454 sha1_to_hex(target
->sha1
), prev_posn
);
1455 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1456 preq
->range_header
= curl_slist_append(NULL
, range
);
1457 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1458 preq
->range_header
);
1469 /* Helpers for fetching objects (loose) */
1470 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
1473 unsigned char expn
[4096];
1474 size_t size
= eltsize
* nmemb
;
1476 struct http_object_request
*freq
=
1477 (struct http_object_request
*)data
;
1479 ssize_t retval
= xwrite(freq
->localfile
,
1480 (char *) ptr
+ posn
, size
- posn
);
1484 } while (posn
< size
);
1486 freq
->stream
.avail_in
= size
;
1487 freq
->stream
.next_in
= (void *)ptr
;
1489 freq
->stream
.next_out
= expn
;
1490 freq
->stream
.avail_out
= sizeof(expn
);
1491 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
1492 git_SHA1_Update(&freq
->c
, expn
,
1493 sizeof(expn
) - freq
->stream
.avail_out
);
1494 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
1498 struct http_object_request
*new_http_object_request(const char *base_url
,
1499 unsigned char *sha1
)
1501 char *hex
= sha1_to_hex(sha1
);
1502 const char *filename
;
1503 char prevfile
[PATH_MAX
];
1505 char prev_buf
[PREV_BUF_SIZE
];
1506 ssize_t prev_read
= 0;
1508 char range
[RANGE_HEADER_SIZE
];
1509 struct curl_slist
*range_header
= NULL
;
1510 struct http_object_request
*freq
;
1512 freq
= xcalloc(1, sizeof(*freq
));
1513 hashcpy(freq
->sha1
, sha1
);
1514 freq
->localfile
= -1;
1516 filename
= sha1_file_name(sha1
);
1517 snprintf(freq
->tmpfile
, sizeof(freq
->tmpfile
),
1518 "%s.temp", filename
);
1520 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", filename
);
1521 unlink_or_warn(prevfile
);
1522 rename(freq
->tmpfile
, prevfile
);
1523 unlink_or_warn(freq
->tmpfile
);
1525 if (freq
->localfile
!= -1)
1526 error("fd leakage in start: %d", freq
->localfile
);
1527 freq
->localfile
= open(freq
->tmpfile
,
1528 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1530 * This could have failed due to the "lazy directory creation";
1531 * try to mkdir the last path component.
1533 if (freq
->localfile
< 0 && errno
== ENOENT
) {
1534 char *dir
= strrchr(freq
->tmpfile
, '/');
1537 mkdir(freq
->tmpfile
, 0777);
1540 freq
->localfile
= open(freq
->tmpfile
,
1541 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1544 if (freq
->localfile
< 0) {
1545 error("Couldn't create temporary file %s: %s",
1546 freq
->tmpfile
, strerror(errno
));
1550 git_inflate_init(&freq
->stream
);
1552 git_SHA1_Init(&freq
->c
);
1554 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
1557 * If a previous temp file is present, process what was already
1560 prevlocal
= open(prevfile
, O_RDONLY
);
1561 if (prevlocal
!= -1) {
1563 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
1565 if (fwrite_sha1_file(prev_buf
,
1568 freq
) == prev_read
) {
1569 prev_posn
+= prev_read
;
1574 } while (prev_read
> 0);
1577 unlink_or_warn(prevfile
);
1580 * Reset inflate/SHA1 if there was an error reading the previous temp
1581 * file; also rewind to the beginning of the local file.
1583 if (prev_read
== -1) {
1584 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
1585 git_inflate_init(&freq
->stream
);
1586 git_SHA1_Init(&freq
->c
);
1589 lseek(freq
->localfile
, 0, SEEK_SET
);
1590 if (ftruncate(freq
->localfile
, 0) < 0) {
1591 error("Couldn't truncate temporary file %s: %s",
1592 freq
->tmpfile
, strerror(errno
));
1598 freq
->slot
= get_active_slot();
1600 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FILE
, freq
);
1601 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
1602 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
1603 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
1604 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
1607 * If we have successfully processed data from a previous fetch
1608 * attempt, only fetch the data we don't already have.
1611 if (http_is_verbose
)
1613 "Resuming fetch of object %s at byte %ld\n",
1615 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1616 range_header
= curl_slist_append(range_header
, range
);
1617 curl_easy_setopt(freq
->slot
->curl
,
1618 CURLOPT_HTTPHEADER
, range_header
);
1629 void process_http_object_request(struct http_object_request
*freq
)
1631 if (freq
->slot
== NULL
)
1633 freq
->curl_result
= freq
->slot
->curl_result
;
1634 freq
->http_code
= freq
->slot
->http_code
;
1638 int finish_http_object_request(struct http_object_request
*freq
)
1642 close(freq
->localfile
);
1643 freq
->localfile
= -1;
1645 process_http_object_request(freq
);
1647 if (freq
->http_code
== 416) {
1648 warning("requested range invalid; we may already have all the data.");
1649 } else if (freq
->curl_result
!= CURLE_OK
) {
1650 if (stat(freq
->tmpfile
, &st
) == 0)
1651 if (st
.st_size
== 0)
1652 unlink_or_warn(freq
->tmpfile
);
1656 git_inflate_end(&freq
->stream
);
1657 git_SHA1_Final(freq
->real_sha1
, &freq
->c
);
1658 if (freq
->zret
!= Z_STREAM_END
) {
1659 unlink_or_warn(freq
->tmpfile
);
1662 if (hashcmp(freq
->sha1
, freq
->real_sha1
)) {
1663 unlink_or_warn(freq
->tmpfile
);
1667 move_temp_to_file(freq
->tmpfile
, sha1_file_name(freq
->sha1
));
1669 return freq
->rename
;
1672 void abort_http_object_request(struct http_object_request
*freq
)
1674 unlink_or_warn(freq
->tmpfile
);
1676 release_http_object_request(freq
);
1679 void release_http_object_request(struct http_object_request
*freq
)
1681 if (freq
->localfile
!= -1) {
1682 close(freq
->localfile
);
1683 freq
->localfile
= -1;
1685 if (freq
->url
!= NULL
) {
1689 if (freq
->slot
!= NULL
) {
1690 freq
->slot
->callback_func
= NULL
;
1691 freq
->slot
->callback_data
= NULL
;
1692 release_active_slot(freq
->slot
);