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;
27 struct curl_slist
*pragma_header
;
28 struct curl_slist
*no_range_header
;
30 struct active_request_slot
*active_queue_head
= NULL
;
32 size_t fread_buffer(void *ptr
, size_t eltsize
, size_t nmemb
,
33 struct buffer
*buffer
)
35 size_t size
= eltsize
* nmemb
;
36 if (size
> buffer
->size
- buffer
->posn
)
37 size
= buffer
->size
- buffer
->posn
;
38 memcpy(ptr
, buffer
->buffer
+ buffer
->posn
, size
);
43 size_t fwrite_buffer(const void *ptr
, size_t eltsize
,
44 size_t nmemb
, struct buffer
*buffer
)
46 size_t size
= eltsize
* nmemb
;
47 if (size
> buffer
->size
- buffer
->posn
) {
48 buffer
->size
= buffer
->size
* 3 / 2;
49 if (buffer
->size
< buffer
->posn
+ size
)
50 buffer
->size
= buffer
->posn
+ size
;
51 buffer
->buffer
= xrealloc(buffer
->buffer
, buffer
->size
);
53 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
59 size_t fwrite_null(const void *ptr
, size_t eltsize
,
60 size_t nmemb
, struct buffer
*buffer
)
63 return eltsize
* nmemb
;
66 static void finish_active_slot(struct active_request_slot
*slot
);
69 static void process_curl_messages(void)
72 struct active_request_slot
*slot
;
73 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
75 while (curl_message
!= NULL
) {
76 if (curl_message
->msg
== CURLMSG_DONE
) {
77 int curl_result
= curl_message
->data
.result
;
78 slot
= active_queue_head
;
79 while (slot
!= NULL
&&
80 slot
->curl
!= curl_message
->easy_handle
)
83 curl_multi_remove_handle(curlm
, slot
->curl
);
84 slot
->curl_result
= curl_result
;
85 finish_active_slot(slot
);
87 fprintf(stderr
, "Received DONE message for unknown request!\n");
90 fprintf(stderr
, "Unknown CURL message received: %d\n",
91 (int)curl_message
->msg
);
93 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
98 static int http_options(const char *var
, const char *value
)
100 if (!strcmp("http.sslverify", var
)) {
101 if (curl_ssl_verify
== -1) {
102 curl_ssl_verify
= git_config_bool(var
, value
);
107 if (!strcmp("http.sslcert", var
)) {
108 if (ssl_cert
== NULL
) {
109 ssl_cert
= xmalloc(strlen(value
)+1);
110 strcpy(ssl_cert
, value
);
114 #if LIBCURL_VERSION_NUM >= 0x070902
115 if (!strcmp("http.sslkey", var
)) {
116 if (ssl_key
== NULL
) {
117 ssl_key
= xmalloc(strlen(value
)+1);
118 strcpy(ssl_key
, value
);
123 #if LIBCURL_VERSION_NUM >= 0x070908
124 if (!strcmp("http.sslcapath", var
)) {
125 if (ssl_capath
== NULL
) {
126 ssl_capath
= xmalloc(strlen(value
)+1);
127 strcpy(ssl_capath
, value
);
132 if (!strcmp("http.sslcainfo", var
)) {
133 if (ssl_cainfo
== NULL
) {
134 ssl_cainfo
= xmalloc(strlen(value
)+1);
135 strcpy(ssl_cainfo
, value
);
140 #ifdef USE_CURL_MULTI
141 if (!strcmp("http.maxrequests", var
)) {
142 if (max_requests
== -1)
143 max_requests
= git_config_int(var
, value
);
148 if (!strcmp("http.lowspeedlimit", var
)) {
149 if (curl_low_speed_limit
== -1)
150 curl_low_speed_limit
= (long)git_config_int(var
, value
);
153 if (!strcmp("http.lowspeedtime", var
)) {
154 if (curl_low_speed_time
== -1)
155 curl_low_speed_time
= (long)git_config_int(var
, value
);
159 /* Fall back on the default ones */
160 return git_default_config(var
, value
);
163 static CURL
* get_curl_handle(void)
165 CURL
* result
= curl_easy_init();
167 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
168 #if LIBCURL_VERSION_NUM >= 0x070907
169 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
172 if (ssl_cert
!= NULL
)
173 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
174 #if LIBCURL_VERSION_NUM >= 0x070902
176 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
178 #if LIBCURL_VERSION_NUM >= 0x070908
179 if (ssl_capath
!= NULL
)
180 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
182 if (ssl_cainfo
!= NULL
)
183 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
184 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
186 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
187 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
188 curl_low_speed_limit
);
189 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
190 curl_low_speed_time
);
193 curl_easy_setopt(result
, CURLOPT_FOLLOWLOCATION
, 1);
195 if (getenv("GIT_CURL_VERBOSE"))
196 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
203 char *low_speed_limit
;
204 char *low_speed_time
;
206 curl_global_init(CURL_GLOBAL_ALL
);
208 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
209 no_range_header
= curl_slist_append(no_range_header
, "Range:");
211 #ifdef USE_CURL_MULTI
213 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
214 if (http_max_requests
!= NULL
)
215 max_requests
= atoi(http_max_requests
);
218 curlm
= curl_multi_init();
220 fprintf(stderr
, "Error creating curl multi handle.\n");
225 if (getenv("GIT_SSL_NO_VERIFY"))
228 ssl_cert
= getenv("GIT_SSL_CERT");
229 #if LIBCURL_VERSION_NUM >= 0x070902
230 ssl_key
= getenv("GIT_SSL_KEY");
232 #if LIBCURL_VERSION_NUM >= 0x070908
233 ssl_capath
= getenv("GIT_SSL_CAPATH");
235 ssl_cainfo
= getenv("GIT_SSL_CAINFO");
237 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
238 if (low_speed_limit
!= NULL
)
239 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
240 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
241 if (low_speed_time
!= NULL
)
242 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
244 git_config(http_options
);
246 if (curl_ssl_verify
== -1)
249 #ifdef USE_CURL_MULTI
250 if (max_requests
< 1)
251 max_requests
= DEFAULT_MAX_REQUESTS
;
254 #ifndef NO_CURL_EASY_DUPHANDLE
255 curl_default
= get_curl_handle();
259 void http_cleanup(void)
261 struct active_request_slot
*slot
= active_queue_head
;
262 #ifdef USE_CURL_MULTI
266 while (slot
!= NULL
) {
267 #ifdef USE_CURL_MULTI
269 curl_easy_getinfo(slot
->curl
,
270 CURLINFO_EFFECTIVE_URL
,
272 fprintf(stderr
, "Waiting for %s\n", wait_url
);
273 run_active_slot(slot
);
276 if (slot
->curl
!= NULL
)
277 curl_easy_cleanup(slot
->curl
);
281 #ifndef NO_CURL_EASY_DUPHANDLE
282 curl_easy_cleanup(curl_default
);
285 #ifdef USE_CURL_MULTI
286 curl_multi_cleanup(curlm
);
288 curl_global_cleanup();
292 struct active_request_slot
*get_active_slot(void)
294 struct active_request_slot
*slot
= active_queue_head
;
295 struct active_request_slot
*newslot
;
297 #ifdef USE_CURL_MULTI
300 /* Wait for a slot to open up if the queue is full */
301 while (active_requests
>= max_requests
) {
302 curl_multi_perform(curlm
, &num_transfers
);
303 if (num_transfers
< active_requests
) {
304 process_curl_messages();
309 while (slot
!= NULL
&& slot
->in_use
) {
313 newslot
= xmalloc(sizeof(*newslot
));
314 newslot
->curl
= NULL
;
316 newslot
->next
= NULL
;
318 slot
= active_queue_head
;
320 active_queue_head
= newslot
;
322 while (slot
->next
!= NULL
) {
325 slot
->next
= newslot
;
330 if (slot
->curl
== NULL
) {
331 #ifdef NO_CURL_EASY_DUPHANDLE
332 slot
->curl
= get_curl_handle();
334 slot
->curl
= curl_easy_duphandle(curl_default
);
341 slot
->results
= NULL
;
342 slot
->callback_data
= NULL
;
343 slot
->callback_func
= NULL
;
344 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
345 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_range_header
);
346 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
351 int start_active_slot(struct active_request_slot
*slot
)
353 #ifdef USE_CURL_MULTI
354 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
356 if (curlm_result
!= CURLM_OK
&&
357 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
366 #ifdef USE_CURL_MULTI
367 void step_active_slots(void)
370 CURLMcode curlm_result
;
373 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
374 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
375 if (num_transfers
< active_requests
) {
376 process_curl_messages();
382 void run_active_slot(struct active_request_slot
*slot
)
384 #ifdef USE_CURL_MULTI
391 struct timeval select_timeout
;
393 while (slot
->in_use
) {
397 if (!data_received
&& slot
->local
!= NULL
) {
398 current_pos
= ftell(slot
->local
);
399 if (current_pos
> last_pos
)
401 last_pos
= current_pos
;
404 if (slot
->in_use
&& !data_received
) {
409 select_timeout
.tv_sec
= 0;
410 select_timeout
.tv_usec
= 50000;
411 select(max_fd
, &readfds
, &writefds
,
412 &excfds
, &select_timeout
);
416 while (slot
->in_use
) {
417 slot
->curl_result
= curl_easy_perform(slot
->curl
);
418 finish_active_slot(slot
);
423 static void closedown_active_slot(struct active_request_slot
*slot
)
429 void release_active_slot(struct active_request_slot
*slot
)
431 closedown_active_slot(slot
);
433 curl_multi_remove_handle(curlm
, slot
->curl
);
434 curl_easy_cleanup(slot
->curl
);
440 static void finish_active_slot(struct active_request_slot
*slot
)
442 closedown_active_slot(slot
);
443 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
445 /* Store slot results so they can be read after the slot is reused */
446 if (slot
->results
!= NULL
) {
447 slot
->results
->curl_result
= slot
->curl_result
;
448 slot
->results
->http_code
= slot
->http_code
;
451 /* Run callback if appropriate */
452 if (slot
->callback_func
!= NULL
) {
453 slot
->callback_func(slot
->callback_data
);
457 void finish_all_active_slots(void)
459 struct active_request_slot
*slot
= active_queue_head
;
463 run_active_slot(slot
);
464 slot
= active_queue_head
;