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