]> git.ipfire.org Git - thirdparty/git.git/blame - http.c
Merge git://git.bogomips.org/git-svn
[thirdparty/git.git] / http.c
CommitLineData
29508e1e
NH
1#include "http.h"
2
3int data_received;
4int active_requests = 0;
5
6#ifdef USE_CURL_MULTI
cc3530e8
MH
7static int max_requests = -1;
8static CURLM *curlm;
29508e1e
NH
9#endif
10#ifndef NO_CURL_EASY_DUPHANDLE
cc3530e8 11static CURL *curl_default;
29508e1e
NH
12#endif
13char curl_errorstr[CURL_ERROR_SIZE];
14
cc3530e8 15static int curl_ssl_verify = -1;
7ef8ea70 16static const char *ssl_cert = NULL;
29508e1e 17#if LIBCURL_VERSION_NUM >= 0x070902
7ef8ea70 18static const char *ssl_key = NULL;
29508e1e
NH
19#endif
20#if LIBCURL_VERSION_NUM >= 0x070908
7ef8ea70 21static const char *ssl_capath = NULL;
29508e1e 22#endif
7ef8ea70 23static const char *ssl_cainfo = NULL;
cc3530e8
MH
24static long curl_low_speed_limit = -1;
25static long curl_low_speed_time = -1;
26static int curl_ftp_no_epsv = 0;
e4a80ecf 27static const char *curl_http_proxy = NULL;
29508e1e 28
cc3530e8 29static struct curl_slist *pragma_header;
29508e1e 30
cc3530e8 31static struct active_request_slot *active_queue_head = NULL;
29508e1e 32
f444e528 33size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb, void *buffer_)
29508e1e
NH
34{
35 size_t size = eltsize * nmemb;
f444e528
JH
36 struct buffer *buffer = buffer_;
37
028c2976
MH
38 if (size > buffer->buf.len - buffer->posn)
39 size = buffer->buf.len - buffer->posn;
40 memcpy(ptr, buffer->buf.buf + buffer->posn, size);
29508e1e 41 buffer->posn += size;
028c2976 42
29508e1e
NH
43 return size;
44}
45
f444e528 46size_t fwrite_buffer(const void *ptr, size_t eltsize, size_t nmemb, void *buffer_)
29508e1e
NH
47{
48 size_t size = eltsize * nmemb;
f444e528
JH
49 struct strbuf *buffer = buffer_;
50
028c2976 51 strbuf_add(buffer, ptr, size);
29508e1e
NH
52 data_received++;
53 return size;
54}
55
f444e528 56size_t fwrite_null(const void *ptr, size_t eltsize, size_t nmemb, void *strbuf)
29508e1e
NH
57{
58 data_received++;
59 return eltsize * nmemb;
60}
61
62static void finish_active_slot(struct active_request_slot *slot);
63
64#ifdef USE_CURL_MULTI
65static void process_curl_messages(void)
66{
67 int num_messages;
68 struct active_request_slot *slot;
69 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
70
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");
84 }
85 } else {
86 fprintf(stderr, "Unknown CURL message received: %d\n",
87 (int)curl_message->msg);
88 }
89 curl_message = curl_multi_info_read(curlm, &num_messages);
90 }
91}
92#endif
93
ef90d6d4 94static int http_options(const char *var, const char *value, void *cb)
29508e1e
NH
95{
96 if (!strcmp("http.sslverify", var)) {
97 if (curl_ssl_verify == -1) {
98 curl_ssl_verify = git_config_bool(var, value);
99 }
100 return 0;
101 }
102
103 if (!strcmp("http.sslcert", var)) {
7ef8ea70
BH
104 if (ssl_cert == NULL)
105 return git_config_string(&ssl_cert, var, value);
29508e1e
NH
106 return 0;
107 }
108#if LIBCURL_VERSION_NUM >= 0x070902
109 if (!strcmp("http.sslkey", var)) {
7ef8ea70
BH
110 if (ssl_key == NULL)
111 return git_config_string(&ssl_key, var, value);
29508e1e
NH
112 return 0;
113 }
114#endif
115#if LIBCURL_VERSION_NUM >= 0x070908
116 if (!strcmp("http.sslcapath", var)) {
7ef8ea70
BH
117 if (ssl_capath == NULL)
118 return git_config_string(&ssl_capath, var, value);
29508e1e
NH
119 return 0;
120 }
121#endif
122 if (!strcmp("http.sslcainfo", var)) {
7ef8ea70
BH
123 if (ssl_cainfo == NULL)
124 return git_config_string(&ssl_cainfo, var, value);
29508e1e
NH
125 return 0;
126 }
127
a6080a0a 128#ifdef USE_CURL_MULTI
29508e1e
NH
129 if (!strcmp("http.maxrequests", var)) {
130 if (max_requests == -1)
131 max_requests = git_config_int(var, value);
132 return 0;
133 }
134#endif
135
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;
140 }
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;
145 }
146
3ea099d4
SK
147 if (!strcmp("http.noepsv", var)) {
148 curl_ftp_no_epsv = git_config_bool(var, value);
149 return 0;
150 }
9c5665aa 151 if (!strcmp("http.proxy", var)) {
e4a80ecf
MV
152 if (curl_http_proxy == NULL)
153 return git_config_string(&curl_http_proxy, var, value);
9c5665aa
SV
154 return 0;
155 }
3ea099d4 156
29508e1e 157 /* Fall back on the default ones */
ef90d6d4 158 return git_default_config(var, value, cb);
29508e1e
NH
159}
160
11979b98
JH
161static CURL* get_curl_handle(void)
162{
163 CURL* result = curl_easy_init();
164
a5ccc597
JH
165 if (!curl_ssl_verify) {
166 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
167 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
168 } else {
169 /* Verify authenticity of the peer's certificate */
170 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
171 /* The name in the cert must match whom we tried to connect */
172 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
173 }
174
11979b98
JH
175#if LIBCURL_VERSION_NUM >= 0x070907
176 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
177#endif
178
179 if (ssl_cert != NULL)
180 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
181#if LIBCURL_VERSION_NUM >= 0x070902
182 if (ssl_key != NULL)
183 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
184#endif
185#if LIBCURL_VERSION_NUM >= 0x070908
186 if (ssl_capath != NULL)
187 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
188#endif
189 if (ssl_cainfo != NULL)
190 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
191 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
192
193 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
194 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
195 curl_low_speed_limit);
196 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
197 curl_low_speed_time);
198 }
199
200 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
201
7982d74e
MW
202 if (getenv("GIT_CURL_VERBOSE"))
203 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
204
20fc9bc5
NH
205 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
206
3ea099d4
SK
207 if (curl_ftp_no_epsv)
208 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
209
9c5665aa
SV
210 if (curl_http_proxy)
211 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
212
11979b98
JH
213 return result;
214}
215
9fc6440d 216void http_init(struct remote *remote)
29508e1e
NH
217{
218 char *low_speed_limit;
219 char *low_speed_time;
220
221 curl_global_init(CURL_GLOBAL_ALL);
222
9fc6440d
MH
223 if (remote && remote->http_proxy)
224 curl_http_proxy = xstrdup(remote->http_proxy);
225
29508e1e 226 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
29508e1e
NH
227
228#ifdef USE_CURL_MULTI
229 {
230 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
231 if (http_max_requests != NULL)
232 max_requests = atoi(http_max_requests);
233 }
234
235 curlm = curl_multi_init();
236 if (curlm == NULL) {
237 fprintf(stderr, "Error creating curl multi handle.\n");
238 exit(1);
239 }
240#endif
241
242 if (getenv("GIT_SSL_NO_VERIFY"))
243 curl_ssl_verify = 0;
244
245 ssl_cert = getenv("GIT_SSL_CERT");
246#if LIBCURL_VERSION_NUM >= 0x070902
247 ssl_key = getenv("GIT_SSL_KEY");
248#endif
249#if LIBCURL_VERSION_NUM >= 0x070908
250 ssl_capath = getenv("GIT_SSL_CAPATH");
251#endif
252 ssl_cainfo = getenv("GIT_SSL_CAINFO");
253
254 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
255 if (low_speed_limit != NULL)
256 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
257 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
258 if (low_speed_time != NULL)
259 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
260
ef90d6d4 261 git_config(http_options, NULL);
29508e1e
NH
262
263 if (curl_ssl_verify == -1)
264 curl_ssl_verify = 1;
265
266#ifdef USE_CURL_MULTI
267 if (max_requests < 1)
268 max_requests = DEFAULT_MAX_REQUESTS;
269#endif
270
3ea099d4
SK
271 if (getenv("GIT_CURL_FTP_NO_EPSV"))
272 curl_ftp_no_epsv = 1;
273
29508e1e
NH
274#ifndef NO_CURL_EASY_DUPHANDLE
275 curl_default = get_curl_handle();
276#endif
277}
278
279void http_cleanup(void)
280{
281 struct active_request_slot *slot = active_queue_head;
29508e1e
NH
282
283 while (slot != NULL) {
3278cd0a 284 struct active_request_slot *next = slot->next;
f23d1f76 285 if (slot->curl != NULL) {
29508e1e 286#ifdef USE_CURL_MULTI
f23d1f76 287 curl_multi_remove_handle(curlm, slot->curl);
29508e1e 288#endif
29508e1e 289 curl_easy_cleanup(slot->curl);
f23d1f76 290 }
3278cd0a
SP
291 free(slot);
292 slot = next;
29508e1e 293 }
3278cd0a 294 active_queue_head = NULL;
29508e1e
NH
295
296#ifndef NO_CURL_EASY_DUPHANDLE
297 curl_easy_cleanup(curl_default);
298#endif
299
300#ifdef USE_CURL_MULTI
301 curl_multi_cleanup(curlm);
302#endif
303 curl_global_cleanup();
b3ca4e4e
NH
304
305 curl_slist_free_all(pragma_header);
3278cd0a 306 pragma_header = NULL;
9fc6440d
MH
307
308 if (curl_http_proxy) {
e4a80ecf 309 free((void *)curl_http_proxy);
9fc6440d
MH
310 curl_http_proxy = NULL;
311 }
29508e1e
NH
312}
313
29508e1e
NH
314struct active_request_slot *get_active_slot(void)
315{
316 struct active_request_slot *slot = active_queue_head;
317 struct active_request_slot *newslot;
318
319#ifdef USE_CURL_MULTI
320 int num_transfers;
321
322 /* Wait for a slot to open up if the queue is full */
323 while (active_requests >= max_requests) {
324 curl_multi_perform(curlm, &num_transfers);
325 if (num_transfers < active_requests) {
326 process_curl_messages();
327 }
328 }
329#endif
330
331 while (slot != NULL && slot->in_use) {
332 slot = slot->next;
333 }
334 if (slot == NULL) {
335 newslot = xmalloc(sizeof(*newslot));
336 newslot->curl = NULL;
337 newslot->in_use = 0;
338 newslot->next = NULL;
339
340 slot = active_queue_head;
341 if (slot == NULL) {
342 active_queue_head = newslot;
343 } else {
344 while (slot->next != NULL) {
345 slot = slot->next;
346 }
347 slot->next = newslot;
348 }
349 slot = newslot;
350 }
351
352 if (slot->curl == NULL) {
353#ifdef NO_CURL_EASY_DUPHANDLE
354 slot->curl = get_curl_handle();
355#else
356 slot->curl = curl_easy_duphandle(curl_default);
357#endif
358 }
359
360 active_requests++;
361 slot->in_use = 1;
362 slot->local = NULL;
c8568e13 363 slot->results = NULL;
baa7b67d 364 slot->finished = NULL;
29508e1e
NH
365 slot->callback_data = NULL;
366 slot->callback_func = NULL;
367 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
29508e1e 368 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
9094950d
NH
369 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
370 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
371 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
372 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
373 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
29508e1e
NH
374
375 return slot;
376}
377
378int start_active_slot(struct active_request_slot *slot)
379{
380#ifdef USE_CURL_MULTI
381 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
45c17412 382 int num_transfers;
29508e1e
NH
383
384 if (curlm_result != CURLM_OK &&
385 curlm_result != CURLM_CALL_MULTI_PERFORM) {
386 active_requests--;
387 slot->in_use = 0;
388 return 0;
389 }
45c17412
DB
390
391 /*
392 * We know there must be something to do, since we just added
393 * something.
394 */
395 curl_multi_perform(curlm, &num_transfers);
29508e1e
NH
396#endif
397 return 1;
398}
399
400#ifdef USE_CURL_MULTI
fc57b6aa
DB
401struct fill_chain {
402 void *data;
403 int (*fill)(void *);
404 struct fill_chain *next;
405};
406
407static struct fill_chain *fill_cfg = NULL;
408
409void add_fill_function(void *data, int (*fill)(void *))
410{
e8eec71d 411 struct fill_chain *new = xmalloc(sizeof(*new));
fc57b6aa
DB
412 struct fill_chain **linkp = &fill_cfg;
413 new->data = data;
414 new->fill = fill;
415 new->next = NULL;
416 while (*linkp)
417 linkp = &(*linkp)->next;
418 *linkp = new;
419}
420
45c17412
DB
421void fill_active_slots(void)
422{
423 struct active_request_slot *slot = active_queue_head;
424
fc57b6aa
DB
425 while (active_requests < max_requests) {
426 struct fill_chain *fill;
427 for (fill = fill_cfg; fill; fill = fill->next)
428 if (fill->fill(fill->data))
429 break;
430
431 if (!fill)
45c17412 432 break;
fc57b6aa 433 }
45c17412
DB
434
435 while (slot != NULL) {
436 if (!slot->in_use && slot->curl != NULL) {
437 curl_easy_cleanup(slot->curl);
438 slot->curl = NULL;
439 }
440 slot = slot->next;
441 }
442}
443
29508e1e
NH
444void step_active_slots(void)
445{
446 int num_transfers;
447 CURLMcode curlm_result;
448
449 do {
450 curlm_result = curl_multi_perform(curlm, &num_transfers);
451 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
452 if (num_transfers < active_requests) {
453 process_curl_messages();
454 fill_active_slots();
455 }
456}
457#endif
458
459void run_active_slot(struct active_request_slot *slot)
460{
461#ifdef USE_CURL_MULTI
462 long last_pos = 0;
463 long current_pos;
464 fd_set readfds;
465 fd_set writefds;
466 fd_set excfds;
467 int max_fd;
468 struct timeval select_timeout;
baa7b67d 469 int finished = 0;
29508e1e 470
baa7b67d
NH
471 slot->finished = &finished;
472 while (!finished) {
29508e1e
NH
473 data_received = 0;
474 step_active_slots();
475
476 if (!data_received && slot->local != NULL) {
477 current_pos = ftell(slot->local);
478 if (current_pos > last_pos)
479 data_received++;
480 last_pos = current_pos;
481 }
482
483 if (slot->in_use && !data_received) {
484 max_fd = 0;
485 FD_ZERO(&readfds);
486 FD_ZERO(&writefds);
487 FD_ZERO(&excfds);
488 select_timeout.tv_sec = 0;
489 select_timeout.tv_usec = 50000;
490 select(max_fd, &readfds, &writefds,
491 &excfds, &select_timeout);
492 }
493 }
494#else
495 while (slot->in_use) {
496 slot->curl_result = curl_easy_perform(slot->curl);
497 finish_active_slot(slot);
498 }
499#endif
500}
501
53f31389 502static void closedown_active_slot(struct active_request_slot *slot)
29508e1e 503{
028c2976
MH
504 active_requests--;
505 slot->in_use = 0;
53f31389
MW
506}
507
508void release_active_slot(struct active_request_slot *slot)
509{
510 closedown_active_slot(slot);
511 if (slot->curl) {
b3ca4e4e 512#ifdef USE_CURL_MULTI
53f31389 513 curl_multi_remove_handle(curlm, slot->curl);
b3ca4e4e 514#endif
53f31389
MW
515 curl_easy_cleanup(slot->curl);
516 slot->curl = NULL;
517 }
b3ca4e4e 518#ifdef USE_CURL_MULTI
53f31389 519 fill_active_slots();
b3ca4e4e 520#endif
53f31389
MW
521}
522
523static void finish_active_slot(struct active_request_slot *slot)
524{
525 closedown_active_slot(slot);
028c2976 526 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
c8568e13 527
baa7b67d
NH
528 if (slot->finished != NULL)
529 (*slot->finished) = 1;
530
c8568e13
NH
531 /* Store slot results so they can be read after the slot is reused */
532 if (slot->results != NULL) {
533 slot->results->curl_result = slot->curl_result;
534 slot->results->http_code = slot->http_code;
535 }
536
028c2976
MH
537 /* Run callback if appropriate */
538 if (slot->callback_func != NULL) {
539 slot->callback_func(slot->callback_data);
540 }
29508e1e
NH
541}
542
543void finish_all_active_slots(void)
544{
545 struct active_request_slot *slot = active_queue_head;
546
547 while (slot != NULL)
548 if (slot->in_use) {
549 run_active_slot(slot);
550 slot = active_queue_head;
551 } else {
552 slot = slot->next;
553 }
554}
d7e92806
MH
555
556static inline int needs_quote(int ch)
557{
558 if (((ch >= 'A') && (ch <= 'Z'))
559 || ((ch >= 'a') && (ch <= 'z'))
560 || ((ch >= '0') && (ch <= '9'))
561 || (ch == '/')
562 || (ch == '-')
563 || (ch == '.'))
564 return 0;
565 return 1;
566}
567
568static inline int hex(int v)
569{
570 if (v < 10) return '0' + v;
571 else return 'A' + v - 10;
572}
573
574static char *quote_ref_url(const char *base, const char *ref)
575{
113106e0 576 struct strbuf buf = STRBUF_INIT;
d7e92806 577 const char *cp;
113106e0 578 int ch;
d7e92806 579
113106e0
TRC
580 strbuf_addstr(&buf, base);
581 if (buf.len && buf.buf[buf.len - 1] != '/' && *ref != '/')
582 strbuf_addstr(&buf, "/");
583
584 for (cp = ref; (ch = *cp) != 0; cp++)
d7e92806 585 if (needs_quote(ch))
113106e0 586 strbuf_addf(&buf, "%%%02x", ch);
d7e92806 587 else
113106e0 588 strbuf_addch(&buf, *cp);
d7e92806 589
113106e0 590 return strbuf_detach(&buf, NULL);
d7e92806
MH
591}
592
c13b2633 593int http_fetch_ref(const char *base, struct ref *ref)
d7e92806
MH
594{
595 char *url;
596 struct strbuf buffer = STRBUF_INIT;
597 struct active_request_slot *slot;
598 struct slot_results results;
599 int ret;
600
c13b2633 601 url = quote_ref_url(base, ref->name);
d7e92806
MH
602 slot = get_active_slot();
603 slot->results = &results;
604 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
605 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
606 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
607 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
608 if (start_active_slot(slot)) {
609 run_active_slot(slot);
610 if (results.curl_result == CURLE_OK) {
611 strbuf_rtrim(&buffer);
612 if (buffer.len == 40)
c13b2633 613 ret = get_sha1_hex(buffer.buf, ref->old_sha1);
be885d96
DB
614 else if (!prefixcmp(buffer.buf, "ref: ")) {
615 ref->symref = xstrdup(buffer.buf + 5);
616 ret = 0;
617 } else
d7e92806
MH
618 ret = 1;
619 } else {
620 ret = error("Couldn't get %s for %s\n%s",
c13b2633 621 url, ref->name, curl_errorstr);
d7e92806
MH
622 }
623 } else {
624 ret = error("Unable to start request");
625 }
626
627 strbuf_release(&buffer);
628 free(url);
629 return ret;
630}