| 2401 | } |
| 2402 | |
| 2403 | static int http_request_recoverable(const char *url, |
| 2404 | void *result, int target, |
| 2405 | struct http_get_options *options) |
| 2406 | { |
| 2407 | static struct http_get_options empty_opts; |
| 2408 | int i = 3; |
| 2409 | int ret; |
| 2410 | int rate_limit_retries = http_max_retries; |
| 2411 | |
| 2412 | if (!options) |
| 2413 | options = &empty_opts; |
| 2414 | |
| 2415 | if (always_auth_proactively()) |
| 2416 | credential_fill(the_repository, &http_auth, 1); |
| 2417 | |
| 2418 | ret = http_request(url, result, target, options); |
| 2419 | |
| 2420 | if (ret != HTTP_OK && ret != HTTP_REAUTH && ret != HTTP_RATE_LIMITED) |
| 2421 | return ret; |
| 2422 | |
| 2423 | /* If retries are disabled and we got a 429, fail immediately */ |
| 2424 | if (ret == HTTP_RATE_LIMITED && !http_max_retries) |
| 2425 | return HTTP_ERROR; |
| 2426 | |
| 2427 | if (options->effective_url && options->base_url) { |
| 2428 | if (update_url_from_redirect(options->base_url, |
| 2429 | url, options->effective_url)) { |
| 2430 | credential_from_url(&http_auth, options->base_url->buf); |
| 2431 | url = options->effective_url->buf; |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | while ((ret == HTTP_REAUTH && --i) || |
| 2436 | (ret == HTTP_RATE_LIMITED && --rate_limit_retries)) { |
| 2437 | long retry_delay = -1; |
| 2438 | /* |
| 2439 | * The previous request may have put cruft into our output stream; we |
| 2440 | * should clear it out before making our next request. |
| 2441 | */ |
| 2442 | switch (target) { |
| 2443 | case HTTP_REQUEST_STRBUF: |
| 2444 | strbuf_reset(result); |
| 2445 | break; |
| 2446 | case HTTP_REQUEST_FILE: { |
| 2447 | FILE *f = result; |
| 2448 | if (fflush(f)) { |
| 2449 | error_errno("unable to flush a file"); |
| 2450 | return HTTP_START_FAILED; |
| 2451 | } |
| 2452 | rewind(f); |
| 2453 | if (ftruncate(fileno(f), 0) < 0) { |
| 2454 | error_errno("unable to truncate a file"); |
| 2455 | return HTTP_START_FAILED; |
| 2456 | } |
| 2457 | break; |
| 2458 | } |
| 2459 | default: |
| 2460 | BUG("Unknown http_request target"); |
no test coverage detected