blob: ed59b79709b11dc6f6d85e86d75a1a8883799f21 [file] [log] [blame]
Nick Hengeveld29508e12005-11-18 11:02:58 -08001#include "http.h"
2
3int data_received;
4int active_requests = 0;
5
6#ifdef USE_CURL_MULTI
Mike Hommeycc3530e2007-12-09 18:04:57 +01007static int max_requests = -1;
8static CURLM *curlm;
Nick Hengeveld29508e12005-11-18 11:02:58 -08009#endif
10#ifndef NO_CURL_EASY_DUPHANDLE
Mike Hommeycc3530e2007-12-09 18:04:57 +010011static CURL *curl_default;
Nick Hengeveld29508e12005-11-18 11:02:58 -080012#endif
13char curl_errorstr[CURL_ERROR_SIZE];
14
Mike Hommeycc3530e2007-12-09 18:04:57 +010015static int curl_ssl_verify = -1;
Brian Hetro7ef8ea72008-07-05 01:24:44 -040016static const char *ssl_cert = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -080017#if LIBCURL_VERSION_NUM >= 0x070902
Brian Hetro7ef8ea72008-07-05 01:24:44 -040018static const char *ssl_key = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -080019#endif
20#if LIBCURL_VERSION_NUM >= 0x070908
Brian Hetro7ef8ea72008-07-05 01:24:44 -040021static const char *ssl_capath = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -080022#endif
Brian Hetro7ef8ea72008-07-05 01:24:44 -040023static const char *ssl_cainfo = NULL;
Mike Hommeycc3530e2007-12-09 18:04:57 +010024static long curl_low_speed_limit = -1;
25static long curl_low_speed_time = -1;
26static int curl_ftp_no_epsv = 0;
27static char *curl_http_proxy = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -080028
Mike Hommeycc3530e2007-12-09 18:04:57 +010029static struct curl_slist *pragma_header;
Nick Hengeveld29508e12005-11-18 11:02:58 -080030
Mike Hommeycc3530e2007-12-09 18:04:57 +010031static struct active_request_slot *active_queue_head = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -080032
Junio C Hamanof444e522008-07-04 00:37:40 -070033size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb, void *buffer_)
Nick Hengeveld29508e12005-11-18 11:02:58 -080034{
35 size_t size = eltsize * nmemb;
Junio C Hamanof444e522008-07-04 00:37:40 -070036 struct buffer *buffer = buffer_;
37
Mike Hommey028c2972007-12-09 20:30:59 +010038 if (size > buffer->buf.len - buffer->posn)
39 size = buffer->buf.len - buffer->posn;
40 memcpy(ptr, buffer->buf.buf + buffer->posn, size);
Nick Hengeveld29508e12005-11-18 11:02:58 -080041 buffer->posn += size;
Mike Hommey028c2972007-12-09 20:30:59 +010042
Nick Hengeveld29508e12005-11-18 11:02:58 -080043 return size;
44}
45
Junio C Hamanof444e522008-07-04 00:37:40 -070046size_t fwrite_buffer(const void *ptr, size_t eltsize, size_t nmemb, void *buffer_)
Nick Hengeveld29508e12005-11-18 11:02:58 -080047{
48 size_t size = eltsize * nmemb;
Junio C Hamanof444e522008-07-04 00:37:40 -070049 struct strbuf *buffer = buffer_;
50
Mike Hommey028c2972007-12-09 20:30:59 +010051 strbuf_add(buffer, ptr, size);
Nick Hengeveld29508e12005-11-18 11:02:58 -080052 data_received++;
53 return size;
54}
55
Junio C Hamanof444e522008-07-04 00:37:40 -070056size_t fwrite_null(const void *ptr, size_t eltsize, size_t nmemb, void *strbuf)
Nick Hengeveld29508e12005-11-18 11:02:58 -080057{
58 data_received++;
59 return eltsize * nmemb;
60}
61
62static void finish_active_slot(struct active_request_slot *slot);
63
64#ifdef USE_CURL_MULTI
65static void process_curl_messages(void)
66{
67 int num_messages;
68 struct active_request_slot *slot;
69 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
70
71 while (curl_message != NULL) {
72 if (curl_message->msg == CURLMSG_DONE) {
73 int curl_result = curl_message->data.result;
74 slot = active_queue_head;
75 while (slot != NULL &&
76 slot->curl != curl_message->easy_handle)
77 slot = slot->next;
78 if (slot != NULL) {
79 curl_multi_remove_handle(curlm, slot->curl);
80 slot->curl_result = curl_result;
81 finish_active_slot(slot);
82 } else {
83 fprintf(stderr, "Received DONE message for unknown request!\n");
84 }
85 } else {
86 fprintf(stderr, "Unknown CURL message received: %d\n",
87 (int)curl_message->msg);
88 }
89 curl_message = curl_multi_info_read(curlm, &num_messages);
90 }
91}
92#endif
93
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010094static int http_options(const char *var, const char *value, void *cb)
Nick Hengeveld29508e12005-11-18 11:02:58 -080095{
96 if (!strcmp("http.sslverify", var)) {
97 if (curl_ssl_verify == -1) {
98 curl_ssl_verify = git_config_bool(var, value);
99 }
100 return 0;
101 }
102
103 if (!strcmp("http.sslcert", var)) {
Brian Hetro7ef8ea72008-07-05 01:24:44 -0400104 if (ssl_cert == NULL)
105 return git_config_string(&ssl_cert, var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800106 return 0;
107 }
108#if LIBCURL_VERSION_NUM >= 0x070902
109 if (!strcmp("http.sslkey", var)) {
Brian Hetro7ef8ea72008-07-05 01:24:44 -0400110 if (ssl_key == NULL)
111 return git_config_string(&ssl_key, var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800112 return 0;
113 }
114#endif
115#if LIBCURL_VERSION_NUM >= 0x070908
116 if (!strcmp("http.sslcapath", var)) {
Brian Hetro7ef8ea72008-07-05 01:24:44 -0400117 if (ssl_capath == NULL)
118 return git_config_string(&ssl_capath, var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800119 return 0;
120 }
121#endif
122 if (!strcmp("http.sslcainfo", var)) {
Brian Hetro7ef8ea72008-07-05 01:24:44 -0400123 if (ssl_cainfo == NULL)
124 return git_config_string(&ssl_cainfo, var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800125 return 0;
126 }
127
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700128#ifdef USE_CURL_MULTI
Nick Hengeveld29508e12005-11-18 11:02:58 -0800129 if (!strcmp("http.maxrequests", var)) {
130 if (max_requests == -1)
131 max_requests = git_config_int(var, value);
132 return 0;
133 }
134#endif
135
136 if (!strcmp("http.lowspeedlimit", var)) {
137 if (curl_low_speed_limit == -1)
138 curl_low_speed_limit = (long)git_config_int(var, value);
139 return 0;
140 }
141 if (!strcmp("http.lowspeedtime", var)) {
142 if (curl_low_speed_time == -1)
143 curl_low_speed_time = (long)git_config_int(var, value);
144 return 0;
145 }
146
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300147 if (!strcmp("http.noepsv", var)) {
148 curl_ftp_no_epsv = git_config_bool(var, value);
149 return 0;
150 }
Sam Vilain9c5665a2007-11-23 13:07:00 +1300151 if (!strcmp("http.proxy", var)) {
152 if (curl_http_proxy == NULL) {
Junio C Hamano111dd252008-02-11 10:57:22 -0800153 if (!value)
154 return config_error_nonbool(var);
155 curl_http_proxy = xstrdup(value);
Sam Vilain9c5665a2007-11-23 13:07:00 +1300156 }
157 return 0;
158 }
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300159
Nick Hengeveld29508e12005-11-18 11:02:58 -0800160 /* Fall back on the default ones */
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100161 return git_default_config(var, value, cb);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800162}
163
Junio C Hamano11979b92005-11-18 17:06:46 -0800164static CURL* get_curl_handle(void)
165{
166 CURL* result = curl_easy_init();
167
Junio C Hamanoa5ccc592008-02-21 15:10:37 -0800168 if (!curl_ssl_verify) {
169 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
170 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
171 } else {
172 /* Verify authenticity of the peer's certificate */
173 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
174 /* The name in the cert must match whom we tried to connect */
175 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
176 }
177
Junio C Hamano11979b92005-11-18 17:06:46 -0800178#if LIBCURL_VERSION_NUM >= 0x070907
179 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
180#endif
181
182 if (ssl_cert != NULL)
183 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
184#if LIBCURL_VERSION_NUM >= 0x070902
185 if (ssl_key != NULL)
186 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
187#endif
188#if LIBCURL_VERSION_NUM >= 0x070908
189 if (ssl_capath != NULL)
190 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
191#endif
192 if (ssl_cainfo != NULL)
193 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
194 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
195
196 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
197 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
198 curl_low_speed_limit);
199 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
200 curl_low_speed_time);
201 }
202
203 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
204
Mark Wooding7982d742006-02-01 11:44:37 +0000205 if (getenv("GIT_CURL_VERBOSE"))
206 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
207
Nick Hengeveld20fc9bc2006-04-04 10:11:29 -0700208 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
209
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300210 if (curl_ftp_no_epsv)
211 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
212
Sam Vilain9c5665a2007-11-23 13:07:00 +1300213 if (curl_http_proxy)
214 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
215
Junio C Hamano11979b92005-11-18 17:06:46 -0800216 return result;
217}
218
Mike Hommey9fc64402008-02-27 21:35:50 +0100219void http_init(struct remote *remote)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800220{
221 char *low_speed_limit;
222 char *low_speed_time;
223
224 curl_global_init(CURL_GLOBAL_ALL);
225
Mike Hommey9fc64402008-02-27 21:35:50 +0100226 if (remote && remote->http_proxy)
227 curl_http_proxy = xstrdup(remote->http_proxy);
228
Nick Hengeveld29508e12005-11-18 11:02:58 -0800229 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
Nick Hengeveld29508e12005-11-18 11:02:58 -0800230
231#ifdef USE_CURL_MULTI
232 {
233 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
234 if (http_max_requests != NULL)
235 max_requests = atoi(http_max_requests);
236 }
237
238 curlm = curl_multi_init();
239 if (curlm == NULL) {
240 fprintf(stderr, "Error creating curl multi handle.\n");
241 exit(1);
242 }
243#endif
244
245 if (getenv("GIT_SSL_NO_VERIFY"))
246 curl_ssl_verify = 0;
247
248 ssl_cert = getenv("GIT_SSL_CERT");
249#if LIBCURL_VERSION_NUM >= 0x070902
250 ssl_key = getenv("GIT_SSL_KEY");
251#endif
252#if LIBCURL_VERSION_NUM >= 0x070908
253 ssl_capath = getenv("GIT_SSL_CAPATH");
254#endif
255 ssl_cainfo = getenv("GIT_SSL_CAINFO");
256
257 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
258 if (low_speed_limit != NULL)
259 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
260 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
261 if (low_speed_time != NULL)
262 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
263
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100264 git_config(http_options, NULL);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800265
266 if (curl_ssl_verify == -1)
267 curl_ssl_verify = 1;
268
269#ifdef USE_CURL_MULTI
270 if (max_requests < 1)
271 max_requests = DEFAULT_MAX_REQUESTS;
272#endif
273
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300274 if (getenv("GIT_CURL_FTP_NO_EPSV"))
275 curl_ftp_no_epsv = 1;
276
Nick Hengeveld29508e12005-11-18 11:02:58 -0800277#ifndef NO_CURL_EASY_DUPHANDLE
278 curl_default = get_curl_handle();
279#endif
280}
281
282void http_cleanup(void)
283{
284 struct active_request_slot *slot = active_queue_head;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800285
286 while (slot != NULL) {
Shawn O. Pearce3278cd02007-09-15 03:23:00 -0400287 struct active_request_slot *next = slot->next;
Mike Hommeyf23d1f72008-03-03 20:30:16 +0100288 if (slot->curl != NULL) {
Nick Hengeveld29508e12005-11-18 11:02:58 -0800289#ifdef USE_CURL_MULTI
Mike Hommeyf23d1f72008-03-03 20:30:16 +0100290 curl_multi_remove_handle(curlm, slot->curl);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800291#endif
Nick Hengeveld29508e12005-11-18 11:02:58 -0800292 curl_easy_cleanup(slot->curl);
Mike Hommeyf23d1f72008-03-03 20:30:16 +0100293 }
Shawn O. Pearce3278cd02007-09-15 03:23:00 -0400294 free(slot);
295 slot = next;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800296 }
Shawn O. Pearce3278cd02007-09-15 03:23:00 -0400297 active_queue_head = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800298
299#ifndef NO_CURL_EASY_DUPHANDLE
300 curl_easy_cleanup(curl_default);
301#endif
302
303#ifdef USE_CURL_MULTI
304 curl_multi_cleanup(curlm);
305#endif
306 curl_global_cleanup();
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700307
308 curl_slist_free_all(pragma_header);
Shawn O. Pearce3278cd02007-09-15 03:23:00 -0400309 pragma_header = NULL;
Mike Hommey9fc64402008-02-27 21:35:50 +0100310
311 if (curl_http_proxy) {
312 free(curl_http_proxy);
313 curl_http_proxy = NULL;
314 }
Nick Hengeveld29508e12005-11-18 11:02:58 -0800315}
316
Nick Hengeveld29508e12005-11-18 11:02:58 -0800317struct active_request_slot *get_active_slot(void)
318{
319 struct active_request_slot *slot = active_queue_head;
320 struct active_request_slot *newslot;
321
322#ifdef USE_CURL_MULTI
323 int num_transfers;
324
325 /* Wait for a slot to open up if the queue is full */
326 while (active_requests >= max_requests) {
327 curl_multi_perform(curlm, &num_transfers);
328 if (num_transfers < active_requests) {
329 process_curl_messages();
330 }
331 }
332#endif
333
334 while (slot != NULL && slot->in_use) {
335 slot = slot->next;
336 }
337 if (slot == NULL) {
338 newslot = xmalloc(sizeof(*newslot));
339 newslot->curl = NULL;
340 newslot->in_use = 0;
341 newslot->next = NULL;
342
343 slot = active_queue_head;
344 if (slot == NULL) {
345 active_queue_head = newslot;
346 } else {
347 while (slot->next != NULL) {
348 slot = slot->next;
349 }
350 slot->next = newslot;
351 }
352 slot = newslot;
353 }
354
355 if (slot->curl == NULL) {
356#ifdef NO_CURL_EASY_DUPHANDLE
357 slot->curl = get_curl_handle();
358#else
359 slot->curl = curl_easy_duphandle(curl_default);
360#endif
361 }
362
363 active_requests++;
364 slot->in_use = 1;
365 slot->local = NULL;
Nick Hengeveldc8568e12006-01-31 11:06:55 -0800366 slot->results = NULL;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800367 slot->finished = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800368 slot->callback_data = NULL;
369 slot->callback_func = NULL;
370 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800371 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
Nick Hengeveld90949502006-05-31 16:25:03 -0700372 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
373 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
374 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
375 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
376 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800377
378 return slot;
379}
380
381int start_active_slot(struct active_request_slot *slot)
382{
383#ifdef USE_CURL_MULTI
384 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
Daniel Barkalow45c17412007-09-10 23:02:28 -0400385 int num_transfers;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800386
387 if (curlm_result != CURLM_OK &&
388 curlm_result != CURLM_CALL_MULTI_PERFORM) {
389 active_requests--;
390 slot->in_use = 0;
391 return 0;
392 }
Daniel Barkalow45c17412007-09-10 23:02:28 -0400393
394 /*
395 * We know there must be something to do, since we just added
396 * something.
397 */
398 curl_multi_perform(curlm, &num_transfers);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800399#endif
400 return 1;
401}
402
403#ifdef USE_CURL_MULTI
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -0400404struct fill_chain {
405 void *data;
406 int (*fill)(void *);
407 struct fill_chain *next;
408};
409
410static struct fill_chain *fill_cfg = NULL;
411
412void add_fill_function(void *data, int (*fill)(void *))
413{
Dotan Barake8eec712008-09-09 21:57:10 +0300414 struct fill_chain *new = xmalloc(sizeof(*new));
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -0400415 struct fill_chain **linkp = &fill_cfg;
416 new->data = data;
417 new->fill = fill;
418 new->next = NULL;
419 while (*linkp)
420 linkp = &(*linkp)->next;
421 *linkp = new;
422}
423
Daniel Barkalow45c17412007-09-10 23:02:28 -0400424void fill_active_slots(void)
425{
426 struct active_request_slot *slot = active_queue_head;
427
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -0400428 while (active_requests < max_requests) {
429 struct fill_chain *fill;
430 for (fill = fill_cfg; fill; fill = fill->next)
431 if (fill->fill(fill->data))
432 break;
433
434 if (!fill)
Daniel Barkalow45c17412007-09-10 23:02:28 -0400435 break;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -0400436 }
Daniel Barkalow45c17412007-09-10 23:02:28 -0400437
438 while (slot != NULL) {
439 if (!slot->in_use && slot->curl != NULL) {
440 curl_easy_cleanup(slot->curl);
441 slot->curl = NULL;
442 }
443 slot = slot->next;
444 }
445}
446
Nick Hengeveld29508e12005-11-18 11:02:58 -0800447void step_active_slots(void)
448{
449 int num_transfers;
450 CURLMcode curlm_result;
451
452 do {
453 curlm_result = curl_multi_perform(curlm, &num_transfers);
454 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
455 if (num_transfers < active_requests) {
456 process_curl_messages();
457 fill_active_slots();
458 }
459}
460#endif
461
462void run_active_slot(struct active_request_slot *slot)
463{
464#ifdef USE_CURL_MULTI
465 long last_pos = 0;
466 long current_pos;
467 fd_set readfds;
468 fd_set writefds;
469 fd_set excfds;
470 int max_fd;
471 struct timeval select_timeout;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800472 int finished = 0;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800473
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800474 slot->finished = &finished;
475 while (!finished) {
Nick Hengeveld29508e12005-11-18 11:02:58 -0800476 data_received = 0;
477 step_active_slots();
478
479 if (!data_received && slot->local != NULL) {
480 current_pos = ftell(slot->local);
481 if (current_pos > last_pos)
482 data_received++;
483 last_pos = current_pos;
484 }
485
486 if (slot->in_use && !data_received) {
487 max_fd = 0;
488 FD_ZERO(&readfds);
489 FD_ZERO(&writefds);
490 FD_ZERO(&excfds);
491 select_timeout.tv_sec = 0;
492 select_timeout.tv_usec = 50000;
493 select(max_fd, &readfds, &writefds,
494 &excfds, &select_timeout);
495 }
496 }
497#else
498 while (slot->in_use) {
499 slot->curl_result = curl_easy_perform(slot->curl);
500 finish_active_slot(slot);
501 }
502#endif
503}
504
Mark Wooding53f31382006-02-07 10:07:39 +0000505static void closedown_active_slot(struct active_request_slot *slot)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800506{
Mike Hommey028c2972007-12-09 20:30:59 +0100507 active_requests--;
508 slot->in_use = 0;
Mark Wooding53f31382006-02-07 10:07:39 +0000509}
510
511void release_active_slot(struct active_request_slot *slot)
512{
513 closedown_active_slot(slot);
514 if (slot->curl) {
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700515#ifdef USE_CURL_MULTI
Mark Wooding53f31382006-02-07 10:07:39 +0000516 curl_multi_remove_handle(curlm, slot->curl);
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700517#endif
Mark Wooding53f31382006-02-07 10:07:39 +0000518 curl_easy_cleanup(slot->curl);
519 slot->curl = NULL;
520 }
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700521#ifdef USE_CURL_MULTI
Mark Wooding53f31382006-02-07 10:07:39 +0000522 fill_active_slots();
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700523#endif
Mark Wooding53f31382006-02-07 10:07:39 +0000524}
525
526static void finish_active_slot(struct active_request_slot *slot)
527{
528 closedown_active_slot(slot);
Mike Hommey028c2972007-12-09 20:30:59 +0100529 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
Nick Hengeveldc8568e12006-01-31 11:06:55 -0800530
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800531 if (slot->finished != NULL)
532 (*slot->finished) = 1;
533
Nick Hengeveldc8568e12006-01-31 11:06:55 -0800534 /* Store slot results so they can be read after the slot is reused */
535 if (slot->results != NULL) {
536 slot->results->curl_result = slot->curl_result;
537 slot->results->http_code = slot->http_code;
538 }
539
Mike Hommey028c2972007-12-09 20:30:59 +0100540 /* Run callback if appropriate */
541 if (slot->callback_func != NULL) {
542 slot->callback_func(slot->callback_data);
543 }
Nick Hengeveld29508e12005-11-18 11:02:58 -0800544}
545
546void finish_all_active_slots(void)
547{
548 struct active_request_slot *slot = active_queue_head;
549
550 while (slot != NULL)
551 if (slot->in_use) {
552 run_active_slot(slot);
553 slot = active_queue_head;
554 } else {
555 slot = slot->next;
556 }
557}
Mike Hommeyd7e92802007-12-11 00:08:25 +0100558
559static inline int needs_quote(int ch)
560{
561 if (((ch >= 'A') && (ch <= 'Z'))
562 || ((ch >= 'a') && (ch <= 'z'))
563 || ((ch >= '0') && (ch <= '9'))
564 || (ch == '/')
565 || (ch == '-')
566 || (ch == '.'))
567 return 0;
568 return 1;
569}
570
571static inline int hex(int v)
572{
573 if (v < 10) return '0' + v;
574 else return 'A' + v - 10;
575}
576
577static char *quote_ref_url(const char *base, const char *ref)
578{
579 const char *cp;
580 char *dp, *qref;
581 int len, baselen, ch;
582
583 baselen = strlen(base);
Mike Hommey7c1a9e72008-06-14 12:02:22 +0200584 len = baselen + 2; /* '/' after base and terminating NUL */
Mike Hommeyd7e92802007-12-11 00:08:25 +0100585 for (cp = ref; (ch = *cp) != 0; cp++, len++)
586 if (needs_quote(ch))
587 len += 2; /* extra two hex plus replacement % */
588 qref = xmalloc(len);
589 memcpy(qref, base, baselen);
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400590 dp = qref + baselen;
591 *(dp++) = '/';
592 for (cp = ref; (ch = *cp) != 0; cp++) {
Mike Hommeyd7e92802007-12-11 00:08:25 +0100593 if (needs_quote(ch)) {
594 *dp++ = '%';
595 *dp++ = hex((ch >> 4) & 0xF);
596 *dp++ = hex(ch & 0xF);
597 }
598 else
599 *dp++ = ch;
600 }
601 *dp = 0;
602
603 return qref;
604}
605
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400606int http_fetch_ref(const char *base, struct ref *ref)
Mike Hommeyd7e92802007-12-11 00:08:25 +0100607{
608 char *url;
609 struct strbuf buffer = STRBUF_INIT;
610 struct active_request_slot *slot;
611 struct slot_results results;
612 int ret;
613
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400614 url = quote_ref_url(base, ref->name);
Mike Hommeyd7e92802007-12-11 00:08:25 +0100615 slot = get_active_slot();
616 slot->results = &results;
617 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
618 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
619 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
620 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
621 if (start_active_slot(slot)) {
622 run_active_slot(slot);
623 if (results.curl_result == CURLE_OK) {
624 strbuf_rtrim(&buffer);
625 if (buffer.len == 40)
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400626 ret = get_sha1_hex(buffer.buf, ref->old_sha1);
Daniel Barkalowbe885d92008-04-26 15:53:12 -0400627 else if (!prefixcmp(buffer.buf, "ref: ")) {
628 ref->symref = xstrdup(buffer.buf + 5);
629 ret = 0;
630 } else
Mike Hommeyd7e92802007-12-11 00:08:25 +0100631 ret = 1;
632 } else {
633 ret = error("Couldn't get %s for %s\n%s",
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400634 url, ref->name, curl_errorstr);
Mike Hommeyd7e92802007-12-11 00:08:25 +0100635 }
636 } else {
637 ret = error("Unable to start request");
638 }
639
640 strbuf_release(&buffer);
641 free(url);
642 return ret;
643}