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