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