Code

gitweb: blame table row no highlight fix
[git.git] / http.c
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)
33 {
34         size_t size = eltsize * nmemb;
35         if (size > buffer->size - buffer->posn)
36                 size = buffer->size - buffer->posn;
37         memcpy(ptr, (char *) buffer->buffer + buffer->posn, size);
38         buffer->posn += size;
39         return size;
40 }
42 size_t fwrite_buffer(const void *ptr, size_t eltsize,
43                             size_t nmemb, struct buffer *buffer)
44 {
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);
51         }
52         memcpy((char *) buffer->buffer + buffer->posn, ptr, size);
53         buffer->posn += size;
54         data_received++;
55         return size;
56 }
58 size_t fwrite_null(const void *ptr, size_t eltsize,
59                           size_t nmemb, struct buffer *buffer)
60 {
61         data_received++;
62         return eltsize * nmemb;
63 }
65 static void finish_active_slot(struct active_request_slot *slot);
67 #ifdef USE_CURL_MULTI
68 static void process_curl_messages(void)
69 {
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");
87                         }
88                 } else {
89                         fprintf(stderr, "Unknown CURL message received: %d\n",
90                                 (int)curl_message->msg);
91                 }
92                 curl_message = curl_multi_info_read(curlm, &num_messages);
93         }
94 }
95 #endif
97 static int http_options(const char *var, const char *value)
98 {
99         if (!strcmp("http.sslverify", var)) {
100                 if (curl_ssl_verify == -1) {
101                         curl_ssl_verify = git_config_bool(var, value);
102                 }
103                 return 0;
104         }
106         if (!strcmp("http.sslcert", var)) {
107                 if (ssl_cert == NULL) {
108                         ssl_cert = xmalloc(strlen(value)+1);
109                         strcpy(ssl_cert, value);
110                 }
111                 return 0;
112         }
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);
118                 }
119                 return 0;
120         }
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);
127                 }
128                 return 0;
129         }
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);
135                 }
136                 return 0;
137         }
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;
144         }
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;
151         }
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;
156         }
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);
190         }
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
212         {
213                 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
214                 if (http_max_requests != NULL)
215                         max_requests = atoi(http_max_requests);
216         }
218         curlm = curl_multi_init();
219         if (curlm == NULL) {
220                 fprintf(stderr, "Error creating curl multi handle.\n");
221                 exit(1);
222         }
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);
274                 }
275 #endif
276                 if (slot->curl != NULL)
277                         curl_easy_cleanup(slot->curl);
278                 slot = slot->next;
279         }
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();
290         curl_slist_free_all(pragma_header);
293 struct active_request_slot *get_active_slot(void)
295         struct active_request_slot *slot = active_queue_head;
296         struct active_request_slot *newslot;
298 #ifdef USE_CURL_MULTI
299         int num_transfers;
301         /* Wait for a slot to open up if the queue is full */
302         while (active_requests >= max_requests) {
303                 curl_multi_perform(curlm, &num_transfers);
304                 if (num_transfers < active_requests) {
305                         process_curl_messages();
306                 }
307         }
308 #endif
310         while (slot != NULL && slot->in_use) {
311                 slot = slot->next;
312         }
313         if (slot == NULL) {
314                 newslot = xmalloc(sizeof(*newslot));
315                 newslot->curl = NULL;
316                 newslot->in_use = 0;
317                 newslot->next = NULL;
319                 slot = active_queue_head;
320                 if (slot == NULL) {
321                         active_queue_head = newslot;
322                 } else {
323                         while (slot->next != NULL) {
324                                 slot = slot->next;
325                         }
326                         slot->next = newslot;
327                 }
328                 slot = newslot;
329         }
331         if (slot->curl == NULL) {
332 #ifdef NO_CURL_EASY_DUPHANDLE
333                 slot->curl = get_curl_handle();
334 #else
335                 slot->curl = curl_easy_duphandle(curl_default);
336 #endif
337         }
339         active_requests++;
340         slot->in_use = 1;
341         slot->local = NULL;
342         slot->results = NULL;
343         slot->finished = NULL;
344         slot->callback_data = NULL;
345         slot->callback_func = NULL;
346         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
347         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
348         curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
349         curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
350         curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
351         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
352         curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
353         curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
355         return slot;
358 int start_active_slot(struct active_request_slot *slot)
360 #ifdef USE_CURL_MULTI
361         CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
363         if (curlm_result != CURLM_OK &&
364             curlm_result != CURLM_CALL_MULTI_PERFORM) {
365                 active_requests--;
366                 slot->in_use = 0;
367                 return 0;
368         }
369 #endif
370         return 1;
373 #ifdef USE_CURL_MULTI
374 void step_active_slots(void)
376         int num_transfers;
377         CURLMcode curlm_result;
379         do {
380                 curlm_result = curl_multi_perform(curlm, &num_transfers);
381         } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
382         if (num_transfers < active_requests) {
383                 process_curl_messages();
384                 fill_active_slots();
385         }
387 #endif
389 void run_active_slot(struct active_request_slot *slot)
391 #ifdef USE_CURL_MULTI
392         long last_pos = 0;
393         long current_pos;
394         fd_set readfds;
395         fd_set writefds;
396         fd_set excfds;
397         int max_fd;
398         struct timeval select_timeout;
399         int finished = 0;
401         slot->finished = &finished;
402         while (!finished) {
403                 data_received = 0;
404                 step_active_slots();
406                 if (!data_received && slot->local != NULL) {
407                         current_pos = ftell(slot->local);
408                         if (current_pos > last_pos)
409                                 data_received++;
410                         last_pos = current_pos;
411                 }
413                 if (slot->in_use && !data_received) {
414                         max_fd = 0;
415                         FD_ZERO(&readfds);
416                         FD_ZERO(&writefds);
417                         FD_ZERO(&excfds);
418                         select_timeout.tv_sec = 0;
419                         select_timeout.tv_usec = 50000;
420                         select(max_fd, &readfds, &writefds,
421                                &excfds, &select_timeout);
422                 }
423         }
424 #else
425         while (slot->in_use) {
426                 slot->curl_result = curl_easy_perform(slot->curl);
427                 finish_active_slot(slot);
428         }
429 #endif
432 static void closedown_active_slot(struct active_request_slot *slot)
434         active_requests--;
435         slot->in_use = 0;
438 void release_active_slot(struct active_request_slot *slot)
440         closedown_active_slot(slot);
441         if (slot->curl) {
442 #ifdef USE_CURL_MULTI
443                 curl_multi_remove_handle(curlm, slot->curl);
444 #endif
445                 curl_easy_cleanup(slot->curl);
446                 slot->curl = NULL;
447         }
448 #ifdef USE_CURL_MULTI
449         fill_active_slots();
450 #endif
453 static void finish_active_slot(struct active_request_slot *slot)
455         closedown_active_slot(slot);
456         curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
458         if (slot->finished != NULL)
459                 (*slot->finished) = 1;
461         /* Store slot results so they can be read after the slot is reused */
462         if (slot->results != NULL) {
463                 slot->results->curl_result = slot->curl_result;
464                 slot->results->http_code = slot->http_code;
465         }
467         /* Run callback if appropriate */
468         if (slot->callback_func != NULL) {
469                 slot->callback_func(slot->callback_data);
470         }
473 void finish_all_active_slots(void)
475         struct active_request_slot *slot = active_queue_head;
477         while (slot != NULL)
478                 if (slot->in_use) {
479                         run_active_slot(slot);
480                         slot = active_queue_head;
481                 } else {
482                         slot = slot->next;
483                 }