convert.c: Use 'git_config_string' to get 'smudge' and 'clean'
[git/debian.git] / http.c
blobad146404129b8522269605076e8391542a3241f5
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 char *ssl_cert = NULL;
17 #if LIBCURL_VERSION_NUM >= 0x070902
18 static char *ssl_key = NULL;
19 #endif
20 #if LIBCURL_VERSION_NUM >= 0x070908
21 static char *ssl_capath = NULL;
22 #endif
23 static 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 if (!value)
106 return config_error_nonbool(var);
107 ssl_cert = xstrdup(value);
109 return 0;
111 #if LIBCURL_VERSION_NUM >= 0x070902
112 if (!strcmp("http.sslkey", var)) {
113 if (ssl_key == NULL) {
114 if (!value)
115 return config_error_nonbool(var);
116 ssl_key = xstrdup(value);
118 return 0;
120 #endif
121 #if LIBCURL_VERSION_NUM >= 0x070908
122 if (!strcmp("http.sslcapath", var)) {
123 if (ssl_capath == NULL) {
124 if (!value)
125 return config_error_nonbool(var);
126 ssl_capath = xstrdup(value);
128 return 0;
130 #endif
131 if (!strcmp("http.sslcainfo", var)) {
132 if (ssl_cainfo == NULL) {
133 if (!value)
134 return config_error_nonbool(var);
135 ssl_cainfo = xstrdup(value);
137 return 0;
140 #ifdef USE_CURL_MULTI
141 if (!strcmp("http.maxrequests", var)) {
142 if (max_requests == -1)
143 max_requests = git_config_int(var, value);
144 return 0;
146 #endif
148 if (!strcmp("http.lowspeedlimit", var)) {
149 if (curl_low_speed_limit == -1)
150 curl_low_speed_limit = (long)git_config_int(var, value);
151 return 0;
153 if (!strcmp("http.lowspeedtime", var)) {
154 if (curl_low_speed_time == -1)
155 curl_low_speed_time = (long)git_config_int(var, value);
156 return 0;
159 if (!strcmp("http.noepsv", var)) {
160 curl_ftp_no_epsv = git_config_bool(var, value);
161 return 0;
163 if (!strcmp("http.proxy", var)) {
164 if (curl_http_proxy == NULL) {
165 if (!value)
166 return config_error_nonbool(var);
167 curl_http_proxy = xstrdup(value);
169 return 0;
172 /* Fall back on the default ones */
173 return git_default_config(var, value, cb);
176 static CURL* get_curl_handle(void)
178 CURL* result = curl_easy_init();
180 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
181 #if LIBCURL_VERSION_NUM >= 0x070907
182 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
183 #endif
185 if (ssl_cert != NULL)
186 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
187 #if LIBCURL_VERSION_NUM >= 0x070902
188 if (ssl_key != NULL)
189 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
190 #endif
191 #if LIBCURL_VERSION_NUM >= 0x070908
192 if (ssl_capath != NULL)
193 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
194 #endif
195 if (ssl_cainfo != NULL)
196 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
197 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
199 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
200 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
201 curl_low_speed_limit);
202 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
203 curl_low_speed_time);
206 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
208 if (getenv("GIT_CURL_VERBOSE"))
209 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
211 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
213 if (curl_ftp_no_epsv)
214 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
216 if (curl_http_proxy)
217 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
219 return result;
222 void http_init(struct remote *remote)
224 char *low_speed_limit;
225 char *low_speed_time;
227 curl_global_init(CURL_GLOBAL_ALL);
229 if (remote && remote->http_proxy)
230 curl_http_proxy = xstrdup(remote->http_proxy);
232 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
234 #ifdef USE_CURL_MULTI
236 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
237 if (http_max_requests != NULL)
238 max_requests = atoi(http_max_requests);
241 curlm = curl_multi_init();
242 if (curlm == NULL) {
243 fprintf(stderr, "Error creating curl multi handle.\n");
244 exit(1);
246 #endif
248 if (getenv("GIT_SSL_NO_VERIFY"))
249 curl_ssl_verify = 0;
251 ssl_cert = getenv("GIT_SSL_CERT");
252 #if LIBCURL_VERSION_NUM >= 0x070902
253 ssl_key = getenv("GIT_SSL_KEY");
254 #endif
255 #if LIBCURL_VERSION_NUM >= 0x070908
256 ssl_capath = getenv("GIT_SSL_CAPATH");
257 #endif
258 ssl_cainfo = getenv("GIT_SSL_CAINFO");
260 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
261 if (low_speed_limit != NULL)
262 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
263 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
264 if (low_speed_time != NULL)
265 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
267 git_config(http_options, NULL);
269 if (curl_ssl_verify == -1)
270 curl_ssl_verify = 1;
272 #ifdef USE_CURL_MULTI
273 if (max_requests < 1)
274 max_requests = DEFAULT_MAX_REQUESTS;
275 #endif
277 if (getenv("GIT_CURL_FTP_NO_EPSV"))
278 curl_ftp_no_epsv = 1;
280 #ifndef NO_CURL_EASY_DUPHANDLE
281 curl_default = get_curl_handle();
282 #endif
285 void http_cleanup(void)
287 struct active_request_slot *slot = active_queue_head;
289 while (slot != NULL) {
290 struct active_request_slot *next = slot->next;
291 if (slot->curl != NULL) {
292 #ifdef USE_CURL_MULTI
293 curl_multi_remove_handle(curlm, slot->curl);
294 #endif
295 curl_easy_cleanup(slot->curl);
297 free(slot);
298 slot = next;
300 active_queue_head = NULL;
302 #ifndef NO_CURL_EASY_DUPHANDLE
303 curl_easy_cleanup(curl_default);
304 #endif
306 #ifdef USE_CURL_MULTI
307 curl_multi_cleanup(curlm);
308 #endif
309 curl_global_cleanup();
311 curl_slist_free_all(pragma_header);
312 pragma_header = NULL;
314 if (curl_http_proxy) {
315 free(curl_http_proxy);
316 curl_http_proxy = NULL;
320 struct active_request_slot *get_active_slot(void)
322 struct active_request_slot *slot = active_queue_head;
323 struct active_request_slot *newslot;
325 #ifdef USE_CURL_MULTI
326 int num_transfers;
328 /* Wait for a slot to open up if the queue is full */
329 while (active_requests >= max_requests) {
330 curl_multi_perform(curlm, &num_transfers);
331 if (num_transfers < active_requests) {
332 process_curl_messages();
335 #endif
337 while (slot != NULL && slot->in_use) {
338 slot = slot->next;
340 if (slot == NULL) {
341 newslot = xmalloc(sizeof(*newslot));
342 newslot->curl = NULL;
343 newslot->in_use = 0;
344 newslot->next = NULL;
346 slot = active_queue_head;
347 if (slot == NULL) {
348 active_queue_head = newslot;
349 } else {
350 while (slot->next != NULL) {
351 slot = slot->next;
353 slot->next = newslot;
355 slot = newslot;
358 if (slot->curl == NULL) {
359 #ifdef NO_CURL_EASY_DUPHANDLE
360 slot->curl = get_curl_handle();
361 #else
362 slot->curl = curl_easy_duphandle(curl_default);
363 #endif
366 active_requests++;
367 slot->in_use = 1;
368 slot->local = NULL;
369 slot->results = NULL;
370 slot->finished = NULL;
371 slot->callback_data = NULL;
372 slot->callback_func = NULL;
373 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
374 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
375 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
376 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
377 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
378 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
379 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
381 return slot;
384 int start_active_slot(struct active_request_slot *slot)
386 #ifdef USE_CURL_MULTI
387 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
388 int num_transfers;
390 if (curlm_result != CURLM_OK &&
391 curlm_result != CURLM_CALL_MULTI_PERFORM) {
392 active_requests--;
393 slot->in_use = 0;
394 return 0;
398 * We know there must be something to do, since we just added
399 * something.
401 curl_multi_perform(curlm, &num_transfers);
402 #endif
403 return 1;
406 #ifdef USE_CURL_MULTI
407 struct fill_chain {
408 void *data;
409 int (*fill)(void *);
410 struct fill_chain *next;
413 static struct fill_chain *fill_cfg = NULL;
415 void add_fill_function(void *data, int (*fill)(void *))
417 struct fill_chain *new = malloc(sizeof(*new));
418 struct fill_chain **linkp = &fill_cfg;
419 new->data = data;
420 new->fill = fill;
421 new->next = NULL;
422 while (*linkp)
423 linkp = &(*linkp)->next;
424 *linkp = new;
427 void fill_active_slots(void)
429 struct active_request_slot *slot = active_queue_head;
431 while (active_requests < max_requests) {
432 struct fill_chain *fill;
433 for (fill = fill_cfg; fill; fill = fill->next)
434 if (fill->fill(fill->data))
435 break;
437 if (!fill)
438 break;
441 while (slot != NULL) {
442 if (!slot->in_use && slot->curl != NULL) {
443 curl_easy_cleanup(slot->curl);
444 slot->curl = NULL;
446 slot = slot->next;
450 void step_active_slots(void)
452 int num_transfers;
453 CURLMcode curlm_result;
455 do {
456 curlm_result = curl_multi_perform(curlm, &num_transfers);
457 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
458 if (num_transfers < active_requests) {
459 process_curl_messages();
460 fill_active_slots();
463 #endif
465 void run_active_slot(struct active_request_slot *slot)
467 #ifdef USE_CURL_MULTI
468 long last_pos = 0;
469 long current_pos;
470 fd_set readfds;
471 fd_set writefds;
472 fd_set excfds;
473 int max_fd;
474 struct timeval select_timeout;
475 int finished = 0;
477 slot->finished = &finished;
478 while (!finished) {
479 data_received = 0;
480 step_active_slots();
482 if (!data_received && slot->local != NULL) {
483 current_pos = ftell(slot->local);
484 if (current_pos > last_pos)
485 data_received++;
486 last_pos = current_pos;
489 if (slot->in_use && !data_received) {
490 max_fd = 0;
491 FD_ZERO(&readfds);
492 FD_ZERO(&writefds);
493 FD_ZERO(&excfds);
494 select_timeout.tv_sec = 0;
495 select_timeout.tv_usec = 50000;
496 select(max_fd, &readfds, &writefds,
497 &excfds, &select_timeout);
500 #else
501 while (slot->in_use) {
502 slot->curl_result = curl_easy_perform(slot->curl);
503 finish_active_slot(slot);
505 #endif
508 static void closedown_active_slot(struct active_request_slot *slot)
510 active_requests--;
511 slot->in_use = 0;
514 void release_active_slot(struct active_request_slot *slot)
516 closedown_active_slot(slot);
517 if (slot->curl) {
518 #ifdef USE_CURL_MULTI
519 curl_multi_remove_handle(curlm, slot->curl);
520 #endif
521 curl_easy_cleanup(slot->curl);
522 slot->curl = NULL;
524 #ifdef USE_CURL_MULTI
525 fill_active_slots();
526 #endif
529 static void finish_active_slot(struct active_request_slot *slot)
531 closedown_active_slot(slot);
532 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
534 if (slot->finished != NULL)
535 (*slot->finished) = 1;
537 /* Store slot results so they can be read after the slot is reused */
538 if (slot->results != NULL) {
539 slot->results->curl_result = slot->curl_result;
540 slot->results->http_code = slot->http_code;
543 /* Run callback if appropriate */
544 if (slot->callback_func != NULL) {
545 slot->callback_func(slot->callback_data);
549 void finish_all_active_slots(void)
551 struct active_request_slot *slot = active_queue_head;
553 while (slot != NULL)
554 if (slot->in_use) {
555 run_active_slot(slot);
556 slot = active_queue_head;
557 } else {
558 slot = slot->next;
562 static inline int needs_quote(int ch)
564 if (((ch >= 'A') && (ch <= 'Z'))
565 || ((ch >= 'a') && (ch <= 'z'))
566 || ((ch >= '0') && (ch <= '9'))
567 || (ch == '/')
568 || (ch == '-')
569 || (ch == '.'))
570 return 0;
571 return 1;
574 static inline int hex(int v)
576 if (v < 10) return '0' + v;
577 else return 'A' + v - 10;
580 static char *quote_ref_url(const char *base, const char *ref)
582 const char *cp;
583 char *dp, *qref;
584 int len, baselen, ch;
586 baselen = strlen(base);
587 len = baselen + 2; /* '/' after base and terminating NUL */
588 for (cp = ref; (ch = *cp) != 0; cp++, len++)
589 if (needs_quote(ch))
590 len += 2; /* extra two hex plus replacement % */
591 qref = xmalloc(len);
592 memcpy(qref, base, baselen);
593 dp = qref + baselen;
594 *(dp++) = '/';
595 for (cp = ref; (ch = *cp) != 0; cp++) {
596 if (needs_quote(ch)) {
597 *dp++ = '%';
598 *dp++ = hex((ch >> 4) & 0xF);
599 *dp++ = hex(ch & 0xF);
601 else
602 *dp++ = ch;
604 *dp = 0;
606 return qref;
609 int http_fetch_ref(const char *base, struct ref *ref)
611 char *url;
612 struct strbuf buffer = STRBUF_INIT;
613 struct active_request_slot *slot;
614 struct slot_results results;
615 int ret;
617 url = quote_ref_url(base, ref->name);
618 slot = get_active_slot();
619 slot->results = &results;
620 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
621 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
622 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
623 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
624 if (start_active_slot(slot)) {
625 run_active_slot(slot);
626 if (results.curl_result == CURLE_OK) {
627 strbuf_rtrim(&buffer);
628 if (buffer.len == 40)
629 ret = get_sha1_hex(buffer.buf, ref->old_sha1);
630 else if (!prefixcmp(buffer.buf, "ref: ")) {
631 ref->symref = xstrdup(buffer.buf + 5);
632 ret = 0;
633 } else
634 ret = 1;
635 } else {
636 ret = error("Couldn't get %s for %s\n%s",
637 url, ref->name, curl_errorstr);
639 } else {
640 ret = error("Unable to start request");
643 strbuf_release(&buffer);
644 free(url);
645 return ret;