4 int active_requests
= 0;
7 static int max_requests
= -1;
10 #ifndef NO_CURL_EASY_DUPHANDLE
11 static CURL
*curl_default
;
13 char curl_errorstr
[CURL_ERROR_SIZE
];
15 static int curl_ssl_verify
= -1;
16 static const char *ssl_cert
= NULL
;
17 #if LIBCURL_VERSION_NUM >= 0x070902
18 static const char *ssl_key
= NULL
;
20 #if LIBCURL_VERSION_NUM >= 0x070908
21 static const char *ssl_capath
= NULL
;
23 static const char *ssl_cainfo
= NULL
;
24 static long curl_low_speed_limit
= -1;
25 static long curl_low_speed_time
= -1;
26 static int curl_ftp_no_epsv
= 0;
27 static const char *curl_http_proxy
= NULL
;
29 static struct curl_slist
*pragma_header
;
31 static struct active_request_slot
*active_queue_head
= NULL
;
33 size_t fread_buffer(void *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
35 size_t size
= eltsize
* nmemb
;
36 struct buffer
*buffer
= buffer_
;
38 if (size
> buffer
->buf
.len
- buffer
->posn
)
39 size
= buffer
->buf
.len
- buffer
->posn
;
40 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
46 size_t fwrite_buffer(const void *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
48 size_t size
= eltsize
* nmemb
;
49 struct strbuf
*buffer
= buffer_
;
51 strbuf_add(buffer
, ptr
, size
);
56 size_t fwrite_null(const void *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
59 return eltsize
* nmemb
;
62 static void finish_active_slot(struct active_request_slot
*slot
);
65 static void process_curl_messages(void)
68 struct active_request_slot
*slot
;
69 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
71 while (curl_message
!= NULL
) {
72 if (curl_message
->msg
== CURLMSG_DONE
) {
73 int curl_result
= curl_message
->data
.result
;
74 slot
= active_queue_head
;
75 while (slot
!= NULL
&&
76 slot
->curl
!= curl_message
->easy_handle
)
79 curl_multi_remove_handle(curlm
, slot
->curl
);
80 slot
->curl_result
= curl_result
;
81 finish_active_slot(slot
);
83 fprintf(stderr
, "Received DONE message for unknown request!\n");
86 fprintf(stderr
, "Unknown CURL message received: %d\n",
87 (int)curl_message
->msg
);
89 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
94 static int http_options(const char *var
, const char *value
, void *cb
)
96 if (!strcmp("http.sslverify", var
)) {
97 if (curl_ssl_verify
== -1) {
98 curl_ssl_verify
= git_config_bool(var
, value
);
103 if (!strcmp("http.sslcert", var
)) {
104 if (ssl_cert
== NULL
)
105 return git_config_string(&ssl_cert
, var
, value
);
108 #if LIBCURL_VERSION_NUM >= 0x070902
109 if (!strcmp("http.sslkey", var
)) {
111 return git_config_string(&ssl_key
, var
, value
);
115 #if LIBCURL_VERSION_NUM >= 0x070908
116 if (!strcmp("http.sslcapath", var
)) {
117 if (ssl_capath
== NULL
)
118 return git_config_string(&ssl_capath
, var
, value
);
122 if (!strcmp("http.sslcainfo", var
)) {
123 if (ssl_cainfo
== NULL
)
124 return git_config_string(&ssl_cainfo
, var
, value
);
128 #ifdef USE_CURL_MULTI
129 if (!strcmp("http.maxrequests", var
)) {
130 if (max_requests
== -1)
131 max_requests
= git_config_int(var
, value
);
136 if (!strcmp("http.lowspeedlimit", var
)) {
137 if (curl_low_speed_limit
== -1)
138 curl_low_speed_limit
= (long)git_config_int(var
, value
);
141 if (!strcmp("http.lowspeedtime", var
)) {
142 if (curl_low_speed_time
== -1)
143 curl_low_speed_time
= (long)git_config_int(var
, value
);
147 if (!strcmp("http.noepsv", var
)) {
148 curl_ftp_no_epsv
= git_config_bool(var
, value
);
151 if (!strcmp("http.proxy", var
)) {
152 if (curl_http_proxy
== NULL
)
153 return git_config_string(&curl_http_proxy
, var
, value
);
157 /* Fall back on the default ones */
158 return git_default_config(var
, value
, cb
);
161 static CURL
* get_curl_handle(void)
163 CURL
* result
= curl_easy_init();
165 if (!curl_ssl_verify
) {
166 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
167 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
169 /* Verify authenticity of the peer's certificate */
170 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
171 /* The name in the cert must match whom we tried to connect */
172 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
175 #if LIBCURL_VERSION_NUM >= 0x070907
176 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
179 if (ssl_cert
!= NULL
)
180 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
181 #if LIBCURL_VERSION_NUM >= 0x070902
183 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
185 #if LIBCURL_VERSION_NUM >= 0x070908
186 if (ssl_capath
!= NULL
)
187 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
189 if (ssl_cainfo
!= NULL
)
190 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
191 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
193 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
194 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
195 curl_low_speed_limit
);
196 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
197 curl_low_speed_time
);
200 curl_easy_setopt(result
, CURLOPT_FOLLOWLOCATION
, 1);
202 if (getenv("GIT_CURL_VERBOSE"))
203 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
205 curl_easy_setopt(result
, CURLOPT_USERAGENT
, GIT_USER_AGENT
);
207 if (curl_ftp_no_epsv
)
208 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
211 curl_easy_setopt(result
, CURLOPT_PROXY
, curl_http_proxy
);
216 void http_init(struct remote
*remote
)
218 char *low_speed_limit
;
219 char *low_speed_time
;
221 curl_global_init(CURL_GLOBAL_ALL
);
223 if (remote
&& remote
->http_proxy
)
224 curl_http_proxy
= xstrdup(remote
->http_proxy
);
226 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
228 #ifdef USE_CURL_MULTI
230 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
231 if (http_max_requests
!= NULL
)
232 max_requests
= atoi(http_max_requests
);
235 curlm
= curl_multi_init();
237 fprintf(stderr
, "Error creating curl multi handle.\n");
242 if (getenv("GIT_SSL_NO_VERIFY"))
245 ssl_cert
= getenv("GIT_SSL_CERT");
246 #if LIBCURL_VERSION_NUM >= 0x070902
247 ssl_key
= getenv("GIT_SSL_KEY");
249 #if LIBCURL_VERSION_NUM >= 0x070908
250 ssl_capath
= getenv("GIT_SSL_CAPATH");
252 ssl_cainfo
= getenv("GIT_SSL_CAINFO");
254 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
255 if (low_speed_limit
!= NULL
)
256 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
257 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
258 if (low_speed_time
!= NULL
)
259 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
261 git_config(http_options
, NULL
);
263 if (curl_ssl_verify
== -1)
266 #ifdef USE_CURL_MULTI
267 if (max_requests
< 1)
268 max_requests
= DEFAULT_MAX_REQUESTS
;
271 if (getenv("GIT_CURL_FTP_NO_EPSV"))
272 curl_ftp_no_epsv
= 1;
274 #ifndef NO_CURL_EASY_DUPHANDLE
275 curl_default
= get_curl_handle();
279 void http_cleanup(void)
281 struct active_request_slot
*slot
= active_queue_head
;
283 while (slot
!= NULL
) {
284 struct active_request_slot
*next
= slot
->next
;
285 if (slot
->curl
!= NULL
) {
286 #ifdef USE_CURL_MULTI
287 curl_multi_remove_handle(curlm
, slot
->curl
);
289 curl_easy_cleanup(slot
->curl
);
294 active_queue_head
= NULL
;
296 #ifndef NO_CURL_EASY_DUPHANDLE
297 curl_easy_cleanup(curl_default
);
300 #ifdef USE_CURL_MULTI
301 curl_multi_cleanup(curlm
);
303 curl_global_cleanup();
305 curl_slist_free_all(pragma_header
);
306 pragma_header
= NULL
;
308 if (curl_http_proxy
) {
309 free((void *)curl_http_proxy
);
310 curl_http_proxy
= NULL
;
314 struct active_request_slot
*get_active_slot(void)
316 struct active_request_slot
*slot
= active_queue_head
;
317 struct active_request_slot
*newslot
;
319 #ifdef USE_CURL_MULTI
322 /* Wait for a slot to open up if the queue is full */
323 while (active_requests
>= max_requests
) {
324 curl_multi_perform(curlm
, &num_transfers
);
325 if (num_transfers
< active_requests
) {
326 process_curl_messages();
331 while (slot
!= NULL
&& slot
->in_use
) {
335 newslot
= xmalloc(sizeof(*newslot
));
336 newslot
->curl
= NULL
;
338 newslot
->next
= NULL
;
340 slot
= active_queue_head
;
342 active_queue_head
= newslot
;
344 while (slot
->next
!= NULL
) {
347 slot
->next
= newslot
;
352 if (slot
->curl
== NULL
) {
353 #ifdef NO_CURL_EASY_DUPHANDLE
354 slot
->curl
= get_curl_handle();
356 slot
->curl
= curl_easy_duphandle(curl_default
);
363 slot
->results
= NULL
;
364 slot
->finished
= NULL
;
365 slot
->callback_data
= NULL
;
366 slot
->callback_func
= NULL
;
367 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
368 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
369 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
370 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
371 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
372 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
373 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
378 int start_active_slot(struct active_request_slot
*slot
)
380 #ifdef USE_CURL_MULTI
381 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
384 if (curlm_result
!= CURLM_OK
&&
385 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
392 * We know there must be something to do, since we just added
395 curl_multi_perform(curlm
, &num_transfers
);
400 #ifdef USE_CURL_MULTI
404 struct fill_chain
*next
;
407 static struct fill_chain
*fill_cfg
= NULL
;
409 void add_fill_function(void *data
, int (*fill
)(void *))
411 struct fill_chain
*new = xmalloc(sizeof(*new));
412 struct fill_chain
**linkp
= &fill_cfg
;
417 linkp
= &(*linkp
)->next
;
421 void fill_active_slots(void)
423 struct active_request_slot
*slot
= active_queue_head
;
425 while (active_requests
< max_requests
) {
426 struct fill_chain
*fill
;
427 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
428 if (fill
->fill(fill
->data
))
435 while (slot
!= NULL
) {
436 if (!slot
->in_use
&& slot
->curl
!= NULL
) {
437 curl_easy_cleanup(slot
->curl
);
444 void step_active_slots(void)
447 CURLMcode curlm_result
;
450 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
451 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
452 if (num_transfers
< active_requests
) {
453 process_curl_messages();
459 void run_active_slot(struct active_request_slot
*slot
)
461 #ifdef USE_CURL_MULTI
468 struct timeval select_timeout
;
471 slot
->finished
= &finished
;
476 if (!data_received
&& slot
->local
!= NULL
) {
477 current_pos
= ftell(slot
->local
);
478 if (current_pos
> last_pos
)
480 last_pos
= current_pos
;
483 if (slot
->in_use
&& !data_received
) {
488 select_timeout
.tv_sec
= 0;
489 select_timeout
.tv_usec
= 50000;
490 select(max_fd
, &readfds
, &writefds
,
491 &excfds
, &select_timeout
);
495 while (slot
->in_use
) {
496 slot
->curl_result
= curl_easy_perform(slot
->curl
);
497 finish_active_slot(slot
);
502 static void closedown_active_slot(struct active_request_slot
*slot
)
508 void release_active_slot(struct active_request_slot
*slot
)
510 closedown_active_slot(slot
);
512 #ifdef USE_CURL_MULTI
513 curl_multi_remove_handle(curlm
, slot
->curl
);
515 curl_easy_cleanup(slot
->curl
);
518 #ifdef USE_CURL_MULTI
523 static void finish_active_slot(struct active_request_slot
*slot
)
525 closedown_active_slot(slot
);
526 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
528 if (slot
->finished
!= NULL
)
529 (*slot
->finished
) = 1;
531 /* Store slot results so they can be read after the slot is reused */
532 if (slot
->results
!= NULL
) {
533 slot
->results
->curl_result
= slot
->curl_result
;
534 slot
->results
->http_code
= slot
->http_code
;
537 /* Run callback if appropriate */
538 if (slot
->callback_func
!= NULL
) {
539 slot
->callback_func(slot
->callback_data
);
543 void finish_all_active_slots(void)
545 struct active_request_slot
*slot
= active_queue_head
;
549 run_active_slot(slot
);
550 slot
= active_queue_head
;
556 static inline int needs_quote(int ch
)
558 if (((ch
>= 'A') && (ch
<= 'Z'))
559 || ((ch
>= 'a') && (ch
<= 'z'))
560 || ((ch
>= '0') && (ch
<= '9'))
568 static inline int hex(int v
)
570 if (v
< 10) return '0' + v
;
571 else return 'A' + v
- 10;
574 static char *quote_ref_url(const char *base
, const char *ref
)
578 int len
, baselen
, ch
;
580 baselen
= strlen(base
);
581 len
= baselen
+ 2; /* '/' after base and terminating NUL */
582 for (cp
= ref
; (ch
= *cp
) != 0; cp
++, len
++)
584 len
+= 2; /* extra two hex plus replacement % */
586 memcpy(qref
, base
, baselen
);
589 for (cp
= ref
; (ch
= *cp
) != 0; cp
++) {
590 if (needs_quote(ch
)) {
592 *dp
++ = hex((ch
>> 4) & 0xF);
593 *dp
++ = hex(ch
& 0xF);
603 int http_fetch_ref(const char *base
, struct ref
*ref
)
606 struct strbuf buffer
= STRBUF_INIT
;
607 struct active_request_slot
*slot
;
608 struct slot_results results
;
611 url
= quote_ref_url(base
, ref
->name
);
612 slot
= get_active_slot();
613 slot
->results
= &results
;
614 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
615 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
616 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
617 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
618 if (start_active_slot(slot
)) {
619 run_active_slot(slot
);
620 if (results
.curl_result
== CURLE_OK
) {
621 strbuf_rtrim(&buffer
);
622 if (buffer
.len
== 40)
623 ret
= get_sha1_hex(buffer
.buf
, ref
->old_sha1
);
624 else if (!prefixcmp(buffer
.buf
, "ref: ")) {
625 ref
->symref
= xstrdup(buffer
.buf
+ 5);
630 ret
= error("Couldn't get %s for %s\n%s",
631 url
, ref
->name
, curl_errorstr
);
634 ret
= error("Unable to start request");
637 strbuf_release(&buffer
);