builtin-clone: use strbuf in clone_local() and copy_or_link_directory()
[git/mingw/j6t.git] / http.c
blobed59b79709b11dc6f6d85e86d75a1a8883799f21
1 #include "http.h"
3 int data_received;
4 int active_requests = 0;
6 #ifdef USE_CURL_MULTI
7 static int max_requests = -1;
8 static CURLM *curlm;
9 #endif
10 #ifndef NO_CURL_EASY_DUPHANDLE
11 static CURL *curl_default;
12 #endif
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;
19 #endif
20 #if LIBCURL_VERSION_NUM >= 0x070908
21 static const char *ssl_capath = NULL;
22 #endif
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);
41 buffer->posn += size;
43 return 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);
52 data_received++;
53 return size;
56 size_t fwrite_null(const void *ptr, size_t eltsize, size_t nmemb, void *strbuf)
58 data_received++;
59 return eltsize * nmemb;
62 static void finish_active_slot(struct active_request_slot *slot);
64 #ifdef USE_CURL_MULTI
65 static void process_curl_messages(void)
67 int num_messages;
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)
77 slot = slot->next;
78 if (slot != NULL) {
79 curl_multi_remove_handle(curlm, slot->curl);
80 slot->curl_result = curl_result;
81 finish_active_slot(slot);
82 } else {
83 fprintf(stderr, "Received DONE message for unknown request!\n");
85 } else {
86 fprintf(stderr, "Unknown CURL message received: %d\n",
87 (int)curl_message->msg);
89 curl_message = curl_multi_info_read(curlm, &num_messages);
92 #endif
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);
100 return 0;
103 if (!strcmp("http.sslcert", var)) {
104 if (ssl_cert == NULL)
105 return git_config_string(&ssl_cert, var, value);
106 return 0;
108 #if LIBCURL_VERSION_NUM >= 0x070902
109 if (!strcmp("http.sslkey", var)) {
110 if (ssl_key == NULL)
111 return git_config_string(&ssl_key, var, value);
112 return 0;
114 #endif
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);
119 return 0;
121 #endif
122 if (!strcmp("http.sslcainfo", var)) {
123 if (ssl_cainfo == NULL)
124 return git_config_string(&ssl_cainfo, var, value);
125 return 0;
128 #ifdef USE_CURL_MULTI
129 if (!strcmp("http.maxrequests", var)) {
130 if (max_requests == -1)
131 max_requests = git_config_int(var, value);
132 return 0;
134 #endif
136 if (!strcmp("http.lowspeedlimit", var)) {
137 if (curl_low_speed_limit == -1)
138 curl_low_speed_limit = (long)git_config_int(var, value);
139 return 0;
141 if (!strcmp("http.lowspeedtime", var)) {
142 if (curl_low_speed_time == -1)
143 curl_low_speed_time = (long)git_config_int(var, value);
144 return 0;
147 if (!strcmp("http.noepsv", var)) {
148 curl_ftp_no_epsv = git_config_bool(var, value);
149 return 0;
151 if (!strcmp("http.proxy", var)) {
152 if (curl_http_proxy == NULL) {
153 if (!value)
154 return config_error_nonbool(var);
155 curl_http_proxy = xstrdup(value);
157 return 0;
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);
171 } else {
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);
180 #endif
182 if (ssl_cert != NULL)
183 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
184 #if LIBCURL_VERSION_NUM >= 0x070902
185 if (ssl_key != NULL)
186 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
187 #endif
188 #if LIBCURL_VERSION_NUM >= 0x070908
189 if (ssl_capath != NULL)
190 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
191 #endif
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);
213 if (curl_http_proxy)
214 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
216 return result;
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();
239 if (curlm == NULL) {
240 fprintf(stderr, "Error creating curl multi handle.\n");
241 exit(1);
243 #endif
245 if (getenv("GIT_SSL_NO_VERIFY"))
246 curl_ssl_verify = 0;
248 ssl_cert = getenv("GIT_SSL_CERT");
249 #if LIBCURL_VERSION_NUM >= 0x070902
250 ssl_key = getenv("GIT_SSL_KEY");
251 #endif
252 #if LIBCURL_VERSION_NUM >= 0x070908
253 ssl_capath = getenv("GIT_SSL_CAPATH");
254 #endif
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)
267 curl_ssl_verify = 1;
269 #ifdef USE_CURL_MULTI
270 if (max_requests < 1)
271 max_requests = DEFAULT_MAX_REQUESTS;
272 #endif
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();
279 #endif
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);
291 #endif
292 curl_easy_cleanup(slot->curl);
294 free(slot);
295 slot = next;
297 active_queue_head = NULL;
299 #ifndef NO_CURL_EASY_DUPHANDLE
300 curl_easy_cleanup(curl_default);
301 #endif
303 #ifdef USE_CURL_MULTI
304 curl_multi_cleanup(curlm);
305 #endif
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
323 int num_transfers;
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();
332 #endif
334 while (slot != NULL && slot->in_use) {
335 slot = slot->next;
337 if (slot == NULL) {
338 newslot = xmalloc(sizeof(*newslot));
339 newslot->curl = NULL;
340 newslot->in_use = 0;
341 newslot->next = NULL;
343 slot = active_queue_head;
344 if (slot == NULL) {
345 active_queue_head = newslot;
346 } else {
347 while (slot->next != NULL) {
348 slot = slot->next;
350 slot->next = newslot;
352 slot = newslot;
355 if (slot->curl == NULL) {
356 #ifdef NO_CURL_EASY_DUPHANDLE
357 slot->curl = get_curl_handle();
358 #else
359 slot->curl = curl_easy_duphandle(curl_default);
360 #endif
363 active_requests++;
364 slot->in_use = 1;
365 slot->local = NULL;
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);
378 return slot;
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);
385 int num_transfers;
387 if (curlm_result != CURLM_OK &&
388 curlm_result != CURLM_CALL_MULTI_PERFORM) {
389 active_requests--;
390 slot->in_use = 0;
391 return 0;
395 * We know there must be something to do, since we just added
396 * something.
398 curl_multi_perform(curlm, &num_transfers);
399 #endif
400 return 1;
403 #ifdef USE_CURL_MULTI
404 struct fill_chain {
405 void *data;
406 int (*fill)(void *);
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;
416 new->data = data;
417 new->fill = fill;
418 new->next = NULL;
419 while (*linkp)
420 linkp = &(*linkp)->next;
421 *linkp = new;
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))
432 break;
434 if (!fill)
435 break;
438 while (slot != NULL) {
439 if (!slot->in_use && slot->curl != NULL) {
440 curl_easy_cleanup(slot->curl);
441 slot->curl = NULL;
443 slot = slot->next;
447 void step_active_slots(void)
449 int num_transfers;
450 CURLMcode curlm_result;
452 do {
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();
457 fill_active_slots();
460 #endif
462 void run_active_slot(struct active_request_slot *slot)
464 #ifdef USE_CURL_MULTI
465 long last_pos = 0;
466 long current_pos;
467 fd_set readfds;
468 fd_set writefds;
469 fd_set excfds;
470 int max_fd;
471 struct timeval select_timeout;
472 int finished = 0;
474 slot->finished = &finished;
475 while (!finished) {
476 data_received = 0;
477 step_active_slots();
479 if (!data_received && slot->local != NULL) {
480 current_pos = ftell(slot->local);
481 if (current_pos > last_pos)
482 data_received++;
483 last_pos = current_pos;
486 if (slot->in_use && !data_received) {
487 max_fd = 0;
488 FD_ZERO(&readfds);
489 FD_ZERO(&writefds);
490 FD_ZERO(&excfds);
491 select_timeout.tv_sec = 0;
492 select_timeout.tv_usec = 50000;
493 select(max_fd, &readfds, &writefds,
494 &excfds, &select_timeout);
497 #else
498 while (slot->in_use) {
499 slot->curl_result = curl_easy_perform(slot->curl);
500 finish_active_slot(slot);
502 #endif
505 static void closedown_active_slot(struct active_request_slot *slot)
507 active_requests--;
508 slot->in_use = 0;
511 void release_active_slot(struct active_request_slot *slot)
513 closedown_active_slot(slot);
514 if (slot->curl) {
515 #ifdef USE_CURL_MULTI
516 curl_multi_remove_handle(curlm, slot->curl);
517 #endif
518 curl_easy_cleanup(slot->curl);
519 slot->curl = NULL;
521 #ifdef USE_CURL_MULTI
522 fill_active_slots();
523 #endif
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;
550 while (slot != NULL)
551 if (slot->in_use) {
552 run_active_slot(slot);
553 slot = active_queue_head;
554 } else {
555 slot = slot->next;
559 static inline int needs_quote(int ch)
561 if (((ch >= 'A') && (ch <= 'Z'))
562 || ((ch >= 'a') && (ch <= 'z'))
563 || ((ch >= '0') && (ch <= '9'))
564 || (ch == '/')
565 || (ch == '-')
566 || (ch == '.'))
567 return 0;
568 return 1;
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)
579 const char *cp;
580 char *dp, *qref;
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++)
586 if (needs_quote(ch))
587 len += 2; /* extra two hex plus replacement % */
588 qref = xmalloc(len);
589 memcpy(qref, base, baselen);
590 dp = qref + baselen;
591 *(dp++) = '/';
592 for (cp = ref; (ch = *cp) != 0; cp++) {
593 if (needs_quote(ch)) {
594 *dp++ = '%';
595 *dp++ = hex((ch >> 4) & 0xF);
596 *dp++ = hex(ch & 0xF);
598 else
599 *dp++ = ch;
601 *dp = 0;
603 return qref;
606 int http_fetch_ref(const char *base, struct ref *ref)
608 char *url;
609 struct strbuf buffer = STRBUF_INIT;
610 struct active_request_slot *slot;
611 struct slot_results results;
612 int ret;
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);
629 ret = 0;
630 } else
631 ret = 1;
632 } else {
633 ret = error("Couldn't get %s for %s\n%s",
634 url, ref->name, curl_errorstr);
636 } else {
637 ret = error("Unable to start request");
640 strbuf_release(&buffer);
641 free(url);
642 return ret;