]> git.ipfire.org Git - thirdparty/git.git/blame - http.c
Avoid redundant declaration of missing_target()
[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
MH
15static int curl_ssl_verify = -1;
16static char *ssl_cert = NULL;
29508e1e 17#if LIBCURL_VERSION_NUM >= 0x070902
cc3530e8 18static char *ssl_key = NULL;
29508e1e
NH
19#endif
20#if LIBCURL_VERSION_NUM >= 0x070908
cc3530e8 21static char *ssl_capath = NULL;
29508e1e 22#endif
cc3530e8
MH
23static char *ssl_cainfo = NULL;
24static long curl_low_speed_limit = -1;
25static long curl_low_speed_time = -1;
26static int curl_ftp_no_epsv = 0;
27static 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
NH
32
33size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb,
34 struct buffer *buffer)
35{
36 size_t size = eltsize * nmemb;
37 if (size > buffer->size - buffer->posn)
38 size = buffer->size - buffer->posn;
1d7f171c 39 memcpy(ptr, (char *) buffer->buffer + buffer->posn, size);
29508e1e
NH
40 buffer->posn += size;
41 return size;
42}
43
44size_t fwrite_buffer(const void *ptr, size_t eltsize,
45 size_t nmemb, struct buffer *buffer)
46{
47 size_t size = eltsize * nmemb;
48 if (size > buffer->size - buffer->posn) {
49 buffer->size = buffer->size * 3 / 2;
50 if (buffer->size < buffer->posn + size)
51 buffer->size = buffer->posn + size;
52 buffer->buffer = xrealloc(buffer->buffer, buffer->size);
53 }
1d7f171c 54 memcpy((char *) buffer->buffer + buffer->posn, ptr, size);
29508e1e
NH
55 buffer->posn += size;
56 data_received++;
57 return size;
58}
59
60size_t fwrite_null(const void *ptr, size_t eltsize,
61 size_t nmemb, struct buffer *buffer)
62{
63 data_received++;
64 return eltsize * nmemb;
65}
66
67static void finish_active_slot(struct active_request_slot *slot);
68
69#ifdef USE_CURL_MULTI
70static void process_curl_messages(void)
71{
72 int num_messages;
73 struct active_request_slot *slot;
74 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
75
76 while (curl_message != NULL) {
77 if (curl_message->msg == CURLMSG_DONE) {
78 int curl_result = curl_message->data.result;
79 slot = active_queue_head;
80 while (slot != NULL &&
81 slot->curl != curl_message->easy_handle)
82 slot = slot->next;
83 if (slot != NULL) {
84 curl_multi_remove_handle(curlm, slot->curl);
85 slot->curl_result = curl_result;
86 finish_active_slot(slot);
87 } else {
88 fprintf(stderr, "Received DONE message for unknown request!\n");
89 }
90 } else {
91 fprintf(stderr, "Unknown CURL message received: %d\n",
92 (int)curl_message->msg);
93 }
94 curl_message = curl_multi_info_read(curlm, &num_messages);
95 }
96}
97#endif
98
99static int http_options(const char *var, const char *value)
100{
101 if (!strcmp("http.sslverify", var)) {
102 if (curl_ssl_verify == -1) {
103 curl_ssl_verify = git_config_bool(var, value);
104 }
105 return 0;
106 }
107
108 if (!strcmp("http.sslcert", var)) {
109 if (ssl_cert == NULL) {
110 ssl_cert = xmalloc(strlen(value)+1);
111 strcpy(ssl_cert, value);
112 }
113 return 0;
114 }
115#if LIBCURL_VERSION_NUM >= 0x070902
116 if (!strcmp("http.sslkey", var)) {
117 if (ssl_key == NULL) {
118 ssl_key = xmalloc(strlen(value)+1);
119 strcpy(ssl_key, value);
120 }
121 return 0;
122 }
123#endif
124#if LIBCURL_VERSION_NUM >= 0x070908
125 if (!strcmp("http.sslcapath", var)) {
126 if (ssl_capath == NULL) {
127 ssl_capath = xmalloc(strlen(value)+1);
128 strcpy(ssl_capath, value);
129 }
130 return 0;
131 }
132#endif
133 if (!strcmp("http.sslcainfo", var)) {
134 if (ssl_cainfo == NULL) {
135 ssl_cainfo = xmalloc(strlen(value)+1);
136 strcpy(ssl_cainfo, value);
137 }
138 return 0;
139 }
140
a6080a0a 141#ifdef USE_CURL_MULTI
29508e1e
NH
142 if (!strcmp("http.maxrequests", var)) {
143 if (max_requests == -1)
144 max_requests = git_config_int(var, value);
145 return 0;
146 }
147#endif
148
149 if (!strcmp("http.lowspeedlimit", var)) {
150 if (curl_low_speed_limit == -1)
151 curl_low_speed_limit = (long)git_config_int(var, value);
152 return 0;
153 }
154 if (!strcmp("http.lowspeedtime", var)) {
155 if (curl_low_speed_time == -1)
156 curl_low_speed_time = (long)git_config_int(var, value);
157 return 0;
158 }
159
3ea099d4
SK
160 if (!strcmp("http.noepsv", var)) {
161 curl_ftp_no_epsv = git_config_bool(var, value);
162 return 0;
163 }
9c5665aa
SV
164 if (!strcmp("http.proxy", var)) {
165 if (curl_http_proxy == NULL) {
166 curl_http_proxy = xmalloc(strlen(value)+1);
167 strcpy(curl_http_proxy, value);
168 }
169 return 0;
170 }
3ea099d4 171
29508e1e
NH
172 /* Fall back on the default ones */
173 return git_default_config(var, value);
174}
175
11979b98
JH
176static CURL* get_curl_handle(void)
177{
178 CURL* result = curl_easy_init();
179
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
184
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);
198
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);
204 }
205
206 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
207
7982d74e
MW
208 if (getenv("GIT_CURL_VERBOSE"))
209 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
210
20fc9bc5
NH
211 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
212
3ea099d4
SK
213 if (curl_ftp_no_epsv)
214 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
215
9c5665aa
SV
216 if (curl_http_proxy)
217 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
218
11979b98
JH
219 return result;
220}
221
29508e1e
NH
222void http_init(void)
223{
224 char *low_speed_limit;
225 char *low_speed_time;
226
227 curl_global_init(CURL_GLOBAL_ALL);
228
229 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
29508e1e
NH
230
231#ifdef USE_CURL_MULTI
232 {
233 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
234 if (http_max_requests != NULL)
235 max_requests = atoi(http_max_requests);
236 }
237
238 curlm = curl_multi_init();
239 if (curlm == NULL) {
240 fprintf(stderr, "Error creating curl multi handle.\n");
241 exit(1);
242 }
243#endif
244
245 if (getenv("GIT_SSL_NO_VERIFY"))
246 curl_ssl_verify = 0;
247
248 ssl_cert = getenv("GIT_SSL_CERT");
249#if LIBCURL_VERSION_NUM >= 0x070902
250 ssl_key = getenv("GIT_SSL_KEY");
251#endif
252#if LIBCURL_VERSION_NUM >= 0x070908
253 ssl_capath = getenv("GIT_SSL_CAPATH");
254#endif
255 ssl_cainfo = getenv("GIT_SSL_CAINFO");
256
257 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
258 if (low_speed_limit != NULL)
259 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
260 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
261 if (low_speed_time != NULL)
262 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
263
264 git_config(http_options);
265
266 if (curl_ssl_verify == -1)
267 curl_ssl_verify = 1;
268
269#ifdef USE_CURL_MULTI
270 if (max_requests < 1)
271 max_requests = DEFAULT_MAX_REQUESTS;
272#endif
273
3ea099d4
SK
274 if (getenv("GIT_CURL_FTP_NO_EPSV"))
275 curl_ftp_no_epsv = 1;
276
29508e1e
NH
277#ifndef NO_CURL_EASY_DUPHANDLE
278 curl_default = get_curl_handle();
279#endif
280}
281
282void http_cleanup(void)
283{
284 struct active_request_slot *slot = active_queue_head;
285#ifdef USE_CURL_MULTI
286 char *wait_url;
29508e1e
NH
287#endif
288
289 while (slot != NULL) {
3278cd0a 290 struct active_request_slot *next = slot->next;
29508e1e
NH
291#ifdef USE_CURL_MULTI
292 if (slot->in_use) {
293 curl_easy_getinfo(slot->curl,
294 CURLINFO_EFFECTIVE_URL,
295 &wait_url);
296 fprintf(stderr, "Waiting for %s\n", wait_url);
297 run_active_slot(slot);
298 }
299#endif
300 if (slot->curl != NULL)
301 curl_easy_cleanup(slot->curl);
3278cd0a
SP
302 free(slot);
303 slot = next;
29508e1e 304 }
3278cd0a 305 active_queue_head = NULL;
29508e1e
NH
306
307#ifndef NO_CURL_EASY_DUPHANDLE
308 curl_easy_cleanup(curl_default);
309#endif
310
311#ifdef USE_CURL_MULTI
312 curl_multi_cleanup(curlm);
313#endif
314 curl_global_cleanup();
b3ca4e4e
NH
315
316 curl_slist_free_all(pragma_header);
3278cd0a 317 pragma_header = NULL;
29508e1e
NH
318}
319
29508e1e
NH
320struct active_request_slot *get_active_slot(void)
321{
322 struct active_request_slot *slot = active_queue_head;
323 struct active_request_slot *newslot;
324
325#ifdef USE_CURL_MULTI
326 int num_transfers;
327
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();
333 }
334 }
335#endif
336
337 while (slot != NULL && slot->in_use) {
338 slot = slot->next;
339 }
340 if (slot == NULL) {
341 newslot = xmalloc(sizeof(*newslot));
342 newslot->curl = NULL;
343 newslot->in_use = 0;
344 newslot->next = NULL;
345
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;
352 }
353 slot->next = newslot;
354 }
355 slot = newslot;
356 }
357
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
364 }
365
366 active_requests++;
367 slot->in_use = 1;
368 slot->local = NULL;
c8568e13 369 slot->results = NULL;
baa7b67d 370 slot->finished = NULL;
29508e1e
NH
371 slot->callback_data = NULL;
372 slot->callback_func = NULL;
373 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
29508e1e 374 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
9094950d
NH
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);
29508e1e
NH
380
381 return slot;
382}
383
384int start_active_slot(struct active_request_slot *slot)
385{
386#ifdef USE_CURL_MULTI
387 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
45c17412 388 int num_transfers;
29508e1e
NH
389
390 if (curlm_result != CURLM_OK &&
391 curlm_result != CURLM_CALL_MULTI_PERFORM) {
392 active_requests--;
393 slot->in_use = 0;
394 return 0;
395 }
45c17412
DB
396
397 /*
398 * We know there must be something to do, since we just added
399 * something.
400 */
401 curl_multi_perform(curlm, &num_transfers);
29508e1e
NH
402#endif
403 return 1;
404}
405
406#ifdef USE_CURL_MULTI
fc57b6aa
DB
407struct fill_chain {
408 void *data;
409 int (*fill)(void *);
410 struct fill_chain *next;
411};
412
413static struct fill_chain *fill_cfg = NULL;
414
415void add_fill_function(void *data, int (*fill)(void *))
416{
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;
425}
426
45c17412
DB
427void fill_active_slots(void)
428{
429 struct active_request_slot *slot = active_queue_head;
430
fc57b6aa
DB
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;
436
437 if (!fill)
45c17412 438 break;
fc57b6aa 439 }
45c17412
DB
440
441 while (slot != NULL) {
442 if (!slot->in_use && slot->curl != NULL) {
443 curl_easy_cleanup(slot->curl);
444 slot->curl = NULL;
445 }
446 slot = slot->next;
447 }
448}
449
29508e1e
NH
450void step_active_slots(void)
451{
452 int num_transfers;
453 CURLMcode curlm_result;
454
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();
461 }
462}
463#endif
464
465void run_active_slot(struct active_request_slot *slot)
466{
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;
baa7b67d 475 int finished = 0;
29508e1e 476
baa7b67d
NH
477 slot->finished = &finished;
478 while (!finished) {
29508e1e
NH
479 data_received = 0;
480 step_active_slots();
481
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;
487 }
488
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);
498 }
499 }
500#else
501 while (slot->in_use) {
502 slot->curl_result = curl_easy_perform(slot->curl);
503 finish_active_slot(slot);
504 }
505#endif
506}
507
53f31389 508static void closedown_active_slot(struct active_request_slot *slot)
29508e1e
NH
509{
510 active_requests--;
511 slot->in_use = 0;
53f31389
MW
512}
513
514void release_active_slot(struct active_request_slot *slot)
515{
516 closedown_active_slot(slot);
517 if (slot->curl) {
b3ca4e4e 518#ifdef USE_CURL_MULTI
53f31389 519 curl_multi_remove_handle(curlm, slot->curl);
b3ca4e4e 520#endif
53f31389
MW
521 curl_easy_cleanup(slot->curl);
522 slot->curl = NULL;
523 }
b3ca4e4e 524#ifdef USE_CURL_MULTI
53f31389 525 fill_active_slots();
b3ca4e4e 526#endif
53f31389
MW
527}
528
529static void finish_active_slot(struct active_request_slot *slot)
530{
531 closedown_active_slot(slot);
29508e1e 532 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
c8568e13 533
baa7b67d
NH
534 if (slot->finished != NULL)
535 (*slot->finished) = 1;
536
c8568e13
NH
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;
541 }
542
29508e1e
NH
543 /* Run callback if appropriate */
544 if (slot->callback_func != NULL) {
545 slot->callback_func(slot->callback_data);
546 }
547}
548
549void finish_all_active_slots(void)
550{
551 struct active_request_slot *slot = active_queue_head;
552
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;
559 }
560}