4 int active_requests
= 0;
10 #ifndef NO_CURL_EASY_DUPHANDLE
13 char curl_errorstr
[CURL_ERROR_SIZE
];
15 int curl_ssl_verify
= -1;
16 char *ssl_cert
= NULL
;
17 #if LIBCURL_VERSION_NUM >= 0x070902
20 #if LIBCURL_VERSION_NUM >= 0x070908
21 char *ssl_capath
= NULL
;
23 char *ssl_cainfo
= NULL
;
24 long curl_low_speed_limit
= -1;
25 long curl_low_speed_time
= -1;
26 int curl_ftp_no_epsv
= 0;
27 char *curl_http_proxy
= NULL
;
29 struct curl_slist
*pragma_header
;
31 struct active_request_slot
*active_queue_head
= NULL
;
33 size_t fread_buffer(void *ptr
, size_t eltsize
, size_t nmemb
,
34 struct buffer
*buffer
)
36 size_t size
= eltsize
* nmemb
;
37 if (size
> buffer
->size
- buffer
->posn
)
38 size
= buffer
->size
- buffer
->posn
;
39 memcpy(ptr
, (char *) buffer
->buffer
+ buffer
->posn
, size
);
44 size_t fwrite_buffer(const void *ptr
, size_t eltsize
,
45 size_t nmemb
, struct buffer
*buffer
)
47 size_t size
= eltsize
* nmemb
;
48 if (size
> buffer
->size
- buffer
->posn
) {
49 buffer
->size
= buffer
->size
* 3 / 2;
50 if (buffer
->size
< buffer
->posn
+ size
)
51 buffer
->size
= buffer
->posn
+ size
;
52 buffer
->buffer
= xrealloc(buffer
->buffer
, buffer
->size
);
54 memcpy((char *) buffer
->buffer
+ buffer
->posn
, ptr
, size
);
60 size_t fwrite_null(const void *ptr
, size_t eltsize
,
61 size_t nmemb
, struct buffer
*buffer
)
64 return eltsize
* nmemb
;
67 static void finish_active_slot(struct active_request_slot
*slot
);
70 static void process_curl_messages(void)
73 struct active_request_slot
*slot
;
74 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
76 while (curl_message
!= NULL
) {
77 if (curl_message
->msg
== CURLMSG_DONE
) {
78 int curl_result
= curl_message
->data
.result
;
79 slot
= active_queue_head
;
80 while (slot
!= NULL
&&
81 slot
->curl
!= curl_message
->easy_handle
)
84 curl_multi_remove_handle(curlm
, slot
->curl
);
85 slot
->curl_result
= curl_result
;
86 finish_active_slot(slot
);
88 fprintf(stderr
, "Received DONE message for unknown request!\n");
91 fprintf(stderr
, "Unknown CURL message received: %d\n",
92 (int)curl_message
->msg
);
94 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
99 static int http_options(const char *var
, const char *value
)
101 if (!strcmp("http.sslverify", var
)) {
102 if (curl_ssl_verify
== -1) {
103 curl_ssl_verify
= git_config_bool(var
, value
);
108 if (!strcmp("http.sslcert", var
)) {
109 if (ssl_cert
== NULL
) {
110 ssl_cert
= xmalloc(strlen(value
)+1);
111 strcpy(ssl_cert
, value
);
115 #if LIBCURL_VERSION_NUM >= 0x070902
116 if (!strcmp("http.sslkey", var
)) {
117 if (ssl_key
== NULL
) {
118 ssl_key
= xmalloc(strlen(value
)+1);
119 strcpy(ssl_key
, value
);
124 #if LIBCURL_VERSION_NUM >= 0x070908
125 if (!strcmp("http.sslcapath", var
)) {
126 if (ssl_capath
== NULL
) {
127 ssl_capath
= xmalloc(strlen(value
)+1);
128 strcpy(ssl_capath
, value
);
133 if (!strcmp("http.sslcainfo", var
)) {
134 if (ssl_cainfo
== NULL
) {
135 ssl_cainfo
= xmalloc(strlen(value
)+1);
136 strcpy(ssl_cainfo
, value
);
141 #ifdef USE_CURL_MULTI
142 if (!strcmp("http.maxrequests", var
)) {
143 if (max_requests
== -1)
144 max_requests
= git_config_int(var
, value
);
149 if (!strcmp("http.lowspeedlimit", var
)) {
150 if (curl_low_speed_limit
== -1)
151 curl_low_speed_limit
= (long)git_config_int(var
, value
);
154 if (!strcmp("http.lowspeedtime", var
)) {
155 if (curl_low_speed_time
== -1)
156 curl_low_speed_time
= (long)git_config_int(var
, value
);
160 if (!strcmp("http.noepsv", var
)) {
161 curl_ftp_no_epsv
= git_config_bool(var
, value
);
164 if (!strcmp("http.proxy", var
)) {
165 if (curl_http_proxy
== NULL
) {
166 curl_http_proxy
= xmalloc(strlen(value
)+1);
167 strcpy(curl_http_proxy
, value
);
172 /* Fall back on the default ones */
173 return git_default_config(var
, value
);
176 static CURL
* get_curl_handle(void)
178 CURL
* result
= curl_easy_init();
180 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
181 #if LIBCURL_VERSION_NUM >= 0x070907
182 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
185 if (ssl_cert
!= NULL
)
186 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
187 #if LIBCURL_VERSION_NUM >= 0x070902
189 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
191 #if LIBCURL_VERSION_NUM >= 0x070908
192 if (ssl_capath
!= NULL
)
193 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
195 if (ssl_cainfo
!= NULL
)
196 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
197 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
199 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
200 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
201 curl_low_speed_limit
);
202 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
203 curl_low_speed_time
);
206 curl_easy_setopt(result
, CURLOPT_FOLLOWLOCATION
, 1);
208 if (getenv("GIT_CURL_VERBOSE"))
209 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
211 curl_easy_setopt(result
, CURLOPT_USERAGENT
, GIT_USER_AGENT
);
213 if (curl_ftp_no_epsv
)
214 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
217 curl_easy_setopt(result
, CURLOPT_PROXY
, curl_http_proxy
);
224 char *low_speed_limit
;
225 char *low_speed_time
;
227 curl_global_init(CURL_GLOBAL_ALL
);
229 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
231 #ifdef USE_CURL_MULTI
233 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
234 if (http_max_requests
!= NULL
)
235 max_requests
= atoi(http_max_requests
);
238 curlm
= curl_multi_init();
240 fprintf(stderr
, "Error creating curl multi handle.\n");
245 if (getenv("GIT_SSL_NO_VERIFY"))
248 ssl_cert
= getenv("GIT_SSL_CERT");
249 #if LIBCURL_VERSION_NUM >= 0x070902
250 ssl_key
= getenv("GIT_SSL_KEY");
252 #if LIBCURL_VERSION_NUM >= 0x070908
253 ssl_capath
= getenv("GIT_SSL_CAPATH");
255 ssl_cainfo
= getenv("GIT_SSL_CAINFO");
257 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
258 if (low_speed_limit
!= NULL
)
259 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
260 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
261 if (low_speed_time
!= NULL
)
262 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
264 git_config(http_options
);
266 if (curl_ssl_verify
== -1)
269 #ifdef USE_CURL_MULTI
270 if (max_requests
< 1)
271 max_requests
= DEFAULT_MAX_REQUESTS
;
274 if (getenv("GIT_CURL_FTP_NO_EPSV"))
275 curl_ftp_no_epsv
= 1;
277 #ifndef NO_CURL_EASY_DUPHANDLE
278 curl_default
= get_curl_handle();
282 void http_cleanup(void)
284 struct active_request_slot
*slot
= active_queue_head
;
285 #ifdef USE_CURL_MULTI
289 while (slot
!= NULL
) {
290 struct active_request_slot
*next
= slot
->next
;
291 #ifdef USE_CURL_MULTI
293 curl_easy_getinfo(slot
->curl
,
294 CURLINFO_EFFECTIVE_URL
,
296 fprintf(stderr
, "Waiting for %s\n", wait_url
);
297 run_active_slot(slot
);
300 if (slot
->curl
!= NULL
)
301 curl_easy_cleanup(slot
->curl
);
305 active_queue_head
= NULL
;
307 #ifndef NO_CURL_EASY_DUPHANDLE
308 curl_easy_cleanup(curl_default
);
311 #ifdef USE_CURL_MULTI
312 curl_multi_cleanup(curlm
);
314 curl_global_cleanup();
316 curl_slist_free_all(pragma_header
);
317 pragma_header
= NULL
;
320 struct active_request_slot
*get_active_slot(void)
322 struct active_request_slot
*slot
= active_queue_head
;
323 struct active_request_slot
*newslot
;
325 #ifdef USE_CURL_MULTI
328 /* Wait for a slot to open up if the queue is full */
329 while (active_requests
>= max_requests
) {
330 curl_multi_perform(curlm
, &num_transfers
);
331 if (num_transfers
< active_requests
) {
332 process_curl_messages();
337 while (slot
!= NULL
&& slot
->in_use
) {
341 newslot
= xmalloc(sizeof(*newslot
));
342 newslot
->curl
= NULL
;
344 newslot
->next
= NULL
;
346 slot
= active_queue_head
;
348 active_queue_head
= newslot
;
350 while (slot
->next
!= NULL
) {
353 slot
->next
= newslot
;
358 if (slot
->curl
== NULL
) {
359 #ifdef NO_CURL_EASY_DUPHANDLE
360 slot
->curl
= get_curl_handle();
362 slot
->curl
= curl_easy_duphandle(curl_default
);
369 slot
->results
= NULL
;
370 slot
->finished
= NULL
;
371 slot
->callback_data
= NULL
;
372 slot
->callback_func
= NULL
;
373 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
374 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
375 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
376 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
377 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
378 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
379 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
380 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
385 int start_active_slot(struct active_request_slot
*slot
)
387 #ifdef USE_CURL_MULTI
388 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
391 if (curlm_result
!= CURLM_OK
&&
392 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
399 * We know there must be something to do, since we just added
402 curl_multi_perform(curlm
, &num_transfers
);
407 #ifdef USE_CURL_MULTI
411 struct fill_chain
*next
;
414 static struct fill_chain
*fill_cfg
= NULL
;
416 void add_fill_function(void *data
, int (*fill
)(void *))
418 struct fill_chain
*new = malloc(sizeof(*new));
419 struct fill_chain
**linkp
= &fill_cfg
;
424 linkp
= &(*linkp
)->next
;
428 void fill_active_slots(void)
430 struct active_request_slot
*slot
= active_queue_head
;
432 while (active_requests
< max_requests
) {
433 struct fill_chain
*fill
;
434 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
435 if (fill
->fill(fill
->data
))
442 while (slot
!= NULL
) {
443 if (!slot
->in_use
&& slot
->curl
!= NULL
) {
444 curl_easy_cleanup(slot
->curl
);
451 void step_active_slots(void)
454 CURLMcode curlm_result
;
457 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
458 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
459 if (num_transfers
< active_requests
) {
460 process_curl_messages();
466 void run_active_slot(struct active_request_slot
*slot
)
468 #ifdef USE_CURL_MULTI
475 struct timeval select_timeout
;
478 slot
->finished
= &finished
;
483 if (!data_received
&& slot
->local
!= NULL
) {
484 current_pos
= ftell(slot
->local
);
485 if (current_pos
> last_pos
)
487 last_pos
= current_pos
;
490 if (slot
->in_use
&& !data_received
) {
495 select_timeout
.tv_sec
= 0;
496 select_timeout
.tv_usec
= 50000;
497 select(max_fd
, &readfds
, &writefds
,
498 &excfds
, &select_timeout
);
502 while (slot
->in_use
) {
503 slot
->curl_result
= curl_easy_perform(slot
->curl
);
504 finish_active_slot(slot
);
509 static void closedown_active_slot(struct active_request_slot
*slot
)
515 void release_active_slot(struct active_request_slot
*slot
)
517 closedown_active_slot(slot
);
519 #ifdef USE_CURL_MULTI
520 curl_multi_remove_handle(curlm
, slot
->curl
);
522 curl_easy_cleanup(slot
->curl
);
525 #ifdef USE_CURL_MULTI
530 static void finish_active_slot(struct active_request_slot
*slot
)
532 closedown_active_slot(slot
);
533 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
535 if (slot
->finished
!= NULL
)
536 (*slot
->finished
) = 1;
538 /* Store slot results so they can be read after the slot is reused */
539 if (slot
->results
!= NULL
) {
540 slot
->results
->curl_result
= slot
->curl_result
;
541 slot
->results
->http_code
= slot
->http_code
;
544 /* Run callback if appropriate */
545 if (slot
->callback_func
!= NULL
) {
546 slot
->callback_func(slot
->callback_data
);
550 void finish_all_active_slots(void)
552 struct active_request_slot
*slot
= active_queue_head
;
556 run_active_slot(slot
);
557 slot
= active_queue_head
;