http: avoid disconnecting on 404s for loose objects
[git/git.git] / http-walker.c
CommitLineData
6eb7ed54
DB
1#include "cache.h"
2#include "commit.h"
30ae764b 3#include "walker.h"
29508e1e 4#include "http.h"
7baa3e86 5
9cba13ca 6struct alt_base {
2afea3bc 7 char *base;
b3661567
DB
8 int got_indices;
9 struct packed_git *packs;
10 struct alt_base *next;
11};
12
e388ab74 13enum object_request_state {
1d389ab6
NH
14 WAITING,
15 ABORTED,
16 ACTIVE,
4b05548f 17 COMPLETE
1d389ab6 18};
6eb7ed54 19
9cba13ca 20struct object_request {
30ae764b 21 struct walker *walker;
1d389ab6
NH
22 unsigned char sha1[20];
23 struct alt_base *repo;
e388ab74 24 enum object_request_state state;
5424bc55 25 struct http_object_request *req;
e388ab74 26 struct object_request *next;
1d389ab6
NH
27};
28
e388ab74 29struct alternates_request {
30ae764b 30 struct walker *walker;
8e29f6a0 31 const char *base;
54ba4c5f 32 struct strbuf *url;
028c2976 33 struct strbuf *buffer;
acc075a8
NH
34 struct active_request_slot *slot;
35 int http_specific;
36};
37
30ae764b
DB
38struct walker_data {
39 const char *url;
40 int got_alternates;
41 struct alt_base *alt;
30ae764b
DB
42};
43
96f1e58f 44static struct object_request *object_queue_head;
bc8f2652 45
30ae764b 46static void fetch_alternates(struct walker *walker, const char *base);
1d389ab6 47
29508e1e 48static void process_object_response(void *callback_data);
1d389ab6 49
30ae764b
DB
50static void start_object_request(struct walker *walker,
51 struct object_request *obj_req)
1d389ab6 52{
1d389ab6 53 struct active_request_slot *slot;
5424bc55 54 struct http_object_request *req;
1d389ab6 55
5424bc55
TRC
56 req = new_http_object_request(obj_req->repo->base, obj_req->sha1);
57 if (req == NULL) {
e388ab74 58 obj_req->state = ABORTED;
1d389ab6
NH
59 return;
60 }
5424bc55 61 obj_req->req = req;
1d389ab6 62
5424bc55 63 slot = req->slot;
29508e1e 64 slot->callback_func = process_object_response;
e388ab74 65 slot->callback_data = obj_req;
1d389ab6 66
a7a8d378 67 /* Try to get the request started, abort the request on error */
e388ab74 68 obj_req->state = ACTIVE;
1d389ab6 69 if (!start_active_slot(slot)) {
e388ab74 70 obj_req->state = ABORTED;
5424bc55 71 release_http_object_request(req);
e388ab74 72 return;
1d389ab6 73 }
1d389ab6
NH
74}
75
e388ab74 76static void finish_object_request(struct object_request *obj_req)
1d389ab6 77{
5424bc55 78 if (finish_http_object_request(obj_req->req))
1d389ab6 79 return;
1d389ab6 80
5424bc55 81 if (obj_req->req->rename == 0)
30ae764b 82 walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1));
1d389ab6
NH
83}
84
29508e1e
NH
85static void process_object_response(void *callback_data)
86{
e388ab74
NH
87 struct object_request *obj_req =
88 (struct object_request *)callback_data;
30ae764b
DB
89 struct walker *walker = obj_req->walker;
90 struct walker_data *data = walker->data;
91 struct alt_base *alt = data->alt;
29508e1e 92
5424bc55 93 process_http_object_request(obj_req->req);
e388ab74 94 obj_req->state = COMPLETE;
29508e1e
NH
95
96 /* Use alternates if necessary */
5424bc55 97 if (missing_target(obj_req->req)) {
30ae764b 98 fetch_alternates(walker, alt->base);
e388ab74
NH
99 if (obj_req->repo->next != NULL) {
100 obj_req->repo =
101 obj_req->repo->next;
5424bc55 102 release_http_object_request(obj_req->req);
30ae764b 103 start_object_request(walker, obj_req);
29508e1e
NH
104 return;
105 }
106 }
107
e388ab74 108 finish_object_request(obj_req);
29508e1e
NH
109}
110
e388ab74 111static void release_object_request(struct object_request *obj_req)
1d389ab6 112{
e388ab74 113 struct object_request *entry = object_queue_head;
1d389ab6 114
5424bc55
TRC
115 if (obj_req->req !=NULL && obj_req->req->localfile != -1)
116 error("fd leakage in release: %d", obj_req->req->localfile);
e388ab74
NH
117 if (obj_req == object_queue_head) {
118 object_queue_head = obj_req->next;
1d389ab6 119 } else {
e388ab74 120 while (entry->next != NULL && entry->next != obj_req)
1d389ab6 121 entry = entry->next;
e388ab74 122 if (entry->next == obj_req)
1d389ab6
NH
123 entry->next = entry->next->next;
124 }
125
e388ab74 126 free(obj_req);
1d389ab6
NH
127}
128
a7a8d378 129#ifdef USE_CURL_MULTI
30ae764b 130static int fill_active_slot(struct walker *walker)
1d389ab6 131{
45c17412 132 struct object_request *obj_req;
1d389ab6 133
45c17412 134 for (obj_req = object_queue_head; obj_req; obj_req = obj_req->next) {
e388ab74
NH
135 if (obj_req->state == WAITING) {
136 if (has_sha1_file(obj_req->sha1))
09db444f 137 obj_req->state = COMPLETE;
45c17412 138 else {
30ae764b 139 start_object_request(walker, obj_req);
45c17412
DB
140 return 1;
141 }
f1a906a3 142 }
8fcf7f9a 143 }
45c17412 144 return 0;
1d389ab6 145}
a7a8d378 146#endif
1d389ab6 147
30ae764b 148static void prefetch(struct walker *walker, unsigned char *sha1)
1d389ab6 149{
e388ab74
NH
150 struct object_request *newreq;
151 struct object_request *tail;
30ae764b 152 struct walker_data *data = walker->data;
1d389ab6
NH
153
154 newreq = xmalloc(sizeof(*newreq));
30ae764b 155 newreq->walker = walker;
e702496e 156 hashcpy(newreq->sha1, sha1);
30ae764b 157 newreq->repo = data->alt;
1d389ab6 158 newreq->state = WAITING;
5424bc55 159 newreq->req = NULL;
1d389ab6
NH
160 newreq->next = NULL;
161
e9176745
TRC
162 http_is_verbose = walker->get_verbosely;
163
e388ab74
NH
164 if (object_queue_head == NULL) {
165 object_queue_head = newreq;
1d389ab6 166 } else {
e388ab74 167 tail = object_queue_head;
4c42aa1a 168 while (tail->next != NULL)
1d389ab6 169 tail = tail->next;
1d389ab6
NH
170 tail->next = newreq;
171 }
29508e1e 172
a7a8d378 173#ifdef USE_CURL_MULTI
29508e1e
NH
174 fill_active_slots();
175 step_active_slots();
a7a8d378 176#endif
1d389ab6
NH
177}
178
e388ab74 179static void process_alternates_response(void *callback_data)
b3661567 180{
e388ab74
NH
181 struct alternates_request *alt_req =
182 (struct alternates_request *)callback_data;
30ae764b
DB
183 struct walker *walker = alt_req->walker;
184 struct walker_data *cdata = walker->data;
acc075a8 185 struct active_request_slot *slot = alt_req->slot;
30ae764b 186 struct alt_base *tail = cdata->alt;
8e29f6a0 187 const char *base = alt_req->base;
a04ff3ec 188 const char null_byte = '\0';
acc075a8
NH
189 char *data;
190 int i = 0;
1d389ab6 191
acc075a8
NH
192 if (alt_req->http_specific) {
193 if (slot->curl_result != CURLE_OK ||
028c2976 194 !alt_req->buffer->len) {
acc075a8
NH
195
196 /* Try reusing the slot to get non-http alternates */
197 alt_req->http_specific = 0;
54ba4c5f
JK
198 strbuf_reset(alt_req->url);
199 strbuf_addf(alt_req->url, "%s/objects/info/alternates",
200 base);
acc075a8 201 curl_easy_setopt(slot->curl, CURLOPT_URL,
54ba4c5f 202 alt_req->url->buf);
acc075a8
NH
203 active_requests++;
204 slot->in_use = 1;
c9826473
NH
205 if (slot->finished != NULL)
206 (*slot->finished) = 0;
a3f583cb 207 if (!start_active_slot(slot)) {
30ae764b 208 cdata->got_alternates = -1;
29508e1e 209 slot->in_use = 0;
c9826473
NH
210 if (slot->finished != NULL)
211 (*slot->finished) = 1;
1d389ab6 212 }
a3f583cb 213 return;
b3661567 214 }
acc075a8 215 } else if (slot->curl_result != CURLE_OK) {
be4a015b 216 if (!missing_target(slot)) {
30ae764b 217 cdata->got_alternates = -1;
acc075a8
NH
218 return;
219 }
b3661567
DB
220 }
221
a04ff3ec 222 fwrite_buffer((char *)&null_byte, 1, 1, alt_req->buffer);
028c2976
MH
223 alt_req->buffer->len--;
224 data = alt_req->buffer->buf;
1b0c1e67 225
028c2976 226 while (i < alt_req->buffer->len) {
b3661567 227 int posn = i;
028c2976 228 while (posn < alt_req->buffer->len && data[posn] != '\n')
b3661567
DB
229 posn++;
230 if (data[posn] == '\n') {
1b0c1e67
DB
231 int okay = 0;
232 int serverlen = 0;
233 struct alt_base *newalt;
b3661567 234 if (data[i] == '/') {
4c42aa1a
TRC
235 /*
236 * This counts
5df1e0d0
JH
237 * http://git.host/pub/scm/linux.git/
238 * -----------here^
239 * so memcpy(dst, base, serverlen) will
240 * copy up to "...git.host".
241 */
242 const char *colon_ss = strstr(base,"://");
243 if (colon_ss) {
244 serverlen = (strchr(colon_ss + 3, '/')
245 - base);
246 okay = 1;
247 }
1b0c1e67 248 } else if (!memcmp(data + i, "../", 3)) {
4c42aa1a
TRC
249 /*
250 * Relative URL; chop the corresponding
5df1e0d0
JH
251 * number of subpath from base (and ../
252 * from data), and concatenate the result.
253 *
254 * The code first drops ../ from data, and
255 * then drops one ../ from data and one path
256 * from base. IOW, one extra ../ is dropped
257 * from data than path is dropped from base.
258 *
259 * This is not wrong. The alternate in
260 * http://git.host/pub/scm/linux.git/
261 * to borrow from
262 * http://git.host/pub/scm/linus.git/
263 * is ../../linus.git/objects/. You need
264 * two ../../ to borrow from your direct
265 * neighbour.
266 */
1b0c1e67
DB
267 i += 3;
268 serverlen = strlen(base);
8fcf7f9a 269 while (i + 2 < posn &&
1b0c1e67
DB
270 !memcmp(data + i, "../", 3)) {
271 do {
272 serverlen--;
273 } while (serverlen &&
274 base[serverlen - 1] != '/');
275 i += 3;
276 }
a9486b02 277 /* If the server got removed, give up. */
8fcf7f9a 278 okay = strchr(base, ':') - base + 3 <
4c42aa1a 279 serverlen;
acc075a8 280 } else if (alt_req->http_specific) {
1b0c1e67
DB
281 char *colon = strchr(data + i, ':');
282 char *slash = strchr(data + i, '/');
283 if (colon && slash && colon < data + posn &&
284 slash < data + posn && colon < slash) {
285 okay = 1;
286 }
287 }
5df1e0d0 288 /* skip "objects\n" at end */
1b0c1e67 289 if (okay) {
59b8263a
RS
290 struct strbuf target = STRBUF_INIT;
291 strbuf_add(&target, base, serverlen);
292 strbuf_add(&target, data + i, posn - i - 7);
30ae764b 293 if (walker->get_verbosely)
59b8263a
RS
294 fprintf(stderr, "Also look at %s\n",
295 target.buf);
b3661567 296 newalt = xmalloc(sizeof(*newalt));
1d389ab6 297 newalt->next = NULL;
59b8263a 298 newalt->base = strbuf_detach(&target, NULL);
b3661567
DB
299 newalt->got_indices = 0;
300 newalt->packs = NULL;
8d9fbe57 301
1d389ab6
NH
302 while (tail->next != NULL)
303 tail = tail->next;
304 tail->next = newalt;
b3661567
DB
305 }
306 }
307 i = posn + 1;
308 }
bc8f2652 309
30ae764b 310 cdata->got_alternates = 1;
acc075a8
NH
311}
312
30ae764b 313static void fetch_alternates(struct walker *walker, const char *base)
acc075a8 314{
028c2976 315 struct strbuf buffer = STRBUF_INIT;
54ba4c5f 316 struct strbuf url = STRBUF_INIT;
acc075a8 317 struct active_request_slot *slot;
cb754fdf 318 struct alternates_request alt_req;
30ae764b 319 struct walker_data *cdata = walker->data;
acc075a8 320
4c42aa1a
TRC
321 /*
322 * If another request has already started fetching alternates,
323 * wait for them to arrive and return to processing this request's
324 * curl message
325 */
29508e1e 326#ifdef USE_CURL_MULTI
30ae764b 327 while (cdata->got_alternates == 0) {
29508e1e 328 step_active_slots();
acc075a8 329 }
29508e1e 330#endif
acc075a8
NH
331
332 /* Nothing to do if they've already been fetched */
30ae764b 333 if (cdata->got_alternates == 1)
acc075a8
NH
334 return;
335
336 /* Start the fetch */
30ae764b 337 cdata->got_alternates = 0;
acc075a8 338
30ae764b 339 if (walker->get_verbosely)
acc075a8 340 fprintf(stderr, "Getting alternates list for %s\n", base);
8fcf7f9a 341
54ba4c5f 342 strbuf_addf(&url, "%s/objects/info/http-alternates", base);
acc075a8 343
4c42aa1a
TRC
344 /*
345 * Use a callback to process the result, since another request
346 * may fail and need to have alternates loaded before continuing
347 */
acc075a8 348 slot = get_active_slot();
e388ab74 349 slot->callback_func = process_alternates_response;
30ae764b 350 alt_req.walker = walker;
acc075a8
NH
351 slot->callback_data = &alt_req;
352
353 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
29508e1e 354 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
54ba4c5f 355 curl_easy_setopt(slot->curl, CURLOPT_URL, url.buf);
acc075a8
NH
356
357 alt_req.base = base;
54ba4c5f 358 alt_req.url = &url;
acc075a8
NH
359 alt_req.buffer = &buffer;
360 alt_req.http_specific = 1;
361 alt_req.slot = slot;
362
363 if (start_active_slot(slot))
364 run_active_slot(slot);
365 else
30ae764b 366 cdata->got_alternates = -1;
acc075a8 367
028c2976 368 strbuf_release(&buffer);
54ba4c5f 369 strbuf_release(&url);
b3661567
DB
370}
371
30ae764b 372static int fetch_indices(struct walker *walker, struct alt_base *repo)
182005b9 373{
b8caac2b 374 int ret;
1d389ab6 375
b3661567 376 if (repo->got_indices)
182005b9
DB
377 return 0;
378
30ae764b 379 if (walker->get_verbosely)
6fd72e39 380 fprintf(stderr, "Getting pack list for %s\n", repo->base);
8fcf7f9a 381
b8caac2b
TRC
382 switch (http_get_info_packs(repo->base, &repo->packs)) {
383 case HTTP_OK:
384 case HTTP_MISSING_TARGET:
385 repo->got_indices = 1;
386 ret = 0;
387 break;
388 default:
5e3a7691 389 repo->got_indices = 0;
b8caac2b 390 ret = -1;
b3661567 391 }
182005b9 392
3a462bc9 393 return ret;
182005b9
DB
394}
395
07c19e72 396static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
182005b9 397{
182005b9 398 struct packed_git *target;
49a0f240 399 int ret;
cb754fdf 400 struct slot_results results;
2264dfa5 401 struct http_pack_request *preq;
182005b9 402
30ae764b 403 if (fetch_indices(walker, repo))
182005b9 404 return -1;
b3661567 405 target = find_sha1_pack(sha1, repo->packs);
182005b9 406 if (!target)
b3661567 407 return -1;
182005b9 408
30ae764b 409 if (walker->get_verbosely) {
182005b9
DB
410 fprintf(stderr, "Getting pack %s\n",
411 sha1_to_hex(target->sha1));
412 fprintf(stderr, " which contains %s\n",
413 sha1_to_hex(sha1));
414 }
415
2264dfa5
TRC
416 preq = new_http_pack_request(target, repo->base);
417 if (preq == NULL)
418 goto abort;
419 preq->lst = &repo->packs;
420 preq->slot->results = &results;
182005b9 421
2264dfa5
TRC
422 if (start_active_slot(preq->slot)) {
423 run_active_slot(preq->slot);
c8568e13 424 if (results.curl_result != CURLE_OK) {
2264dfa5
TRC
425 error("Unable to get pack file %s\n%s", preq->url,
426 curl_errorstr);
427 goto abort;
1d389ab6
NH
428 }
429 } else {
2264dfa5
TRC
430 error("Unable to start request");
431 goto abort;
182005b9
DB
432 }
433
2264dfa5
TRC
434 ret = finish_http_pack_request(preq);
435 release_http_pack_request(preq);
49a0f240 436 if (ret)
b721e01f 437 return ret;
49a0f240 438
182005b9 439 return 0;
2264dfa5
TRC
440
441abort:
442 return -1;
182005b9
DB
443}
444
53f31389
MW
445static void abort_object_request(struct object_request *obj_req)
446{
53f31389
MW
447 release_object_request(obj_req);
448}
449
43b8bba6 450static int fetch_object(struct walker *walker, unsigned char *sha1)
6eb7ed54
DB
451{
452 char *hex = sha1_to_hex(sha1);
29508e1e 453 int ret = 0;
e388ab74 454 struct object_request *obj_req = object_queue_head;
5424bc55 455 struct http_object_request *req;
1d389ab6 456
a89fccd2 457 while (obj_req != NULL && hashcmp(obj_req->sha1, sha1))
e388ab74
NH
458 obj_req = obj_req->next;
459 if (obj_req == NULL)
1d389ab6
NH
460 return error("Couldn't find request for %s in the queue", hex);
461
e388ab74 462 if (has_sha1_file(obj_req->sha1)) {
5424bc55
TRC
463 if (obj_req->req != NULL)
464 abort_http_object_request(obj_req->req);
53f31389 465 abort_object_request(obj_req);
11f0dafe
NH
466 return 0;
467 }
468
a7a8d378 469#ifdef USE_CURL_MULTI
4c42aa1a 470 while (obj_req->state == WAITING)
29508e1e 471 step_active_slots();
a7a8d378 472#else
30ae764b 473 start_object_request(walker, obj_req);
a7a8d378 474#endif
6eb7ed54 475
5424bc55
TRC
476 /*
477 * obj_req->req might change when fetching alternates in the callback
478 * process_object_response; therefore, the "shortcut" variable, req,
479 * is used only after we're done with slots.
480 */
4c42aa1a 481 while (obj_req->state == ACTIVE)
5424bc55
TRC
482 run_active_slot(obj_req->req->slot);
483
484 req = obj_req->req;
4c42aa1a 485
5424bc55
TRC
486 if (req->localfile != -1) {
487 close(req->localfile);
488 req->localfile = -1;
313c4714 489 }
6eb7ed54 490
17966c0a
EW
491 /*
492 * we turned off CURLOPT_FAILONERROR to avoid losing a
493 * persistent connection and got CURLE_OK.
494 */
495 if (req->http_code == 404 && req->curl_result == CURLE_OK &&
496 (starts_with(req->url, "http://") ||
497 starts_with(req->url, "https://")))
498 req->curl_result = CURLE_HTTP_RETURNED_ERROR;
499
e388ab74 500 if (obj_req->state == ABORTED) {
29508e1e 501 ret = error("Request for %s aborted", hex);
5424bc55
TRC
502 } else if (req->curl_result != CURLE_OK &&
503 req->http_code != 416) {
504 if (missing_target(req))
e2029eb9
PB
505 ret = -1; /* Be silent, it is probably in a pack. */
506 else
507 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
5424bc55
TRC
508 req->errorstr, req->curl_result,
509 req->http_code, hex);
510 } else if (req->zret != Z_STREAM_END) {
30ae764b 511 walker->corrupt_object_found++;
5424bc55
TRC
512 ret = error("File %s (%s) corrupt", hex, req->url);
513 } else if (hashcmp(obj_req->sha1, req->real_sha1)) {
bd2afde8 514 ret = error("File %s has bad hash", hex);
5424bc55 515 } else if (req->rename < 0) {
7b934ec0 516 ret = error("unable to write sha1 filename %s",
0da8b2e7 517 sha1_file_name(req->sha1));
6eb7ed54 518 }
49a0f240 519
5424bc55 520 release_http_object_request(req);
e388ab74 521 release_object_request(obj_req);
29508e1e 522 return ret;
6eb7ed54
DB
523}
524
30ae764b 525static int fetch(struct walker *walker, unsigned char *sha1)
b3661567 526{
30ae764b
DB
527 struct walker_data *data = walker->data;
528 struct alt_base *altbase = data->alt;
1d389ab6 529
43b8bba6 530 if (!fetch_object(walker, sha1))
1d389ab6 531 return 0;
b3661567 532 while (altbase) {
07c19e72 533 if (!http_fetch_pack(walker, altbase, sha1))
b3661567 534 return 0;
30ae764b 535 fetch_alternates(walker, data->alt->base);
b3661567
DB
536 altbase = altbase->next;
537 }
bd2afde8 538 return error("Unable to find %s under %s", sha1_to_hex(sha1),
30ae764b 539 data->alt->base);
b3661567
DB
540}
541
c13b2633 542static int fetch_ref(struct walker *walker, struct ref *ref)
cd541a68 543{
30ae764b 544 struct walker_data *data = walker->data;
c13b2633 545 return http_fetch_ref(data->alt->base, ref);
cd541a68
DB
546}
547
30ae764b
DB
548static void cleanup(struct walker *walker)
549{
09ae9aca
TRC
550 struct walker_data *data = walker->data;
551 struct alt_base *alt, *alt_next;
552
553 if (data) {
554 alt = data->alt;
555 while (alt) {
556 alt_next = alt->next;
557
558 free(alt->base);
559 free(alt);
560
561 alt = alt_next;
562 }
563 free(data);
564 walker->data = NULL;
565 }
30ae764b
DB
566}
567
888692b7 568struct walker *get_http_walker(const char *url)
6eb7ed54 569{
9c880b3e 570 char *s;
30ae764b
DB
571 struct walker_data *data = xmalloc(sizeof(struct walker_data));
572 struct walker *walker = xmalloc(sizeof(struct walker));
6eb7ed54 573
30ae764b 574 data->alt = xmalloc(sizeof(*data->alt));
95244ae3 575 data->alt->base = xstrdup(url);
30ae764b 576 for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
9c880b3e 577 *s = 0;
6eb7ed54 578
30ae764b
DB
579 data->alt->got_indices = 0;
580 data->alt->packs = NULL;
581 data->alt->next = NULL;
582 data->got_alternates = -1;
fc57b6aa 583
30ae764b
DB
584 walker->corrupt_object_found = 0;
585 walker->fetch = fetch;
586 walker->fetch_ref = fetch_ref;
587 walker->prefetch = prefetch;
588 walker->cleanup = cleanup;
589 walker->data = data;
6eb7ed54 590
30ae764b
DB
591#ifdef USE_CURL_MULTI
592 add_fill_function(walker, (int (*)(void *)) fill_active_slot);
593#endif
8e29f6a0 594
30ae764b 595 return walker;
6eb7ed54 596}