http auth fails with multiple curl handles
[git/git.git] / http.c
CommitLineData
29508e1e 1#include "http.h"
2264dfa5 2#include "pack.h"
de1a2fdd 3#include "sideband.h"
fe72d420 4#include "run-command.h"
f39f72d8 5#include "url.h"
148bb6a7 6#include "credential.h"
29508e1e 7
4251ccbd 8int active_requests;
e9176745 9int http_is_verbose;
de1a2fdd 10size_t http_post_buffer = 16 * LARGE_PACKET_MAX;
29508e1e 11
b8ac9230
MS
12#if LIBCURL_VERSION_NUM >= 0x070a06
13#define LIBCURL_CAN_HANDLE_AUTH_ANY
14#endif
15
ad75ebe5
TRC
16static int min_curl_sessions = 1;
17static int curl_session_count;
29508e1e 18#ifdef USE_CURL_MULTI
cc3530e8
MH
19static int max_requests = -1;
20static CURLM *curlm;
29508e1e
NH
21#endif
22#ifndef NO_CURL_EASY_DUPHANDLE
cc3530e8 23static CURL *curl_default;
29508e1e 24#endif
5424bc55
TRC
25
26#define PREV_BUF_SIZE 4096
27#define RANGE_HEADER_SIZE 30
28
29508e1e
NH
29char curl_errorstr[CURL_ERROR_SIZE];
30
cc3530e8 31static int curl_ssl_verify = -1;
4251ccbd 32static const char *ssl_cert;
ef52aafa 33#if LIBCURL_VERSION_NUM >= 0x070903
4251ccbd 34static const char *ssl_key;
29508e1e
NH
35#endif
36#if LIBCURL_VERSION_NUM >= 0x070908
4251ccbd 37static const char *ssl_capath;
29508e1e 38#endif
4251ccbd 39static const char *ssl_cainfo;
cc3530e8
MH
40static long curl_low_speed_limit = -1;
41static long curl_low_speed_time = -1;
4251ccbd
JH
42static int curl_ftp_no_epsv;
43static const char *curl_http_proxy;
bcfb95dd 44static const char *curl_cookie_file;
148bb6a7 45static struct credential http_auth = CREDENTIAL_INIT;
a4ddbc33 46static int http_proactive_auth;
b1d1058c 47static const char *user_agent;
29508e1e 48
30dd9163
ML
49#if LIBCURL_VERSION_NUM >= 0x071700
50/* Use CURLOPT_KEYPASSWD as is */
51#elif LIBCURL_VERSION_NUM >= 0x070903
52#define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
53#else
54#define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
55#endif
56
148bb6a7 57static struct credential cert_auth = CREDENTIAL_INIT;
30dd9163
ML
58static int ssl_cert_password_required;
59
cc3530e8 60static struct curl_slist *pragma_header;
5424bc55 61static struct curl_slist *no_pragma_header;
e9176745 62
4251ccbd 63static struct active_request_slot *active_queue_head;
29508e1e 64
a04ff3ec 65size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
29508e1e
NH
66{
67 size_t size = eltsize * nmemb;
f444e528
JH
68 struct buffer *buffer = buffer_;
69
028c2976
MH
70 if (size > buffer->buf.len - buffer->posn)
71 size = buffer->buf.len - buffer->posn;
72 memcpy(ptr, buffer->buf.buf + buffer->posn, size);
29508e1e 73 buffer->posn += size;
028c2976 74
29508e1e
NH
75 return size;
76}
77
3944ba0c
MS
78#ifndef NO_CURL_IOCTL
79curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
80{
81 struct buffer *buffer = clientp;
82
83 switch (cmd) {
84 case CURLIOCMD_NOP:
85 return CURLIOE_OK;
86
87 case CURLIOCMD_RESTARTREAD:
88 buffer->posn = 0;
89 return CURLIOE_OK;
90
91 default:
92 return CURLIOE_UNKNOWNCMD;
93 }
94}
95#endif
96
a04ff3ec 97size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
29508e1e
NH
98{
99 size_t size = eltsize * nmemb;
f444e528
JH
100 struct strbuf *buffer = buffer_;
101
028c2976 102 strbuf_add(buffer, ptr, size);
29508e1e
NH
103 return size;
104}
105
a04ff3ec 106size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf)
29508e1e 107{
29508e1e
NH
108 return eltsize * nmemb;
109}
110
29508e1e
NH
111#ifdef USE_CURL_MULTI
112static void process_curl_messages(void)
113{
114 int num_messages;
115 struct active_request_slot *slot;
116 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
117
118 while (curl_message != NULL) {
119 if (curl_message->msg == CURLMSG_DONE) {
120 int curl_result = curl_message->data.result;
121 slot = active_queue_head;
122 while (slot != NULL &&
123 slot->curl != curl_message->easy_handle)
124 slot = slot->next;
125 if (slot != NULL) {
126 curl_multi_remove_handle(curlm, slot->curl);
127 slot->curl_result = curl_result;
128 finish_active_slot(slot);
129 } else {
130 fprintf(stderr, "Received DONE message for unknown request!\n");
131 }
132 } else {
133 fprintf(stderr, "Unknown CURL message received: %d\n",
134 (int)curl_message->msg);
135 }
136 curl_message = curl_multi_info_read(curlm, &num_messages);
137 }
138}
139#endif
140
ef90d6d4 141static int http_options(const char *var, const char *value, void *cb)
29508e1e
NH
142{
143 if (!strcmp("http.sslverify", var)) {
7059cd99 144 curl_ssl_verify = git_config_bool(var, value);
29508e1e
NH
145 return 0;
146 }
7059cd99
JH
147 if (!strcmp("http.sslcert", var))
148 return git_config_string(&ssl_cert, var, value);
ef52aafa 149#if LIBCURL_VERSION_NUM >= 0x070903
7059cd99
JH
150 if (!strcmp("http.sslkey", var))
151 return git_config_string(&ssl_key, var, value);
29508e1e
NH
152#endif
153#if LIBCURL_VERSION_NUM >= 0x070908
7059cd99
JH
154 if (!strcmp("http.sslcapath", var))
155 return git_config_string(&ssl_capath, var, value);
29508e1e 156#endif
7059cd99
JH
157 if (!strcmp("http.sslcainfo", var))
158 return git_config_string(&ssl_cainfo, var, value);
754ae192
ML
159 if (!strcmp("http.sslcertpasswordprotected", var)) {
160 if (git_config_bool(var, value))
161 ssl_cert_password_required = 1;
162 return 0;
163 }
ad75ebe5
TRC
164 if (!strcmp("http.minsessions", var)) {
165 min_curl_sessions = git_config_int(var, value);
166#ifndef USE_CURL_MULTI
167 if (min_curl_sessions > 1)
168 min_curl_sessions = 1;
169#endif
170 return 0;
171 }
a6080a0a 172#ifdef USE_CURL_MULTI
29508e1e 173 if (!strcmp("http.maxrequests", var)) {
7059cd99 174 max_requests = git_config_int(var, value);
29508e1e
NH
175 return 0;
176 }
177#endif
29508e1e 178 if (!strcmp("http.lowspeedlimit", var)) {
7059cd99 179 curl_low_speed_limit = (long)git_config_int(var, value);
29508e1e
NH
180 return 0;
181 }
182 if (!strcmp("http.lowspeedtime", var)) {
7059cd99 183 curl_low_speed_time = (long)git_config_int(var, value);
29508e1e
NH
184 return 0;
185 }
186
3ea099d4
SK
187 if (!strcmp("http.noepsv", var)) {
188 curl_ftp_no_epsv = git_config_bool(var, value);
189 return 0;
190 }
7059cd99
JH
191 if (!strcmp("http.proxy", var))
192 return git_config_string(&curl_http_proxy, var, value);
3ea099d4 193
bcfb95dd
DB
194 if (!strcmp("http.cookiefile", var))
195 return git_config_string(&curl_cookie_file, var, value);
196
de1a2fdd
SP
197 if (!strcmp("http.postbuffer", var)) {
198 http_post_buffer = git_config_int(var, value);
199 if (http_post_buffer < LARGE_PACKET_MAX)
200 http_post_buffer = LARGE_PACKET_MAX;
201 return 0;
202 }
203
b1d1058c
SO
204 if (!strcmp("http.useragent", var))
205 return git_config_string(&user_agent, var, value);
206
29508e1e 207 /* Fall back on the default ones */
ef90d6d4 208 return git_default_config(var, value, cb);
29508e1e
NH
209}
210
c33976cb
JH
211static void init_curl_http_auth(CURL *result)
212{
148bb6a7 213 if (http_auth.username) {
c33976cb 214 struct strbuf up = STRBUF_INIT;
148bb6a7
JK
215 credential_fill(&http_auth);
216 strbuf_addf(&up, "%s:%s",
217 http_auth.username, http_auth.password);
c33976cb
JH
218 curl_easy_setopt(result, CURLOPT_USERPWD,
219 strbuf_detach(&up, NULL));
220 }
221}
222
30dd9163
ML
223static int has_cert_password(void)
224{
30dd9163
ML
225 if (ssl_cert == NULL || ssl_cert_password_required != 1)
226 return 0;
148bb6a7
JK
227 if (!cert_auth.password) {
228 cert_auth.protocol = xstrdup("cert");
229 cert_auth.path = xstrdup(ssl_cert);
230 credential_fill(&cert_auth);
231 }
232 return 1;
30dd9163
ML
233}
234
4251ccbd 235static CURL *get_curl_handle(void)
11979b98 236{
4251ccbd 237 CURL *result = curl_easy_init();
11979b98 238
a5ccc597
JH
239 if (!curl_ssl_verify) {
240 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
241 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
242 } else {
243 /* Verify authenticity of the peer's certificate */
244 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
245 /* The name in the cert must match whom we tried to connect */
246 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
247 }
248
11979b98
JH
249#if LIBCURL_VERSION_NUM >= 0x070907
250 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
251#endif
b8ac9230 252#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
525ecd26 253 curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
b8ac9230 254#endif
11979b98 255
a4ddbc33
JK
256 if (http_proactive_auth)
257 init_curl_http_auth(result);
258
11979b98
JH
259 if (ssl_cert != NULL)
260 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
30dd9163 261 if (has_cert_password())
148bb6a7 262 curl_easy_setopt(result, CURLOPT_KEYPASSWD, cert_auth.password);
ef52aafa 263#if LIBCURL_VERSION_NUM >= 0x070903
11979b98
JH
264 if (ssl_key != NULL)
265 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
266#endif
267#if LIBCURL_VERSION_NUM >= 0x070908
268 if (ssl_capath != NULL)
269 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
270#endif
271 if (ssl_cainfo != NULL)
272 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
273 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
274
275 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
276 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
277 curl_low_speed_limit);
278 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
279 curl_low_speed_time);
280 }
281
282 curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
311e2ea0
TRC
283#if LIBCURL_VERSION_NUM >= 0x071301
284 curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
285#elif LIBCURL_VERSION_NUM >= 0x071101
286 curl_easy_setopt(result, CURLOPT_POST301, 1);
287#endif
11979b98 288
7982d74e
MW
289 if (getenv("GIT_CURL_VERBOSE"))
290 curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
291
b1d1058c
SO
292 curl_easy_setopt(result, CURLOPT_USERAGENT,
293 user_agent ? user_agent : GIT_HTTP_USER_AGENT);
20fc9bc5 294
3ea099d4
SK
295 if (curl_ftp_no_epsv)
296 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
297
dd613997 298 if (curl_http_proxy) {
9c5665aa 299 curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
dd613997
NBL
300 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
301 }
9c5665aa 302
11979b98
JH
303 return result;
304}
305
7059cd99
JH
306static void set_from_env(const char **var, const char *envname)
307{
308 const char *val = getenv(envname);
309 if (val)
310 *var = val;
311}
312
a4ddbc33 313void http_init(struct remote *remote, const char *url, int proactive_auth)
29508e1e
NH
314{
315 char *low_speed_limit;
316 char *low_speed_time;
317
e9176745
TRC
318 http_is_verbose = 0;
319
7059cd99
JH
320 git_config(http_options, NULL);
321
29508e1e
NH
322 curl_global_init(CURL_GLOBAL_ALL);
323
a4ddbc33
JK
324 http_proactive_auth = proactive_auth;
325
9fc6440d
MH
326 if (remote && remote->http_proxy)
327 curl_http_proxy = xstrdup(remote->http_proxy);
328
29508e1e 329 pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
e9176745 330 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
29508e1e
NH
331
332#ifdef USE_CURL_MULTI
333 {
334 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
335 if (http_max_requests != NULL)
336 max_requests = atoi(http_max_requests);
337 }
338
339 curlm = curl_multi_init();
340 if (curlm == NULL) {
341 fprintf(stderr, "Error creating curl multi handle.\n");
342 exit(1);
343 }
344#endif
345
346 if (getenv("GIT_SSL_NO_VERIFY"))
347 curl_ssl_verify = 0;
348
7059cd99 349 set_from_env(&ssl_cert, "GIT_SSL_CERT");
ef52aafa 350#if LIBCURL_VERSION_NUM >= 0x070903
7059cd99 351 set_from_env(&ssl_key, "GIT_SSL_KEY");
29508e1e
NH
352#endif
353#if LIBCURL_VERSION_NUM >= 0x070908
7059cd99 354 set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
29508e1e 355#endif
7059cd99 356 set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
29508e1e 357
b1d1058c
SO
358 set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
359
29508e1e
NH
360 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
361 if (low_speed_limit != NULL)
362 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
363 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
364 if (low_speed_time != NULL)
365 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
366
29508e1e
NH
367 if (curl_ssl_verify == -1)
368 curl_ssl_verify = 1;
369
ad75ebe5 370 curl_session_count = 0;
29508e1e
NH
371#ifdef USE_CURL_MULTI
372 if (max_requests < 1)
373 max_requests = DEFAULT_MAX_REQUESTS;
374#endif
375
3ea099d4
SK
376 if (getenv("GIT_CURL_FTP_NO_EPSV"))
377 curl_ftp_no_epsv = 1;
378
deba4937 379 if (url) {
148bb6a7 380 credential_from_url(&http_auth, url);
754ae192
ML
381 if (!ssl_cert_password_required &&
382 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
deba4937 383 !prefixcmp(url, "https://"))
30dd9163
ML
384 ssl_cert_password_required = 1;
385 }
c33976cb 386
29508e1e
NH
387#ifndef NO_CURL_EASY_DUPHANDLE
388 curl_default = get_curl_handle();
389#endif
390}
391
392void http_cleanup(void)
393{
394 struct active_request_slot *slot = active_queue_head;
29508e1e
NH
395
396 while (slot != NULL) {
3278cd0a 397 struct active_request_slot *next = slot->next;
f23d1f76 398 if (slot->curl != NULL) {
29508e1e 399#ifdef USE_CURL_MULTI
f23d1f76 400 curl_multi_remove_handle(curlm, slot->curl);
29508e1e 401#endif
29508e1e 402 curl_easy_cleanup(slot->curl);
f23d1f76 403 }
3278cd0a
SP
404 free(slot);
405 slot = next;
29508e1e 406 }
3278cd0a 407 active_queue_head = NULL;
29508e1e
NH
408
409#ifndef NO_CURL_EASY_DUPHANDLE
410 curl_easy_cleanup(curl_default);
411#endif
412
413#ifdef USE_CURL_MULTI
414 curl_multi_cleanup(curlm);
415#endif
416 curl_global_cleanup();
b3ca4e4e
NH
417
418 curl_slist_free_all(pragma_header);
3278cd0a 419 pragma_header = NULL;
9fc6440d 420
e9176745
TRC
421 curl_slist_free_all(no_pragma_header);
422 no_pragma_header = NULL;
423
9fc6440d 424 if (curl_http_proxy) {
e4a80ecf 425 free((void *)curl_http_proxy);
9fc6440d
MH
426 curl_http_proxy = NULL;
427 }
30dd9163 428
148bb6a7
JK
429 if (cert_auth.password != NULL) {
430 memset(cert_auth.password, 0, strlen(cert_auth.password));
431 free(cert_auth.password);
432 cert_auth.password = NULL;
30dd9163
ML
433 }
434 ssl_cert_password_required = 0;
29508e1e
NH
435}
436
29508e1e
NH
437struct active_request_slot *get_active_slot(void)
438{
439 struct active_request_slot *slot = active_queue_head;
440 struct active_request_slot *newslot;
441
442#ifdef USE_CURL_MULTI
443 int num_transfers;
444
445 /* Wait for a slot to open up if the queue is full */
446 while (active_requests >= max_requests) {
447 curl_multi_perform(curlm, &num_transfers);
4251ccbd 448 if (num_transfers < active_requests)
29508e1e 449 process_curl_messages();
29508e1e
NH
450 }
451#endif
452
4251ccbd 453 while (slot != NULL && slot->in_use)
29508e1e 454 slot = slot->next;
4251ccbd 455
29508e1e
NH
456 if (slot == NULL) {
457 newslot = xmalloc(sizeof(*newslot));
458 newslot->curl = NULL;
459 newslot->in_use = 0;
460 newslot->next = NULL;
461
462 slot = active_queue_head;
463 if (slot == NULL) {
464 active_queue_head = newslot;
465 } else {
4251ccbd 466 while (slot->next != NULL)
29508e1e 467 slot = slot->next;
29508e1e
NH
468 slot->next = newslot;
469 }
470 slot = newslot;
471 }
472
473 if (slot->curl == NULL) {
474#ifdef NO_CURL_EASY_DUPHANDLE
475 slot->curl = get_curl_handle();
476#else
477 slot->curl = curl_easy_duphandle(curl_default);
478#endif
ad75ebe5 479 curl_session_count++;
29508e1e
NH
480 }
481
482 active_requests++;
483 slot->in_use = 1;
c8568e13 484 slot->results = NULL;
baa7b67d 485 slot->finished = NULL;
29508e1e
NH
486 slot->callback_data = NULL;
487 slot->callback_func = NULL;
bcfb95dd 488 curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
29508e1e 489 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
29508e1e 490 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
9094950d
NH
491 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
492 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
493 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
1e41827d 494 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
9094950d
NH
495 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
496 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
29508e1e
NH
497
498 return slot;
499}
500
501int start_active_slot(struct active_request_slot *slot)
502{
503#ifdef USE_CURL_MULTI
504 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
45c17412 505 int num_transfers;
29508e1e
NH
506
507 if (curlm_result != CURLM_OK &&
508 curlm_result != CURLM_CALL_MULTI_PERFORM) {
509 active_requests--;
510 slot->in_use = 0;
511 return 0;
512 }
45c17412
DB
513
514 /*
515 * We know there must be something to do, since we just added
516 * something.
517 */
518 curl_multi_perform(curlm, &num_transfers);
29508e1e
NH
519#endif
520 return 1;
521}
522
523#ifdef USE_CURL_MULTI
fc57b6aa
DB
524struct fill_chain {
525 void *data;
526 int (*fill)(void *);
527 struct fill_chain *next;
528};
529
4251ccbd 530static struct fill_chain *fill_cfg;
fc57b6aa
DB
531
532void add_fill_function(void *data, int (*fill)(void *))
533{
e8eec71d 534 struct fill_chain *new = xmalloc(sizeof(*new));
fc57b6aa
DB
535 struct fill_chain **linkp = &fill_cfg;
536 new->data = data;
537 new->fill = fill;
538 new->next = NULL;
539 while (*linkp)
540 linkp = &(*linkp)->next;
541 *linkp = new;
542}
543
45c17412
DB
544void fill_active_slots(void)
545{
546 struct active_request_slot *slot = active_queue_head;
547
fc57b6aa
DB
548 while (active_requests < max_requests) {
549 struct fill_chain *fill;
550 for (fill = fill_cfg; fill; fill = fill->next)
551 if (fill->fill(fill->data))
552 break;
553
554 if (!fill)
45c17412 555 break;
fc57b6aa 556 }
45c17412
DB
557
558 while (slot != NULL) {
ad75ebe5
TRC
559 if (!slot->in_use && slot->curl != NULL
560 && curl_session_count > min_curl_sessions) {
45c17412
DB
561 curl_easy_cleanup(slot->curl);
562 slot->curl = NULL;
ad75ebe5 563 curl_session_count--;
45c17412
DB
564 }
565 slot = slot->next;
566 }
567}
568
29508e1e
NH
569void step_active_slots(void)
570{
571 int num_transfers;
572 CURLMcode curlm_result;
573
574 do {
575 curlm_result = curl_multi_perform(curlm, &num_transfers);
576 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
577 if (num_transfers < active_requests) {
578 process_curl_messages();
579 fill_active_slots();
580 }
581}
582#endif
583
584void run_active_slot(struct active_request_slot *slot)
585{
586#ifdef USE_CURL_MULTI
29508e1e
NH
587 fd_set readfds;
588 fd_set writefds;
589 fd_set excfds;
590 int max_fd;
591 struct timeval select_timeout;
baa7b67d 592 int finished = 0;
29508e1e 593
baa7b67d
NH
594 slot->finished = &finished;
595 while (!finished) {
29508e1e
NH
596 step_active_slots();
597
df26c471 598 if (slot->in_use) {
eb56c821
MF
599#if LIBCURL_VERSION_NUM >= 0x070f04
600 long curl_timeout;
601 curl_multi_timeout(curlm, &curl_timeout);
602 if (curl_timeout == 0) {
603 continue;
604 } else if (curl_timeout == -1) {
605 select_timeout.tv_sec = 0;
606 select_timeout.tv_usec = 50000;
607 } else {
608 select_timeout.tv_sec = curl_timeout / 1000;
609 select_timeout.tv_usec = (curl_timeout % 1000) * 1000;
610 }
611#else
612 select_timeout.tv_sec = 0;
613 select_timeout.tv_usec = 50000;
614#endif
29508e1e 615
6f9dd67f 616 max_fd = -1;
29508e1e
NH
617 FD_ZERO(&readfds);
618 FD_ZERO(&writefds);
619 FD_ZERO(&excfds);
6f9dd67f 620 curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
eb56c821 621
6f9dd67f 622 select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
29508e1e
NH
623 }
624 }
625#else
626 while (slot->in_use) {
627 slot->curl_result = curl_easy_perform(slot->curl);
628 finish_active_slot(slot);
629 }
630#endif
631}
632
53f31389 633static void closedown_active_slot(struct active_request_slot *slot)
29508e1e 634{
028c2976
MH
635 active_requests--;
636 slot->in_use = 0;
53f31389
MW
637}
638
83e41e2e 639static void release_active_slot(struct active_request_slot *slot)
53f31389
MW
640{
641 closedown_active_slot(slot);
ad75ebe5 642 if (slot->curl && curl_session_count > min_curl_sessions) {
b3ca4e4e 643#ifdef USE_CURL_MULTI
53f31389 644 curl_multi_remove_handle(curlm, slot->curl);
b3ca4e4e 645#endif
53f31389
MW
646 curl_easy_cleanup(slot->curl);
647 slot->curl = NULL;
ad75ebe5 648 curl_session_count--;
53f31389 649 }
b3ca4e4e 650#ifdef USE_CURL_MULTI
53f31389 651 fill_active_slots();
b3ca4e4e 652#endif
53f31389
MW
653}
654
de1a2fdd 655void finish_active_slot(struct active_request_slot *slot)
53f31389
MW
656{
657 closedown_active_slot(slot);
028c2976 658 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
c8568e13 659
baa7b67d
NH
660 if (slot->finished != NULL)
661 (*slot->finished) = 1;
662
c8568e13
NH
663 /* Store slot results so they can be read after the slot is reused */
664 if (slot->results != NULL) {
665 slot->results->curl_result = slot->curl_result;
666 slot->results->http_code = slot->http_code;
667 }
668
028c2976 669 /* Run callback if appropriate */
4251ccbd 670 if (slot->callback_func != NULL)
028c2976 671 slot->callback_func(slot->callback_data);
29508e1e
NH
672}
673
674void finish_all_active_slots(void)
675{
676 struct active_request_slot *slot = active_queue_head;
677
678 while (slot != NULL)
679 if (slot->in_use) {
680 run_active_slot(slot);
681 slot = active_queue_head;
682 } else {
683 slot = slot->next;
684 }
685}
d7e92806 686
5ace994f 687/* Helpers for modifying and creating URLs */
d7e92806
MH
688static inline int needs_quote(int ch)
689{
690 if (((ch >= 'A') && (ch <= 'Z'))
691 || ((ch >= 'a') && (ch <= 'z'))
692 || ((ch >= '0') && (ch <= '9'))
693 || (ch == '/')
694 || (ch == '-')
695 || (ch == '.'))
696 return 0;
697 return 1;
698}
699
d7e92806
MH
700static char *quote_ref_url(const char *base, const char *ref)
701{
113106e0 702 struct strbuf buf = STRBUF_INIT;
d7e92806 703 const char *cp;
113106e0 704 int ch;
d7e92806 705
5ace994f 706 end_url_with_slash(&buf, base);
113106e0
TRC
707
708 for (cp = ref; (ch = *cp) != 0; cp++)
d7e92806 709 if (needs_quote(ch))
113106e0 710 strbuf_addf(&buf, "%%%02x", ch);
d7e92806 711 else
113106e0 712 strbuf_addch(&buf, *cp);
d7e92806 713
113106e0 714 return strbuf_detach(&buf, NULL);
d7e92806
MH
715}
716
5424bc55
TRC
717void append_remote_object_url(struct strbuf *buf, const char *url,
718 const char *hex,
719 int only_two_digit_prefix)
720{
800324c3
TRC
721 end_url_with_slash(buf, url);
722
723 strbuf_addf(buf, "objects/%.*s/", 2, hex);
5424bc55
TRC
724 if (!only_two_digit_prefix)
725 strbuf_addf(buf, "%s", hex+2);
726}
727
728char *get_remote_object_url(const char *url, const char *hex,
729 int only_two_digit_prefix)
730{
731 struct strbuf buf = STRBUF_INIT;
732 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
733 return strbuf_detach(&buf, NULL);
734}
735
e929cd20
MH
736/* http_request() targets */
737#define HTTP_REQUEST_STRBUF 0
738#define HTTP_REQUEST_FILE 1
739
740static int http_request(const char *url, void *result, int target, int options)
741{
742 struct active_request_slot *slot;
743 struct slot_results results;
744 struct curl_slist *headers = NULL;
745 struct strbuf buf = STRBUF_INIT;
746 int ret;
747
748 slot = get_active_slot();
749 slot->results = &results;
750 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
751
752 if (result == NULL) {
753 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
754 } else {
755 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
756 curl_easy_setopt(slot->curl, CURLOPT_FILE, result);
757
758 if (target == HTTP_REQUEST_FILE) {
759 long posn = ftell(result);
760 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
761 fwrite);
762 if (posn > 0) {
763 strbuf_addf(&buf, "Range: bytes=%ld-", posn);
764 headers = curl_slist_append(headers, buf.buf);
765 strbuf_reset(&buf);
766 }
e929cd20
MH
767 } else
768 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
769 fwrite_buffer);
770 }
771
772 strbuf_addstr(&buf, "Pragma:");
773 if (options & HTTP_NO_CACHE)
774 strbuf_addstr(&buf, " no-cache");
775
776 headers = curl_slist_append(headers, buf.buf);
777
778 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
779 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
780
781 if (start_active_slot(slot)) {
782 run_active_slot(slot);
783 if (results.curl_result == CURLE_OK)
784 ret = HTTP_OK;
785 else if (missing_target(&results))
786 ret = HTTP_MISSING_TARGET;
42653c09 787 else if (results.http_code == 401) {
148bb6a7
JK
788 if (http_auth.username && http_auth.password) {
789 credential_reject(&http_auth);
42653c09
SC
790 ret = HTTP_NOAUTH;
791 } else {
148bb6a7 792 credential_fill(&http_auth);
42653c09
SC
793 init_curl_http_auth(slot->curl);
794 ret = HTTP_REAUTH;
795 }
be22d92e
JN
796 } else {
797 if (!curl_errorstr[0])
798 strlcpy(curl_errorstr,
799 curl_easy_strerror(results.curl_result),
800 sizeof(curl_errorstr));
e929cd20 801 ret = HTTP_ERROR;
be22d92e 802 }
e929cd20
MH
803 } else {
804 error("Unable to start HTTP request for %s", url);
805 ret = HTTP_START_FAILED;
806 }
807
e929cd20
MH
808 curl_slist_free_all(headers);
809 strbuf_release(&buf);
810
148bb6a7
JK
811 if (ret == HTTP_OK)
812 credential_approve(&http_auth);
813
e929cd20
MH
814 return ret;
815}
816
8d677edc
JK
817static int http_request_reauth(const char *url, void *result, int target,
818 int options)
819{
820 int ret = http_request(url, result, target, options);
821 if (ret != HTTP_REAUTH)
822 return ret;
823 return http_request(url, result, target, options);
824}
825
e929cd20
MH
826int http_get_strbuf(const char *url, struct strbuf *result, int options)
827{
8d677edc 828 return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
e929cd20
MH
829}
830
83e41e2e 831/*
a7793a74 832 * Downloads a URL and stores the result in the given file.
83e41e2e
JH
833 *
834 * If a previous interrupted download is detected (i.e. a previous temporary
835 * file is still around) the download is resumed.
836 */
837static int http_get_file(const char *url, const char *filename, int options)
e929cd20
MH
838{
839 int ret;
840 struct strbuf tmpfile = STRBUF_INIT;
841 FILE *result;
842
843 strbuf_addf(&tmpfile, "%s.temp", filename);
844 result = fopen(tmpfile.buf, "a");
845 if (! result) {
846 error("Unable to open local file %s", tmpfile.buf);
847 ret = HTTP_ERROR;
848 goto cleanup;
849 }
850
8d677edc 851 ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
e929cd20
MH
852 fclose(result);
853
854 if ((ret == HTTP_OK) && move_temp_to_file(tmpfile.buf, filename))
855 ret = HTTP_ERROR;
856cleanup:
857 strbuf_release(&tmpfile);
858 return ret;
859}
860
861int http_error(const char *url, int ret)
862{
863 /* http_request has already handled HTTP_START_FAILED. */
864 if (ret != HTTP_START_FAILED)
8abc5082 865 error("%s while accessing %s", curl_errorstr, url);
e929cd20
MH
866
867 return ret;
868}
869
c13b2633 870int http_fetch_ref(const char *base, struct ref *ref)
d7e92806
MH
871{
872 char *url;
873 struct strbuf buffer = STRBUF_INIT;
0d5896e1 874 int ret = -1;
d7e92806 875
c13b2633 876 url = quote_ref_url(base, ref->name);
0d5896e1
MH
877 if (http_get_strbuf(url, &buffer, HTTP_NO_CACHE) == HTTP_OK) {
878 strbuf_rtrim(&buffer);
879 if (buffer.len == 40)
880 ret = get_sha1_hex(buffer.buf, ref->old_sha1);
881 else if (!prefixcmp(buffer.buf, "ref: ")) {
882 ref->symref = xstrdup(buffer.buf + 5);
883 ret = 0;
d7e92806 884 }
d7e92806
MH
885 }
886
887 strbuf_release(&buffer);
888 free(url);
889 return ret;
890}
b8caac2b
TRC
891
892/* Helpers for fetching packs */
750ef425 893static char *fetch_pack_index(unsigned char *sha1, const char *base_url)
b8caac2b 894{
750ef425 895 char *url, *tmp;
b8caac2b 896 struct strbuf buf = STRBUF_INIT;
b8caac2b 897
b8caac2b 898 if (http_is_verbose)
162eb5f8 899 fprintf(stderr, "Getting index for pack %s\n", sha1_to_hex(sha1));
b8caac2b
TRC
900
901 end_url_with_slash(&buf, base_url);
162eb5f8 902 strbuf_addf(&buf, "objects/pack/pack-%s.idx", sha1_to_hex(sha1));
b8caac2b
TRC
903 url = strbuf_detach(&buf, NULL);
904
750ef425
SP
905 strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(sha1));
906 tmp = strbuf_detach(&buf, NULL);
907
908 if (http_get_file(url, tmp, 0) != HTTP_OK) {
909 error("Unable to get pack index %s\n", url);
910 free(tmp);
911 tmp = NULL;
912 }
b8caac2b 913
b8caac2b 914 free(url);
750ef425 915 return tmp;
b8caac2b
TRC
916}
917
918static int fetch_and_setup_pack_index(struct packed_git **packs_head,
919 unsigned char *sha1, const char *base_url)
920{
921 struct packed_git *new_pack;
750ef425
SP
922 char *tmp_idx = NULL;
923 int ret;
b8caac2b 924
750ef425
SP
925 if (has_pack_index(sha1)) {
926 new_pack = parse_pack_index(sha1, NULL);
927 if (!new_pack)
928 return -1; /* parse_pack_index() already issued error message */
929 goto add_pack;
930 }
931
932 tmp_idx = fetch_pack_index(sha1, base_url);
933 if (!tmp_idx)
b8caac2b
TRC
934 return -1;
935
750ef425
SP
936 new_pack = parse_pack_index(sha1, tmp_idx);
937 if (!new_pack) {
938 unlink(tmp_idx);
939 free(tmp_idx);
940
b8caac2b 941 return -1; /* parse_pack_index() already issued error message */
750ef425
SP
942 }
943
944 ret = verify_pack_index(new_pack);
945 if (!ret) {
946 close_pack_index(new_pack);
947 ret = move_temp_to_file(tmp_idx, sha1_pack_index_name(sha1));
948 }
949 free(tmp_idx);
950 if (ret)
951 return -1;
952
953add_pack:
b8caac2b
TRC
954 new_pack->next = *packs_head;
955 *packs_head = new_pack;
956 return 0;
957}
958
959int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
960{
961 int ret = 0, i = 0;
962 char *url, *data;
963 struct strbuf buf = STRBUF_INIT;
964 unsigned char sha1[20];
965
966 end_url_with_slash(&buf, base_url);
967 strbuf_addstr(&buf, "objects/info/packs");
968 url = strbuf_detach(&buf, NULL);
969
970 ret = http_get_strbuf(url, &buf, HTTP_NO_CACHE);
971 if (ret != HTTP_OK)
972 goto cleanup;
973
974 data = buf.buf;
975 while (i < buf.len) {
976 switch (data[i]) {
977 case 'P':
978 i++;
979 if (i + 52 <= buf.len &&
980 !prefixcmp(data + i, " pack-") &&
981 !prefixcmp(data + i + 46, ".pack\n")) {
982 get_sha1_hex(data + i + 6, sha1);
983 fetch_and_setup_pack_index(packs_head, sha1,
984 base_url);
985 i += 51;
986 break;
987 }
988 default:
989 while (i < buf.len && data[i] != '\n')
990 i++;
991 }
992 i++;
993 }
994
995cleanup:
996 free(url);
997 return ret;
998}
2264dfa5
TRC
999
1000void release_http_pack_request(struct http_pack_request *preq)
1001{
1002 if (preq->packfile != NULL) {
1003 fclose(preq->packfile);
1004 preq->packfile = NULL;
2264dfa5
TRC
1005 }
1006 if (preq->range_header != NULL) {
1007 curl_slist_free_all(preq->range_header);
1008 preq->range_header = NULL;
1009 }
1010 preq->slot = NULL;
1011 free(preq->url);
1012}
1013
1014int finish_http_pack_request(struct http_pack_request *preq)
1015{
2264dfa5 1016 struct packed_git **lst;
021ab6f0 1017 struct packed_git *p = preq->target;
fe72d420
SP
1018 char *tmp_idx;
1019 struct child_process ip;
1020 const char *ip_argv[8];
2264dfa5 1021
fe72d420 1022 close_pack_index(p);
2264dfa5 1023
3065274c
SP
1024 fclose(preq->packfile);
1025 preq->packfile = NULL;
2264dfa5
TRC
1026
1027 lst = preq->lst;
021ab6f0 1028 while (*lst != p)
2264dfa5
TRC
1029 lst = &((*lst)->next);
1030 *lst = (*lst)->next;
1031
fe72d420
SP
1032 tmp_idx = xstrdup(preq->tmpfile);
1033 strcpy(tmp_idx + strlen(tmp_idx) - strlen(".pack.temp"),
1034 ".idx.temp");
1035
1036 ip_argv[0] = "index-pack";
1037 ip_argv[1] = "-o";
1038 ip_argv[2] = tmp_idx;
1039 ip_argv[3] = preq->tmpfile;
1040 ip_argv[4] = NULL;
1041
1042 memset(&ip, 0, sizeof(ip));
1043 ip.argv = ip_argv;
1044 ip.git_cmd = 1;
1045 ip.no_stdin = 1;
1046 ip.no_stdout = 1;
1047
1048 if (run_command(&ip)) {
1049 unlink(preq->tmpfile);
1050 unlink(tmp_idx);
1051 free(tmp_idx);
2264dfa5 1052 return -1;
fe72d420
SP
1053 }
1054
1055 unlink(sha1_pack_index_name(p->sha1));
2264dfa5 1056
fe72d420
SP
1057 if (move_temp_to_file(preq->tmpfile, sha1_pack_name(p->sha1))
1058 || move_temp_to_file(tmp_idx, sha1_pack_index_name(p->sha1))) {
1059 free(tmp_idx);
2264dfa5 1060 return -1;
fe72d420 1061 }
2264dfa5 1062
fe72d420
SP
1063 install_packed_git(p);
1064 free(tmp_idx);
2264dfa5
TRC
1065 return 0;
1066}
1067
1068struct http_pack_request *new_http_pack_request(
1069 struct packed_git *target, const char *base_url)
1070{
2264dfa5
TRC
1071 long prev_posn = 0;
1072 char range[RANGE_HEADER_SIZE];
1073 struct strbuf buf = STRBUF_INIT;
1074 struct http_pack_request *preq;
1075
ec99c9a8 1076 preq = xcalloc(1, sizeof(*preq));
2264dfa5 1077 preq->target = target;
2264dfa5
TRC
1078
1079 end_url_with_slash(&buf, base_url);
1080 strbuf_addf(&buf, "objects/pack/pack-%s.pack",
1081 sha1_to_hex(target->sha1));
bb99190e 1082 preq->url = strbuf_detach(&buf, NULL);
2264dfa5 1083
90d05713
TRC
1084 snprintf(preq->tmpfile, sizeof(preq->tmpfile), "%s.temp",
1085 sha1_pack_name(target->sha1));
2264dfa5
TRC
1086 preq->packfile = fopen(preq->tmpfile, "a");
1087 if (!preq->packfile) {
1088 error("Unable to open local file %s for pack",
1089 preq->tmpfile);
1090 goto abort;
1091 }
1092
1093 preq->slot = get_active_slot();
2264dfa5
TRC
1094 curl_easy_setopt(preq->slot->curl, CURLOPT_FILE, preq->packfile);
1095 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
bb99190e 1096 curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
2264dfa5
TRC
1097 curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
1098 no_pragma_header);
1099
1100 /*
1101 * If there is data present from a previous transfer attempt,
1102 * resume where it left off
1103 */
1104 prev_posn = ftell(preq->packfile);
1105 if (prev_posn>0) {
1106 if (http_is_verbose)
1107 fprintf(stderr,
1108 "Resuming fetch of pack %s at byte %ld\n",
1109 sha1_to_hex(target->sha1), prev_posn);
1110 sprintf(range, "Range: bytes=%ld-", prev_posn);
1111 preq->range_header = curl_slist_append(NULL, range);
1112 curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
1113 preq->range_header);
1114 }
1115
1116 return preq;
1117
1118abort:
bb99190e 1119 free(preq->url);
5ae9ebfd 1120 free(preq);
2264dfa5
TRC
1121 return NULL;
1122}
5424bc55
TRC
1123
1124/* Helpers for fetching objects (loose) */
a04ff3ec 1125static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
5424bc55
TRC
1126 void *data)
1127{
1128 unsigned char expn[4096];
1129 size_t size = eltsize * nmemb;
1130 int posn = 0;
1131 struct http_object_request *freq =
1132 (struct http_object_request *)data;
1133 do {
1134 ssize_t retval = xwrite(freq->localfile,
1135 (char *) ptr + posn, size - posn);
1136 if (retval < 0)
1137 return posn;
1138 posn += retval;
1139 } while (posn < size);
1140
1141 freq->stream.avail_in = size;
a04ff3ec 1142 freq->stream.next_in = (void *)ptr;
5424bc55
TRC
1143 do {
1144 freq->stream.next_out = expn;
1145 freq->stream.avail_out = sizeof(expn);
1146 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
1147 git_SHA1_Update(&freq->c, expn,
1148 sizeof(expn) - freq->stream.avail_out);
1149 } while (freq->stream.avail_in && freq->zret == Z_OK);
5424bc55
TRC
1150 return size;
1151}
1152
1153struct http_object_request *new_http_object_request(const char *base_url,
1154 unsigned char *sha1)
1155{
1156 char *hex = sha1_to_hex(sha1);
1157 char *filename;
1158 char prevfile[PATH_MAX];
5424bc55 1159 int prevlocal;
a04ff3ec 1160 char prev_buf[PREV_BUF_SIZE];
5424bc55
TRC
1161 ssize_t prev_read = 0;
1162 long prev_posn = 0;
1163 char range[RANGE_HEADER_SIZE];
1164 struct curl_slist *range_header = NULL;
1165 struct http_object_request *freq;
1166
ec99c9a8 1167 freq = xcalloc(1, sizeof(*freq));
5424bc55
TRC
1168 hashcpy(freq->sha1, sha1);
1169 freq->localfile = -1;
1170
1171 filename = sha1_file_name(sha1);
5424bc55
TRC
1172 snprintf(freq->tmpfile, sizeof(freq->tmpfile),
1173 "%s.temp", filename);
1174
1175 snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
1176 unlink_or_warn(prevfile);
1177 rename(freq->tmpfile, prevfile);
1178 unlink_or_warn(freq->tmpfile);
1179
1180 if (freq->localfile != -1)
1181 error("fd leakage in start: %d", freq->localfile);
1182 freq->localfile = open(freq->tmpfile,
1183 O_WRONLY | O_CREAT | O_EXCL, 0666);
1184 /*
1185 * This could have failed due to the "lazy directory creation";
1186 * try to mkdir the last path component.
1187 */
1188 if (freq->localfile < 0 && errno == ENOENT) {
1189 char *dir = strrchr(freq->tmpfile, '/');
1190 if (dir) {
1191 *dir = 0;
1192 mkdir(freq->tmpfile, 0777);
1193 *dir = '/';
1194 }
1195 freq->localfile = open(freq->tmpfile,
1196 O_WRONLY | O_CREAT | O_EXCL, 0666);
1197 }
1198
1199 if (freq->localfile < 0) {
0da8b2e7
SP
1200 error("Couldn't create temporary file %s: %s",
1201 freq->tmpfile, strerror(errno));
5424bc55
TRC
1202 goto abort;
1203 }
1204
5424bc55
TRC
1205 git_inflate_init(&freq->stream);
1206
1207 git_SHA1_Init(&freq->c);
1208
bb99190e 1209 freq->url = get_remote_object_url(base_url, hex, 0);
5424bc55
TRC
1210
1211 /*
1212 * If a previous temp file is present, process what was already
1213 * fetched.
1214 */
1215 prevlocal = open(prevfile, O_RDONLY);
1216 if (prevlocal != -1) {
1217 do {
1218 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
1219 if (prev_read>0) {
1220 if (fwrite_sha1_file(prev_buf,
1221 1,
1222 prev_read,
1223 freq) == prev_read) {
1224 prev_posn += prev_read;
1225 } else {
1226 prev_read = -1;
1227 }
1228 }
1229 } while (prev_read > 0);
1230 close(prevlocal);
1231 }
1232 unlink_or_warn(prevfile);
1233
1234 /*
1235 * Reset inflate/SHA1 if there was an error reading the previous temp
1236 * file; also rewind to the beginning of the local file.
1237 */
1238 if (prev_read == -1) {
1239 memset(&freq->stream, 0, sizeof(freq->stream));
1240 git_inflate_init(&freq->stream);
1241 git_SHA1_Init(&freq->c);
1242 if (prev_posn>0) {
1243 prev_posn = 0;
1244 lseek(freq->localfile, 0, SEEK_SET);
0c4f21e4 1245 if (ftruncate(freq->localfile, 0) < 0) {
0da8b2e7
SP
1246 error("Couldn't truncate temporary file %s: %s",
1247 freq->tmpfile, strerror(errno));
0c4f21e4
JL
1248 goto abort;
1249 }
5424bc55
TRC
1250 }
1251 }
1252
1253 freq->slot = get_active_slot();
1254
1255 curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq);
1256 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
1257 curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
bb99190e 1258 curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
5424bc55
TRC
1259 curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1260
1261 /*
1262 * If we have successfully processed data from a previous fetch
1263 * attempt, only fetch the data we don't already have.
1264 */
1265 if (prev_posn>0) {
1266 if (http_is_verbose)
1267 fprintf(stderr,
1268 "Resuming fetch of object %s at byte %ld\n",
1269 hex, prev_posn);
1270 sprintf(range, "Range: bytes=%ld-", prev_posn);
1271 range_header = curl_slist_append(range_header, range);
1272 curl_easy_setopt(freq->slot->curl,
1273 CURLOPT_HTTPHEADER, range_header);
1274 }
1275
1276 return freq;
1277
5424bc55 1278abort:
bb99190e 1279 free(freq->url);
5424bc55
TRC
1280 free(freq);
1281 return NULL;
1282}
1283
1284void process_http_object_request(struct http_object_request *freq)
1285{
1286 if (freq->slot == NULL)
1287 return;
1288 freq->curl_result = freq->slot->curl_result;
1289 freq->http_code = freq->slot->http_code;
1290 freq->slot = NULL;
1291}
1292
1293int finish_http_object_request(struct http_object_request *freq)
1294{
1295 struct stat st;
1296
1297 close(freq->localfile);
1298 freq->localfile = -1;
1299
1300 process_http_object_request(freq);
1301
1302 if (freq->http_code == 416) {
bd757c18 1303 warning("requested range invalid; we may already have all the data.");
5424bc55
TRC
1304 } else if (freq->curl_result != CURLE_OK) {
1305 if (stat(freq->tmpfile, &st) == 0)
1306 if (st.st_size == 0)
1307 unlink_or_warn(freq->tmpfile);
1308 return -1;
1309 }
1310
1311 git_inflate_end(&freq->stream);
1312 git_SHA1_Final(freq->real_sha1, &freq->c);
1313 if (freq->zret != Z_STREAM_END) {
1314 unlink_or_warn(freq->tmpfile);
1315 return -1;
1316 }
1317 if (hashcmp(freq->sha1, freq->real_sha1)) {
1318 unlink_or_warn(freq->tmpfile);
1319 return -1;
1320 }
1321 freq->rename =
0da8b2e7 1322 move_temp_to_file(freq->tmpfile, sha1_file_name(freq->sha1));
5424bc55
TRC
1323
1324 return freq->rename;
1325}
1326
1327void abort_http_object_request(struct http_object_request *freq)
1328{
1329 unlink_or_warn(freq->tmpfile);
1330
1331 release_http_object_request(freq);
1332}
1333
1334void release_http_object_request(struct http_object_request *freq)
1335{
1336 if (freq->localfile != -1) {
1337 close(freq->localfile);
1338 freq->localfile = -1;
1339 }
1340 if (freq->url != NULL) {
1341 free(freq->url);
1342 freq->url = NULL;
1343 }
4b9fa0e3
TRC
1344 if (freq->slot != NULL) {
1345 freq->slot->callback_func = NULL;
1346 freq->slot->callback_data = NULL;
1347 release_active_slot(freq->slot);
1348 freq->slot = NULL;
1349 }
5424bc55 1350}