8 size_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
10 static int min_curl_sessions
= 1;
11 static int curl_session_count
;
13 static int max_requests
= -1;
16 #ifndef NO_CURL_EASY_DUPHANDLE
17 static CURL
*curl_default
;
20 #define PREV_BUF_SIZE 4096
21 #define RANGE_HEADER_SIZE 30
23 char curl_errorstr
[CURL_ERROR_SIZE
];
25 static int curl_ssl_verify
= -1;
26 static const char *ssl_cert
;
27 #if LIBCURL_VERSION_NUM >= 0x070903
28 static const char *ssl_key
;
30 #if LIBCURL_VERSION_NUM >= 0x070908
31 static const char *ssl_capath
;
33 static const char *ssl_cainfo
;
34 static long curl_low_speed_limit
= -1;
35 static long curl_low_speed_time
= -1;
36 static int curl_ftp_no_epsv
;
37 static const char *curl_http_proxy
;
38 static char *user_name
, *user_pass
;
40 #if LIBCURL_VERSION_NUM >= 0x071700
41 /* Use CURLOPT_KEYPASSWD as is */
42 #elif LIBCURL_VERSION_NUM >= 0x070903
43 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
45 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
48 static char *ssl_cert_password
;
49 static int ssl_cert_password_required
;
51 static struct curl_slist
*pragma_header
;
52 static struct curl_slist
*no_pragma_header
;
54 static struct active_request_slot
*active_queue_head
;
56 size_t fread_buffer(void *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
58 size_t size
= eltsize
* nmemb
;
59 struct buffer
*buffer
= buffer_
;
61 if (size
> buffer
->buf
.len
- buffer
->posn
)
62 size
= buffer
->buf
.len
- buffer
->posn
;
63 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
70 curlioerr
ioctl_buffer(CURL
*handle
, int cmd
, void *clientp
)
72 struct buffer
*buffer
= clientp
;
78 case CURLIOCMD_RESTARTREAD
:
83 return CURLIOE_UNKNOWNCMD
;
88 size_t fwrite_buffer(const void *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
90 size_t size
= eltsize
* nmemb
;
91 struct strbuf
*buffer
= buffer_
;
93 strbuf_add(buffer
, ptr
, size
);
98 size_t fwrite_null(const void *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
101 return eltsize
* nmemb
;
104 #ifdef USE_CURL_MULTI
105 static void process_curl_messages(void)
108 struct active_request_slot
*slot
;
109 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
111 while (curl_message
!= NULL
) {
112 if (curl_message
->msg
== CURLMSG_DONE
) {
113 int curl_result
= curl_message
->data
.result
;
114 slot
= active_queue_head
;
115 while (slot
!= NULL
&&
116 slot
->curl
!= curl_message
->easy_handle
)
119 curl_multi_remove_handle(curlm
, slot
->curl
);
120 slot
->curl_result
= curl_result
;
121 finish_active_slot(slot
);
123 fprintf(stderr
, "Received DONE message for unknown request!\n");
126 fprintf(stderr
, "Unknown CURL message received: %d\n",
127 (int)curl_message
->msg
);
129 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
134 static int http_options(const char *var
, const char *value
, void *cb
)
136 if (!strcmp("http.sslverify", var
)) {
137 curl_ssl_verify
= git_config_bool(var
, value
);
140 if (!strcmp("http.sslcert", var
))
141 return git_config_string(&ssl_cert
, var
, value
);
142 #if LIBCURL_VERSION_NUM >= 0x070903
143 if (!strcmp("http.sslkey", var
))
144 return git_config_string(&ssl_key
, var
, value
);
146 #if LIBCURL_VERSION_NUM >= 0x070908
147 if (!strcmp("http.sslcapath", var
))
148 return git_config_string(&ssl_capath
, var
, value
);
150 if (!strcmp("http.sslcainfo", var
))
151 return git_config_string(&ssl_cainfo
, var
, value
);
152 if (!strcmp("http.sslcertpasswordprotected", var
)) {
153 if (git_config_bool(var
, value
))
154 ssl_cert_password_required
= 1;
157 if (!strcmp("http.minsessions", var
)) {
158 min_curl_sessions
= git_config_int(var
, value
);
159 #ifndef USE_CURL_MULTI
160 if (min_curl_sessions
> 1)
161 min_curl_sessions
= 1;
165 #ifdef USE_CURL_MULTI
166 if (!strcmp("http.maxrequests", var
)) {
167 max_requests
= git_config_int(var
, value
);
171 if (!strcmp("http.lowspeedlimit", var
)) {
172 curl_low_speed_limit
= (long)git_config_int(var
, value
);
175 if (!strcmp("http.lowspeedtime", var
)) {
176 curl_low_speed_time
= (long)git_config_int(var
, value
);
180 if (!strcmp("http.noepsv", var
)) {
181 curl_ftp_no_epsv
= git_config_bool(var
, value
);
184 if (!strcmp("http.proxy", var
))
185 return git_config_string(&curl_http_proxy
, var
, value
);
187 if (!strcmp("http.postbuffer", var
)) {
188 http_post_buffer
= git_config_int(var
, value
);
189 if (http_post_buffer
< LARGE_PACKET_MAX
)
190 http_post_buffer
= LARGE_PACKET_MAX
;
194 /* Fall back on the default ones */
195 return git_default_config(var
, value
, cb
);
198 static void init_curl_http_auth(CURL
*result
)
201 struct strbuf up
= STRBUF_INIT
;
203 user_pass
= xstrdup(getpass("Password: "));
204 strbuf_addf(&up
, "%s:%s", user_name
, user_pass
);
205 curl_easy_setopt(result
, CURLOPT_USERPWD
,
206 strbuf_detach(&up
, NULL
));
210 static int has_cert_password(void)
212 if (ssl_cert_password
!= NULL
)
214 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
216 /* Only prompt the user once. */
217 ssl_cert_password_required
= -1;
218 ssl_cert_password
= getpass("Certificate Password: ");
219 if (ssl_cert_password
!= NULL
) {
220 ssl_cert_password
= xstrdup(ssl_cert_password
);
226 static CURL
*get_curl_handle(void)
228 CURL
*result
= curl_easy_init();
230 if (!curl_ssl_verify
) {
231 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
232 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
234 /* Verify authenticity of the peer's certificate */
235 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
236 /* The name in the cert must match whom we tried to connect */
237 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
240 #if LIBCURL_VERSION_NUM >= 0x070907
241 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
244 init_curl_http_auth(result
);
246 if (ssl_cert
!= NULL
)
247 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
248 if (has_cert_password())
249 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, ssl_cert_password
);
250 #if LIBCURL_VERSION_NUM >= 0x070903
252 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
254 #if LIBCURL_VERSION_NUM >= 0x070908
255 if (ssl_capath
!= NULL
)
256 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
258 if (ssl_cainfo
!= NULL
)
259 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
260 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
262 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
263 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
264 curl_low_speed_limit
);
265 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
266 curl_low_speed_time
);
269 curl_easy_setopt(result
, CURLOPT_FOLLOWLOCATION
, 1);
271 if (getenv("GIT_CURL_VERBOSE"))
272 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
274 curl_easy_setopt(result
, CURLOPT_USERAGENT
, GIT_USER_AGENT
);
276 if (curl_ftp_no_epsv
)
277 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
280 curl_easy_setopt(result
, CURLOPT_PROXY
, curl_http_proxy
);
285 static void http_auth_init(const char *url
)
287 char *at
, *colon
, *cp
, *slash
;
290 cp
= strstr(url
, "://");
295 * Ok, the URL looks like "proto://something". Which one?
296 * "proto://<user>:<pass>@<host>/...",
297 * "proto://<user>@<host>/...", or just
298 * "proto://<host>/..."?
301 at
= strchr(cp
, '@');
302 colon
= strchr(cp
, ':');
303 slash
= strchrnul(cp
, '/');
304 if (!at
|| slash
<= at
)
305 return; /* No credentials */
306 if (!colon
|| at
<= colon
) {
309 user_name
= xmalloc(len
+ 1);
310 memcpy(user_name
, cp
, len
);
311 user_name
[len
] = '\0';
315 user_name
= xmalloc(len
+ 1);
316 memcpy(user_name
, cp
, len
);
317 user_name
[len
] = '\0';
318 len
= at
- (colon
+ 1);
319 user_pass
= xmalloc(len
+ 1);
320 memcpy(user_pass
, colon
+ 1, len
);
321 user_pass
[len
] = '\0';
325 static void set_from_env(const char **var
, const char *envname
)
327 const char *val
= getenv(envname
);
332 void http_init(struct remote
*remote
)
334 char *low_speed_limit
;
335 char *low_speed_time
;
339 git_config(http_options
, NULL
);
341 curl_global_init(CURL_GLOBAL_ALL
);
343 if (remote
&& remote
->http_proxy
)
344 curl_http_proxy
= xstrdup(remote
->http_proxy
);
346 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
347 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
349 #ifdef USE_CURL_MULTI
351 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
352 if (http_max_requests
!= NULL
)
353 max_requests
= atoi(http_max_requests
);
356 curlm
= curl_multi_init();
358 fprintf(stderr
, "Error creating curl multi handle.\n");
363 if (getenv("GIT_SSL_NO_VERIFY"))
366 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
367 #if LIBCURL_VERSION_NUM >= 0x070903
368 set_from_env(&ssl_key
, "GIT_SSL_KEY");
370 #if LIBCURL_VERSION_NUM >= 0x070908
371 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
373 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
375 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
376 if (low_speed_limit
!= NULL
)
377 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
378 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
379 if (low_speed_time
!= NULL
)
380 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
382 if (curl_ssl_verify
== -1)
385 curl_session_count
= 0;
386 #ifdef USE_CURL_MULTI
387 if (max_requests
< 1)
388 max_requests
= DEFAULT_MAX_REQUESTS
;
391 if (getenv("GIT_CURL_FTP_NO_EPSV"))
392 curl_ftp_no_epsv
= 1;
394 if (remote
&& remote
->url
&& remote
->url
[0]) {
395 http_auth_init(remote
->url
[0]);
396 if (!ssl_cert_password_required
&&
397 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
398 !prefixcmp(remote
->url
[0], "https://"))
399 ssl_cert_password_required
= 1;
402 #ifndef NO_CURL_EASY_DUPHANDLE
403 curl_default
= get_curl_handle();
407 void http_cleanup(void)
409 struct active_request_slot
*slot
= active_queue_head
;
411 while (slot
!= NULL
) {
412 struct active_request_slot
*next
= slot
->next
;
413 if (slot
->curl
!= NULL
) {
414 #ifdef USE_CURL_MULTI
415 curl_multi_remove_handle(curlm
, slot
->curl
);
417 curl_easy_cleanup(slot
->curl
);
422 active_queue_head
= NULL
;
424 #ifndef NO_CURL_EASY_DUPHANDLE
425 curl_easy_cleanup(curl_default
);
428 #ifdef USE_CURL_MULTI
429 curl_multi_cleanup(curlm
);
431 curl_global_cleanup();
433 curl_slist_free_all(pragma_header
);
434 pragma_header
= NULL
;
436 curl_slist_free_all(no_pragma_header
);
437 no_pragma_header
= NULL
;
439 if (curl_http_proxy
) {
440 free((void *)curl_http_proxy
);
441 curl_http_proxy
= NULL
;
444 if (ssl_cert_password
!= NULL
) {
445 memset(ssl_cert_password
, 0, strlen(ssl_cert_password
));
446 free(ssl_cert_password
);
447 ssl_cert_password
= NULL
;
449 ssl_cert_password_required
= 0;
452 struct active_request_slot
*get_active_slot(void)
454 struct active_request_slot
*slot
= active_queue_head
;
455 struct active_request_slot
*newslot
;
457 #ifdef USE_CURL_MULTI
460 /* Wait for a slot to open up if the queue is full */
461 while (active_requests
>= max_requests
) {
462 curl_multi_perform(curlm
, &num_transfers
);
463 if (num_transfers
< active_requests
)
464 process_curl_messages();
468 while (slot
!= NULL
&& slot
->in_use
)
472 newslot
= xmalloc(sizeof(*newslot
));
473 newslot
->curl
= NULL
;
475 newslot
->next
= NULL
;
477 slot
= active_queue_head
;
479 active_queue_head
= newslot
;
481 while (slot
->next
!= NULL
)
483 slot
->next
= newslot
;
488 if (slot
->curl
== NULL
) {
489 #ifdef NO_CURL_EASY_DUPHANDLE
490 slot
->curl
= get_curl_handle();
492 slot
->curl
= curl_easy_duphandle(curl_default
);
494 curl_session_count
++;
500 slot
->results
= NULL
;
501 slot
->finished
= NULL
;
502 slot
->callback_data
= NULL
;
503 slot
->callback_func
= NULL
;
504 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
505 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
506 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
507 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
508 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
509 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
510 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
515 int start_active_slot(struct active_request_slot
*slot
)
517 #ifdef USE_CURL_MULTI
518 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
521 if (curlm_result
!= CURLM_OK
&&
522 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
529 * We know there must be something to do, since we just added
532 curl_multi_perform(curlm
, &num_transfers
);
537 #ifdef USE_CURL_MULTI
541 struct fill_chain
*next
;
544 static struct fill_chain
*fill_cfg
;
546 void add_fill_function(void *data
, int (*fill
)(void *))
548 struct fill_chain
*new = xmalloc(sizeof(*new));
549 struct fill_chain
**linkp
= &fill_cfg
;
554 linkp
= &(*linkp
)->next
;
558 void fill_active_slots(void)
560 struct active_request_slot
*slot
= active_queue_head
;
562 while (active_requests
< max_requests
) {
563 struct fill_chain
*fill
;
564 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
565 if (fill
->fill(fill
->data
))
572 while (slot
!= NULL
) {
573 if (!slot
->in_use
&& slot
->curl
!= NULL
574 && curl_session_count
> min_curl_sessions
) {
575 curl_easy_cleanup(slot
->curl
);
577 curl_session_count
--;
583 void step_active_slots(void)
586 CURLMcode curlm_result
;
589 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
590 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
591 if (num_transfers
< active_requests
) {
592 process_curl_messages();
598 void run_active_slot(struct active_request_slot
*slot
)
600 #ifdef USE_CURL_MULTI
607 struct timeval select_timeout
;
610 slot
->finished
= &finished
;
615 if (!data_received
&& slot
->local
!= NULL
) {
616 current_pos
= ftell(slot
->local
);
617 if (current_pos
> last_pos
)
619 last_pos
= current_pos
;
622 if (slot
->in_use
&& !data_received
) {
627 select_timeout
.tv_sec
= 0;
628 select_timeout
.tv_usec
= 50000;
629 select(max_fd
, &readfds
, &writefds
,
630 &excfds
, &select_timeout
);
634 while (slot
->in_use
) {
635 slot
->curl_result
= curl_easy_perform(slot
->curl
);
636 finish_active_slot(slot
);
641 static void closedown_active_slot(struct active_request_slot
*slot
)
647 void release_active_slot(struct active_request_slot
*slot
)
649 closedown_active_slot(slot
);
650 if (slot
->curl
&& curl_session_count
> min_curl_sessions
) {
651 #ifdef USE_CURL_MULTI
652 curl_multi_remove_handle(curlm
, slot
->curl
);
654 curl_easy_cleanup(slot
->curl
);
656 curl_session_count
--;
658 #ifdef USE_CURL_MULTI
663 void finish_active_slot(struct active_request_slot
*slot
)
665 closedown_active_slot(slot
);
666 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
668 if (slot
->finished
!= NULL
)
669 (*slot
->finished
) = 1;
671 /* Store slot results so they can be read after the slot is reused */
672 if (slot
->results
!= NULL
) {
673 slot
->results
->curl_result
= slot
->curl_result
;
674 slot
->results
->http_code
= slot
->http_code
;
677 /* Run callback if appropriate */
678 if (slot
->callback_func
!= NULL
)
679 slot
->callback_func(slot
->callback_data
);
682 void finish_all_active_slots(void)
684 struct active_request_slot
*slot
= active_queue_head
;
688 run_active_slot(slot
);
689 slot
= active_queue_head
;
695 /* Helpers for modifying and creating URLs */
696 static inline int needs_quote(int ch
)
698 if (((ch
>= 'A') && (ch
<= 'Z'))
699 || ((ch
>= 'a') && (ch
<= 'z'))
700 || ((ch
>= '0') && (ch
<= '9'))
708 static inline int hex(int v
)
716 static void end_url_with_slash(struct strbuf
*buf
, const char *url
)
718 strbuf_addstr(buf
, url
);
719 if (buf
->len
&& buf
->buf
[buf
->len
- 1] != '/')
720 strbuf_addstr(buf
, "/");
723 static char *quote_ref_url(const char *base
, const char *ref
)
725 struct strbuf buf
= STRBUF_INIT
;
729 end_url_with_slash(&buf
, base
);
731 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
733 strbuf_addf(&buf
, "%%%02x", ch
);
735 strbuf_addch(&buf
, *cp
);
737 return strbuf_detach(&buf
, NULL
);
740 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
742 int only_two_digit_prefix
)
744 end_url_with_slash(buf
, url
);
746 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
747 if (!only_two_digit_prefix
)
748 strbuf_addf(buf
, "%s", hex
+2);
751 char *get_remote_object_url(const char *url
, const char *hex
,
752 int only_two_digit_prefix
)
754 struct strbuf buf
= STRBUF_INIT
;
755 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
756 return strbuf_detach(&buf
, NULL
);
759 /* http_request() targets */
760 #define HTTP_REQUEST_STRBUF 0
761 #define HTTP_REQUEST_FILE 1
763 static int http_request(const char *url
, void *result
, int target
, int options
)
765 struct active_request_slot
*slot
;
766 struct slot_results results
;
767 struct curl_slist
*headers
= NULL
;
768 struct strbuf buf
= STRBUF_INIT
;
771 slot
= get_active_slot();
772 slot
->results
= &results
;
773 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
775 if (result
== NULL
) {
776 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
778 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
779 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, result
);
781 if (target
== HTTP_REQUEST_FILE
) {
782 long posn
= ftell(result
);
783 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
786 strbuf_addf(&buf
, "Range: bytes=%ld-", posn
);
787 headers
= curl_slist_append(headers
, buf
.buf
);
790 slot
->local
= result
;
792 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
796 strbuf_addstr(&buf
, "Pragma:");
797 if (options
& HTTP_NO_CACHE
)
798 strbuf_addstr(&buf
, " no-cache");
800 headers
= curl_slist_append(headers
, buf
.buf
);
802 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
803 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
805 if (start_active_slot(slot
)) {
806 run_active_slot(slot
);
807 if (results
.curl_result
== CURLE_OK
)
809 else if (missing_target(&results
))
810 ret
= HTTP_MISSING_TARGET
;
814 error("Unable to start HTTP request for %s", url
);
815 ret
= HTTP_START_FAILED
;
819 curl_slist_free_all(headers
);
820 strbuf_release(&buf
);
825 int http_get_strbuf(const char *url
, struct strbuf
*result
, int options
)
827 return http_request(url
, result
, HTTP_REQUEST_STRBUF
, options
);
830 int http_get_file(const char *url
, const char *filename
, int options
)
833 struct strbuf tmpfile
= STRBUF_INIT
;
836 strbuf_addf(&tmpfile
, "%s.temp", filename
);
837 result
= fopen(tmpfile
.buf
, "a");
839 error("Unable to open local file %s", tmpfile
.buf
);
844 ret
= http_request(url
, result
, HTTP_REQUEST_FILE
, options
);
847 if ((ret
== HTTP_OK
) && move_temp_to_file(tmpfile
.buf
, filename
))
850 strbuf_release(&tmpfile
);
854 int http_error(const char *url
, int ret
)
856 /* http_request has already handled HTTP_START_FAILED. */
857 if (ret
!= HTTP_START_FAILED
)
858 error("%s while accessing %s\n", curl_errorstr
, url
);
863 int http_fetch_ref(const char *base
, struct ref
*ref
)
866 struct strbuf buffer
= STRBUF_INIT
;
869 url
= quote_ref_url(base
, ref
->name
);
870 if (http_get_strbuf(url
, &buffer
, HTTP_NO_CACHE
) == HTTP_OK
) {
871 strbuf_rtrim(&buffer
);
872 if (buffer
.len
== 40)
873 ret
= get_sha1_hex(buffer
.buf
, ref
->old_sha1
);
874 else if (!prefixcmp(buffer
.buf
, "ref: ")) {
875 ref
->symref
= xstrdup(buffer
.buf
+ 5);
880 strbuf_release(&buffer
);
885 /* Helpers for fetching packs */
886 static int fetch_pack_index(unsigned char *sha1
, const char *base_url
)
889 char *hex
= xstrdup(sha1_to_hex(sha1
));
892 struct strbuf buf
= STRBUF_INIT
;
894 if (has_pack_index(sha1
)) {
900 fprintf(stderr
, "Getting index for pack %s\n", hex
);
902 end_url_with_slash(&buf
, base_url
);
903 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", hex
);
904 url
= strbuf_detach(&buf
, NULL
);
906 filename
= sha1_pack_index_name(sha1
);
907 if (http_get_file(url
, filename
, 0) != HTTP_OK
)
908 ret
= error("Unable to get pack index %s\n", url
);
916 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
917 unsigned char *sha1
, const char *base_url
)
919 struct packed_git
*new_pack
;
921 if (fetch_pack_index(sha1
, base_url
))
924 new_pack
= parse_pack_index(sha1
);
926 return -1; /* parse_pack_index() already issued error message */
927 new_pack
->next
= *packs_head
;
928 *packs_head
= new_pack
;
932 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
936 struct strbuf buf
= STRBUF_INIT
;
937 unsigned char sha1
[20];
939 end_url_with_slash(&buf
, base_url
);
940 strbuf_addstr(&buf
, "objects/info/packs");
941 url
= strbuf_detach(&buf
, NULL
);
943 ret
= http_get_strbuf(url
, &buf
, HTTP_NO_CACHE
);
948 while (i
< buf
.len
) {
952 if (i
+ 52 <= buf
.len
&&
953 !prefixcmp(data
+ i
, " pack-") &&
954 !prefixcmp(data
+ i
+ 46, ".pack\n")) {
955 get_sha1_hex(data
+ i
+ 6, sha1
);
956 fetch_and_setup_pack_index(packs_head
, sha1
,
962 while (i
< buf
.len
&& data
[i
] != '\n')
973 void release_http_pack_request(struct http_pack_request
*preq
)
975 if (preq
->packfile
!= NULL
) {
976 fclose(preq
->packfile
);
977 preq
->packfile
= NULL
;
978 preq
->slot
->local
= NULL
;
980 if (preq
->range_header
!= NULL
) {
981 curl_slist_free_all(preq
->range_header
);
982 preq
->range_header
= NULL
;
988 int finish_http_pack_request(struct http_pack_request
*preq
)
991 struct packed_git
**lst
;
993 preq
->target
->pack_size
= ftell(preq
->packfile
);
995 if (preq
->packfile
!= NULL
) {
996 fclose(preq
->packfile
);
997 preq
->packfile
= NULL
;
998 preq
->slot
->local
= NULL
;
1001 ret
= move_temp_to_file(preq
->tmpfile
, preq
->filename
);
1006 while (*lst
!= preq
->target
)
1007 lst
= &((*lst
)->next
);
1008 *lst
= (*lst
)->next
;
1010 if (verify_pack(preq
->target
))
1012 install_packed_git(preq
->target
);
1017 struct http_pack_request
*new_http_pack_request(
1018 struct packed_git
*target
, const char *base_url
)
1022 char range
[RANGE_HEADER_SIZE
];
1023 struct strbuf buf
= STRBUF_INIT
;
1024 struct http_pack_request
*preq
;
1026 preq
= xmalloc(sizeof(*preq
));
1027 preq
->target
= target
;
1028 preq
->range_header
= NULL
;
1030 end_url_with_slash(&buf
, base_url
);
1031 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
1032 sha1_to_hex(target
->sha1
));
1033 preq
->url
= strbuf_detach(&buf
, NULL
);
1035 filename
= sha1_pack_name(target
->sha1
);
1036 snprintf(preq
->filename
, sizeof(preq
->filename
), "%s", filename
);
1037 snprintf(preq
->tmpfile
, sizeof(preq
->tmpfile
), "%s.temp", filename
);
1038 preq
->packfile
= fopen(preq
->tmpfile
, "a");
1039 if (!preq
->packfile
) {
1040 error("Unable to open local file %s for pack",
1045 preq
->slot
= get_active_slot();
1046 preq
->slot
->local
= preq
->packfile
;
1047 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_FILE
, preq
->packfile
);
1048 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
1049 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
1050 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1054 * If there is data present from a previous transfer attempt,
1055 * resume where it left off
1057 prev_posn
= ftell(preq
->packfile
);
1059 if (http_is_verbose
)
1061 "Resuming fetch of pack %s at byte %ld\n",
1062 sha1_to_hex(target
->sha1
), prev_posn
);
1063 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1064 preq
->range_header
= curl_slist_append(NULL
, range
);
1065 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1066 preq
->range_header
);
1078 /* Helpers for fetching objects (loose) */
1079 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
1082 unsigned char expn
[4096];
1083 size_t size
= eltsize
* nmemb
;
1085 struct http_object_request
*freq
=
1086 (struct http_object_request
*)data
;
1088 ssize_t retval
= xwrite(freq
->localfile
,
1089 (char *) ptr
+ posn
, size
- posn
);
1093 } while (posn
< size
);
1095 freq
->stream
.avail_in
= size
;
1096 freq
->stream
.next_in
= ptr
;
1098 freq
->stream
.next_out
= expn
;
1099 freq
->stream
.avail_out
= sizeof(expn
);
1100 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
1101 git_SHA1_Update(&freq
->c
, expn
,
1102 sizeof(expn
) - freq
->stream
.avail_out
);
1103 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
1108 struct http_object_request
*new_http_object_request(const char *base_url
,
1109 unsigned char *sha1
)
1111 char *hex
= sha1_to_hex(sha1
);
1113 char prevfile
[PATH_MAX
];
1115 unsigned char prev_buf
[PREV_BUF_SIZE
];
1116 ssize_t prev_read
= 0;
1118 char range
[RANGE_HEADER_SIZE
];
1119 struct curl_slist
*range_header
= NULL
;
1120 struct http_object_request
*freq
;
1122 freq
= xmalloc(sizeof(*freq
));
1123 hashcpy(freq
->sha1
, sha1
);
1124 freq
->localfile
= -1;
1126 filename
= sha1_file_name(sha1
);
1127 snprintf(freq
->filename
, sizeof(freq
->filename
), "%s", filename
);
1128 snprintf(freq
->tmpfile
, sizeof(freq
->tmpfile
),
1129 "%s.temp", filename
);
1131 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", filename
);
1132 unlink_or_warn(prevfile
);
1133 rename(freq
->tmpfile
, prevfile
);
1134 unlink_or_warn(freq
->tmpfile
);
1136 if (freq
->localfile
!= -1)
1137 error("fd leakage in start: %d", freq
->localfile
);
1138 freq
->localfile
= open(freq
->tmpfile
,
1139 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1141 * This could have failed due to the "lazy directory creation";
1142 * try to mkdir the last path component.
1144 if (freq
->localfile
< 0 && errno
== ENOENT
) {
1145 char *dir
= strrchr(freq
->tmpfile
, '/');
1148 mkdir(freq
->tmpfile
, 0777);
1151 freq
->localfile
= open(freq
->tmpfile
,
1152 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1155 if (freq
->localfile
< 0) {
1156 error("Couldn't create temporary file %s for %s: %s",
1157 freq
->tmpfile
, freq
->filename
, strerror(errno
));
1161 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
1163 git_inflate_init(&freq
->stream
);
1165 git_SHA1_Init(&freq
->c
);
1167 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
1170 * If a previous temp file is present, process what was already
1173 prevlocal
= open(prevfile
, O_RDONLY
);
1174 if (prevlocal
!= -1) {
1176 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
1178 if (fwrite_sha1_file(prev_buf
,
1181 freq
) == prev_read
) {
1182 prev_posn
+= prev_read
;
1187 } while (prev_read
> 0);
1190 unlink_or_warn(prevfile
);
1193 * Reset inflate/SHA1 if there was an error reading the previous temp
1194 * file; also rewind to the beginning of the local file.
1196 if (prev_read
== -1) {
1197 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
1198 git_inflate_init(&freq
->stream
);
1199 git_SHA1_Init(&freq
->c
);
1202 lseek(freq
->localfile
, 0, SEEK_SET
);
1203 if (ftruncate(freq
->localfile
, 0) < 0) {
1204 error("Couldn't truncate temporary file %s for %s: %s",
1205 freq
->tmpfile
, freq
->filename
, strerror(errno
));
1211 freq
->slot
= get_active_slot();
1213 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FILE
, freq
);
1214 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
1215 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
1216 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
1217 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
1220 * If we have successfully processed data from a previous fetch
1221 * attempt, only fetch the data we don't already have.
1224 if (http_is_verbose
)
1226 "Resuming fetch of object %s at byte %ld\n",
1228 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1229 range_header
= curl_slist_append(range_header
, range
);
1230 curl_easy_setopt(freq
->slot
->curl
,
1231 CURLOPT_HTTPHEADER
, range_header
);
1243 void process_http_object_request(struct http_object_request
*freq
)
1245 if (freq
->slot
== NULL
)
1247 freq
->curl_result
= freq
->slot
->curl_result
;
1248 freq
->http_code
= freq
->slot
->http_code
;
1252 int finish_http_object_request(struct http_object_request
*freq
)
1256 close(freq
->localfile
);
1257 freq
->localfile
= -1;
1259 process_http_object_request(freq
);
1261 if (freq
->http_code
== 416) {
1262 fprintf(stderr
, "Warning: requested range invalid; we may already have all the data.\n");
1263 } else if (freq
->curl_result
!= CURLE_OK
) {
1264 if (stat(freq
->tmpfile
, &st
) == 0)
1265 if (st
.st_size
== 0)
1266 unlink_or_warn(freq
->tmpfile
);
1270 git_inflate_end(&freq
->stream
);
1271 git_SHA1_Final(freq
->real_sha1
, &freq
->c
);
1272 if (freq
->zret
!= Z_STREAM_END
) {
1273 unlink_or_warn(freq
->tmpfile
);
1276 if (hashcmp(freq
->sha1
, freq
->real_sha1
)) {
1277 unlink_or_warn(freq
->tmpfile
);
1281 move_temp_to_file(freq
->tmpfile
, freq
->filename
);
1283 return freq
->rename
;
1286 void abort_http_object_request(struct http_object_request
*freq
)
1288 unlink_or_warn(freq
->tmpfile
);
1290 release_http_object_request(freq
);
1293 void release_http_object_request(struct http_object_request
*freq
)
1295 if (freq
->localfile
!= -1) {
1296 close(freq
->localfile
);
1297 freq
->localfile
= -1;
1299 if (freq
->url
!= NULL
) {
1303 if (freq
->slot
!= NULL
) {
1304 freq
->slot
->callback_func
= NULL
;
1305 freq
->slot
->callback_data
= NULL
;
1306 release_active_slot(freq
->slot
);