blob: 96d83d5c3cfaea66ae9471b84024e34fdd7d5b3b [file] [log] [blame]
Nick Hengeveld29508e12005-11-18 11:02:58 -08001#include "http.h"
2
3int data_received;
Junio C Hamano4251ccb2009-03-09 18:47:29 -07004int active_requests;
Tay Ray Chuane9176742009-06-06 16:43:41 +08005int http_is_verbose;
Nick Hengeveld29508e12005-11-18 11:02:58 -08006
7#ifdef USE_CURL_MULTI
Mike Hommeycc3530e2007-12-09 18:04:57 +01008static int max_requests = -1;
9static CURLM *curlm;
Nick Hengeveld29508e12005-11-18 11:02:58 -080010#endif
11#ifndef NO_CURL_EASY_DUPHANDLE
Mike Hommeycc3530e2007-12-09 18:04:57 +010012static CURL *curl_default;
Nick Hengeveld29508e12005-11-18 11:02:58 -080013#endif
14char curl_errorstr[CURL_ERROR_SIZE];
15
Mike Hommeycc3530e2007-12-09 18:04:57 +010016static int curl_ssl_verify = -1;
Junio C Hamano4251ccb2009-03-09 18:47:29 -070017static const char *ssl_cert;
Nick Hengeveld29508e12005-11-18 11:02:58 -080018#if LIBCURL_VERSION_NUM >= 0x070902
Junio C Hamano4251ccb2009-03-09 18:47:29 -070019static const char *ssl_key;
Nick Hengeveld29508e12005-11-18 11:02:58 -080020#endif
21#if LIBCURL_VERSION_NUM >= 0x070908
Junio C Hamano4251ccb2009-03-09 18:47:29 -070022static const char *ssl_capath;
Nick Hengeveld29508e12005-11-18 11:02:58 -080023#endif
Junio C Hamano4251ccb2009-03-09 18:47:29 -070024static const char *ssl_cainfo;
Mike Hommeycc3530e2007-12-09 18:04:57 +010025static long curl_low_speed_limit = -1;
26static long curl_low_speed_time = -1;
Junio C Hamano4251ccb2009-03-09 18:47:29 -070027static int curl_ftp_no_epsv;
28static const char *curl_http_proxy;
Junio C Hamanoc33976c2009-03-09 23:34:25 -070029static char *user_name, *user_pass;
Nick Hengeveld29508e12005-11-18 11:02:58 -080030
Mike Hommeycc3530e2007-12-09 18:04:57 +010031static struct curl_slist *pragma_header;
Nick Hengeveld29508e12005-11-18 11:02:58 -080032
Tay Ray Chuane9176742009-06-06 16:43:41 +080033struct curl_slist *no_pragma_header;
34
Junio C Hamano4251ccb2009-03-09 18:47:29 -070035static struct active_request_slot *active_queue_head;
Nick Hengeveld29508e12005-11-18 11:02:58 -080036
Junio C Hamanof444e522008-07-04 00:37:40 -070037size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb, void *buffer_)
Nick Hengeveld29508e12005-11-18 11:02:58 -080038{
39 size_t size = eltsize * nmemb;
Junio C Hamanof444e522008-07-04 00:37:40 -070040 struct buffer *buffer = buffer_;
41
Mike Hommey028c2972007-12-09 20:30:59 +010042 if (size > buffer->buf.len - buffer->posn)
43 size = buffer->buf.len - buffer->posn;
44 memcpy(ptr, buffer->buf.buf + buffer->posn, size);
Nick Hengeveld29508e12005-11-18 11:02:58 -080045 buffer->posn += size;
Mike Hommey028c2972007-12-09 20:30:59 +010046
Nick Hengeveld29508e12005-11-18 11:02:58 -080047 return size;
48}
49
Martin Storsjö3944ba02009-04-01 19:48:24 +030050#ifndef NO_CURL_IOCTL
51curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
52{
53 struct buffer *buffer = clientp;
54
55 switch (cmd) {
56 case CURLIOCMD_NOP:
57 return CURLIOE_OK;
58
59 case CURLIOCMD_RESTARTREAD:
60 buffer->posn = 0;
61 return CURLIOE_OK;
62
63 default:
64 return CURLIOE_UNKNOWNCMD;
65 }
66}
67#endif
68
Junio C Hamanof444e522008-07-04 00:37:40 -070069size_t fwrite_buffer(const void *ptr, size_t eltsize, size_t nmemb, void *buffer_)
Nick Hengeveld29508e12005-11-18 11:02:58 -080070{
71 size_t size = eltsize * nmemb;
Junio C Hamanof444e522008-07-04 00:37:40 -070072 struct strbuf *buffer = buffer_;
73
Mike Hommey028c2972007-12-09 20:30:59 +010074 strbuf_add(buffer, ptr, size);
Nick Hengeveld29508e12005-11-18 11:02:58 -080075 data_received++;
76 return size;
77}
78
Junio C Hamanof444e522008-07-04 00:37:40 -070079size_t fwrite_null(const void *ptr, size_t eltsize, size_t nmemb, void *strbuf)
Nick Hengeveld29508e12005-11-18 11:02:58 -080080{
81 data_received++;
82 return eltsize * nmemb;
83}
84
85static void finish_active_slot(struct active_request_slot *slot);
86
87#ifdef USE_CURL_MULTI
88static void process_curl_messages(void)
89{
90 int num_messages;
91 struct active_request_slot *slot;
92 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
93
94 while (curl_message != NULL) {
95 if (curl_message->msg == CURLMSG_DONE) {
96 int curl_result = curl_message->data.result;
97 slot = active_queue_head;
98 while (slot != NULL &&
99 slot->curl != curl_message->easy_handle)
100 slot = slot->next;
101 if (slot != NULL) {
102 curl_multi_remove_handle(curlm, slot->curl);
103 slot->curl_result = curl_result;
104 finish_active_slot(slot);
105 } else {
106 fprintf(stderr, "Received DONE message for unknown request!\n");
107 }
108 } else {
109 fprintf(stderr, "Unknown CURL message received: %d\n",
110 (int)curl_message->msg);
111 }
112 curl_message = curl_multi_info_read(curlm, &num_messages);
113 }
114}
115#endif
116
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100117static int http_options(const char *var, const char *value, void *cb)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800118{
119 if (!strcmp("http.sslverify", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700120 curl_ssl_verify = git_config_bool(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800121 return 0;
122 }
Junio C Hamano7059cd92009-03-09 19:00:30 -0700123 if (!strcmp("http.sslcert", var))
124 return git_config_string(&ssl_cert, var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800125#if LIBCURL_VERSION_NUM >= 0x070902
Junio C Hamano7059cd92009-03-09 19:00:30 -0700126 if (!strcmp("http.sslkey", var))
127 return git_config_string(&ssl_key, var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800128#endif
129#if LIBCURL_VERSION_NUM >= 0x070908
Junio C Hamano7059cd92009-03-09 19:00:30 -0700130 if (!strcmp("http.sslcapath", var))
131 return git_config_string(&ssl_capath, var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800132#endif
Junio C Hamano7059cd92009-03-09 19:00:30 -0700133 if (!strcmp("http.sslcainfo", var))
134 return git_config_string(&ssl_cainfo, var, value);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700135#ifdef USE_CURL_MULTI
Nick Hengeveld29508e12005-11-18 11:02:58 -0800136 if (!strcmp("http.maxrequests", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700137 max_requests = git_config_int(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800138 return 0;
139 }
140#endif
Nick Hengeveld29508e12005-11-18 11:02:58 -0800141 if (!strcmp("http.lowspeedlimit", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700142 curl_low_speed_limit = (long)git_config_int(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800143 return 0;
144 }
145 if (!strcmp("http.lowspeedtime", var)) {
Junio C Hamano7059cd92009-03-09 19:00:30 -0700146 curl_low_speed_time = (long)git_config_int(var, value);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800147 return 0;
148 }
149
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300150 if (!strcmp("http.noepsv", var)) {
151 curl_ftp_no_epsv = git_config_bool(var, value);
152 return 0;
153 }
Junio C Hamano7059cd92009-03-09 19:00:30 -0700154 if (!strcmp("http.proxy", var))
155 return git_config_string(&curl_http_proxy, var, value);
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300156
Nick Hengeveld29508e12005-11-18 11:02:58 -0800157 /* Fall back on the default ones */
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100158 return git_default_config(var, value, cb);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800159}
160
Junio C Hamanoc33976c2009-03-09 23:34:25 -0700161static void init_curl_http_auth(CURL *result)
162{
Junio C Hamano750d9302009-03-12 22:34:43 -0700163 if (user_name) {
Junio C Hamanoc33976c2009-03-09 23:34:25 -0700164 struct strbuf up = STRBUF_INIT;
165 if (!user_pass)
166 user_pass = xstrdup(getpass("Password: "));
167 strbuf_addf(&up, "%s:%s", user_name, user_pass);
168 curl_easy_setopt(result, CURLOPT_USERPWD,
169 strbuf_detach(&up, NULL));
170 }
171}
172
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700173static CURL *get_curl_handle(void)
Junio C Hamano11979b92005-11-18 17:06:46 -0800174{
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700175 CURL *result = curl_easy_init();
Junio C Hamano11979b92005-11-18 17:06:46 -0800176
Junio C Hamanoa5ccc592008-02-21 15:10:37 -0800177 if (!curl_ssl_verify) {
178 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
179 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
180 } else {
181 /* Verify authenticity of the peer's certificate */
182 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
183 /* The name in the cert must match whom we tried to connect */
184 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
185 }
186
Junio C Hamano11979b92005-11-18 17:06:46 -0800187#if LIBCURL_VERSION_NUM >= 0x070907
188 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
189#endif
190
Junio C Hamanoc33976c2009-03-09 23:34:25 -0700191 init_curl_http_auth(result);
192
Junio C Hamano11979b92005-11-18 17:06:46 -0800193 if (ssl_cert != NULL)
194 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
195#if LIBCURL_VERSION_NUM >= 0x070902
196 if (ssl_key != NULL)
197 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
198#endif
199#if LIBCURL_VERSION_NUM >= 0x070908
200 if (ssl_capath != NULL)
201 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
202#endif
203 if (ssl_cainfo != NULL)
204 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
205 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
206
207 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
208 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
209 curl_low_speed_limit);
210 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
211 curl_low_speed_time);
212 }
213
214 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
215
Mark Wooding7982d742006-02-01 11:44:37 +0000216 if (getenv("GIT_CURL_VERBOSE"))
217 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
218
Nick Hengeveld20fc9bc2006-04-04 10:11:29 -0700219 curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
220
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300221 if (curl_ftp_no_epsv)
222 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
223
Sam Vilain9c5665a2007-11-23 13:07:00 +1300224 if (curl_http_proxy)
225 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
226
Junio C Hamano11979b92005-11-18 17:06:46 -0800227 return result;
228}
229
Junio C Hamanoc33976c2009-03-09 23:34:25 -0700230static void http_auth_init(const char *url)
231{
232 char *at, *colon, *cp, *slash;
233 int len;
234
235 cp = strstr(url, "://");
236 if (!cp)
237 return;
238
239 /*
240 * Ok, the URL looks like "proto://something". Which one?
241 * "proto://<user>:<pass>@<host>/...",
242 * "proto://<user>@<host>/...", or just
243 * "proto://<host>/..."?
244 */
245 cp += 3;
246 at = strchr(cp, '@');
247 colon = strchr(cp, ':');
248 slash = strchrnul(cp, '/');
249 if (!at || slash <= at)
250 return; /* No credentials */
251 if (!colon || at <= colon) {
252 /* Only username */
253 len = at - cp;
254 user_name = xmalloc(len + 1);
255 memcpy(user_name, cp, len);
256 user_name[len] = '\0';
257 user_pass = NULL;
258 } else {
259 len = colon - cp;
260 user_name = xmalloc(len + 1);
261 memcpy(user_name, cp, len);
262 user_name[len] = '\0';
263 len = at - (colon + 1);
264 user_pass = xmalloc(len + 1);
265 memcpy(user_pass, colon + 1, len);
266 user_pass[len] = '\0';
267 }
268}
269
Junio C Hamano7059cd92009-03-09 19:00:30 -0700270static void set_from_env(const char **var, const char *envname)
271{
272 const char *val = getenv(envname);
273 if (val)
274 *var = val;
275}
276
Mike Hommey9fc64402008-02-27 21:35:50 +0100277void http_init(struct remote *remote)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800278{
279 char *low_speed_limit;
280 char *low_speed_time;
281
Tay Ray Chuane9176742009-06-06 16:43:41 +0800282 http_is_verbose = 0;
283
Junio C Hamano7059cd92009-03-09 19:00:30 -0700284 git_config(http_options, NULL);
285
Nick Hengeveld29508e12005-11-18 11:02:58 -0800286 curl_global_init(CURL_GLOBAL_ALL);
287
Mike Hommey9fc64402008-02-27 21:35:50 +0100288 if (remote && remote->http_proxy)
289 curl_http_proxy = xstrdup(remote->http_proxy);
290
Nick Hengeveld29508e12005-11-18 11:02:58 -0800291 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
Tay Ray Chuane9176742009-06-06 16:43:41 +0800292 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
Nick Hengeveld29508e12005-11-18 11:02:58 -0800293
294#ifdef USE_CURL_MULTI
295 {
296 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
297 if (http_max_requests != NULL)
298 max_requests = atoi(http_max_requests);
299 }
300
301 curlm = curl_multi_init();
302 if (curlm == NULL) {
303 fprintf(stderr, "Error creating curl multi handle.\n");
304 exit(1);
305 }
306#endif
307
308 if (getenv("GIT_SSL_NO_VERIFY"))
309 curl_ssl_verify = 0;
310
Junio C Hamano7059cd92009-03-09 19:00:30 -0700311 set_from_env(&ssl_cert, "GIT_SSL_CERT");
Nick Hengeveld29508e12005-11-18 11:02:58 -0800312#if LIBCURL_VERSION_NUM >= 0x070902
Junio C Hamano7059cd92009-03-09 19:00:30 -0700313 set_from_env(&ssl_key, "GIT_SSL_KEY");
Nick Hengeveld29508e12005-11-18 11:02:58 -0800314#endif
315#if LIBCURL_VERSION_NUM >= 0x070908
Junio C Hamano7059cd92009-03-09 19:00:30 -0700316 set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
Nick Hengeveld29508e12005-11-18 11:02:58 -0800317#endif
Junio C Hamano7059cd92009-03-09 19:00:30 -0700318 set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
Nick Hengeveld29508e12005-11-18 11:02:58 -0800319
320 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
321 if (low_speed_limit != NULL)
322 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
323 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
324 if (low_speed_time != NULL)
325 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
326
Nick Hengeveld29508e12005-11-18 11:02:58 -0800327 if (curl_ssl_verify == -1)
328 curl_ssl_verify = 1;
329
330#ifdef USE_CURL_MULTI
331 if (max_requests < 1)
332 max_requests = DEFAULT_MAX_REQUESTS;
333#endif
334
Sasha Khapyorsky3ea099d2006-09-29 03:10:44 +0300335 if (getenv("GIT_CURL_FTP_NO_EPSV"))
336 curl_ftp_no_epsv = 1;
337
Junio C Hamanoc33976c2009-03-09 23:34:25 -0700338 if (remote && remote->url && remote->url[0])
339 http_auth_init(remote->url[0]);
340
Nick Hengeveld29508e12005-11-18 11:02:58 -0800341#ifndef NO_CURL_EASY_DUPHANDLE
342 curl_default = get_curl_handle();
343#endif
344}
345
346void http_cleanup(void)
347{
348 struct active_request_slot *slot = active_queue_head;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800349
350 while (slot != NULL) {
Shawn O. Pearce3278cd02007-09-15 03:23:00 -0400351 struct active_request_slot *next = slot->next;
Mike Hommeyf23d1f72008-03-03 20:30:16 +0100352 if (slot->curl != NULL) {
Nick Hengeveld29508e12005-11-18 11:02:58 -0800353#ifdef USE_CURL_MULTI
Mike Hommeyf23d1f72008-03-03 20:30:16 +0100354 curl_multi_remove_handle(curlm, slot->curl);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800355#endif
Nick Hengeveld29508e12005-11-18 11:02:58 -0800356 curl_easy_cleanup(slot->curl);
Mike Hommeyf23d1f72008-03-03 20:30:16 +0100357 }
Shawn O. Pearce3278cd02007-09-15 03:23:00 -0400358 free(slot);
359 slot = next;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800360 }
Shawn O. Pearce3278cd02007-09-15 03:23:00 -0400361 active_queue_head = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800362
363#ifndef NO_CURL_EASY_DUPHANDLE
364 curl_easy_cleanup(curl_default);
365#endif
366
367#ifdef USE_CURL_MULTI
368 curl_multi_cleanup(curlm);
369#endif
370 curl_global_cleanup();
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700371
372 curl_slist_free_all(pragma_header);
Shawn O. Pearce3278cd02007-09-15 03:23:00 -0400373 pragma_header = NULL;
Mike Hommey9fc64402008-02-27 21:35:50 +0100374
Tay Ray Chuane9176742009-06-06 16:43:41 +0800375 curl_slist_free_all(no_pragma_header);
376 no_pragma_header = NULL;
377
Mike Hommey9fc64402008-02-27 21:35:50 +0100378 if (curl_http_proxy) {
Miklos Vajnae4a80ec2008-12-07 01:45:37 +0100379 free((void *)curl_http_proxy);
Mike Hommey9fc64402008-02-27 21:35:50 +0100380 curl_http_proxy = NULL;
381 }
Nick Hengeveld29508e12005-11-18 11:02:58 -0800382}
383
Nick Hengeveld29508e12005-11-18 11:02:58 -0800384struct active_request_slot *get_active_slot(void)
385{
386 struct active_request_slot *slot = active_queue_head;
387 struct active_request_slot *newslot;
388
389#ifdef USE_CURL_MULTI
390 int num_transfers;
391
392 /* Wait for a slot to open up if the queue is full */
393 while (active_requests >= max_requests) {
394 curl_multi_perform(curlm, &num_transfers);
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700395 if (num_transfers < active_requests)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800396 process_curl_messages();
Nick Hengeveld29508e12005-11-18 11:02:58 -0800397 }
398#endif
399
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700400 while (slot != NULL && slot->in_use)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800401 slot = slot->next;
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700402
Nick Hengeveld29508e12005-11-18 11:02:58 -0800403 if (slot == NULL) {
404 newslot = xmalloc(sizeof(*newslot));
405 newslot->curl = NULL;
406 newslot->in_use = 0;
407 newslot->next = NULL;
408
409 slot = active_queue_head;
410 if (slot == NULL) {
411 active_queue_head = newslot;
412 } else {
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700413 while (slot->next != NULL)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800414 slot = slot->next;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800415 slot->next = newslot;
416 }
417 slot = newslot;
418 }
419
420 if (slot->curl == NULL) {
421#ifdef NO_CURL_EASY_DUPHANDLE
422 slot->curl = get_curl_handle();
423#else
424 slot->curl = curl_easy_duphandle(curl_default);
425#endif
426 }
427
428 active_requests++;
429 slot->in_use = 1;
430 slot->local = NULL;
Nick Hengeveldc8568e12006-01-31 11:06:55 -0800431 slot->results = NULL;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800432 slot->finished = NULL;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800433 slot->callback_data = NULL;
434 slot->callback_func = NULL;
435 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800436 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
Nick Hengeveld90949502006-05-31 16:25:03 -0700437 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
438 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
439 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
440 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
441 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800442
443 return slot;
444}
445
446int start_active_slot(struct active_request_slot *slot)
447{
448#ifdef USE_CURL_MULTI
449 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
Daniel Barkalow45c17412007-09-10 23:02:28 -0400450 int num_transfers;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800451
452 if (curlm_result != CURLM_OK &&
453 curlm_result != CURLM_CALL_MULTI_PERFORM) {
454 active_requests--;
455 slot->in_use = 0;
456 return 0;
457 }
Daniel Barkalow45c17412007-09-10 23:02:28 -0400458
459 /*
460 * We know there must be something to do, since we just added
461 * something.
462 */
463 curl_multi_perform(curlm, &num_transfers);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800464#endif
465 return 1;
466}
467
468#ifdef USE_CURL_MULTI
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -0400469struct fill_chain {
470 void *data;
471 int (*fill)(void *);
472 struct fill_chain *next;
473};
474
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700475static struct fill_chain *fill_cfg;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -0400476
477void add_fill_function(void *data, int (*fill)(void *))
478{
Dotan Barake8eec712008-09-09 21:57:10 +0300479 struct fill_chain *new = xmalloc(sizeof(*new));
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -0400480 struct fill_chain **linkp = &fill_cfg;
481 new->data = data;
482 new->fill = fill;
483 new->next = NULL;
484 while (*linkp)
485 linkp = &(*linkp)->next;
486 *linkp = new;
487}
488
Daniel Barkalow45c17412007-09-10 23:02:28 -0400489void fill_active_slots(void)
490{
491 struct active_request_slot *slot = active_queue_head;
492
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -0400493 while (active_requests < max_requests) {
494 struct fill_chain *fill;
495 for (fill = fill_cfg; fill; fill = fill->next)
496 if (fill->fill(fill->data))
497 break;
498
499 if (!fill)
Daniel Barkalow45c17412007-09-10 23:02:28 -0400500 break;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -0400501 }
Daniel Barkalow45c17412007-09-10 23:02:28 -0400502
503 while (slot != NULL) {
504 if (!slot->in_use && slot->curl != NULL) {
505 curl_easy_cleanup(slot->curl);
506 slot->curl = NULL;
507 }
508 slot = slot->next;
509 }
510}
511
Nick Hengeveld29508e12005-11-18 11:02:58 -0800512void step_active_slots(void)
513{
514 int num_transfers;
515 CURLMcode curlm_result;
516
517 do {
518 curlm_result = curl_multi_perform(curlm, &num_transfers);
519 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
520 if (num_transfers < active_requests) {
521 process_curl_messages();
522 fill_active_slots();
523 }
524}
525#endif
526
527void run_active_slot(struct active_request_slot *slot)
528{
529#ifdef USE_CURL_MULTI
530 long last_pos = 0;
531 long current_pos;
532 fd_set readfds;
533 fd_set writefds;
534 fd_set excfds;
535 int max_fd;
536 struct timeval select_timeout;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800537 int finished = 0;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800538
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800539 slot->finished = &finished;
540 while (!finished) {
Nick Hengeveld29508e12005-11-18 11:02:58 -0800541 data_received = 0;
542 step_active_slots();
543
544 if (!data_received && slot->local != NULL) {
545 current_pos = ftell(slot->local);
546 if (current_pos > last_pos)
547 data_received++;
548 last_pos = current_pos;
549 }
550
551 if (slot->in_use && !data_received) {
552 max_fd = 0;
553 FD_ZERO(&readfds);
554 FD_ZERO(&writefds);
555 FD_ZERO(&excfds);
556 select_timeout.tv_sec = 0;
557 select_timeout.tv_usec = 50000;
558 select(max_fd, &readfds, &writefds,
559 &excfds, &select_timeout);
560 }
561 }
562#else
563 while (slot->in_use) {
564 slot->curl_result = curl_easy_perform(slot->curl);
565 finish_active_slot(slot);
566 }
567#endif
568}
569
Mark Wooding53f31382006-02-07 10:07:39 +0000570static void closedown_active_slot(struct active_request_slot *slot)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800571{
Mike Hommey028c2972007-12-09 20:30:59 +0100572 active_requests--;
573 slot->in_use = 0;
Mark Wooding53f31382006-02-07 10:07:39 +0000574}
575
576void release_active_slot(struct active_request_slot *slot)
577{
578 closedown_active_slot(slot);
579 if (slot->curl) {
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700580#ifdef USE_CURL_MULTI
Mark Wooding53f31382006-02-07 10:07:39 +0000581 curl_multi_remove_handle(curlm, slot->curl);
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700582#endif
Mark Wooding53f31382006-02-07 10:07:39 +0000583 curl_easy_cleanup(slot->curl);
584 slot->curl = NULL;
585 }
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700586#ifdef USE_CURL_MULTI
Mark Wooding53f31382006-02-07 10:07:39 +0000587 fill_active_slots();
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700588#endif
Mark Wooding53f31382006-02-07 10:07:39 +0000589}
590
591static void finish_active_slot(struct active_request_slot *slot)
592{
593 closedown_active_slot(slot);
Mike Hommey028c2972007-12-09 20:30:59 +0100594 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
Nick Hengeveldc8568e12006-01-31 11:06:55 -0800595
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800596 if (slot->finished != NULL)
597 (*slot->finished) = 1;
598
Nick Hengeveldc8568e12006-01-31 11:06:55 -0800599 /* Store slot results so they can be read after the slot is reused */
600 if (slot->results != NULL) {
601 slot->results->curl_result = slot->curl_result;
602 slot->results->http_code = slot->http_code;
603 }
604
Mike Hommey028c2972007-12-09 20:30:59 +0100605 /* Run callback if appropriate */
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700606 if (slot->callback_func != NULL)
Mike Hommey028c2972007-12-09 20:30:59 +0100607 slot->callback_func(slot->callback_data);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800608}
609
610void finish_all_active_slots(void)
611{
612 struct active_request_slot *slot = active_queue_head;
613
614 while (slot != NULL)
615 if (slot->in_use) {
616 run_active_slot(slot);
617 slot = active_queue_head;
618 } else {
619 slot = slot->next;
620 }
621}
Mike Hommeyd7e92802007-12-11 00:08:25 +0100622
Tay Ray Chuan5ace9942009-06-06 16:43:43 +0800623/* Helpers for modifying and creating URLs */
Mike Hommeyd7e92802007-12-11 00:08:25 +0100624static inline int needs_quote(int ch)
625{
626 if (((ch >= 'A') && (ch <= 'Z'))
627 || ((ch >= 'a') && (ch <= 'z'))
628 || ((ch >= '0') && (ch <= '9'))
629 || (ch == '/')
630 || (ch == '-')
631 || (ch == '.'))
632 return 0;
633 return 1;
634}
635
636static inline int hex(int v)
637{
Junio C Hamano4251ccb2009-03-09 18:47:29 -0700638 if (v < 10)
639 return '0' + v;
640 else
641 return 'A' + v - 10;
Mike Hommeyd7e92802007-12-11 00:08:25 +0100642}
643
Tay Ray Chuan5ace9942009-06-06 16:43:43 +0800644static void end_url_with_slash(struct strbuf *buf, const char *url)
645{
646 strbuf_addstr(buf, url);
647 if (buf->len && buf->buf[buf->len - 1] != '/')
648 strbuf_addstr(buf, "/");
649}
650
Mike Hommeyd7e92802007-12-11 00:08:25 +0100651static char *quote_ref_url(const char *base, const char *ref)
652{
Tay Ray Chuan113106e2009-03-08 00:47:21 +0800653 struct strbuf buf = STRBUF_INIT;
Mike Hommeyd7e92802007-12-11 00:08:25 +0100654 const char *cp;
Tay Ray Chuan113106e2009-03-08 00:47:21 +0800655 int ch;
Mike Hommeyd7e92802007-12-11 00:08:25 +0100656
Tay Ray Chuan5ace9942009-06-06 16:43:43 +0800657 end_url_with_slash(&buf, base);
Tay Ray Chuan113106e2009-03-08 00:47:21 +0800658
659 for (cp = ref; (ch = *cp) != 0; cp++)
Mike Hommeyd7e92802007-12-11 00:08:25 +0100660 if (needs_quote(ch))
Tay Ray Chuan113106e2009-03-08 00:47:21 +0800661 strbuf_addf(&buf, "%%%02x", ch);
Mike Hommeyd7e92802007-12-11 00:08:25 +0100662 else
Tay Ray Chuan113106e2009-03-08 00:47:21 +0800663 strbuf_addch(&buf, *cp);
Mike Hommeyd7e92802007-12-11 00:08:25 +0100664
Tay Ray Chuan113106e2009-03-08 00:47:21 +0800665 return strbuf_detach(&buf, NULL);
Mike Hommeyd7e92802007-12-11 00:08:25 +0100666}
667
Mike Hommeye929cd22009-06-06 16:43:53 +0800668/* http_request() targets */
669#define HTTP_REQUEST_STRBUF 0
670#define HTTP_REQUEST_FILE 1
671
672static int http_request(const char *url, void *result, int target, int options)
673{
674 struct active_request_slot *slot;
675 struct slot_results results;
676 struct curl_slist *headers = NULL;
677 struct strbuf buf = STRBUF_INIT;
678 int ret;
679
680 slot = get_active_slot();
681 slot->results = &results;
682 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
683
684 if (result == NULL) {
685 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
686 } else {
687 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
688 curl_easy_setopt(slot->curl, CURLOPT_FILE, result);
689
690 if (target == HTTP_REQUEST_FILE) {
691 long posn = ftell(result);
692 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
693 fwrite);
694 if (posn > 0) {
695 strbuf_addf(&buf, "Range: bytes=%ld-", posn);
696 headers = curl_slist_append(headers, buf.buf);
697 strbuf_reset(&buf);
698 }
699 slot->local = result;
700 } else
701 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
702 fwrite_buffer);
703 }
704
705 strbuf_addstr(&buf, "Pragma:");
706 if (options & HTTP_NO_CACHE)
707 strbuf_addstr(&buf, " no-cache");
708
709 headers = curl_slist_append(headers, buf.buf);
710
711 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
712 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
713
714 if (start_active_slot(slot)) {
715 run_active_slot(slot);
716 if (results.curl_result == CURLE_OK)
717 ret = HTTP_OK;
718 else if (missing_target(&results))
719 ret = HTTP_MISSING_TARGET;
720 else
721 ret = HTTP_ERROR;
722 } else {
723 error("Unable to start HTTP request for %s", url);
724 ret = HTTP_START_FAILED;
725 }
726
727 slot->local = NULL;
728 curl_slist_free_all(headers);
729 strbuf_release(&buf);
730
731 return ret;
732}
733
734int http_get_strbuf(const char *url, struct strbuf *result, int options)
735{
736 return http_request(url, result, HTTP_REQUEST_STRBUF, options);
737}
738
739int http_get_file(const char *url, const char *filename, int options)
740{
741 int ret;
742 struct strbuf tmpfile = STRBUF_INIT;
743 FILE *result;
744
745 strbuf_addf(&tmpfile, "%s.temp", filename);
746 result = fopen(tmpfile.buf, "a");
747 if (! result) {
748 error("Unable to open local file %s", tmpfile.buf);
749 ret = HTTP_ERROR;
750 goto cleanup;
751 }
752
753 ret = http_request(url, result, HTTP_REQUEST_FILE, options);
754 fclose(result);
755
756 if ((ret == HTTP_OK) && move_temp_to_file(tmpfile.buf, filename))
757 ret = HTTP_ERROR;
758cleanup:
759 strbuf_release(&tmpfile);
760 return ret;
761}
762
763int http_error(const char *url, int ret)
764{
765 /* http_request has already handled HTTP_START_FAILED. */
766 if (ret != HTTP_START_FAILED)
767 error("%s while accessing %s\n", curl_errorstr, url);
768
769 return ret;
770}
771
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400772int http_fetch_ref(const char *base, struct ref *ref)
Mike Hommeyd7e92802007-12-11 00:08:25 +0100773{
774 char *url;
775 struct strbuf buffer = STRBUF_INIT;
Mike Hommey0d5896e2009-06-06 16:43:55 +0800776 int ret = -1;
Mike Hommeyd7e92802007-12-11 00:08:25 +0100777
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400778 url = quote_ref_url(base, ref->name);
Mike Hommey0d5896e2009-06-06 16:43:55 +0800779 if (http_get_strbuf(url, &buffer, HTTP_NO_CACHE) == HTTP_OK) {
780 strbuf_rtrim(&buffer);
781 if (buffer.len == 40)
782 ret = get_sha1_hex(buffer.buf, ref->old_sha1);
783 else if (!prefixcmp(buffer.buf, "ref: ")) {
784 ref->symref = xstrdup(buffer.buf + 5);
785 ret = 0;
Mike Hommeyd7e92802007-12-11 00:08:25 +0100786 }
Mike Hommeyd7e92802007-12-11 00:08:25 +0100787 }
788
789 strbuf_release(&buffer);
790 free(url);
791 return ret;
792}
Tay Ray Chuanb8caac22009-06-06 16:43:59 +0800793
794/* Helpers for fetching packs */
795static int fetch_pack_index(unsigned char *sha1, const char *base_url)
796{
797 int ret = 0;
798 char *hex = xstrdup(sha1_to_hex(sha1));
799 char *filename;
800 char *url;
801 char tmpfile[PATH_MAX];
802 long prev_posn = 0;
803 char range[RANGE_HEADER_SIZE];
804 struct strbuf buf = STRBUF_INIT;
805 struct curl_slist *range_header = NULL;
806
807 FILE *indexfile;
808 struct active_request_slot *slot;
809 struct slot_results results;
810
811 /* Don't use the index if the pack isn't there */
812 end_url_with_slash(&buf, base_url);
813 strbuf_addf(&buf, "objects/pack/pack-%s.pack", hex);
814 url = strbuf_detach(&buf, 0);
815
816 slot = get_active_slot();
817 slot->results = &results;
818 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
819 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
820 if (start_active_slot(slot)) {
821 run_active_slot(slot);
822 if (results.curl_result != CURLE_OK) {
823 ret = error("Unable to verify pack %s is available",
824 hex);
825 goto cleanup_pack;
826 }
827 } else {
828 ret = error("Unable to start request");
829 goto cleanup_pack;
830 }
831
832 if (has_pack_index(sha1)) {
833 ret = 0;
834 goto cleanup_pack;
835 }
836
837 if (http_is_verbose)
838 fprintf(stderr, "Getting index for pack %s\n", hex);
839
840 end_url_with_slash(&buf, base_url);
841 strbuf_addf(&buf, "objects/pack/pack-%s.idx", hex);
842 url = strbuf_detach(&buf, NULL);
843
844 filename = sha1_pack_index_name(sha1);
845 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
846 indexfile = fopen(tmpfile, "a");
847 if (!indexfile) {
848 ret = error("Unable to open local file %s for pack index",
849 tmpfile);
850 goto cleanup_pack;
851 }
852
853 slot = get_active_slot();
854 slot->results = &results;
855 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
856 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
857 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
858 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
859 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
860 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
861 slot->local = indexfile;
862
863 /*
864 * If there is data present from a previous transfer attempt,
865 * resume where it left off
866 */
867 prev_posn = ftell(indexfile);
868 if (prev_posn>0) {
869 if (http_is_verbose)
870 fprintf(stderr,
871 "Resuming fetch of index for pack %s at byte %ld\n",
872 hex, prev_posn);
873 sprintf(range, "Range: bytes=%ld-", prev_posn);
874 range_header = curl_slist_append(range_header, range);
875 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
876 }
877
878 if (start_active_slot(slot)) {
879 run_active_slot(slot);
880 if (results.curl_result != CURLE_OK) {
881 ret = error("Unable to get pack index %s\n%s",
882 url, curl_errorstr);
883 goto cleanup_index;
884 }
885 } else {
886 ret = error("Unable to start request");
887 goto cleanup_index;
888 }
889
890 ret = move_temp_to_file(tmpfile, filename);
891
892cleanup_index:
893 fclose(indexfile);
894 slot->local = NULL;
895cleanup_pack:
896 free(hex);
897 free(url);
898 return ret;
899}
900
901static int fetch_and_setup_pack_index(struct packed_git **packs_head,
902 unsigned char *sha1, const char *base_url)
903{
904 struct packed_git *new_pack;
905
906 if (fetch_pack_index(sha1, base_url))
907 return -1;
908
909 new_pack = parse_pack_index(sha1);
910 if (!new_pack)
911 return -1; /* parse_pack_index() already issued error message */
912 new_pack->next = *packs_head;
913 *packs_head = new_pack;
914 return 0;
915}
916
917int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
918{
919 int ret = 0, i = 0;
920 char *url, *data;
921 struct strbuf buf = STRBUF_INIT;
922 unsigned char sha1[20];
923
924 end_url_with_slash(&buf, base_url);
925 strbuf_addstr(&buf, "objects/info/packs");
926 url = strbuf_detach(&buf, NULL);
927
928 ret = http_get_strbuf(url, &buf, HTTP_NO_CACHE);
929 if (ret != HTTP_OK)
930 goto cleanup;
931
932 data = buf.buf;
933 while (i < buf.len) {
934 switch (data[i]) {
935 case 'P':
936 i++;
937 if (i + 52 <= buf.len &&
938 !prefixcmp(data + i, " pack-") &&
939 !prefixcmp(data + i + 46, ".pack\n")) {
940 get_sha1_hex(data + i + 6, sha1);
941 fetch_and_setup_pack_index(packs_head, sha1,
942 base_url);
943 i += 51;
944 break;
945 }
946 default:
947 while (i < buf.len && data[i] != '\n')
948 i++;
949 }
950 i++;
951 }
952
953cleanup:
954 free(url);
955 return ret;
956}