Merge branch 'lt/tree-2'
[git/git.git] / http.c
1 #include "http.h"
2
3 int data_received;
4 int active_requests = 0;
5
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];
14
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
27 struct curl_slist *pragma_header;
28
29 struct active_request_slot *active_queue_head = NULL;
30
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, buffer->buffer + buffer->posn, size);
38 buffer->posn += size;
39 return size;
40 }
41
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(buffer->buffer + buffer->posn, ptr, size);
53 buffer->posn += size;
54 data_received++;
55 return size;
56 }
57
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 }
64
65 static void finish_active_slot(struct active_request_slot *slot);
66
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);
73
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
96
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 }
105
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 }
138
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
146
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 }
157
158 /* Fall back on the default ones */
159 return git_default_config(var, value);
160 }
161
162 static CURL* get_curl_handle(void)
163 {
164 CURL* result = curl_easy_init();
165
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
170
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);
184
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 }
191
192 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
193
194 if (getenv("GIT_CURL_VERBOSE"))
195 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
196
197 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
198
199 return result;
200 }
201
202 void http_init(void)
203 {
204 char *low_speed_limit;
205 char *low_speed_time;
206
207 curl_global_init(CURL_GLOBAL_ALL);
208
209 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
210
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 }
217
218 curlm = curl_multi_init();
219 if (curlm == NULL) {
220 fprintf(stderr, "Error creating curl multi handle.\n");
221 exit(1);
222 }
223 #endif
224
225 if (getenv("GIT_SSL_NO_VERIFY"))
226 curl_ssl_verify = 0;
227
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");
236
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);
243
244 git_config(http_options);
245
246 if (curl_ssl_verify == -1)
247 curl_ssl_verify = 1;
248
249 #ifdef USE_CURL_MULTI
250 if (max_requests < 1)
251 max_requests = DEFAULT_MAX_REQUESTS;
252 #endif
253
254 #ifndef NO_CURL_EASY_DUPHANDLE
255 curl_default = get_curl_handle();
256 #endif
257 }
258
259 void http_cleanup(void)
260 {
261 struct active_request_slot *slot = active_queue_head;
262 #ifdef USE_CURL_MULTI
263 char *wait_url;
264 #endif
265
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 }
280
281 #ifndef NO_CURL_EASY_DUPHANDLE
282 curl_easy_cleanup(curl_default);
283 #endif
284
285 #ifdef USE_CURL_MULTI
286 curl_multi_cleanup(curlm);
287 #endif
288 curl_global_cleanup();
289
290 }
291
292 struct active_request_slot *get_active_slot(void)
293 {
294 struct active_request_slot *slot = active_queue_head;
295 struct active_request_slot *newslot;
296
297 #ifdef USE_CURL_MULTI
298 int num_transfers;
299
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();
305 }
306 }
307 #endif
308
309 while (slot != NULL && slot->in_use) {
310 slot = slot->next;
311 }
312 if (slot == NULL) {
313 newslot = xmalloc(sizeof(*newslot));
314 newslot->curl = NULL;
315 newslot->in_use = 0;
316 newslot->next = NULL;
317
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;
324 }
325 slot->next = newslot;
326 }
327 slot = newslot;
328 }
329
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
336 }
337
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);
353
354 return slot;
355 }
356
357 int start_active_slot(struct active_request_slot *slot)
358 {
359 #ifdef USE_CURL_MULTI
360 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
361
362 if (curlm_result != CURLM_OK &&
363 curlm_result != CURLM_CALL_MULTI_PERFORM) {
364 active_requests--;
365 slot->in_use = 0;
366 return 0;
367 }
368 #endif
369 return 1;
370 }
371
372 #ifdef USE_CURL_MULTI
373 void step_active_slots(void)
374 {
375 int num_transfers;
376 CURLMcode curlm_result;
377
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();
384 }
385 }
386 #endif
387
388 void run_active_slot(struct active_request_slot *slot)
389 {
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;
399
400 slot->finished = &finished;
401 while (!finished) {
402 data_received = 0;
403 step_active_slots();
404
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;
410 }
411
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);
421 }
422 }
423 #else
424 while (slot->in_use) {
425 slot->curl_result = curl_easy_perform(slot->curl);
426 finish_active_slot(slot);
427 }
428 #endif
429 }
430
431 static void closedown_active_slot(struct active_request_slot *slot)
432 {
433 active_requests--;
434 slot->in_use = 0;
435 }
436
437 void release_active_slot(struct active_request_slot *slot)
438 {
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;
444 }
445 fill_active_slots();
446 }
447
448 static void finish_active_slot(struct active_request_slot *slot)
449 {
450 closedown_active_slot(slot);
451 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
452
453 if (slot->finished != NULL)
454 (*slot->finished) = 1;
455
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;
460 }
461
462 /* Run callback if appropriate */
463 if (slot->callback_func != NULL) {
464 slot->callback_func(slot->callback_data);
465 }
466 }
467
468 void finish_all_active_slots(void)
469 {
470 struct active_request_slot *slot = active_queue_head;
471
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;
478 }
479 }