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 curl_easy_setopt(result
, CURLOPT_MAXREDIRS
, 20);
356 #if LIBCURL_VERSION_NUM >= 0x071301
357 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
358 #elif LIBCURL_VERSION_NUM >= 0x071101
359 curl_easy_setopt(result
, CURLOPT_POST301
, 1);
361 #if LIBCURL_VERSION_NUM >= 0x071304
362 if (is_transport_allowed("http"))
363 allowed_protocols
|= CURLPROTO_HTTP
;
364 if (is_transport_allowed("https"))
365 allowed_protocols
|= CURLPROTO_HTTPS
;
366 if (is_transport_allowed("ftp"))
367 allowed_protocols
|= CURLPROTO_FTP
;
368 if (is_transport_allowed("ftps"))
369 allowed_protocols
|= CURLPROTO_FTPS
;
370 curl_easy_setopt(result
, CURLOPT_REDIR_PROTOCOLS
, allowed_protocols
);
372 if (transport_restrict_protocols())
373 warning("protocol restrictions not applied to curl redirects because\n"
374 "your curl version is too old (>= 7.19.4)");
377 if (getenv("GIT_CURL_VERBOSE"))
378 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
380 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
381 user_agent
? user_agent
: git_user_agent());
383 if (curl_ftp_no_epsv
)
384 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
386 #ifdef CURLOPT_USE_SSL
388 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
391 if (curl_http_proxy
) {
392 curl_easy_setopt(result
, CURLOPT_PROXY
, curl_http_proxy
);
393 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
396 set_curl_keepalive(result
);
401 static void set_from_env(const char **var
, const char *envname
)
403 const char *val
= getenv(envname
);
408 void http_init(struct remote
*remote
, const char *url
, int proactive_auth
)
410 char *low_speed_limit
;
411 char *low_speed_time
;
412 char *normalized_url
;
413 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
415 config
.section
= "http";
417 config
.collect_fn
= http_options
;
418 config
.cascade_fn
= git_default_config
;
422 normalized_url
= url_normalize(url
, &config
.url
);
424 git_config(urlmatch_config_entry
, &config
);
425 free(normalized_url
);
427 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
)
428 die("curl_global_init failed");
430 http_proactive_auth
= proactive_auth
;
432 if (remote
&& remote
->http_proxy
)
433 curl_http_proxy
= xstrdup(remote
->http_proxy
);
435 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
436 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
438 #ifdef USE_CURL_MULTI
440 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
441 if (http_max_requests
!= NULL
)
442 max_requests
= atoi(http_max_requests
);
445 curlm
= curl_multi_init();
447 die("curl_multi_init failed");
450 if (getenv("GIT_SSL_NO_VERIFY"))
453 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
454 #if LIBCURL_VERSION_NUM >= 0x070903
455 set_from_env(&ssl_key
, "GIT_SSL_KEY");
457 #if LIBCURL_VERSION_NUM >= 0x070908
458 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
460 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
462 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
464 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
465 if (low_speed_limit
!= NULL
)
466 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
467 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
468 if (low_speed_time
!= NULL
)
469 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
471 if (curl_ssl_verify
== -1)
474 curl_session_count
= 0;
475 #ifdef USE_CURL_MULTI
476 if (max_requests
< 1)
477 max_requests
= DEFAULT_MAX_REQUESTS
;
480 if (getenv("GIT_CURL_FTP_NO_EPSV"))
481 curl_ftp_no_epsv
= 1;
484 credential_from_url(&http_auth
, url
);
485 if (!ssl_cert_password_required
&&
486 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
487 starts_with(url
, "https://"))
488 ssl_cert_password_required
= 1;
491 #ifndef NO_CURL_EASY_DUPHANDLE
492 curl_default
= get_curl_handle();
496 void http_cleanup(void)
498 struct active_request_slot
*slot
= active_queue_head
;
500 while (slot
!= NULL
) {
501 struct active_request_slot
*next
= slot
->next
;
502 if (slot
->curl
!= NULL
) {
503 #ifdef USE_CURL_MULTI
504 curl_multi_remove_handle(curlm
, slot
->curl
);
506 curl_easy_cleanup(slot
->curl
);
511 active_queue_head
= NULL
;
513 #ifndef NO_CURL_EASY_DUPHANDLE
514 curl_easy_cleanup(curl_default
);
517 #ifdef USE_CURL_MULTI
518 curl_multi_cleanup(curlm
);
520 curl_global_cleanup();
522 curl_slist_free_all(pragma_header
);
523 pragma_header
= NULL
;
525 curl_slist_free_all(no_pragma_header
);
526 no_pragma_header
= NULL
;
528 if (curl_http_proxy
) {
529 free((void *)curl_http_proxy
);
530 curl_http_proxy
= NULL
;
533 if (cert_auth
.password
!= NULL
) {
534 memset(cert_auth
.password
, 0, strlen(cert_auth
.password
));
535 free(cert_auth
.password
);
536 cert_auth
.password
= NULL
;
538 ssl_cert_password_required
= 0;
541 struct active_request_slot
*get_active_slot(void)
543 struct active_request_slot
*slot
= active_queue_head
;
544 struct active_request_slot
*newslot
;
546 #ifdef USE_CURL_MULTI
549 /* Wait for a slot to open up if the queue is full */
550 while (active_requests
>= max_requests
) {
551 curl_multi_perform(curlm
, &num_transfers
);
552 if (num_transfers
< active_requests
)
553 process_curl_messages();
557 while (slot
!= NULL
&& slot
->in_use
)
561 newslot
= xmalloc(sizeof(*newslot
));
562 newslot
->curl
= NULL
;
564 newslot
->next
= NULL
;
566 slot
= active_queue_head
;
568 active_queue_head
= newslot
;
570 while (slot
->next
!= NULL
)
572 slot
->next
= newslot
;
577 if (slot
->curl
== NULL
) {
578 #ifdef NO_CURL_EASY_DUPHANDLE
579 slot
->curl
= get_curl_handle();
581 slot
->curl
= curl_easy_duphandle(curl_default
);
583 curl_session_count
++;
588 slot
->results
= NULL
;
589 slot
->finished
= NULL
;
590 slot
->callback_data
= NULL
;
591 slot
->callback_func
= NULL
;
592 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
593 if (curl_save_cookies
)
594 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEJAR
, curl_cookie_file
);
595 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
596 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
597 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
598 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
599 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
600 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
601 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
602 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
603 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 1);
604 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
605 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPAUTH
, http_auth_methods
);
607 if (http_auth
.password
)
608 init_curl_http_auth(slot
->curl
);
613 int start_active_slot(struct active_request_slot
*slot
)
615 #ifdef USE_CURL_MULTI
616 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
619 if (curlm_result
!= CURLM_OK
&&
620 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
627 * We know there must be something to do, since we just added
630 curl_multi_perform(curlm
, &num_transfers
);
635 #ifdef USE_CURL_MULTI
639 struct fill_chain
*next
;
642 static struct fill_chain
*fill_cfg
;
644 void add_fill_function(void *data
, int (*fill
)(void *))
646 struct fill_chain
*new = xmalloc(sizeof(*new));
647 struct fill_chain
**linkp
= &fill_cfg
;
652 linkp
= &(*linkp
)->next
;
656 void fill_active_slots(void)
658 struct active_request_slot
*slot
= active_queue_head
;
660 while (active_requests
< max_requests
) {
661 struct fill_chain
*fill
;
662 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
663 if (fill
->fill(fill
->data
))
670 while (slot
!= NULL
) {
671 if (!slot
->in_use
&& slot
->curl
!= NULL
672 && curl_session_count
> min_curl_sessions
) {
673 curl_easy_cleanup(slot
->curl
);
675 curl_session_count
--;
681 void step_active_slots(void)
684 CURLMcode curlm_result
;
687 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
688 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
689 if (num_transfers
< active_requests
) {
690 process_curl_messages();
696 void run_active_slot(struct active_request_slot
*slot
)
698 #ifdef USE_CURL_MULTI
703 struct timeval select_timeout
;
706 slot
->finished
= &finished
;
711 #if LIBCURL_VERSION_NUM >= 0x070f04
713 curl_multi_timeout(curlm
, &curl_timeout
);
714 if (curl_timeout
== 0) {
716 } else if (curl_timeout
== -1) {
717 select_timeout
.tv_sec
= 0;
718 select_timeout
.tv_usec
= 50000;
720 select_timeout
.tv_sec
= curl_timeout
/ 1000;
721 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
724 select_timeout
.tv_sec
= 0;
725 select_timeout
.tv_usec
= 50000;
732 curl_multi_fdset(curlm
, &readfds
, &writefds
, &excfds
, &max_fd
);
735 * It can happen that curl_multi_timeout returns a pathologically
736 * long timeout when curl_multi_fdset returns no file descriptors
737 * to read. See commit message for more details.
740 (select_timeout
.tv_sec
> 0 ||
741 select_timeout
.tv_usec
> 50000)) {
742 select_timeout
.tv_sec
= 0;
743 select_timeout
.tv_usec
= 50000;
746 select(max_fd
+1, &readfds
, &writefds
, &excfds
, &select_timeout
);
750 while (slot
->in_use
) {
751 slot
->curl_result
= curl_easy_perform(slot
->curl
);
752 finish_active_slot(slot
);
757 static void closedown_active_slot(struct active_request_slot
*slot
)
763 static void release_active_slot(struct active_request_slot
*slot
)
765 closedown_active_slot(slot
);
766 if (slot
->curl
&& curl_session_count
> min_curl_sessions
) {
767 #ifdef USE_CURL_MULTI
768 curl_multi_remove_handle(curlm
, slot
->curl
);
770 curl_easy_cleanup(slot
->curl
);
772 curl_session_count
--;
774 #ifdef USE_CURL_MULTI
779 void finish_active_slot(struct active_request_slot
*slot
)
781 closedown_active_slot(slot
);
782 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
784 if (slot
->finished
!= NULL
)
785 (*slot
->finished
) = 1;
787 /* Store slot results so they can be read after the slot is reused */
788 if (slot
->results
!= NULL
) {
789 slot
->results
->curl_result
= slot
->curl_result
;
790 slot
->results
->http_code
= slot
->http_code
;
791 #if LIBCURL_VERSION_NUM >= 0x070a08
792 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTPAUTH_AVAIL
,
793 &slot
->results
->auth_avail
);
795 slot
->results
->auth_avail
= 0;
799 /* Run callback if appropriate */
800 if (slot
->callback_func
!= NULL
)
801 slot
->callback_func(slot
->callback_data
);
804 void finish_all_active_slots(void)
806 struct active_request_slot
*slot
= active_queue_head
;
810 run_active_slot(slot
);
811 slot
= active_queue_head
;
817 /* Helpers for modifying and creating URLs */
818 static inline int needs_quote(int ch
)
820 if (((ch
>= 'A') && (ch
<= 'Z'))
821 || ((ch
>= 'a') && (ch
<= 'z'))
822 || ((ch
>= '0') && (ch
<= '9'))
830 static char *quote_ref_url(const char *base
, const char *ref
)
832 struct strbuf buf
= STRBUF_INIT
;
836 end_url_with_slash(&buf
, base
);
838 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
840 strbuf_addf(&buf
, "%%%02x", ch
);
842 strbuf_addch(&buf
, *cp
);
844 return strbuf_detach(&buf
, NULL
);
847 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
849 int only_two_digit_prefix
)
851 end_url_with_slash(buf
, url
);
853 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
854 if (!only_two_digit_prefix
)
855 strbuf_addf(buf
, "%s", hex
+2);
858 char *get_remote_object_url(const char *url
, const char *hex
,
859 int only_two_digit_prefix
)
861 struct strbuf buf
= STRBUF_INIT
;
862 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
863 return strbuf_detach(&buf
, NULL
);
866 int handle_curl_result(struct slot_results
*results
)
869 * If we see a failing http code with CURLE_OK, we have turned off
870 * FAILONERROR (to keep the server's custom error response), and should
871 * translate the code into failure here.
873 if (results
->curl_result
== CURLE_OK
&&
874 results
->http_code
>= 400) {
875 results
->curl_result
= CURLE_HTTP_RETURNED_ERROR
;
877 * Normally curl will already have put the "reason phrase"
878 * from the server into curl_errorstr; unfortunately without
879 * FAILONERROR it is lost, so we can give only the numeric
882 snprintf(curl_errorstr
, sizeof(curl_errorstr
),
883 "The requested URL returned error: %ld",
887 if (results
->curl_result
== CURLE_OK
) {
888 credential_approve(&http_auth
);
890 } else if (missing_target(results
))
891 return HTTP_MISSING_TARGET
;
892 else if (results
->http_code
== 401) {
893 if (http_auth
.username
&& http_auth
.password
) {
894 credential_reject(&http_auth
);
897 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
898 http_auth_methods
&= ~CURLAUTH_GSSNEGOTIATE
;
903 #if LIBCURL_VERSION_NUM >= 0x070c00
904 if (!curl_errorstr
[0])
905 strlcpy(curl_errorstr
,
906 curl_easy_strerror(results
->curl_result
),
907 sizeof(curl_errorstr
));
913 int run_one_slot(struct active_request_slot
*slot
,
914 struct slot_results
*results
)
916 slot
->results
= results
;
917 if (!start_active_slot(slot
)) {
918 snprintf(curl_errorstr
, sizeof(curl_errorstr
),
919 "failed to start HTTP request");
920 return HTTP_START_FAILED
;
923 run_active_slot(slot
);
924 return handle_curl_result(results
);
927 static CURLcode
curlinfo_strbuf(CURL
*curl
, CURLINFO info
, struct strbuf
*buf
)
933 ret
= curl_easy_getinfo(curl
, info
, &ptr
);
935 strbuf_addstr(buf
, ptr
);
940 * Check for and extract a content-type parameter. "raw"
941 * should be positioned at the start of the potential
942 * parameter, with any whitespace already removed.
944 * "name" is the name of the parameter. The value is appended
947 static int extract_param(const char *raw
, const char *name
,
950 size_t len
= strlen(name
);
952 if (strncasecmp(raw
, name
, len
))
960 while (*raw
&& !isspace(*raw
) && *raw
!= ';')
961 strbuf_addch(out
, *raw
++);
966 * Extract a normalized version of the content type, with any
967 * spaces suppressed, all letters lowercased, and no trailing ";"
970 * Note that we will silently remove even invalid whitespace. For
971 * example, "text / plain" is specifically forbidden by RFC 2616,
972 * but "text/plain" is the only reasonable output, and this keeps
975 * If the "charset" argument is not NULL, store the value of any
976 * charset parameter there.
979 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
980 * "text / plain" -> "text/plain"
982 static void extract_content_type(struct strbuf
*raw
, struct strbuf
*type
,
983 struct strbuf
*charset
)
988 strbuf_grow(type
, raw
->len
);
989 for (p
= raw
->buf
; *p
; p
++) {
996 strbuf_addch(type
, tolower(*p
));
1002 strbuf_reset(charset
);
1004 while (isspace(*p
) || *p
== ';')
1006 if (!extract_param(p
, "charset", charset
))
1008 while (*p
&& !isspace(*p
))
1012 if (!charset
->len
&& starts_with(type
->buf
, "text/"))
1013 strbuf_addstr(charset
, "ISO-8859-1");
1017 /* http_request() targets */
1018 #define HTTP_REQUEST_STRBUF 0
1019 #define HTTP_REQUEST_FILE 1
1021 static int http_request(const char *url
,
1022 void *result
, int target
,
1023 const struct http_get_options
*options
)
1025 struct active_request_slot
*slot
;
1026 struct slot_results results
;
1027 struct curl_slist
*headers
= NULL
;
1028 struct strbuf buf
= STRBUF_INIT
;
1031 slot
= get_active_slot();
1032 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1034 if (result
== NULL
) {
1035 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
1037 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1038 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, result
);
1040 if (target
== HTTP_REQUEST_FILE
) {
1041 long posn
= ftell(result
);
1042 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1045 strbuf_addf(&buf
, "Range: bytes=%ld-", posn
);
1046 headers
= curl_slist_append(headers
, buf
.buf
);
1050 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1054 strbuf_addstr(&buf
, "Pragma:");
1055 if (options
&& options
->no_cache
)
1056 strbuf_addstr(&buf
, " no-cache");
1057 if (options
&& options
->keep_error
)
1058 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1060 headers
= curl_slist_append(headers
, buf
.buf
);
1062 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1063 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1064 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
1066 ret
= run_one_slot(slot
, &results
);
1068 if (options
&& options
->content_type
) {
1069 struct strbuf raw
= STRBUF_INIT
;
1070 curlinfo_strbuf(slot
->curl
, CURLINFO_CONTENT_TYPE
, &raw
);
1071 extract_content_type(&raw
, options
->content_type
,
1073 strbuf_release(&raw
);
1076 if (options
&& options
->effective_url
)
1077 curlinfo_strbuf(slot
->curl
, CURLINFO_EFFECTIVE_URL
,
1078 options
->effective_url
);
1080 curl_slist_free_all(headers
);
1081 strbuf_release(&buf
);
1087 * Update the "base" url to a more appropriate value, as deduced by
1088 * redirects seen when requesting a URL starting with "url".
1090 * The "asked" parameter is a URL that we asked curl to access, and must begin
1093 * The "got" parameter is the URL that curl reported to us as where we ended
1096 * Returns 1 if we updated the base url, 0 otherwise.
1098 * Our basic strategy is to compare "base" and "asked" to find the bits
1099 * specific to our request. We then strip those bits off of "got" to yield the
1100 * new base. So for example, if our base is "http://example.com/foo.git",
1101 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1102 * with "https://other.example.com/foo.git/info/refs". We would want the
1103 * new URL to become "https://other.example.com/foo.git".
1105 * Note that this assumes a sane redirect scheme. It's entirely possible
1106 * in the example above to end up at a URL that does not even end in
1107 * "info/refs". In such a case we simply punt, as there is not much we can
1108 * do (and such a scheme is unlikely to represent a real git repository,
1109 * which means we are likely about to abort anyway).
1111 static int update_url_from_redirect(struct strbuf
*base
,
1113 const struct strbuf
*got
)
1118 if (!strcmp(asked
, got
->buf
))
1121 if (!skip_prefix(asked
, base
->buf
, &tail
))
1122 die("BUG: update_url_from_redirect: %s is not a superset of %s",
1125 tail_len
= strlen(tail
);
1127 if (got
->len
< tail_len
||
1128 strcmp(tail
, got
->buf
+ got
->len
- tail_len
))
1129 return 0; /* insane redirect scheme */
1132 strbuf_add(base
, got
->buf
, got
->len
- tail_len
);
1136 static int http_request_reauth(const char *url
,
1137 void *result
, int target
,
1138 struct http_get_options
*options
)
1140 int ret
= http_request(url
, result
, target
, options
);
1142 if (options
&& options
->effective_url
&& options
->base_url
) {
1143 if (update_url_from_redirect(options
->base_url
,
1144 url
, options
->effective_url
)) {
1145 credential_from_url(&http_auth
, options
->base_url
->buf
);
1146 url
= options
->effective_url
->buf
;
1150 if (ret
!= HTTP_REAUTH
)
1154 * If we are using KEEP_ERROR, the previous request may have
1155 * put cruft into our output stream; we should clear it out before
1156 * making our next request. We only know how to do this for
1157 * the strbuf case, but that is enough to satisfy current callers.
1159 if (options
&& options
->keep_error
) {
1161 case HTTP_REQUEST_STRBUF
:
1162 strbuf_reset(result
);
1165 die("BUG: HTTP_KEEP_ERROR is only supported with strbufs");
1169 credential_fill(&http_auth
);
1171 return http_request(url
, result
, target
, options
);
1174 int http_get_strbuf(const char *url
,
1175 struct strbuf
*result
,
1176 struct http_get_options
*options
)
1178 return http_request_reauth(url
, result
, HTTP_REQUEST_STRBUF
, options
);
1182 * Downloads a URL and stores the result in the given file.
1184 * If a previous interrupted download is detected (i.e. a previous temporary
1185 * file is still around) the download is resumed.
1187 static int http_get_file(const char *url
, const char *filename
,
1188 struct http_get_options
*options
)
1191 struct strbuf tmpfile
= STRBUF_INIT
;
1194 strbuf_addf(&tmpfile
, "%s.temp", filename
);
1195 result
= fopen(tmpfile
.buf
, "a");
1197 error("Unable to open local file %s", tmpfile
.buf
);
1202 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
1205 if (ret
== HTTP_OK
&& move_temp_to_file(tmpfile
.buf
, filename
))
1208 strbuf_release(&tmpfile
);
1212 int http_fetch_ref(const char *base
, struct ref
*ref
)
1214 struct http_get_options options
= {0};
1216 struct strbuf buffer
= STRBUF_INIT
;
1219 options
.no_cache
= 1;
1221 url
= quote_ref_url(base
, ref
->name
);
1222 if (http_get_strbuf(url
, &buffer
, &options
) == HTTP_OK
) {
1223 strbuf_rtrim(&buffer
);
1224 if (buffer
.len
== 40)
1225 ret
= get_sha1_hex(buffer
.buf
, ref
->old_sha1
);
1226 else if (starts_with(buffer
.buf
, "ref: ")) {
1227 ref
->symref
= xstrdup(buffer
.buf
+ 5);
1232 strbuf_release(&buffer
);
1237 /* Helpers for fetching packs */
1238 static char *fetch_pack_index(unsigned char *sha1
, const char *base_url
)
1241 struct strbuf buf
= STRBUF_INIT
;
1243 if (http_is_verbose
)
1244 fprintf(stderr
, "Getting index for pack %s\n", sha1_to_hex(sha1
));
1246 end_url_with_slash(&buf
, base_url
);
1247 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", sha1_to_hex(sha1
));
1248 url
= strbuf_detach(&buf
, NULL
);
1250 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(sha1
));
1251 tmp
= strbuf_detach(&buf
, NULL
);
1253 if (http_get_file(url
, tmp
, NULL
) != HTTP_OK
) {
1254 error("Unable to get pack index %s", url
);
1263 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
1264 unsigned char *sha1
, const char *base_url
)
1266 struct packed_git
*new_pack
;
1267 char *tmp_idx
= NULL
;
1270 if (has_pack_index(sha1
)) {
1271 new_pack
= parse_pack_index(sha1
, sha1_pack_index_name(sha1
));
1273 return -1; /* parse_pack_index() already issued error message */
1277 tmp_idx
= fetch_pack_index(sha1
, base_url
);
1281 new_pack
= parse_pack_index(sha1
, tmp_idx
);
1286 return -1; /* parse_pack_index() already issued error message */
1289 ret
= verify_pack_index(new_pack
);
1291 close_pack_index(new_pack
);
1292 ret
= move_temp_to_file(tmp_idx
, sha1_pack_index_name(sha1
));
1299 new_pack
->next
= *packs_head
;
1300 *packs_head
= new_pack
;
1304 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
1306 struct http_get_options options
= {0};
1309 struct strbuf buf
= STRBUF_INIT
;
1310 unsigned char sha1
[20];
1312 end_url_with_slash(&buf
, base_url
);
1313 strbuf_addstr(&buf
, "objects/info/packs");
1314 url
= strbuf_detach(&buf
, NULL
);
1316 options
.no_cache
= 1;
1317 ret
= http_get_strbuf(url
, &buf
, &options
);
1322 while (i
< buf
.len
) {
1326 if (i
+ 52 <= buf
.len
&&
1327 starts_with(data
+ i
, " pack-") &&
1328 starts_with(data
+ i
+ 46, ".pack\n")) {
1329 get_sha1_hex(data
+ i
+ 6, sha1
);
1330 fetch_and_setup_pack_index(packs_head
, sha1
,
1336 while (i
< buf
.len
&& data
[i
] != '\n')
1347 void release_http_pack_request(struct http_pack_request
*preq
)
1349 if (preq
->packfile
!= NULL
) {
1350 fclose(preq
->packfile
);
1351 preq
->packfile
= NULL
;
1353 if (preq
->range_header
!= NULL
) {
1354 curl_slist_free_all(preq
->range_header
);
1355 preq
->range_header
= NULL
;
1361 int finish_http_pack_request(struct http_pack_request
*preq
)
1363 struct packed_git
**lst
;
1364 struct packed_git
*p
= preq
->target
;
1366 struct child_process ip
= CHILD_PROCESS_INIT
;
1367 const char *ip_argv
[8];
1369 close_pack_index(p
);
1371 fclose(preq
->packfile
);
1372 preq
->packfile
= NULL
;
1376 lst
= &((*lst
)->next
);
1377 *lst
= (*lst
)->next
;
1379 tmp_idx
= xstrdup(preq
->tmpfile
);
1380 strcpy(tmp_idx
+ strlen(tmp_idx
) - strlen(".pack.temp"),
1383 ip_argv
[0] = "index-pack";
1385 ip_argv
[2] = tmp_idx
;
1386 ip_argv
[3] = preq
->tmpfile
;
1394 if (run_command(&ip
)) {
1395 unlink(preq
->tmpfile
);
1401 unlink(sha1_pack_index_name(p
->sha1
));
1403 if (move_temp_to_file(preq
->tmpfile
, sha1_pack_name(p
->sha1
))
1404 || move_temp_to_file(tmp_idx
, sha1_pack_index_name(p
->sha1
))) {
1409 install_packed_git(p
);
1414 struct http_pack_request
*new_http_pack_request(
1415 struct packed_git
*target
, const char *base_url
)
1418 char range
[RANGE_HEADER_SIZE
];
1419 struct strbuf buf
= STRBUF_INIT
;
1420 struct http_pack_request
*preq
;
1422 preq
= xcalloc(1, sizeof(*preq
));
1423 preq
->target
= target
;
1425 end_url_with_slash(&buf
, base_url
);
1426 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
1427 sha1_to_hex(target
->sha1
));
1428 preq
->url
= strbuf_detach(&buf
, NULL
);
1430 snprintf(preq
->tmpfile
, sizeof(preq
->tmpfile
), "%s.temp",
1431 sha1_pack_name(target
->sha1
));
1432 preq
->packfile
= fopen(preq
->tmpfile
, "a");
1433 if (!preq
->packfile
) {
1434 error("Unable to open local file %s for pack",
1439 preq
->slot
= get_active_slot();
1440 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_FILE
, preq
->packfile
);
1441 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
1442 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
1443 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1447 * If there is data present from a previous transfer attempt,
1448 * resume where it left off
1450 prev_posn
= ftell(preq
->packfile
);
1452 if (http_is_verbose
)
1454 "Resuming fetch of pack %s at byte %ld\n",
1455 sha1_to_hex(target
->sha1
), prev_posn
);
1456 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1457 preq
->range_header
= curl_slist_append(NULL
, range
);
1458 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1459 preq
->range_header
);
1470 /* Helpers for fetching objects (loose) */
1471 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
1474 unsigned char expn
[4096];
1475 size_t size
= eltsize
* nmemb
;
1477 struct http_object_request
*freq
=
1478 (struct http_object_request
*)data
;
1480 ssize_t retval
= xwrite(freq
->localfile
,
1481 (char *) ptr
+ posn
, size
- posn
);
1485 } while (posn
< size
);
1487 freq
->stream
.avail_in
= size
;
1488 freq
->stream
.next_in
= (void *)ptr
;
1490 freq
->stream
.next_out
= expn
;
1491 freq
->stream
.avail_out
= sizeof(expn
);
1492 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
1493 git_SHA1_Update(&freq
->c
, expn
,
1494 sizeof(expn
) - freq
->stream
.avail_out
);
1495 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
1499 struct http_object_request
*new_http_object_request(const char *base_url
,
1500 unsigned char *sha1
)
1502 char *hex
= sha1_to_hex(sha1
);
1503 const char *filename
;
1504 char prevfile
[PATH_MAX
];
1506 char prev_buf
[PREV_BUF_SIZE
];
1507 ssize_t prev_read
= 0;
1509 char range
[RANGE_HEADER_SIZE
];
1510 struct curl_slist
*range_header
= NULL
;
1511 struct http_object_request
*freq
;
1513 freq
= xcalloc(1, sizeof(*freq
));
1514 hashcpy(freq
->sha1
, sha1
);
1515 freq
->localfile
= -1;
1517 filename
= sha1_file_name(sha1
);
1518 snprintf(freq
->tmpfile
, sizeof(freq
->tmpfile
),
1519 "%s.temp", filename
);
1521 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", filename
);
1522 unlink_or_warn(prevfile
);
1523 rename(freq
->tmpfile
, prevfile
);
1524 unlink_or_warn(freq
->tmpfile
);
1526 if (freq
->localfile
!= -1)
1527 error("fd leakage in start: %d", freq
->localfile
);
1528 freq
->localfile
= open(freq
->tmpfile
,
1529 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1531 * This could have failed due to the "lazy directory creation";
1532 * try to mkdir the last path component.
1534 if (freq
->localfile
< 0 && errno
== ENOENT
) {
1535 char *dir
= strrchr(freq
->tmpfile
, '/');
1538 mkdir(freq
->tmpfile
, 0777);
1541 freq
->localfile
= open(freq
->tmpfile
,
1542 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1545 if (freq
->localfile
< 0) {
1546 error("Couldn't create temporary file %s: %s",
1547 freq
->tmpfile
, strerror(errno
));
1551 git_inflate_init(&freq
->stream
);
1553 git_SHA1_Init(&freq
->c
);
1555 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
1558 * If a previous temp file is present, process what was already
1561 prevlocal
= open(prevfile
, O_RDONLY
);
1562 if (prevlocal
!= -1) {
1564 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
1566 if (fwrite_sha1_file(prev_buf
,
1569 freq
) == prev_read
) {
1570 prev_posn
+= prev_read
;
1575 } while (prev_read
> 0);
1578 unlink_or_warn(prevfile
);
1581 * Reset inflate/SHA1 if there was an error reading the previous temp
1582 * file; also rewind to the beginning of the local file.
1584 if (prev_read
== -1) {
1585 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
1586 git_inflate_init(&freq
->stream
);
1587 git_SHA1_Init(&freq
->c
);
1590 lseek(freq
->localfile
, 0, SEEK_SET
);
1591 if (ftruncate(freq
->localfile
, 0) < 0) {
1592 error("Couldn't truncate temporary file %s: %s",
1593 freq
->tmpfile
, strerror(errno
));
1599 freq
->slot
= get_active_slot();
1601 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FILE
, freq
);
1602 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
1603 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
1604 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
1605 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
1608 * If we have successfully processed data from a previous fetch
1609 * attempt, only fetch the data we don't already have.
1612 if (http_is_verbose
)
1614 "Resuming fetch of object %s at byte %ld\n",
1616 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1617 range_header
= curl_slist_append(range_header
, range
);
1618 curl_easy_setopt(freq
->slot
->curl
,
1619 CURLOPT_HTTPHEADER
, range_header
);
1630 void process_http_object_request(struct http_object_request
*freq
)
1632 if (freq
->slot
== NULL
)
1634 freq
->curl_result
= freq
->slot
->curl_result
;
1635 freq
->http_code
= freq
->slot
->http_code
;
1639 int finish_http_object_request(struct http_object_request
*freq
)
1643 close(freq
->localfile
);
1644 freq
->localfile
= -1;
1646 process_http_object_request(freq
);
1648 if (freq
->http_code
== 416) {
1649 warning("requested range invalid; we may already have all the data.");
1650 } else if (freq
->curl_result
!= CURLE_OK
) {
1651 if (stat(freq
->tmpfile
, &st
) == 0)
1652 if (st
.st_size
== 0)
1653 unlink_or_warn(freq
->tmpfile
);
1657 git_inflate_end(&freq
->stream
);
1658 git_SHA1_Final(freq
->real_sha1
, &freq
->c
);
1659 if (freq
->zret
!= Z_STREAM_END
) {
1660 unlink_or_warn(freq
->tmpfile
);
1663 if (hashcmp(freq
->sha1
, freq
->real_sha1
)) {
1664 unlink_or_warn(freq
->tmpfile
);
1668 move_temp_to_file(freq
->tmpfile
, sha1_file_name(freq
->sha1
));
1670 return freq
->rename
;
1673 void abort_http_object_request(struct http_object_request
*freq
)
1675 unlink_or_warn(freq
->tmpfile
);
1677 release_http_object_request(freq
);
1680 void release_http_object_request(struct http_object_request
*freq
)
1682 if (freq
->localfile
!= -1) {
1683 close(freq
->localfile
);
1684 freq
->localfile
= -1;
1686 if (freq
->url
!= NULL
) {
1690 if (freq
->slot
!= NULL
) {
1691 freq
->slot
->callback_func
= NULL
;
1692 freq
->slot
->callback_data
= NULL
;
1693 release_active_slot(freq
->slot
);