Improve XML parsing in http-push
[git/dkf.git] / http.c
blob470b56eab5adad69e866a243fd1bb4dc83d0baab
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;
28 struct curl_slist *no_range_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, 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(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 /* Fall back on the default ones */
160 return git_default_config(var, value);
163 void http_init(void)
165 char *low_speed_limit;
166 char *low_speed_time;
168 curl_global_init(CURL_GLOBAL_ALL);
170 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
171 no_range_header = curl_slist_append(no_range_header, "Range:");
173 #ifdef USE_CURL_MULTI
175 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
176 if (http_max_requests != NULL)
177 max_requests = atoi(http_max_requests);
180 curlm = curl_multi_init();
181 if (curlm == NULL) {
182 fprintf(stderr, "Error creating curl multi handle.\n");
183 exit(1);
185 #endif
187 if (getenv("GIT_SSL_NO_VERIFY"))
188 curl_ssl_verify = 0;
190 ssl_cert = getenv("GIT_SSL_CERT");
191 #if LIBCURL_VERSION_NUM >= 0x070902
192 ssl_key = getenv("GIT_SSL_KEY");
193 #endif
194 #if LIBCURL_VERSION_NUM >= 0x070908
195 ssl_capath = getenv("GIT_SSL_CAPATH");
196 #endif
197 ssl_cainfo = getenv("GIT_SSL_CAINFO");
199 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
200 if (low_speed_limit != NULL)
201 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
202 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
203 if (low_speed_time != NULL)
204 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
206 git_config(http_options);
208 if (curl_ssl_verify == -1)
209 curl_ssl_verify = 1;
211 #ifdef USE_CURL_MULTI
212 if (max_requests < 1)
213 max_requests = DEFAULT_MAX_REQUESTS;
214 #endif
216 #ifndef NO_CURL_EASY_DUPHANDLE
217 curl_default = get_curl_handle();
218 #endif
221 void http_cleanup(void)
223 struct active_request_slot *slot = active_queue_head;
224 #ifdef USE_CURL_MULTI
225 char *wait_url;
226 CURLMcode curlm_result;
227 #endif
229 while (slot != NULL) {
230 #ifdef USE_CURL_MULTI
231 if (slot->in_use) {
232 curl_easy_getinfo(slot->curl,
233 CURLINFO_EFFECTIVE_URL,
234 &wait_url);
235 fprintf(stderr, "Waiting for %s\n", wait_url);
236 run_active_slot(slot);
238 #endif
239 if (slot->curl != NULL)
240 curl_easy_cleanup(slot->curl);
241 slot = slot->next;
244 #ifndef NO_CURL_EASY_DUPHANDLE
245 curl_easy_cleanup(curl_default);
246 #endif
248 #ifdef USE_CURL_MULTI
249 curl_multi_cleanup(curlm);
250 #endif
251 curl_global_cleanup();
255 static CURL* get_curl_handle(void)
257 CURL* result = curl_easy_init();
259 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
260 #if LIBCURL_VERSION_NUM >= 0x070907
261 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
262 #endif
264 if (ssl_cert != NULL)
265 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
266 #if LIBCURL_VERSION_NUM >= 0x070902
267 if (ssl_key != NULL)
268 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
269 #endif
270 #if LIBCURL_VERSION_NUM >= 0x070908
271 if (ssl_capath != NULL)
272 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
273 #endif
274 if (ssl_cainfo != NULL)
275 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
276 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
278 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
279 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
280 curl_low_speed_limit);
281 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
282 curl_low_speed_time);
285 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
287 return result;
290 struct active_request_slot *get_active_slot(void)
292 struct active_request_slot *slot = active_queue_head;
293 struct active_request_slot *newslot;
295 #ifdef USE_CURL_MULTI
296 int num_transfers;
298 /* Wait for a slot to open up if the queue is full */
299 while (active_requests >= max_requests) {
300 curl_multi_perform(curlm, &num_transfers);
301 if (num_transfers < active_requests) {
302 process_curl_messages();
305 #endif
307 while (slot != NULL && slot->in_use) {
308 slot = slot->next;
310 if (slot == NULL) {
311 newslot = xmalloc(sizeof(*newslot));
312 newslot->curl = NULL;
313 newslot->in_use = 0;
314 newslot->next = NULL;
316 slot = active_queue_head;
317 if (slot == NULL) {
318 active_queue_head = newslot;
319 } else {
320 while (slot->next != NULL) {
321 slot = slot->next;
323 slot->next = newslot;
325 slot = newslot;
328 if (slot->curl == NULL) {
329 #ifdef NO_CURL_EASY_DUPHANDLE
330 slot->curl = get_curl_handle();
331 #else
332 slot->curl = curl_easy_duphandle(curl_default);
333 #endif
336 active_requests++;
337 slot->in_use = 1;
338 slot->local = NULL;
339 slot->callback_data = NULL;
340 slot->callback_func = NULL;
341 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
342 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_range_header);
343 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
345 return slot;
348 int start_active_slot(struct active_request_slot *slot)
350 #ifdef USE_CURL_MULTI
351 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
353 if (curlm_result != CURLM_OK &&
354 curlm_result != CURLM_CALL_MULTI_PERFORM) {
355 active_requests--;
356 slot->in_use = 0;
357 return 0;
359 #endif
360 return 1;
363 #ifdef USE_CURL_MULTI
364 void step_active_slots(void)
366 int num_transfers;
367 CURLMcode curlm_result;
369 do {
370 curlm_result = curl_multi_perform(curlm, &num_transfers);
371 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
372 if (num_transfers < active_requests) {
373 process_curl_messages();
374 fill_active_slots();
377 #endif
379 void run_active_slot(struct active_request_slot *slot)
381 #ifdef USE_CURL_MULTI
382 long last_pos = 0;
383 long current_pos;
384 fd_set readfds;
385 fd_set writefds;
386 fd_set excfds;
387 int max_fd;
388 struct timeval select_timeout;
390 while (slot->in_use) {
391 data_received = 0;
392 step_active_slots();
394 if (!data_received && slot->local != NULL) {
395 current_pos = ftell(slot->local);
396 if (current_pos > last_pos)
397 data_received++;
398 last_pos = current_pos;
401 if (slot->in_use && !data_received) {
402 max_fd = 0;
403 FD_ZERO(&readfds);
404 FD_ZERO(&writefds);
405 FD_ZERO(&excfds);
406 select_timeout.tv_sec = 0;
407 select_timeout.tv_usec = 50000;
408 select(max_fd, &readfds, &writefds,
409 &excfds, &select_timeout);
412 #else
413 while (slot->in_use) {
414 slot->curl_result = curl_easy_perform(slot->curl);
415 finish_active_slot(slot);
417 #endif
420 static void finish_active_slot(struct active_request_slot *slot)
422 active_requests--;
423 slot->in_use = 0;
424 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
426 /* Run callback if appropriate */
427 if (slot->callback_func != NULL) {
428 slot->callback_func(slot->callback_data);
432 void finish_all_active_slots(void)
434 struct active_request_slot *slot = active_queue_head;
436 while (slot != NULL)
437 if (slot->in_use) {
438 run_active_slot(slot);
439 slot = active_queue_head;
440 } else {
441 slot = slot->next;