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;
28 static char *user_name, *user_pass;
30 static struct curl_slist *pragma_header;
32 static struct active_request_slot *active_queue_head;
34 size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb, void *buffer_)
36 size_t size = eltsize * nmemb;
37 struct buffer *buffer = buffer_;
39 if (size > buffer->buf.len - buffer->posn)
40 size = buffer->buf.len - buffer->posn;
41 memcpy(ptr, buffer->buf.buf + buffer->posn, size);
47 size_t fwrite_buffer(const void *ptr, size_t eltsize, size_t nmemb, void *buffer_)
49 size_t size = eltsize * nmemb;
50 struct strbuf *buffer = buffer_;
52 strbuf_add(buffer, ptr, size);
57 size_t fwrite_null(const void *ptr, size_t eltsize, size_t nmemb, void *strbuf)
60 return eltsize * nmemb;
63 static void finish_active_slot(struct active_request_slot *slot);
66 static void process_curl_messages(void)
69 struct active_request_slot *slot;
70 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
72 while (curl_message != NULL) {
73 if (curl_message->msg == CURLMSG_DONE) {
74 int curl_result = curl_message->data.result;
75 slot = active_queue_head;
76 while (slot != NULL &&
77 slot->curl != curl_message->easy_handle)
80 curl_multi_remove_handle(curlm, slot->curl);
81 slot->curl_result = curl_result;
82 finish_active_slot(slot);
84 fprintf(stderr, "Received DONE message for unknown request!\n");
87 fprintf(stderr, "Unknown CURL message received: %d\n",
88 (int)curl_message->msg);
90 curl_message = curl_multi_info_read(curlm, &num_messages);
95 static int http_options(const char *var, const char *value, void *cb)
97 if (!strcmp("http.sslverify", var)) {
98 curl_ssl_verify = git_config_bool(var, value);
101 if (!strcmp("http.sslcert", var))
102 return git_config_string(&ssl_cert, var, value);
103 #if LIBCURL_VERSION_NUM >= 0x070902
104 if (!strcmp("http.sslkey", var))
105 return git_config_string(&ssl_key, var, value);
107 #if LIBCURL_VERSION_NUM >= 0x070908
108 if (!strcmp("http.sslcapath", var))
109 return git_config_string(&ssl_capath, var, value);
111 if (!strcmp("http.sslcainfo", var))
112 return git_config_string(&ssl_cainfo, var, value);
113 #ifdef USE_CURL_MULTI
114 if (!strcmp("http.maxrequests", var)) {
115 max_requests = git_config_int(var, value);
119 if (!strcmp("http.lowspeedlimit", var)) {
120 curl_low_speed_limit = (long)git_config_int(var, value);
123 if (!strcmp("http.lowspeedtime", var)) {
124 curl_low_speed_time = (long)git_config_int(var, value);
128 if (!strcmp("http.noepsv", var)) {
129 curl_ftp_no_epsv = git_config_bool(var, value);
132 if (!strcmp("http.proxy", var))
133 return git_config_string(&curl_http_proxy, var, value);
135 /* Fall back on the default ones */
136 return git_default_config(var, value, cb);
139 static void init_curl_http_auth(CURL *result)
142 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
144 struct strbuf up = STRBUF_INIT;
146 user_pass = xstrdup(getpass("Password: "));
147 strbuf_addf(&up, "%s:%s", user_name, user_pass);
148 curl_easy_setopt(result, CURLOPT_USERPWD,
149 strbuf_detach(&up, NULL));
153 static CURL *get_curl_handle(void)
155 CURL *result = curl_easy_init();
157 if (!curl_ssl_verify) {
158 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
159 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
161 /* Verify authenticity of the peer's certificate */
162 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
163 /* The name in the cert must match whom we tried to connect */
164 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
167 #if LIBCURL_VERSION_NUM >= 0x070907
168 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
171 init_curl_http_auth(result);
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 static void http_auth_init(const char *url)
212 char *at, *colon, *cp, *slash;
215 cp = strstr(url, "://");
220 * Ok, the URL looks like "proto://something". Which one?
221 * "proto://<user>:<pass>@<host>/...",
222 * "proto://<user>@<host>/...", or just
223 * "proto://<host>/..."?
226 at = strchr(cp, '@');
227 colon = strchr(cp, ':');
228 slash = strchrnul(cp, '/');
229 if (!at || slash <= at)
230 return; /* No credentials */
231 if (!colon || at <= colon) {
234 user_name = xmalloc(len + 1);
235 memcpy(user_name, cp, len);
236 user_name[len] = '\0';
240 user_name = xmalloc(len + 1);
241 memcpy(user_name, cp, len);
242 user_name[len] = '\0';
243 len = at - (colon + 1);
244 user_pass = xmalloc(len + 1);
245 memcpy(user_pass, colon + 1, len);
246 user_pass[len] = '\0';
250 static void set_from_env(const char **var, const char *envname)
252 const char *val = getenv(envname);
257 void http_init(struct remote *remote)
259 char *low_speed_limit;
260 char *low_speed_time;
262 git_config(http_options, NULL);
264 curl_global_init(CURL_GLOBAL_ALL);
266 if (remote && remote->http_proxy)
267 curl_http_proxy = xstrdup(remote->http_proxy);
269 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
271 #ifdef USE_CURL_MULTI
273 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
274 if (http_max_requests != NULL)
275 max_requests = atoi(http_max_requests);
278 curlm = curl_multi_init();
280 fprintf(stderr, "Error creating curl multi handle.\n");
285 if (getenv("GIT_SSL_NO_VERIFY"))
288 set_from_env(&ssl_cert, "GIT_SSL_CERT");
289 #if LIBCURL_VERSION_NUM >= 0x070902
290 set_from_env(&ssl_key, "GIT_SSL_KEY");
292 #if LIBCURL_VERSION_NUM >= 0x070908
293 set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
295 set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
297 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
298 if (low_speed_limit != NULL)
299 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
300 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
301 if (low_speed_time != NULL)
302 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
304 if (curl_ssl_verify == -1)
307 #ifdef USE_CURL_MULTI
308 if (max_requests < 1)
309 max_requests = DEFAULT_MAX_REQUESTS;
312 if (getenv("GIT_CURL_FTP_NO_EPSV"))
313 curl_ftp_no_epsv = 1;
315 if (remote && remote->url && remote->url[0])
316 http_auth_init(remote->url[0]);
318 #ifndef NO_CURL_EASY_DUPHANDLE
319 curl_default = get_curl_handle();
323 void http_cleanup(void)
325 struct active_request_slot *slot = active_queue_head;
327 while (slot != NULL) {
328 struct active_request_slot *next = slot->next;
329 if (slot->curl != NULL) {
330 #ifdef USE_CURL_MULTI
331 curl_multi_remove_handle(curlm, slot->curl);
333 curl_easy_cleanup(slot->curl);
338 active_queue_head = NULL;
340 #ifndef NO_CURL_EASY_DUPHANDLE
341 curl_easy_cleanup(curl_default);
344 #ifdef USE_CURL_MULTI
345 curl_multi_cleanup(curlm);
347 curl_global_cleanup();
349 curl_slist_free_all(pragma_header);
350 pragma_header = NULL;
352 if (curl_http_proxy) {
353 free((void *)curl_http_proxy);
354 curl_http_proxy = NULL;
358 struct active_request_slot *get_active_slot(void)
360 struct active_request_slot *slot = active_queue_head;
361 struct active_request_slot *newslot;
363 #ifdef USE_CURL_MULTI
366 /* Wait for a slot to open up if the queue is full */
367 while (active_requests >= max_requests) {
368 curl_multi_perform(curlm, &num_transfers);
369 if (num_transfers < active_requests)
370 process_curl_messages();
374 while (slot != NULL && slot->in_use)
378 newslot = xmalloc(sizeof(*newslot));
379 newslot->curl = NULL;
381 newslot->next = NULL;
383 slot = active_queue_head;
385 active_queue_head = newslot;
387 while (slot->next != NULL)
389 slot->next = newslot;
394 if (slot->curl == NULL) {
395 #ifdef NO_CURL_EASY_DUPHANDLE
396 slot->curl = get_curl_handle();
398 slot->curl = curl_easy_duphandle(curl_default);
405 slot->results = NULL;
406 slot->finished = NULL;
407 slot->callback_data = NULL;
408 slot->callback_func = NULL;
409 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
410 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
411 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
412 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
413 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
414 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
415 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
420 int start_active_slot(struct active_request_slot *slot)
422 #ifdef USE_CURL_MULTI
423 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
426 if (curlm_result != CURLM_OK &&
427 curlm_result != CURLM_CALL_MULTI_PERFORM) {
434 * We know there must be something to do, since we just added
437 curl_multi_perform(curlm, &num_transfers);
442 #ifdef USE_CURL_MULTI
446 struct fill_chain *next;
449 static struct fill_chain *fill_cfg;
451 void add_fill_function(void *data, int (*fill)(void *))
453 struct fill_chain *new = xmalloc(sizeof(*new));
454 struct fill_chain **linkp = &fill_cfg;
459 linkp = &(*linkp)->next;
463 void fill_active_slots(void)
465 struct active_request_slot *slot = active_queue_head;
467 while (active_requests < max_requests) {
468 struct fill_chain *fill;
469 for (fill = fill_cfg; fill; fill = fill->next)
470 if (fill->fill(fill->data))
477 while (slot != NULL) {
478 if (!slot->in_use && slot->curl != NULL) {
479 curl_easy_cleanup(slot->curl);
486 void step_active_slots(void)
489 CURLMcode curlm_result;
492 curlm_result = curl_multi_perform(curlm, &num_transfers);
493 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
494 if (num_transfers < active_requests) {
495 process_curl_messages();
501 void run_active_slot(struct active_request_slot *slot)
503 #ifdef USE_CURL_MULTI
510 struct timeval select_timeout;
513 slot->finished = &finished;
518 if (!data_received && slot->local != NULL) {
519 current_pos = ftell(slot->local);
520 if (current_pos > last_pos)
522 last_pos = current_pos;
525 if (slot->in_use && !data_received) {
530 select_timeout.tv_sec = 0;
531 select_timeout.tv_usec = 50000;
532 select(max_fd, &readfds, &writefds,
533 &excfds, &select_timeout);
537 while (slot->in_use) {
538 slot->curl_result = curl_easy_perform(slot->curl);
539 finish_active_slot(slot);
544 static void closedown_active_slot(struct active_request_slot *slot)
550 void release_active_slot(struct active_request_slot *slot)
552 closedown_active_slot(slot);
554 #ifdef USE_CURL_MULTI
555 curl_multi_remove_handle(curlm, slot->curl);
557 curl_easy_cleanup(slot->curl);
560 #ifdef USE_CURL_MULTI
565 static void finish_active_slot(struct active_request_slot *slot)
567 closedown_active_slot(slot);
568 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
570 if (slot->finished != NULL)
571 (*slot->finished) = 1;
573 /* Store slot results so they can be read after the slot is reused */
574 if (slot->results != NULL) {
575 slot->results->curl_result = slot->curl_result;
576 slot->results->http_code = slot->http_code;
579 /* Run callback if appropriate */
580 if (slot->callback_func != NULL)
581 slot->callback_func(slot->callback_data);
584 void finish_all_active_slots(void)
586 struct active_request_slot *slot = active_queue_head;
590 run_active_slot(slot);
591 slot = active_queue_head;
597 static inline int needs_quote(int ch)
599 if (((ch >= 'A') && (ch <= 'Z'))
600 || ((ch >= 'a') && (ch <= 'z'))
601 || ((ch >= '0') && (ch <= '9'))
609 static inline int hex(int v)
617 static char *quote_ref_url(const char *base, const char *ref)
619 struct strbuf buf = STRBUF_INIT;
623 strbuf_addstr(&buf, base);
624 if (buf.len && buf.buf[buf.len - 1] != '/' && *ref != '/')
625 strbuf_addstr(&buf, "/");
627 for (cp = ref; (ch = *cp) != 0; cp++)
629 strbuf_addf(&buf, "%%%02x", ch);
631 strbuf_addch(&buf, *cp);
633 return strbuf_detach(&buf, NULL);
636 int http_fetch_ref(const char *base, struct ref *ref)
639 struct strbuf buffer = STRBUF_INIT;
640 struct active_request_slot *slot;
641 struct slot_results results;
644 url = quote_ref_url(base, ref->name);
645 slot = get_active_slot();
646 slot->results = &results;
647 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
648 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
649 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
650 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
651 if (start_active_slot(slot)) {
652 run_active_slot(slot);
653 if (results.curl_result == CURLE_OK) {
654 strbuf_rtrim(&buffer);
655 if (buffer.len == 40)
656 ret = get_sha1_hex(buffer.buf, ref->old_sha1);
657 else if (!prefixcmp(buffer.buf, "ref: ")) {
658 ref->symref = xstrdup(buffer.buf + 5);
663 ret = error("Couldn't get %s for %s\n%s",
664 url, ref->name, curl_errorstr);
667 ret = error("Unable to start request");
670 strbuf_release(&buffer);