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
;
29 struct active_request_slot
*active_queue_head
= NULL
;
31 size_t fread_buffer(void *ptr
, size_t eltsize
, size_t nmemb
,
32 struct buffer
*buffer
)
34 size_t size
= eltsize
* nmemb
;
35 if (size
> buffer
->size
- buffer
->posn
)
36 size
= buffer
->size
- buffer
->posn
;
37 memcpy(ptr
, (char *) buffer
->buffer
+ buffer
->posn
, size
);
42 size_t fwrite_buffer(const void *ptr
, size_t eltsize
,
43 size_t nmemb
, struct buffer
*buffer
)
45 size_t size
= eltsize
* nmemb
;
46 if (size
> buffer
->size
- buffer
->posn
) {
47 buffer
->size
= buffer
->size
* 3 / 2;
48 if (buffer
->size
< buffer
->posn
+ size
)
49 buffer
->size
= buffer
->posn
+ size
;
50 buffer
->buffer
= xrealloc(buffer
->buffer
, buffer
->size
);
52 memcpy((char *) buffer
->buffer
+ buffer
->posn
, ptr
, size
);
58 size_t fwrite_null(const void *ptr
, size_t eltsize
,
59 size_t nmemb
, struct buffer
*buffer
)
62 return eltsize
* nmemb
;
65 static void finish_active_slot(struct active_request_slot
*slot
);
68 static void process_curl_messages(void)
71 struct active_request_slot
*slot
;
72 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
74 while (curl_message
!= NULL
) {
75 if (curl_message
->msg
== CURLMSG_DONE
) {
76 int curl_result
= curl_message
->data
.result
;
77 slot
= active_queue_head
;
78 while (slot
!= NULL
&&
79 slot
->curl
!= curl_message
->easy_handle
)
82 curl_multi_remove_handle(curlm
, slot
->curl
);
83 slot
->curl_result
= curl_result
;
84 finish_active_slot(slot
);
86 fprintf(stderr
, "Received DONE message for unknown request!\n");
89 fprintf(stderr
, "Unknown CURL message received: %d\n",
90 (int)curl_message
->msg
);
92 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
97 static int http_options(const char *var
, const char *value
)
99 if (!strcmp("http.sslverify", var
)) {
100 if (curl_ssl_verify
== -1) {
101 curl_ssl_verify
= git_config_bool(var
, value
);
106 if (!strcmp("http.sslcert", var
)) {
107 if (ssl_cert
== NULL
) {
108 ssl_cert
= xmalloc(strlen(value
)+1);
109 strcpy(ssl_cert
, value
);
113 #if LIBCURL_VERSION_NUM >= 0x070902
114 if (!strcmp("http.sslkey", var
)) {
115 if (ssl_key
== NULL
) {
116 ssl_key
= xmalloc(strlen(value
)+1);
117 strcpy(ssl_key
, value
);
122 #if LIBCURL_VERSION_NUM >= 0x070908
123 if (!strcmp("http.sslcapath", var
)) {
124 if (ssl_capath
== NULL
) {
125 ssl_capath
= xmalloc(strlen(value
)+1);
126 strcpy(ssl_capath
, value
);
131 if (!strcmp("http.sslcainfo", var
)) {
132 if (ssl_cainfo
== NULL
) {
133 ssl_cainfo
= xmalloc(strlen(value
)+1);
134 strcpy(ssl_cainfo
, value
);
139 #ifdef USE_CURL_MULTI
140 if (!strcmp("http.maxrequests", var
)) {
141 if (max_requests
== -1)
142 max_requests
= git_config_int(var
, value
);
147 if (!strcmp("http.lowspeedlimit", var
)) {
148 if (curl_low_speed_limit
== -1)
149 curl_low_speed_limit
= (long)git_config_int(var
, value
);
152 if (!strcmp("http.lowspeedtime", var
)) {
153 if (curl_low_speed_time
== -1)
154 curl_low_speed_time
= (long)git_config_int(var
, value
);
158 /* Fall back on the default ones */
159 return git_default_config(var
, value
);
162 static CURL
* get_curl_handle(void)
164 CURL
* result
= curl_easy_init();
166 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
167 #if LIBCURL_VERSION_NUM >= 0x070907
168 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
171 if (ssl_cert
!= NULL
)
172 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
173 #if LIBCURL_VERSION_NUM >= 0x070902
175 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
177 #if LIBCURL_VERSION_NUM >= 0x070908
178 if (ssl_capath
!= NULL
)
179 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
181 if (ssl_cainfo
!= NULL
)
182 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
183 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
185 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
186 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
187 curl_low_speed_limit
);
188 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
189 curl_low_speed_time
);
192 curl_easy_setopt(result
, CURLOPT_FOLLOWLOCATION
, 1);
194 if (getenv("GIT_CURL_VERBOSE"))
195 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
197 curl_easy_setopt(result
, CURLOPT_USERAGENT
, GIT_USER_AGENT
);
204 char *low_speed_limit
;
205 char *low_speed_time
;
207 curl_global_init(CURL_GLOBAL_ALL
);
209 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
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();
290 curl_slist_free_all(pragma_header
);
293 struct active_request_slot
*get_active_slot(void)
295 struct active_request_slot
*slot
= active_queue_head
;
296 struct active_request_slot
*newslot
;
298 #ifdef USE_CURL_MULTI
301 /* Wait for a slot to open up if the queue is full */
302 while (active_requests
>= max_requests
) {
303 curl_multi_perform(curlm
, &num_transfers
);
304 if (num_transfers
< active_requests
) {
305 process_curl_messages();
310 while (slot
!= NULL
&& slot
->in_use
) {
314 newslot
= xmalloc(sizeof(*newslot
));
315 newslot
->curl
= NULL
;
317 newslot
->next
= NULL
;
319 slot
= active_queue_head
;
321 active_queue_head
= newslot
;
323 while (slot
->next
!= NULL
) {
326 slot
->next
= newslot
;
331 if (slot
->curl
== NULL
) {
332 #ifdef NO_CURL_EASY_DUPHANDLE
333 slot
->curl
= get_curl_handle();
335 slot
->curl
= curl_easy_duphandle(curl_default
);
342 slot
->results
= NULL
;
343 slot
->finished
= NULL
;
344 slot
->callback_data
= NULL
;
345 slot
->callback_func
= NULL
;
346 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
347 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
348 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
349 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
350 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
351 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
352 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
353 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
358 int start_active_slot(struct active_request_slot
*slot
)
360 #ifdef USE_CURL_MULTI
361 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
363 if (curlm_result
!= CURLM_OK
&&
364 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
373 #ifdef USE_CURL_MULTI
374 void step_active_slots(void)
377 CURLMcode curlm_result
;
380 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
381 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
382 if (num_transfers
< active_requests
) {
383 process_curl_messages();
389 void run_active_slot(struct active_request_slot
*slot
)
391 #ifdef USE_CURL_MULTI
398 struct timeval select_timeout
;
401 slot
->finished
= &finished
;
406 if (!data_received
&& slot
->local
!= NULL
) {
407 current_pos
= ftell(slot
->local
);
408 if (current_pos
> last_pos
)
410 last_pos
= current_pos
;
413 if (slot
->in_use
&& !data_received
) {
418 select_timeout
.tv_sec
= 0;
419 select_timeout
.tv_usec
= 50000;
420 select(max_fd
, &readfds
, &writefds
,
421 &excfds
, &select_timeout
);
425 while (slot
->in_use
) {
426 slot
->curl_result
= curl_easy_perform(slot
->curl
);
427 finish_active_slot(slot
);
432 static void closedown_active_slot(struct active_request_slot
*slot
)
438 void release_active_slot(struct active_request_slot
*slot
)
440 closedown_active_slot(slot
);
442 #ifdef USE_CURL_MULTI
443 curl_multi_remove_handle(curlm
, slot
->curl
);
445 curl_easy_cleanup(slot
->curl
);
448 #ifdef USE_CURL_MULTI
453 static void finish_active_slot(struct active_request_slot
*slot
)
455 closedown_active_slot(slot
);
456 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
458 if (slot
->finished
!= NULL
)
459 (*slot
->finished
) = 1;
461 /* Store slot results so they can be read after the slot is reused */
462 if (slot
->results
!= NULL
) {
463 slot
->results
->curl_result
= slot
->curl_result
;
464 slot
->results
->http_code
= slot
->http_code
;
467 /* Run callback if appropriate */
468 if (slot
->callback_func
!= NULL
) {
469 slot
->callback_func(slot
->callback_data
);
473 void finish_all_active_slots(void)
475 struct active_request_slot
*slot
= active_queue_head
;
479 run_active_slot(slot
);
480 slot
= active_queue_head
;