git checkout's reflog: even when detaching the HEAD, say from where
[git/platforms.git] / http.c
blob87ebf7b86548d229afbfd9263d2470296a7b2ac7
1 #include "http.h"
3 int data_received;
4 int active_requests = 0;
6 #ifdef USE_CURL_MULTI
7 int max_requests = -1;
8 CURLM *curlm;
9 #endif
10 #ifndef NO_CURL_EASY_DUPHANDLE
11 CURL *curl_default;
12 #endif
13 char curl_errorstr[CURL_ERROR_SIZE];
15 int curl_ssl_verify = -1;
16 char *ssl_cert = NULL;
17 #if LIBCURL_VERSION_NUM >= 0x070902
18 char *ssl_key = NULL;
19 #endif
20 #if LIBCURL_VERSION_NUM >= 0x070908
21 char *ssl_capath = NULL;
22 #endif
23 char *ssl_cainfo = NULL;
24 long curl_low_speed_limit = -1;
25 long curl_low_speed_time = -1;
26 int curl_ftp_no_epsv = 0;
28 struct curl_slist *pragma_header;
30 struct active_request_slot *active_queue_head = NULL;
32 size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb,
33 struct buffer *buffer)
35 size_t size = eltsize * nmemb;
36 if (size > buffer->size - buffer->posn)
37 size = buffer->size - buffer->posn;
38 memcpy(ptr, (char *) buffer->buffer + buffer->posn, size);
39 buffer->posn += size;
40 return size;
43 size_t fwrite_buffer(const void *ptr, size_t eltsize,
44 size_t nmemb, struct buffer *buffer)
46 size_t size = eltsize * nmemb;
47 if (size > buffer->size - buffer->posn) {
48 buffer->size = buffer->size * 3 / 2;
49 if (buffer->size < buffer->posn + size)
50 buffer->size = buffer->posn + size;
51 buffer->buffer = xrealloc(buffer->buffer, buffer->size);
53 memcpy((char *) buffer->buffer + buffer->posn, ptr, size);
54 buffer->posn += size;
55 data_received++;
56 return size;
59 size_t fwrite_null(const void *ptr, size_t eltsize,
60 size_t nmemb, struct buffer *buffer)
62 data_received++;
63 return eltsize * nmemb;
66 static void finish_active_slot(struct active_request_slot *slot);
68 #ifdef USE_CURL_MULTI
69 static void process_curl_messages(void)
71 int num_messages;
72 struct active_request_slot *slot;
73 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
75 while (curl_message != NULL) {
76 if (curl_message->msg == CURLMSG_DONE) {
77 int curl_result = curl_message->data.result;
78 slot = active_queue_head;
79 while (slot != NULL &&
80 slot->curl != curl_message->easy_handle)
81 slot = slot->next;
82 if (slot != NULL) {
83 curl_multi_remove_handle(curlm, slot->curl);
84 slot->curl_result = curl_result;
85 finish_active_slot(slot);
86 } else {
87 fprintf(stderr, "Received DONE message for unknown request!\n");
89 } else {
90 fprintf(stderr, "Unknown CURL message received: %d\n",
91 (int)curl_message->msg);
93 curl_message = curl_multi_info_read(curlm, &num_messages);
96 #endif
98 static int http_options(const char *var, const char *value)
100 if (!strcmp("http.sslverify", var)) {
101 if (curl_ssl_verify == -1) {
102 curl_ssl_verify = git_config_bool(var, value);
104 return 0;
107 if (!strcmp("http.sslcert", var)) {
108 if (ssl_cert == NULL) {
109 ssl_cert = xmalloc(strlen(value)+1);
110 strcpy(ssl_cert, value);
112 return 0;
114 #if LIBCURL_VERSION_NUM >= 0x070902
115 if (!strcmp("http.sslkey", var)) {
116 if (ssl_key == NULL) {
117 ssl_key = xmalloc(strlen(value)+1);
118 strcpy(ssl_key, value);
120 return 0;
122 #endif
123 #if LIBCURL_VERSION_NUM >= 0x070908
124 if (!strcmp("http.sslcapath", var)) {
125 if (ssl_capath == NULL) {
126 ssl_capath = xmalloc(strlen(value)+1);
127 strcpy(ssl_capath, value);
129 return 0;
131 #endif
132 if (!strcmp("http.sslcainfo", var)) {
133 if (ssl_cainfo == NULL) {
134 ssl_cainfo = xmalloc(strlen(value)+1);
135 strcpy(ssl_cainfo, value);
137 return 0;
140 #ifdef USE_CURL_MULTI
141 if (!strcmp("http.maxrequests", var)) {
142 if (max_requests == -1)
143 max_requests = git_config_int(var, value);
144 return 0;
146 #endif
148 if (!strcmp("http.lowspeedlimit", var)) {
149 if (curl_low_speed_limit == -1)
150 curl_low_speed_limit = (long)git_config_int(var, value);
151 return 0;
153 if (!strcmp("http.lowspeedtime", var)) {
154 if (curl_low_speed_time == -1)
155 curl_low_speed_time = (long)git_config_int(var, value);
156 return 0;
159 if (!strcmp("http.noepsv", var)) {
160 curl_ftp_no_epsv = git_config_bool(var, value);
161 return 0;
164 /* Fall back on the default ones */
165 return git_default_config(var, value);
168 static CURL* get_curl_handle(void)
170 CURL* result = curl_easy_init();
172 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
173 #if LIBCURL_VERSION_NUM >= 0x070907
174 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
175 #endif
177 if (ssl_cert != NULL)
178 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
179 #if LIBCURL_VERSION_NUM >= 0x070902
180 if (ssl_key != NULL)
181 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
182 #endif
183 #if LIBCURL_VERSION_NUM >= 0x070908
184 if (ssl_capath != NULL)
185 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
186 #endif
187 if (ssl_cainfo != NULL)
188 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
189 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
191 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
192 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
193 curl_low_speed_limit);
194 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
195 curl_low_speed_time);
198 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
200 if (getenv("GIT_CURL_VERBOSE"))
201 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
203 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
205 if (curl_ftp_no_epsv)
206 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
208 return result;
211 void http_init(void)
213 char *low_speed_limit;
214 char *low_speed_time;
216 curl_global_init(CURL_GLOBAL_ALL);
218 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
220 #ifdef USE_CURL_MULTI
222 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
223 if (http_max_requests != NULL)
224 max_requests = atoi(http_max_requests);
227 curlm = curl_multi_init();
228 if (curlm == NULL) {
229 fprintf(stderr, "Error creating curl multi handle.\n");
230 exit(1);
232 #endif
234 if (getenv("GIT_SSL_NO_VERIFY"))
235 curl_ssl_verify = 0;
237 ssl_cert = getenv("GIT_SSL_CERT");
238 #if LIBCURL_VERSION_NUM >= 0x070902
239 ssl_key = getenv("GIT_SSL_KEY");
240 #endif
241 #if LIBCURL_VERSION_NUM >= 0x070908
242 ssl_capath = getenv("GIT_SSL_CAPATH");
243 #endif
244 ssl_cainfo = getenv("GIT_SSL_CAINFO");
246 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
247 if (low_speed_limit != NULL)
248 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
249 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
250 if (low_speed_time != NULL)
251 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
253 git_config(http_options);
255 if (curl_ssl_verify == -1)
256 curl_ssl_verify = 1;
258 #ifdef USE_CURL_MULTI
259 if (max_requests < 1)
260 max_requests = DEFAULT_MAX_REQUESTS;
261 #endif
263 if (getenv("GIT_CURL_FTP_NO_EPSV"))
264 curl_ftp_no_epsv = 1;
266 #ifndef NO_CURL_EASY_DUPHANDLE
267 curl_default = get_curl_handle();
268 #endif
271 void http_cleanup(void)
273 struct active_request_slot *slot = active_queue_head;
274 #ifdef USE_CURL_MULTI
275 char *wait_url;
276 #endif
278 while (slot != NULL) {
279 struct active_request_slot *next = slot->next;
280 #ifdef USE_CURL_MULTI
281 if (slot->in_use) {
282 curl_easy_getinfo(slot->curl,
283 CURLINFO_EFFECTIVE_URL,
284 &wait_url);
285 fprintf(stderr, "Waiting for %s\n", wait_url);
286 run_active_slot(slot);
288 #endif
289 if (slot->curl != NULL)
290 curl_easy_cleanup(slot->curl);
291 free(slot);
292 slot = next;
294 active_queue_head = NULL;
296 #ifndef NO_CURL_EASY_DUPHANDLE
297 curl_easy_cleanup(curl_default);
298 #endif
300 #ifdef USE_CURL_MULTI
301 curl_multi_cleanup(curlm);
302 #endif
303 curl_global_cleanup();
305 curl_slist_free_all(pragma_header);
306 pragma_header = NULL;
309 struct active_request_slot *get_active_slot(void)
311 struct active_request_slot *slot = active_queue_head;
312 struct active_request_slot *newslot;
314 #ifdef USE_CURL_MULTI
315 int num_transfers;
317 /* Wait for a slot to open up if the queue is full */
318 while (active_requests >= max_requests) {
319 curl_multi_perform(curlm, &num_transfers);
320 if (num_transfers < active_requests) {
321 process_curl_messages();
324 #endif
326 while (slot != NULL && slot->in_use) {
327 slot = slot->next;
329 if (slot == NULL) {
330 newslot = xmalloc(sizeof(*newslot));
331 newslot->curl = NULL;
332 newslot->in_use = 0;
333 newslot->next = NULL;
335 slot = active_queue_head;
336 if (slot == NULL) {
337 active_queue_head = newslot;
338 } else {
339 while (slot->next != NULL) {
340 slot = slot->next;
342 slot->next = newslot;
344 slot = newslot;
347 if (slot->curl == NULL) {
348 #ifdef NO_CURL_EASY_DUPHANDLE
349 slot->curl = get_curl_handle();
350 #else
351 slot->curl = curl_easy_duphandle(curl_default);
352 #endif
355 active_requests++;
356 slot->in_use = 1;
357 slot->local = NULL;
358 slot->results = NULL;
359 slot->finished = NULL;
360 slot->callback_data = NULL;
361 slot->callback_func = NULL;
362 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
363 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
364 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
365 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
366 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
367 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
368 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
369 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
371 return slot;
374 int start_active_slot(struct active_request_slot *slot)
376 #ifdef USE_CURL_MULTI
377 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
378 int num_transfers;
380 if (curlm_result != CURLM_OK &&
381 curlm_result != CURLM_CALL_MULTI_PERFORM) {
382 active_requests--;
383 slot->in_use = 0;
384 return 0;
388 * We know there must be something to do, since we just added
389 * something.
391 curl_multi_perform(curlm, &num_transfers);
392 #endif
393 return 1;
396 #ifdef USE_CURL_MULTI
397 struct fill_chain {
398 void *data;
399 int (*fill)(void *);
400 struct fill_chain *next;
403 static struct fill_chain *fill_cfg = NULL;
405 void add_fill_function(void *data, int (*fill)(void *))
407 struct fill_chain *new = malloc(sizeof(*new));
408 struct fill_chain **linkp = &fill_cfg;
409 new->data = data;
410 new->fill = fill;
411 new->next = NULL;
412 while (*linkp)
413 linkp = &(*linkp)->next;
414 *linkp = new;
417 void fill_active_slots(void)
419 struct active_request_slot *slot = active_queue_head;
421 while (active_requests < max_requests) {
422 struct fill_chain *fill;
423 for (fill = fill_cfg; fill; fill = fill->next)
424 if (fill->fill(fill->data))
425 break;
427 if (!fill)
428 break;
431 while (slot != NULL) {
432 if (!slot->in_use && slot->curl != NULL) {
433 curl_easy_cleanup(slot->curl);
434 slot->curl = NULL;
436 slot = slot->next;
440 void step_active_slots(void)
442 int num_transfers;
443 CURLMcode curlm_result;
445 do {
446 curlm_result = curl_multi_perform(curlm, &num_transfers);
447 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
448 if (num_transfers < active_requests) {
449 process_curl_messages();
450 fill_active_slots();
453 #endif
455 void run_active_slot(struct active_request_slot *slot)
457 #ifdef USE_CURL_MULTI
458 long last_pos = 0;
459 long current_pos;
460 fd_set readfds;
461 fd_set writefds;
462 fd_set excfds;
463 int max_fd;
464 struct timeval select_timeout;
465 int finished = 0;
467 slot->finished = &finished;
468 while (!finished) {
469 data_received = 0;
470 step_active_slots();
472 if (!data_received && slot->local != NULL) {
473 current_pos = ftell(slot->local);
474 if (current_pos > last_pos)
475 data_received++;
476 last_pos = current_pos;
479 if (slot->in_use && !data_received) {
480 max_fd = 0;
481 FD_ZERO(&readfds);
482 FD_ZERO(&writefds);
483 FD_ZERO(&excfds);
484 select_timeout.tv_sec = 0;
485 select_timeout.tv_usec = 50000;
486 select(max_fd, &readfds, &writefds,
487 &excfds, &select_timeout);
490 #else
491 while (slot->in_use) {
492 slot->curl_result = curl_easy_perform(slot->curl);
493 finish_active_slot(slot);
495 #endif
498 static void closedown_active_slot(struct active_request_slot *slot)
500 active_requests--;
501 slot->in_use = 0;
504 void release_active_slot(struct active_request_slot *slot)
506 closedown_active_slot(slot);
507 if (slot->curl) {
508 #ifdef USE_CURL_MULTI
509 curl_multi_remove_handle(curlm, slot->curl);
510 #endif
511 curl_easy_cleanup(slot->curl);
512 slot->curl = NULL;
514 #ifdef USE_CURL_MULTI
515 fill_active_slots();
516 #endif
519 static void finish_active_slot(struct active_request_slot *slot)
521 closedown_active_slot(slot);
522 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
524 if (slot->finished != NULL)
525 (*slot->finished) = 1;
527 /* Store slot results so they can be read after the slot is reused */
528 if (slot->results != NULL) {
529 slot->results->curl_result = slot->curl_result;
530 slot->results->http_code = slot->http_code;
533 /* Run callback if appropriate */
534 if (slot->callback_func != NULL) {
535 slot->callback_func(slot->callback_data);
539 void finish_all_active_slots(void)
541 struct active_request_slot *slot = active_queue_head;
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;