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 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) {
154 return config_error_nonbool(var);
155 curl_http_proxy = xstrdup(value);
160 /* Fall back on the default ones */
161 return git_default_config(var, value, cb);
164 static CURL* get_curl_handle(void)
166 CURL* result = curl_easy_init();
168 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
169 #if LIBCURL_VERSION_NUM >= 0x070907
170 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
173 if (ssl_cert != NULL)
174 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
175 #if LIBCURL_VERSION_NUM >= 0x070902
177 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
179 #if LIBCURL_VERSION_NUM >= 0x070908
180 if (ssl_capath != NULL)
181 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
183 if (ssl_cainfo != NULL)
184 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
185 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
187 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
188 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
189 curl_low_speed_limit);
190 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
191 curl_low_speed_time);
194 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
196 if (getenv("GIT_CURL_VERBOSE"))
197 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
199 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
201 if (curl_ftp_no_epsv)
202 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
205 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
210 void http_init(struct remote *remote)
212 char *low_speed_limit;
213 char *low_speed_time;
215 curl_global_init(CURL_GLOBAL_ALL);
217 if (remote && remote->http_proxy)
218 curl_http_proxy = xstrdup(remote->http_proxy);
220 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
222 #ifdef USE_CURL_MULTI
224 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
225 if (http_max_requests != NULL)
226 max_requests = atoi(http_max_requests);
229 curlm = curl_multi_init();
231 fprintf(stderr, "Error creating curl multi handle.\n");
236 if (getenv("GIT_SSL_NO_VERIFY"))
239 ssl_cert = getenv("GIT_SSL_CERT");
240 #if LIBCURL_VERSION_NUM >= 0x070902
241 ssl_key = getenv("GIT_SSL_KEY");
243 #if LIBCURL_VERSION_NUM >= 0x070908
244 ssl_capath = getenv("GIT_SSL_CAPATH");
246 ssl_cainfo = getenv("GIT_SSL_CAINFO");
248 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
249 if (low_speed_limit != NULL)
250 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
251 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
252 if (low_speed_time != NULL)
253 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
255 git_config(http_options, NULL);
257 if (curl_ssl_verify == -1)
260 #ifdef USE_CURL_MULTI
261 if (max_requests < 1)
262 max_requests = DEFAULT_MAX_REQUESTS;
265 if (getenv("GIT_CURL_FTP_NO_EPSV"))
266 curl_ftp_no_epsv = 1;
268 #ifndef NO_CURL_EASY_DUPHANDLE
269 curl_default = get_curl_handle();
273 void http_cleanup(void)
275 struct active_request_slot *slot = active_queue_head;
277 while (slot != NULL) {
278 struct active_request_slot *next = slot->next;
279 if (slot->curl != NULL) {
280 #ifdef USE_CURL_MULTI
281 curl_multi_remove_handle(curlm, slot->curl);
283 curl_easy_cleanup(slot->curl);
288 active_queue_head = NULL;
290 #ifndef NO_CURL_EASY_DUPHANDLE
291 curl_easy_cleanup(curl_default);
294 #ifdef USE_CURL_MULTI
295 curl_multi_cleanup(curlm);
297 curl_global_cleanup();
299 curl_slist_free_all(pragma_header);
300 pragma_header = NULL;
302 if (curl_http_proxy) {
303 free(curl_http_proxy);
304 curl_http_proxy = NULL;
308 struct active_request_slot *get_active_slot(void)
310 struct active_request_slot *slot = active_queue_head;
311 struct active_request_slot *newslot;
313 #ifdef USE_CURL_MULTI
316 /* Wait for a slot to open up if the queue is full */
317 while (active_requests >= max_requests) {
318 curl_multi_perform(curlm, &num_transfers);
319 if (num_transfers < active_requests) {
320 process_curl_messages();
325 while (slot != NULL && slot->in_use) {
329 newslot = xmalloc(sizeof(*newslot));
330 newslot->curl = NULL;
332 newslot->next = NULL;
334 slot = active_queue_head;
336 active_queue_head = newslot;
338 while (slot->next != NULL) {
341 slot->next = newslot;
346 if (slot->curl == NULL) {
347 #ifdef NO_CURL_EASY_DUPHANDLE
348 slot->curl = get_curl_handle();
350 slot->curl = curl_easy_duphandle(curl_default);
357 slot->results = NULL;
358 slot->finished = NULL;
359 slot->callback_data = NULL;
360 slot->callback_func = NULL;
361 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
362 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
363 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
364 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
365 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
366 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
367 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
372 int start_active_slot(struct active_request_slot *slot)
374 #ifdef USE_CURL_MULTI
375 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
378 if (curlm_result != CURLM_OK &&
379 curlm_result != CURLM_CALL_MULTI_PERFORM) {
386 * We know there must be something to do, since we just added
389 curl_multi_perform(curlm, &num_transfers);
394 #ifdef USE_CURL_MULTI
398 struct fill_chain *next;
401 static struct fill_chain *fill_cfg = NULL;
403 void add_fill_function(void *data, int (*fill)(void *))
405 struct fill_chain *new = malloc(sizeof(*new));
406 struct fill_chain **linkp = &fill_cfg;
411 linkp = &(*linkp)->next;
415 void fill_active_slots(void)
417 struct active_request_slot *slot = active_queue_head;
419 while (active_requests < max_requests) {
420 struct fill_chain *fill;
421 for (fill = fill_cfg; fill; fill = fill->next)
422 if (fill->fill(fill->data))
429 while (slot != NULL) {
430 if (!slot->in_use && slot->curl != NULL) {
431 curl_easy_cleanup(slot->curl);
438 void step_active_slots(void)
441 CURLMcode curlm_result;
444 curlm_result = curl_multi_perform(curlm, &num_transfers);
445 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
446 if (num_transfers < active_requests) {
447 process_curl_messages();
453 void run_active_slot(struct active_request_slot *slot)
455 #ifdef USE_CURL_MULTI
462 struct timeval select_timeout;
465 slot->finished = &finished;
470 if (!data_received && slot->local != NULL) {
471 current_pos = ftell(slot->local);
472 if (current_pos > last_pos)
474 last_pos = current_pos;
477 if (slot->in_use && !data_received) {
482 select_timeout.tv_sec = 0;
483 select_timeout.tv_usec = 50000;
484 select(max_fd, &readfds, &writefds,
485 &excfds, &select_timeout);
489 while (slot->in_use) {
490 slot->curl_result = curl_easy_perform(slot->curl);
491 finish_active_slot(slot);
496 static void closedown_active_slot(struct active_request_slot *slot)
502 void release_active_slot(struct active_request_slot *slot)
504 closedown_active_slot(slot);
506 #ifdef USE_CURL_MULTI
507 curl_multi_remove_handle(curlm, slot->curl);
509 curl_easy_cleanup(slot->curl);
512 #ifdef USE_CURL_MULTI
517 static void finish_active_slot(struct active_request_slot *slot)
519 closedown_active_slot(slot);
520 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
522 if (slot->finished != NULL)
523 (*slot->finished) = 1;
525 /* Store slot results so they can be read after the slot is reused */
526 if (slot->results != NULL) {
527 slot->results->curl_result = slot->curl_result;
528 slot->results->http_code = slot->http_code;
531 /* Run callback if appropriate */
532 if (slot->callback_func != NULL) {
533 slot->callback_func(slot->callback_data);
537 void finish_all_active_slots(void)
539 struct active_request_slot *slot = active_queue_head;
543 run_active_slot(slot);
544 slot = active_queue_head;
550 static inline int needs_quote(int ch)
552 if (((ch >= 'A') && (ch <= 'Z'))
553 || ((ch >= 'a') && (ch <= 'z'))
554 || ((ch >= '0') && (ch <= '9'))
562 static inline int hex(int v)
564 if (v < 10) return '0' + v;
565 else return 'A' + v - 10;
568 static char *quote_ref_url(const char *base, const char *ref)
572 int len, baselen, ch;
574 baselen = strlen(base);
575 len = baselen + 2; /* '/' after base and terminating NUL */
576 for (cp = ref; (ch = *cp) != 0; cp++, len++)
578 len += 2; /* extra two hex plus replacement % */
580 memcpy(qref, base, baselen);
583 for (cp = ref; (ch = *cp) != 0; cp++) {
584 if (needs_quote(ch)) {
586 *dp++ = hex((ch >> 4) & 0xF);
587 *dp++ = hex(ch & 0xF);
597 int http_fetch_ref(const char *base, struct ref *ref)
600 struct strbuf buffer = STRBUF_INIT;
601 struct active_request_slot *slot;
602 struct slot_results results;
605 url = quote_ref_url(base, ref->name);
606 slot = get_active_slot();
607 slot->results = &results;
608 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
609 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
610 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
611 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
612 if (start_active_slot(slot)) {
613 run_active_slot(slot);
614 if (results.curl_result == CURLE_OK) {
615 strbuf_rtrim(&buffer);
616 if (buffer.len == 40)
617 ret = get_sha1_hex(buffer.buf, ref->old_sha1);
618 else if (!prefixcmp(buffer.buf, "ref: ")) {
619 ref->symref = xstrdup(buffer.buf + 5);
624 ret = error("Couldn't get %s for %s\n%s",
625 url, ref->name, curl_errorstr);
628 ret = error("Unable to start request");
631 strbuf_release(&buffer);