gitview: Add some useful keybindings.
[git/dscho.git] / http.c
blob146cf7bf36d715ea410c0646d83c47148d426444
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;
27 struct curl_slist *pragma_header;
29 struct active_request_slot *active_queue_head = NULL;
31 size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb,
32 struct buffer *buffer)
34 size_t size = eltsize * nmemb;
35 if (size > buffer->size - buffer->posn)
36 size = buffer->size - buffer->posn;
37 memcpy(ptr, buffer->buffer + buffer->posn, size);
38 buffer->posn += size;
39 return size;
42 size_t fwrite_buffer(const void *ptr, size_t eltsize,
43 size_t nmemb, struct buffer *buffer)
45 size_t size = eltsize * nmemb;
46 if (size > buffer->size - buffer->posn) {
47 buffer->size = buffer->size * 3 / 2;
48 if (buffer->size < buffer->posn + size)
49 buffer->size = buffer->posn + size;
50 buffer->buffer = xrealloc(buffer->buffer, buffer->size);
52 memcpy(buffer->buffer + buffer->posn, ptr, size);
53 buffer->posn += size;
54 data_received++;
55 return size;
58 size_t fwrite_null(const void *ptr, size_t eltsize,
59 size_t nmemb, struct buffer *buffer)
61 data_received++;
62 return eltsize * nmemb;
65 static void finish_active_slot(struct active_request_slot *slot);
67 #ifdef USE_CURL_MULTI
68 static void process_curl_messages(void)
70 int num_messages;
71 struct active_request_slot *slot;
72 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
74 while (curl_message != NULL) {
75 if (curl_message->msg == CURLMSG_DONE) {
76 int curl_result = curl_message->data.result;
77 slot = active_queue_head;
78 while (slot != NULL &&
79 slot->curl != curl_message->easy_handle)
80 slot = slot->next;
81 if (slot != NULL) {
82 curl_multi_remove_handle(curlm, slot->curl);
83 slot->curl_result = curl_result;
84 finish_active_slot(slot);
85 } else {
86 fprintf(stderr, "Received DONE message for unknown request!\n");
88 } else {
89 fprintf(stderr, "Unknown CURL message received: %d\n",
90 (int)curl_message->msg);
92 curl_message = curl_multi_info_read(curlm, &num_messages);
95 #endif
97 static int http_options(const char *var, const char *value)
99 if (!strcmp("http.sslverify", var)) {
100 if (curl_ssl_verify == -1) {
101 curl_ssl_verify = git_config_bool(var, value);
103 return 0;
106 if (!strcmp("http.sslcert", var)) {
107 if (ssl_cert == NULL) {
108 ssl_cert = xmalloc(strlen(value)+1);
109 strcpy(ssl_cert, value);
111 return 0;
113 #if LIBCURL_VERSION_NUM >= 0x070902
114 if (!strcmp("http.sslkey", var)) {
115 if (ssl_key == NULL) {
116 ssl_key = xmalloc(strlen(value)+1);
117 strcpy(ssl_key, value);
119 return 0;
121 #endif
122 #if LIBCURL_VERSION_NUM >= 0x070908
123 if (!strcmp("http.sslcapath", var)) {
124 if (ssl_capath == NULL) {
125 ssl_capath = xmalloc(strlen(value)+1);
126 strcpy(ssl_capath, value);
128 return 0;
130 #endif
131 if (!strcmp("http.sslcainfo", var)) {
132 if (ssl_cainfo == NULL) {
133 ssl_cainfo = xmalloc(strlen(value)+1);
134 strcpy(ssl_cainfo, value);
136 return 0;
139 #ifdef USE_CURL_MULTI
140 if (!strcmp("http.maxrequests", var)) {
141 if (max_requests == -1)
142 max_requests = git_config_int(var, value);
143 return 0;
145 #endif
147 if (!strcmp("http.lowspeedlimit", var)) {
148 if (curl_low_speed_limit == -1)
149 curl_low_speed_limit = (long)git_config_int(var, value);
150 return 0;
152 if (!strcmp("http.lowspeedtime", var)) {
153 if (curl_low_speed_time == -1)
154 curl_low_speed_time = (long)git_config_int(var, value);
155 return 0;
158 /* Fall back on the default ones */
159 return git_default_config(var, value);
162 static CURL* get_curl_handle(void)
164 CURL* result = curl_easy_init();
166 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
167 #if LIBCURL_VERSION_NUM >= 0x070907
168 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
169 #endif
171 if (ssl_cert != NULL)
172 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
173 #if LIBCURL_VERSION_NUM >= 0x070902
174 if (ssl_key != NULL)
175 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
176 #endif
177 #if LIBCURL_VERSION_NUM >= 0x070908
178 if (ssl_capath != NULL)
179 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
180 #endif
181 if (ssl_cainfo != NULL)
182 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
183 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
185 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
186 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
187 curl_low_speed_limit);
188 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
189 curl_low_speed_time);
192 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
194 if (getenv("GIT_CURL_VERBOSE"))
195 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
197 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
199 return result;
202 void http_init(void)
204 char *low_speed_limit;
205 char *low_speed_time;
207 curl_global_init(CURL_GLOBAL_ALL);
209 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
211 #ifdef USE_CURL_MULTI
213 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
214 if (http_max_requests != NULL)
215 max_requests = atoi(http_max_requests);
218 curlm = curl_multi_init();
219 if (curlm == NULL) {
220 fprintf(stderr, "Error creating curl multi handle.\n");
221 exit(1);
223 #endif
225 if (getenv("GIT_SSL_NO_VERIFY"))
226 curl_ssl_verify = 0;
228 ssl_cert = getenv("GIT_SSL_CERT");
229 #if LIBCURL_VERSION_NUM >= 0x070902
230 ssl_key = getenv("GIT_SSL_KEY");
231 #endif
232 #if LIBCURL_VERSION_NUM >= 0x070908
233 ssl_capath = getenv("GIT_SSL_CAPATH");
234 #endif
235 ssl_cainfo = getenv("GIT_SSL_CAINFO");
237 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
238 if (low_speed_limit != NULL)
239 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
240 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
241 if (low_speed_time != NULL)
242 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
244 git_config(http_options);
246 if (curl_ssl_verify == -1)
247 curl_ssl_verify = 1;
249 #ifdef USE_CURL_MULTI
250 if (max_requests < 1)
251 max_requests = DEFAULT_MAX_REQUESTS;
252 #endif
254 #ifndef NO_CURL_EASY_DUPHANDLE
255 curl_default = get_curl_handle();
256 #endif
259 void http_cleanup(void)
261 struct active_request_slot *slot = active_queue_head;
262 #ifdef USE_CURL_MULTI
263 char *wait_url;
264 #endif
266 while (slot != NULL) {
267 #ifdef USE_CURL_MULTI
268 if (slot->in_use) {
269 curl_easy_getinfo(slot->curl,
270 CURLINFO_EFFECTIVE_URL,
271 &wait_url);
272 fprintf(stderr, "Waiting for %s\n", wait_url);
273 run_active_slot(slot);
275 #endif
276 if (slot->curl != NULL)
277 curl_easy_cleanup(slot->curl);
278 slot = slot->next;
281 #ifndef NO_CURL_EASY_DUPHANDLE
282 curl_easy_cleanup(curl_default);
283 #endif
285 #ifdef USE_CURL_MULTI
286 curl_multi_cleanup(curlm);
287 #endif
288 curl_global_cleanup();
292 struct active_request_slot *get_active_slot(void)
294 struct active_request_slot *slot = active_queue_head;
295 struct active_request_slot *newslot;
297 #ifdef USE_CURL_MULTI
298 int num_transfers;
300 /* Wait for a slot to open up if the queue is full */
301 while (active_requests >= max_requests) {
302 curl_multi_perform(curlm, &num_transfers);
303 if (num_transfers < active_requests) {
304 process_curl_messages();
307 #endif
309 while (slot != NULL && slot->in_use) {
310 slot = slot->next;
312 if (slot == NULL) {
313 newslot = xmalloc(sizeof(*newslot));
314 newslot->curl = NULL;
315 newslot->in_use = 0;
316 newslot->next = NULL;
318 slot = active_queue_head;
319 if (slot == NULL) {
320 active_queue_head = newslot;
321 } else {
322 while (slot->next != NULL) {
323 slot = slot->next;
325 slot->next = newslot;
327 slot = newslot;
330 if (slot->curl == NULL) {
331 #ifdef NO_CURL_EASY_DUPHANDLE
332 slot->curl = get_curl_handle();
333 #else
334 slot->curl = curl_easy_duphandle(curl_default);
335 #endif
338 active_requests++;
339 slot->in_use = 1;
340 slot->local = NULL;
341 slot->results = NULL;
342 slot->finished = NULL;
343 slot->callback_data = NULL;
344 slot->callback_func = NULL;
345 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
346 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
347 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
348 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
349 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
350 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
351 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
352 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
354 return slot;
357 int start_active_slot(struct active_request_slot *slot)
359 #ifdef USE_CURL_MULTI
360 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
362 if (curlm_result != CURLM_OK &&
363 curlm_result != CURLM_CALL_MULTI_PERFORM) {
364 active_requests--;
365 slot->in_use = 0;
366 return 0;
368 #endif
369 return 1;
372 #ifdef USE_CURL_MULTI
373 void step_active_slots(void)
375 int num_transfers;
376 CURLMcode curlm_result;
378 do {
379 curlm_result = curl_multi_perform(curlm, &num_transfers);
380 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
381 if (num_transfers < active_requests) {
382 process_curl_messages();
383 fill_active_slots();
386 #endif
388 void run_active_slot(struct active_request_slot *slot)
390 #ifdef USE_CURL_MULTI
391 long last_pos = 0;
392 long current_pos;
393 fd_set readfds;
394 fd_set writefds;
395 fd_set excfds;
396 int max_fd;
397 struct timeval select_timeout;
398 int finished = 0;
400 slot->finished = &finished;
401 while (!finished) {
402 data_received = 0;
403 step_active_slots();
405 if (!data_received && slot->local != NULL) {
406 current_pos = ftell(slot->local);
407 if (current_pos > last_pos)
408 data_received++;
409 last_pos = current_pos;
412 if (slot->in_use && !data_received) {
413 max_fd = 0;
414 FD_ZERO(&readfds);
415 FD_ZERO(&writefds);
416 FD_ZERO(&excfds);
417 select_timeout.tv_sec = 0;
418 select_timeout.tv_usec = 50000;
419 select(max_fd, &readfds, &writefds,
420 &excfds, &select_timeout);
423 #else
424 while (slot->in_use) {
425 slot->curl_result = curl_easy_perform(slot->curl);
426 finish_active_slot(slot);
428 #endif
431 static void closedown_active_slot(struct active_request_slot *slot)
433 active_requests--;
434 slot->in_use = 0;
437 void release_active_slot(struct active_request_slot *slot)
439 closedown_active_slot(slot);
440 if (slot->curl) {
441 curl_multi_remove_handle(curlm, slot->curl);
442 curl_easy_cleanup(slot->curl);
443 slot->curl = NULL;
445 fill_active_slots();
448 static void finish_active_slot(struct active_request_slot *slot)
450 closedown_active_slot(slot);
451 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
453 if (slot->finished != NULL)
454 (*slot->finished) = 1;
456 /* Store slot results so they can be read after the slot is reused */
457 if (slot->results != NULL) {
458 slot->results->curl_result = slot->curl_result;
459 slot->results->http_code = slot->http_code;
462 /* Run callback if appropriate */
463 if (slot->callback_func != NULL) {
464 slot->callback_func(slot->callback_data);
468 void finish_all_active_slots(void)
470 struct active_request_slot *slot = active_queue_head;
472 while (slot != NULL)
473 if (slot->in_use) {
474 run_active_slot(slot);
475 slot = active_queue_head;
476 } else {
477 slot = slot->next;