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
;
17 #if LIBCURL_VERSION_NUM >= 0x070902
18 static const char *ssl_key
;
20 #if LIBCURL_VERSION_NUM >= 0x070908
21 static const char *ssl_capath
;
23 static const char *ssl_cainfo
;
24 static long curl_low_speed_limit
= -1;
25 static long curl_low_speed_time
= -1;
26 static int curl_ftp_no_epsv
;
27 static const char *curl_http_proxy
;
29 static struct curl_slist
*pragma_header
;
31 static struct active_request_slot
*active_queue_head
;
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 curl_ssl_verify
= git_config_bool(var
, value
);
100 if (!strcmp("http.sslcert", var
))
101 return git_config_string(&ssl_cert
, var
, value
);
102 #if LIBCURL_VERSION_NUM >= 0x070902
103 if (!strcmp("http.sslkey", var
))
104 return git_config_string(&ssl_key
, var
, value
);
106 #if LIBCURL_VERSION_NUM >= 0x070908
107 if (!strcmp("http.sslcapath", var
))
108 return git_config_string(&ssl_capath
, var
, value
);
110 if (!strcmp("http.sslcainfo", var
))
111 return git_config_string(&ssl_cainfo
, var
, value
);
112 #ifdef USE_CURL_MULTI
113 if (!strcmp("http.maxrequests", var
)) {
114 max_requests
= git_config_int(var
, value
);
118 if (!strcmp("http.lowspeedlimit", var
)) {
119 curl_low_speed_limit
= (long)git_config_int(var
, value
);
122 if (!strcmp("http.lowspeedtime", var
)) {
123 curl_low_speed_time
= (long)git_config_int(var
, value
);
127 if (!strcmp("http.noepsv", var
)) {
128 curl_ftp_no_epsv
= git_config_bool(var
, value
);
131 if (!strcmp("http.proxy", var
))
132 return git_config_string(&curl_http_proxy
, var
, value
);
134 /* Fall back on the default ones */
135 return git_default_config(var
, value
, cb
);
138 static CURL
*get_curl_handle(void)
140 CURL
*result
= curl_easy_init();
142 if (!curl_ssl_verify
) {
143 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
144 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
146 /* Verify authenticity of the peer's certificate */
147 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
148 /* The name in the cert must match whom we tried to connect */
149 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
152 #if LIBCURL_VERSION_NUM >= 0x070907
153 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
156 if (ssl_cert
!= NULL
)
157 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
158 #if LIBCURL_VERSION_NUM >= 0x070902
160 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
162 #if LIBCURL_VERSION_NUM >= 0x070908
163 if (ssl_capath
!= NULL
)
164 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
166 if (ssl_cainfo
!= NULL
)
167 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
168 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
170 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
171 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
172 curl_low_speed_limit
);
173 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
174 curl_low_speed_time
);
177 curl_easy_setopt(result
, CURLOPT_FOLLOWLOCATION
, 1);
179 if (getenv("GIT_CURL_VERBOSE"))
180 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
182 curl_easy_setopt(result
, CURLOPT_USERAGENT
, GIT_USER_AGENT
);
184 if (curl_ftp_no_epsv
)
185 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
188 curl_easy_setopt(result
, CURLOPT_PROXY
, curl_http_proxy
);
193 static void set_from_env(const char **var
, const char *envname
)
195 const char *val
= getenv(envname
);
200 void http_init(struct remote
*remote
)
202 char *low_speed_limit
;
203 char *low_speed_time
;
205 git_config(http_options
, NULL
);
207 curl_global_init(CURL_GLOBAL_ALL
);
209 if (remote
&& remote
->http_proxy
)
210 curl_http_proxy
= xstrdup(remote
->http_proxy
);
212 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
214 #ifdef USE_CURL_MULTI
216 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
217 if (http_max_requests
!= NULL
)
218 max_requests
= atoi(http_max_requests
);
221 curlm
= curl_multi_init();
223 fprintf(stderr
, "Error creating curl multi handle.\n");
228 if (getenv("GIT_SSL_NO_VERIFY"))
231 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
232 #if LIBCURL_VERSION_NUM >= 0x070902
233 set_from_env(&ssl_key
, "GIT_SSL_KEY");
235 #if LIBCURL_VERSION_NUM >= 0x070908
236 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
238 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
240 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
241 if (low_speed_limit
!= NULL
)
242 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
243 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
244 if (low_speed_time
!= NULL
)
245 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
247 if (curl_ssl_verify
== -1)
250 #ifdef USE_CURL_MULTI
251 if (max_requests
< 1)
252 max_requests
= DEFAULT_MAX_REQUESTS
;
255 if (getenv("GIT_CURL_FTP_NO_EPSV"))
256 curl_ftp_no_epsv
= 1;
258 #ifndef NO_CURL_EASY_DUPHANDLE
259 curl_default
= get_curl_handle();
263 void http_cleanup(void)
265 struct active_request_slot
*slot
= active_queue_head
;
267 while (slot
!= NULL
) {
268 struct active_request_slot
*next
= slot
->next
;
269 if (slot
->curl
!= NULL
) {
270 #ifdef USE_CURL_MULTI
271 curl_multi_remove_handle(curlm
, slot
->curl
);
273 curl_easy_cleanup(slot
->curl
);
278 active_queue_head
= NULL
;
280 #ifndef NO_CURL_EASY_DUPHANDLE
281 curl_easy_cleanup(curl_default
);
284 #ifdef USE_CURL_MULTI
285 curl_multi_cleanup(curlm
);
287 curl_global_cleanup();
289 curl_slist_free_all(pragma_header
);
290 pragma_header
= NULL
;
292 if (curl_http_proxy
) {
293 free((void *)curl_http_proxy
);
294 curl_http_proxy
= NULL
;
298 struct active_request_slot
*get_active_slot(void)
300 struct active_request_slot
*slot
= active_queue_head
;
301 struct active_request_slot
*newslot
;
303 #ifdef USE_CURL_MULTI
306 /* Wait for a slot to open up if the queue is full */
307 while (active_requests
>= max_requests
) {
308 curl_multi_perform(curlm
, &num_transfers
);
309 if (num_transfers
< active_requests
)
310 process_curl_messages();
314 while (slot
!= NULL
&& slot
->in_use
)
318 newslot
= xmalloc(sizeof(*newslot
));
319 newslot
->curl
= NULL
;
321 newslot
->next
= NULL
;
323 slot
= active_queue_head
;
325 active_queue_head
= newslot
;
327 while (slot
->next
!= NULL
)
329 slot
->next
= newslot
;
334 if (slot
->curl
== NULL
) {
335 #ifdef NO_CURL_EASY_DUPHANDLE
336 slot
->curl
= get_curl_handle();
338 slot
->curl
= curl_easy_duphandle(curl_default
);
345 slot
->results
= NULL
;
346 slot
->finished
= NULL
;
347 slot
->callback_data
= NULL
;
348 slot
->callback_func
= NULL
;
349 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
350 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
351 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
352 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
353 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
354 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
355 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
360 int start_active_slot(struct active_request_slot
*slot
)
362 #ifdef USE_CURL_MULTI
363 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
366 if (curlm_result
!= CURLM_OK
&&
367 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
374 * We know there must be something to do, since we just added
377 curl_multi_perform(curlm
, &num_transfers
);
382 #ifdef USE_CURL_MULTI
386 struct fill_chain
*next
;
389 static struct fill_chain
*fill_cfg
;
391 void add_fill_function(void *data
, int (*fill
)(void *))
393 struct fill_chain
*new = xmalloc(sizeof(*new));
394 struct fill_chain
**linkp
= &fill_cfg
;
399 linkp
= &(*linkp
)->next
;
403 void fill_active_slots(void)
405 struct active_request_slot
*slot
= active_queue_head
;
407 while (active_requests
< max_requests
) {
408 struct fill_chain
*fill
;
409 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
410 if (fill
->fill(fill
->data
))
417 while (slot
!= NULL
) {
418 if (!slot
->in_use
&& slot
->curl
!= NULL
) {
419 curl_easy_cleanup(slot
->curl
);
426 void step_active_slots(void)
429 CURLMcode curlm_result
;
432 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
433 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
434 if (num_transfers
< active_requests
) {
435 process_curl_messages();
441 void run_active_slot(struct active_request_slot
*slot
)
443 #ifdef USE_CURL_MULTI
450 struct timeval select_timeout
;
453 slot
->finished
= &finished
;
458 if (!data_received
&& slot
->local
!= NULL
) {
459 current_pos
= ftell(slot
->local
);
460 if (current_pos
> last_pos
)
462 last_pos
= current_pos
;
465 if (slot
->in_use
&& !data_received
) {
470 select_timeout
.tv_sec
= 0;
471 select_timeout
.tv_usec
= 50000;
472 select(max_fd
, &readfds
, &writefds
,
473 &excfds
, &select_timeout
);
477 while (slot
->in_use
) {
478 slot
->curl_result
= curl_easy_perform(slot
->curl
);
479 finish_active_slot(slot
);
484 static void closedown_active_slot(struct active_request_slot
*slot
)
490 void release_active_slot(struct active_request_slot
*slot
)
492 closedown_active_slot(slot
);
494 #ifdef USE_CURL_MULTI
495 curl_multi_remove_handle(curlm
, slot
->curl
);
497 curl_easy_cleanup(slot
->curl
);
500 #ifdef USE_CURL_MULTI
505 static void finish_active_slot(struct active_request_slot
*slot
)
507 closedown_active_slot(slot
);
508 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
510 if (slot
->finished
!= NULL
)
511 (*slot
->finished
) = 1;
513 /* Store slot results so they can be read after the slot is reused */
514 if (slot
->results
!= NULL
) {
515 slot
->results
->curl_result
= slot
->curl_result
;
516 slot
->results
->http_code
= slot
->http_code
;
519 /* Run callback if appropriate */
520 if (slot
->callback_func
!= NULL
)
521 slot
->callback_func(slot
->callback_data
);
524 void finish_all_active_slots(void)
526 struct active_request_slot
*slot
= active_queue_head
;
530 run_active_slot(slot
);
531 slot
= active_queue_head
;
537 static inline int needs_quote(int ch
)
539 if (((ch
>= 'A') && (ch
<= 'Z'))
540 || ((ch
>= 'a') && (ch
<= 'z'))
541 || ((ch
>= '0') && (ch
<= '9'))
549 static inline int hex(int v
)
557 static char *quote_ref_url(const char *base
, const char *ref
)
559 struct strbuf buf
= STRBUF_INIT
;
563 strbuf_addstr(&buf
, base
);
564 if (buf
.len
&& buf
.buf
[buf
.len
- 1] != '/' && *ref
!= '/')
565 strbuf_addstr(&buf
, "/");
567 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
569 strbuf_addf(&buf
, "%%%02x", ch
);
571 strbuf_addch(&buf
, *cp
);
573 return strbuf_detach(&buf
, NULL
);
576 int http_fetch_ref(const char *base
, struct ref
*ref
)
579 struct strbuf buffer
= STRBUF_INIT
;
580 struct active_request_slot
*slot
;
581 struct slot_results results
;
584 url
= quote_ref_url(base
, ref
->name
);
585 slot
= get_active_slot();
586 slot
->results
= &results
;
587 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
588 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
589 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
590 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
591 if (start_active_slot(slot
)) {
592 run_active_slot(slot
);
593 if (results
.curl_result
== CURLE_OK
) {
594 strbuf_rtrim(&buffer
);
595 if (buffer
.len
== 40)
596 ret
= get_sha1_hex(buffer
.buf
, ref
->old_sha1
);
597 else if (!prefixcmp(buffer
.buf
, "ref: ")) {
598 ref
->symref
= xstrdup(buffer
.buf
+ 5);
603 ret
= error("Couldn't get %s for %s\n%s",
604 url
, ref
->name
, curl_errorstr
);
607 ret
= error("Unable to start request");
610 strbuf_release(&buffer
);