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 if (!curl_ssl_verify) {
169 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
170 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
172 /* Verify authenticity of the peer's certificate */
173 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
174 /* The name in the cert must match whom we tried to connect */
175 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
178 #if LIBCURL_VERSION_NUM >= 0x070907
179 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
182 if (ssl_cert != NULL)
183 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
184 #if LIBCURL_VERSION_NUM >= 0x070902
186 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
188 #if LIBCURL_VERSION_NUM >= 0x070908
189 if (ssl_capath != NULL)
190 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
192 if (ssl_cainfo != NULL)
193 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
194 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
196 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
197 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
198 curl_low_speed_limit);
199 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
200 curl_low_speed_time);
203 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
205 if (getenv("GIT_CURL_VERBOSE"))
206 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
208 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
210 if (curl_ftp_no_epsv)
211 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
214 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
219 void http_init(struct remote *remote)
221 char *low_speed_limit;
222 char *low_speed_time;
224 curl_global_init(CURL_GLOBAL_ALL);
226 if (remote && remote->http_proxy)
227 curl_http_proxy = xstrdup(remote->http_proxy);
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, NULL);
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;
286 while (slot != NULL) {
287 struct active_request_slot *next = slot->next;
288 if (slot->curl != NULL) {
289 #ifdef USE_CURL_MULTI
290 curl_multi_remove_handle(curlm, slot->curl);
292 curl_easy_cleanup(slot->curl);
297 active_queue_head = NULL;
299 #ifndef NO_CURL_EASY_DUPHANDLE
300 curl_easy_cleanup(curl_default);
303 #ifdef USE_CURL_MULTI
304 curl_multi_cleanup(curlm);
306 curl_global_cleanup();
308 curl_slist_free_all(pragma_header);
309 pragma_header = NULL;
311 if (curl_http_proxy) {
312 free(curl_http_proxy);
313 curl_http_proxy = NULL;
317 struct active_request_slot *get_active_slot(void)
319 struct active_request_slot *slot = active_queue_head;
320 struct active_request_slot *newslot;
322 #ifdef USE_CURL_MULTI
325 /* Wait for a slot to open up if the queue is full */
326 while (active_requests >= max_requests) {
327 curl_multi_perform(curlm, &num_transfers);
328 if (num_transfers < active_requests) {
329 process_curl_messages();
334 while (slot != NULL && slot->in_use) {
338 newslot = xmalloc(sizeof(*newslot));
339 newslot->curl = NULL;
341 newslot->next = NULL;
343 slot = active_queue_head;
345 active_queue_head = newslot;
347 while (slot->next != NULL) {
350 slot->next = newslot;
355 if (slot->curl == NULL) {
356 #ifdef NO_CURL_EASY_DUPHANDLE
357 slot->curl = get_curl_handle();
359 slot->curl = curl_easy_duphandle(curl_default);
366 slot->results = NULL;
367 slot->finished = NULL;
368 slot->callback_data = NULL;
369 slot->callback_func = NULL;
370 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
371 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
372 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
373 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
374 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
375 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
376 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
381 int start_active_slot(struct active_request_slot *slot)
383 #ifdef USE_CURL_MULTI
384 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
387 if (curlm_result != CURLM_OK &&
388 curlm_result != CURLM_CALL_MULTI_PERFORM) {
395 * We know there must be something to do, since we just added
398 curl_multi_perform(curlm, &num_transfers);
403 #ifdef USE_CURL_MULTI
407 struct fill_chain *next;
410 static struct fill_chain *fill_cfg = NULL;
412 void add_fill_function(void *data, int (*fill)(void *))
414 struct fill_chain *new = xmalloc(sizeof(*new));
415 struct fill_chain **linkp = &fill_cfg;
420 linkp = &(*linkp)->next;
424 void fill_active_slots(void)
426 struct active_request_slot *slot = active_queue_head;
428 while (active_requests < max_requests) {
429 struct fill_chain *fill;
430 for (fill = fill_cfg; fill; fill = fill->next)
431 if (fill->fill(fill->data))
438 while (slot != NULL) {
439 if (!slot->in_use && slot->curl != NULL) {
440 curl_easy_cleanup(slot->curl);
447 void step_active_slots(void)
450 CURLMcode curlm_result;
453 curlm_result = curl_multi_perform(curlm, &num_transfers);
454 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
455 if (num_transfers < active_requests) {
456 process_curl_messages();
462 void run_active_slot(struct active_request_slot *slot)
464 #ifdef USE_CURL_MULTI
471 struct timeval select_timeout;
474 slot->finished = &finished;
479 if (!data_received && slot->local != NULL) {
480 current_pos = ftell(slot->local);
481 if (current_pos > last_pos)
483 last_pos = current_pos;
486 if (slot->in_use && !data_received) {
491 select_timeout.tv_sec = 0;
492 select_timeout.tv_usec = 50000;
493 select(max_fd, &readfds, &writefds,
494 &excfds, &select_timeout);
498 while (slot->in_use) {
499 slot->curl_result = curl_easy_perform(slot->curl);
500 finish_active_slot(slot);
505 static void closedown_active_slot(struct active_request_slot *slot)
511 void release_active_slot(struct active_request_slot *slot)
513 closedown_active_slot(slot);
515 #ifdef USE_CURL_MULTI
516 curl_multi_remove_handle(curlm, slot->curl);
518 curl_easy_cleanup(slot->curl);
521 #ifdef USE_CURL_MULTI
526 static void finish_active_slot(struct active_request_slot *slot)
528 closedown_active_slot(slot);
529 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
531 if (slot->finished != NULL)
532 (*slot->finished) = 1;
534 /* Store slot results so they can be read after the slot is reused */
535 if (slot->results != NULL) {
536 slot->results->curl_result = slot->curl_result;
537 slot->results->http_code = slot->http_code;
540 /* Run callback if appropriate */
541 if (slot->callback_func != NULL) {
542 slot->callback_func(slot->callback_data);
546 void finish_all_active_slots(void)
548 struct active_request_slot *slot = active_queue_head;
552 run_active_slot(slot);
553 slot = active_queue_head;
559 static inline int needs_quote(int ch)
561 if (((ch >= 'A') && (ch <= 'Z'))
562 || ((ch >= 'a') && (ch <= 'z'))
563 || ((ch >= '0') && (ch <= '9'))
571 static inline int hex(int v)
573 if (v < 10) return '0' + v;
574 else return 'A' + v - 10;
577 static char *quote_ref_url(const char *base, const char *ref)
581 int len, baselen, ch;
583 baselen = strlen(base);
584 len = baselen + 2; /* '/' after base and terminating NUL */
585 for (cp = ref; (ch = *cp) != 0; cp++, len++)
587 len += 2; /* extra two hex plus replacement % */
589 memcpy(qref, base, baselen);
592 for (cp = ref; (ch = *cp) != 0; cp++) {
593 if (needs_quote(ch)) {
595 *dp++ = hex((ch >> 4) & 0xF);
596 *dp++ = hex(ch & 0xF);
606 int http_fetch_ref(const char *base, struct ref *ref)
609 struct strbuf buffer = STRBUF_INIT;
610 struct active_request_slot *slot;
611 struct slot_results results;
614 url = quote_ref_url(base, ref->name);
615 slot = get_active_slot();
616 slot->results = &results;
617 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
618 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
619 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
620 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
621 if (start_active_slot(slot)) {
622 run_active_slot(slot);
623 if (results.curl_result == CURLE_OK) {
624 strbuf_rtrim(&buffer);
625 if (buffer.len == 40)
626 ret = get_sha1_hex(buffer.buf, ref->old_sha1);
627 else if (!prefixcmp(buffer.buf, "ref: ")) {
628 ref->symref = xstrdup(buffer.buf + 5);
633 ret = error("Couldn't get %s for %s\n%s",
634 url, ref->name, curl_errorstr);
637 ret = error("Unable to start request");
640 strbuf_release(&buffer);