GIT 1.6.0-rc3
[git/mingw/4msysgit/kblees.git] / http.c
blob1108ab4a3101fb4768cad420ccfdb52d87890a18
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 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
169 #if LIBCURL_VERSION_NUM >= 0x070907
170 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
171 #endif
173 if (ssl_cert != NULL)
174 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
175 #if LIBCURL_VERSION_NUM >= 0x070902
176 if (ssl_key != NULL)
177 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
178 #endif
179 #if LIBCURL_VERSION_NUM >= 0x070908
180 if (ssl_capath != NULL)
181 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
182 #endif
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);
204 if (curl_http_proxy)
205 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
207 return result;
210 void http_init(struct remote *remote)
212 char *low_speed_limit;
213 char *low_speed_time;
215 curl_global_init(CURL_GLOBAL_ALL);
217 if (remote && remote->http_proxy)
218 curl_http_proxy = xstrdup(remote->http_proxy);
220 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
222 #ifdef USE_CURL_MULTI
224 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
225 if (http_max_requests != NULL)
226 max_requests = atoi(http_max_requests);
229 curlm = curl_multi_init();
230 if (curlm == NULL) {
231 fprintf(stderr, "Error creating curl multi handle.\n");
232 exit(1);
234 #endif
236 if (getenv("GIT_SSL_NO_VERIFY"))
237 curl_ssl_verify = 0;
239 ssl_cert = getenv("GIT_SSL_CERT");
240 #if LIBCURL_VERSION_NUM >= 0x070902
241 ssl_key = getenv("GIT_SSL_KEY");
242 #endif
243 #if LIBCURL_VERSION_NUM >= 0x070908
244 ssl_capath = getenv("GIT_SSL_CAPATH");
245 #endif
246 ssl_cainfo = getenv("GIT_SSL_CAINFO");
248 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
249 if (low_speed_limit != NULL)
250 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
251 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
252 if (low_speed_time != NULL)
253 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
255 git_config(http_options, NULL);
257 if (curl_ssl_verify == -1)
258 curl_ssl_verify = 1;
260 #ifdef USE_CURL_MULTI
261 if (max_requests < 1)
262 max_requests = DEFAULT_MAX_REQUESTS;
263 #endif
265 if (getenv("GIT_CURL_FTP_NO_EPSV"))
266 curl_ftp_no_epsv = 1;
268 #ifndef NO_CURL_EASY_DUPHANDLE
269 curl_default = get_curl_handle();
270 #endif
273 void http_cleanup(void)
275 struct active_request_slot *slot = active_queue_head;
277 while (slot != NULL) {
278 struct active_request_slot *next = slot->next;
279 if (slot->curl != NULL) {
280 #ifdef USE_CURL_MULTI
281 curl_multi_remove_handle(curlm, slot->curl);
282 #endif
283 curl_easy_cleanup(slot->curl);
285 free(slot);
286 slot = next;
288 active_queue_head = NULL;
290 #ifndef NO_CURL_EASY_DUPHANDLE
291 curl_easy_cleanup(curl_default);
292 #endif
294 #ifdef USE_CURL_MULTI
295 curl_multi_cleanup(curlm);
296 #endif
297 curl_global_cleanup();
299 curl_slist_free_all(pragma_header);
300 pragma_header = NULL;
302 if (curl_http_proxy) {
303 free(curl_http_proxy);
304 curl_http_proxy = NULL;
308 struct active_request_slot *get_active_slot(void)
310 struct active_request_slot *slot = active_queue_head;
311 struct active_request_slot *newslot;
313 #ifdef USE_CURL_MULTI
314 int num_transfers;
316 /* Wait for a slot to open up if the queue is full */
317 while (active_requests >= max_requests) {
318 curl_multi_perform(curlm, &num_transfers);
319 if (num_transfers < active_requests) {
320 process_curl_messages();
323 #endif
325 while (slot != NULL && slot->in_use) {
326 slot = slot->next;
328 if (slot == NULL) {
329 newslot = xmalloc(sizeof(*newslot));
330 newslot->curl = NULL;
331 newslot->in_use = 0;
332 newslot->next = NULL;
334 slot = active_queue_head;
335 if (slot == NULL) {
336 active_queue_head = newslot;
337 } else {
338 while (slot->next != NULL) {
339 slot = slot->next;
341 slot->next = newslot;
343 slot = newslot;
346 if (slot->curl == NULL) {
347 #ifdef NO_CURL_EASY_DUPHANDLE
348 slot->curl = get_curl_handle();
349 #else
350 slot->curl = curl_easy_duphandle(curl_default);
351 #endif
354 active_requests++;
355 slot->in_use = 1;
356 slot->local = NULL;
357 slot->results = NULL;
358 slot->finished = NULL;
359 slot->callback_data = NULL;
360 slot->callback_func = NULL;
361 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
362 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
363 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
364 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
365 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
366 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
367 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
369 return slot;
372 int start_active_slot(struct active_request_slot *slot)
374 #ifdef USE_CURL_MULTI
375 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
376 int num_transfers;
378 if (curlm_result != CURLM_OK &&
379 curlm_result != CURLM_CALL_MULTI_PERFORM) {
380 active_requests--;
381 slot->in_use = 0;
382 return 0;
386 * We know there must be something to do, since we just added
387 * something.
389 curl_multi_perform(curlm, &num_transfers);
390 #endif
391 return 1;
394 #ifdef USE_CURL_MULTI
395 struct fill_chain {
396 void *data;
397 int (*fill)(void *);
398 struct fill_chain *next;
401 static struct fill_chain *fill_cfg = NULL;
403 void add_fill_function(void *data, int (*fill)(void *))
405 struct fill_chain *new = malloc(sizeof(*new));
406 struct fill_chain **linkp = &fill_cfg;
407 new->data = data;
408 new->fill = fill;
409 new->next = NULL;
410 while (*linkp)
411 linkp = &(*linkp)->next;
412 *linkp = new;
415 void fill_active_slots(void)
417 struct active_request_slot *slot = active_queue_head;
419 while (active_requests < max_requests) {
420 struct fill_chain *fill;
421 for (fill = fill_cfg; fill; fill = fill->next)
422 if (fill->fill(fill->data))
423 break;
425 if (!fill)
426 break;
429 while (slot != NULL) {
430 if (!slot->in_use && slot->curl != NULL) {
431 curl_easy_cleanup(slot->curl);
432 slot->curl = NULL;
434 slot = slot->next;
438 void step_active_slots(void)
440 int num_transfers;
441 CURLMcode curlm_result;
443 do {
444 curlm_result = curl_multi_perform(curlm, &num_transfers);
445 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
446 if (num_transfers < active_requests) {
447 process_curl_messages();
448 fill_active_slots();
451 #endif
453 void run_active_slot(struct active_request_slot *slot)
455 #ifdef USE_CURL_MULTI
456 long last_pos = 0;
457 long current_pos;
458 fd_set readfds;
459 fd_set writefds;
460 fd_set excfds;
461 int max_fd;
462 struct timeval select_timeout;
463 int finished = 0;
465 slot->finished = &finished;
466 while (!finished) {
467 data_received = 0;
468 step_active_slots();
470 if (!data_received && slot->local != NULL) {
471 current_pos = ftell(slot->local);
472 if (current_pos > last_pos)
473 data_received++;
474 last_pos = current_pos;
477 if (slot->in_use && !data_received) {
478 max_fd = 0;
479 FD_ZERO(&readfds);
480 FD_ZERO(&writefds);
481 FD_ZERO(&excfds);
482 select_timeout.tv_sec = 0;
483 select_timeout.tv_usec = 50000;
484 select(max_fd, &readfds, &writefds,
485 &excfds, &select_timeout);
488 #else
489 while (slot->in_use) {
490 slot->curl_result = curl_easy_perform(slot->curl);
491 finish_active_slot(slot);
493 #endif
496 static void closedown_active_slot(struct active_request_slot *slot)
498 active_requests--;
499 slot->in_use = 0;
502 void release_active_slot(struct active_request_slot *slot)
504 closedown_active_slot(slot);
505 if (slot->curl) {
506 #ifdef USE_CURL_MULTI
507 curl_multi_remove_handle(curlm, slot->curl);
508 #endif
509 curl_easy_cleanup(slot->curl);
510 slot->curl = NULL;
512 #ifdef USE_CURL_MULTI
513 fill_active_slots();
514 #endif
517 static void finish_active_slot(struct active_request_slot *slot)
519 closedown_active_slot(slot);
520 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
522 if (slot->finished != NULL)
523 (*slot->finished) = 1;
525 /* Store slot results so they can be read after the slot is reused */
526 if (slot->results != NULL) {
527 slot->results->curl_result = slot->curl_result;
528 slot->results->http_code = slot->http_code;
531 /* Run callback if appropriate */
532 if (slot->callback_func != NULL) {
533 slot->callback_func(slot->callback_data);
537 void finish_all_active_slots(void)
539 struct active_request_slot *slot = active_queue_head;
541 while (slot != NULL)
542 if (slot->in_use) {
543 run_active_slot(slot);
544 slot = active_queue_head;
545 } else {
546 slot = slot->next;
550 static inline int needs_quote(int ch)
552 if (((ch >= 'A') && (ch <= 'Z'))
553 || ((ch >= 'a') && (ch <= 'z'))
554 || ((ch >= '0') && (ch <= '9'))
555 || (ch == '/')
556 || (ch == '-')
557 || (ch == '.'))
558 return 0;
559 return 1;
562 static inline int hex(int v)
564 if (v < 10) return '0' + v;
565 else return 'A' + v - 10;
568 static char *quote_ref_url(const char *base, const char *ref)
570 const char *cp;
571 char *dp, *qref;
572 int len, baselen, ch;
574 baselen = strlen(base);
575 len = baselen + 2; /* '/' after base and terminating NUL */
576 for (cp = ref; (ch = *cp) != 0; cp++, len++)
577 if (needs_quote(ch))
578 len += 2; /* extra two hex plus replacement % */
579 qref = xmalloc(len);
580 memcpy(qref, base, baselen);
581 dp = qref + baselen;
582 *(dp++) = '/';
583 for (cp = ref; (ch = *cp) != 0; cp++) {
584 if (needs_quote(ch)) {
585 *dp++ = '%';
586 *dp++ = hex((ch >> 4) & 0xF);
587 *dp++ = hex(ch & 0xF);
589 else
590 *dp++ = ch;
592 *dp = 0;
594 return qref;
597 int http_fetch_ref(const char *base, struct ref *ref)
599 char *url;
600 struct strbuf buffer = STRBUF_INIT;
601 struct active_request_slot *slot;
602 struct slot_results results;
603 int ret;
605 url = quote_ref_url(base, ref->name);
606 slot = get_active_slot();
607 slot->results = &results;
608 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
609 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
610 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
611 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
612 if (start_active_slot(slot)) {
613 run_active_slot(slot);
614 if (results.curl_result == CURLE_OK) {
615 strbuf_rtrim(&buffer);
616 if (buffer.len == 40)
617 ret = get_sha1_hex(buffer.buf, ref->old_sha1);
618 else if (!prefixcmp(buffer.buf, "ref: ")) {
619 ref->symref = xstrdup(buffer.buf + 5);
620 ret = 0;
621 } else
622 ret = 1;
623 } else {
624 ret = error("Couldn't get %s for %s\n%s",
625 url, ref->name, curl_errorstr);
627 } else {
628 ret = error("Unable to start request");
631 strbuf_release(&buffer);
632 free(url);
633 return ret;