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 if (curl_ssl_verify == -1)
98 curl_ssl_verify = git_config_bool(var, value);
102 if (!strcmp("http.sslcert", var)) {
103 if (ssl_cert == NULL)
104 return git_config_string(&ssl_cert, var, value);
107 #if LIBCURL_VERSION_NUM >= 0x070902
108 if (!strcmp("http.sslkey", var)) {
110 return git_config_string(&ssl_key, var, value);
114 #if LIBCURL_VERSION_NUM >= 0x070908
115 if (!strcmp("http.sslcapath", var)) {
116 if (ssl_capath == NULL)
117 return git_config_string(&ssl_capath, var, value);
121 if (!strcmp("http.sslcainfo", var)) {
122 if (ssl_cainfo == NULL)
123 return git_config_string(&ssl_cainfo, var, value);
127 #ifdef USE_CURL_MULTI
128 if (!strcmp("http.maxrequests", var)) {
129 if (max_requests == -1)
130 max_requests = git_config_int(var, value);
135 if (!strcmp("http.lowspeedlimit", var)) {
136 if (curl_low_speed_limit == -1)
137 curl_low_speed_limit = (long)git_config_int(var, value);
140 if (!strcmp("http.lowspeedtime", var)) {
141 if (curl_low_speed_time == -1)
142 curl_low_speed_time = (long)git_config_int(var, value);
146 if (!strcmp("http.noepsv", var)) {
147 curl_ftp_no_epsv = git_config_bool(var, value);
150 if (!strcmp("http.proxy", var)) {
151 if (curl_http_proxy == NULL)
152 return git_config_string(&curl_http_proxy, var, value);
156 /* Fall back on the default ones */
157 return git_default_config(var, value, cb);
160 static CURL *get_curl_handle(void)
162 CURL *result = curl_easy_init();
164 if (!curl_ssl_verify) {
165 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
166 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
168 /* Verify authenticity of the peer's certificate */
169 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
170 /* The name in the cert must match whom we tried to connect */
171 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
174 #if LIBCURL_VERSION_NUM >= 0x070907
175 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
178 if (ssl_cert != NULL)
179 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
180 #if LIBCURL_VERSION_NUM >= 0x070902
182 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
184 #if LIBCURL_VERSION_NUM >= 0x070908
185 if (ssl_capath != NULL)
186 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
188 if (ssl_cainfo != NULL)
189 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
190 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
192 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
193 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
194 curl_low_speed_limit);
195 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
196 curl_low_speed_time);
199 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
201 if (getenv("GIT_CURL_VERBOSE"))
202 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
204 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
206 if (curl_ftp_no_epsv)
207 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
210 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
215 void http_init(struct remote *remote)
217 char *low_speed_limit;
218 char *low_speed_time;
220 curl_global_init(CURL_GLOBAL_ALL);
222 if (remote && remote->http_proxy)
223 curl_http_proxy = xstrdup(remote->http_proxy);
225 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
227 #ifdef USE_CURL_MULTI
229 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
230 if (http_max_requests != NULL)
231 max_requests = atoi(http_max_requests);
234 curlm = curl_multi_init();
236 fprintf(stderr, "Error creating curl multi handle.\n");
241 if (getenv("GIT_SSL_NO_VERIFY"))
244 ssl_cert = getenv("GIT_SSL_CERT");
245 #if LIBCURL_VERSION_NUM >= 0x070902
246 ssl_key = getenv("GIT_SSL_KEY");
248 #if LIBCURL_VERSION_NUM >= 0x070908
249 ssl_capath = getenv("GIT_SSL_CAPATH");
251 ssl_cainfo = getenv("GIT_SSL_CAINFO");
253 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
254 if (low_speed_limit != NULL)
255 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
256 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
257 if (low_speed_time != NULL)
258 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
260 git_config(http_options, NULL);
262 if (curl_ssl_verify == -1)
265 #ifdef USE_CURL_MULTI
266 if (max_requests < 1)
267 max_requests = DEFAULT_MAX_REQUESTS;
270 if (getenv("GIT_CURL_FTP_NO_EPSV"))
271 curl_ftp_no_epsv = 1;
273 #ifndef NO_CURL_EASY_DUPHANDLE
274 curl_default = get_curl_handle();
278 void http_cleanup(void)
280 struct active_request_slot *slot = active_queue_head;
282 while (slot != NULL) {
283 struct active_request_slot *next = slot->next;
284 if (slot->curl != NULL) {
285 #ifdef USE_CURL_MULTI
286 curl_multi_remove_handle(curlm, slot->curl);
288 curl_easy_cleanup(slot->curl);
293 active_queue_head = NULL;
295 #ifndef NO_CURL_EASY_DUPHANDLE
296 curl_easy_cleanup(curl_default);
299 #ifdef USE_CURL_MULTI
300 curl_multi_cleanup(curlm);
302 curl_global_cleanup();
304 curl_slist_free_all(pragma_header);
305 pragma_header = NULL;
307 if (curl_http_proxy) {
308 free((void *)curl_http_proxy);
309 curl_http_proxy = NULL;
313 struct active_request_slot *get_active_slot(void)
315 struct active_request_slot *slot = active_queue_head;
316 struct active_request_slot *newslot;
318 #ifdef USE_CURL_MULTI
321 /* Wait for a slot to open up if the queue is full */
322 while (active_requests >= max_requests) {
323 curl_multi_perform(curlm, &num_transfers);
324 if (num_transfers < active_requests)
325 process_curl_messages();
329 while (slot != NULL && slot->in_use)
333 newslot = xmalloc(sizeof(*newslot));
334 newslot->curl = NULL;
336 newslot->next = NULL;
338 slot = active_queue_head;
340 active_queue_head = newslot;
342 while (slot->next != NULL)
344 slot->next = newslot;
349 if (slot->curl == NULL) {
350 #ifdef NO_CURL_EASY_DUPHANDLE
351 slot->curl = get_curl_handle();
353 slot->curl = curl_easy_duphandle(curl_default);
360 slot->results = NULL;
361 slot->finished = NULL;
362 slot->callback_data = NULL;
363 slot->callback_func = NULL;
364 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
365 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
366 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
367 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
368 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
369 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
370 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
375 int start_active_slot(struct active_request_slot *slot)
377 #ifdef USE_CURL_MULTI
378 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
381 if (curlm_result != CURLM_OK &&
382 curlm_result != CURLM_CALL_MULTI_PERFORM) {
389 * We know there must be something to do, since we just added
392 curl_multi_perform(curlm, &num_transfers);
397 #ifdef USE_CURL_MULTI
401 struct fill_chain *next;
404 static struct fill_chain *fill_cfg;
406 void add_fill_function(void *data, int (*fill)(void *))
408 struct fill_chain *new = xmalloc(sizeof(*new));
409 struct fill_chain **linkp = &fill_cfg;
414 linkp = &(*linkp)->next;
418 void fill_active_slots(void)
420 struct active_request_slot *slot = active_queue_head;
422 while (active_requests < max_requests) {
423 struct fill_chain *fill;
424 for (fill = fill_cfg; fill; fill = fill->next)
425 if (fill->fill(fill->data))
432 while (slot != NULL) {
433 if (!slot->in_use && slot->curl != NULL) {
434 curl_easy_cleanup(slot->curl);
441 void step_active_slots(void)
444 CURLMcode curlm_result;
447 curlm_result = curl_multi_perform(curlm, &num_transfers);
448 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
449 if (num_transfers < active_requests) {
450 process_curl_messages();
456 void run_active_slot(struct active_request_slot *slot)
458 #ifdef USE_CURL_MULTI
465 struct timeval select_timeout;
468 slot->finished = &finished;
473 if (!data_received && slot->local != NULL) {
474 current_pos = ftell(slot->local);
475 if (current_pos > last_pos)
477 last_pos = current_pos;
480 if (slot->in_use && !data_received) {
485 select_timeout.tv_sec = 0;
486 select_timeout.tv_usec = 50000;
487 select(max_fd, &readfds, &writefds,
488 &excfds, &select_timeout);
492 while (slot->in_use) {
493 slot->curl_result = curl_easy_perform(slot->curl);
494 finish_active_slot(slot);
499 static void closedown_active_slot(struct active_request_slot *slot)
505 void release_active_slot(struct active_request_slot *slot)
507 closedown_active_slot(slot);
509 #ifdef USE_CURL_MULTI
510 curl_multi_remove_handle(curlm, slot->curl);
512 curl_easy_cleanup(slot->curl);
515 #ifdef USE_CURL_MULTI
520 static void finish_active_slot(struct active_request_slot *slot)
522 closedown_active_slot(slot);
523 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
525 if (slot->finished != NULL)
526 (*slot->finished) = 1;
528 /* Store slot results so they can be read after the slot is reused */
529 if (slot->results != NULL) {
530 slot->results->curl_result = slot->curl_result;
531 slot->results->http_code = slot->http_code;
534 /* Run callback if appropriate */
535 if (slot->callback_func != NULL)
536 slot->callback_func(slot->callback_data);
539 void finish_all_active_slots(void)
541 struct active_request_slot *slot = active_queue_head;
545 run_active_slot(slot);
546 slot = active_queue_head;
552 static inline int needs_quote(int ch)
554 if (((ch >= 'A') && (ch <= 'Z'))
555 || ((ch >= 'a') && (ch <= 'z'))
556 || ((ch >= '0') && (ch <= '9'))
564 static inline int hex(int v)
572 static char *quote_ref_url(const char *base, const char *ref)
574 struct strbuf buf = STRBUF_INIT;
578 strbuf_addstr(&buf, base);
579 if (buf.len && buf.buf[buf.len - 1] != '/' && *ref != '/')
580 strbuf_addstr(&buf, "/");
582 for (cp = ref; (ch = *cp) != 0; cp++)
584 strbuf_addf(&buf, "%%%02x", ch);
586 strbuf_addch(&buf, *cp);
588 return strbuf_detach(&buf, NULL);
591 int http_fetch_ref(const char *base, struct ref *ref)
594 struct strbuf buffer = STRBUF_INIT;
595 struct active_request_slot *slot;
596 struct slot_results results;
599 url = quote_ref_url(base, ref->name);
600 slot = get_active_slot();
601 slot->results = &results;
602 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
603 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
604 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
605 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
606 if (start_active_slot(slot)) {
607 run_active_slot(slot);
608 if (results.curl_result == CURLE_OK) {
609 strbuf_rtrim(&buffer);
610 if (buffer.len == 40)
611 ret = get_sha1_hex(buffer.buf, ref->old_sha1);
612 else if (!prefixcmp(buffer.buf, "ref: ")) {
613 ref->symref = xstrdup(buffer.buf + 5);
618 ret = error("Couldn't get %s for %s\n%s",
619 url, ref->name, curl_errorstr);
622 ret = error("Unable to start request");
625 strbuf_release(&buffer);