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