|
| 1 | +/* |
| 2 | + * Copyright (c) Forge Development LLC |
| 3 | + * SPDX-License-Identifier: LGPL-2.1-only |
| 4 | + */ |
| 5 | +package net.minecraftforge.util.download; |
| 6 | + |
| 7 | +import java.io.FileNotFoundException; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.net.HttpURLConnection; |
| 11 | +import java.net.URI; |
| 12 | +import java.net.URISyntaxException; |
| 13 | +import java.net.http.HttpClient; |
| 14 | +import java.net.http.HttpRequest; |
| 15 | +import java.net.http.HttpResponse; |
| 16 | +import java.time.Duration; |
| 17 | +import java.util.ArrayList; |
| 18 | + |
| 19 | +final class DownloadUtilsImpl { |
| 20 | + private DownloadUtilsImpl() { } |
| 21 | + |
| 22 | + private static final Duration TIMEOUT = Duration.ofSeconds(5); |
| 23 | + private static final int MAX_REDIRECTS = 3; |
| 24 | + |
| 25 | + private static final HttpClient CLIENT = HttpClient |
| 26 | + .newBuilder() |
| 27 | + .connectTimeout(TIMEOUT) |
| 28 | + .followRedirects(HttpClient.Redirect.NEVER) |
| 29 | + .build(); |
| 30 | + |
| 31 | + private static final HttpRequest.Builder REQUEST_BUILDER = HttpRequest |
| 32 | + .newBuilder() |
| 33 | + .timeout(TIMEOUT) |
| 34 | + .header("User-Agent", "MinecraftForge-Utils") |
| 35 | + .header("Accept", "application/json"); |
| 36 | + |
| 37 | + static InputStream connect(String address) throws IOException, InterruptedException { |
| 38 | + URI uri; |
| 39 | + try { |
| 40 | + uri = new URI(address); |
| 41 | + } catch (URISyntaxException e) { |
| 42 | + throw new IOException(e); |
| 43 | + } |
| 44 | + |
| 45 | + // if not http, then try a URLConnection. we might be trying to make a file or jar connection. |
| 46 | + // see jdk.internal.net.http.HttpRequestBuilderImpl#checkUri |
| 47 | + var scheme = uri.getScheme(); |
| 48 | + if (scheme == null || !(scheme.equalsIgnoreCase("https") || scheme.equalsIgnoreCase("http"))) { |
| 49 | + return uri.toURL().openStream(); |
| 50 | + } |
| 51 | + |
| 52 | + var redirections = new ArrayList<String>(); |
| 53 | + HttpResponse<InputStream> con; |
| 54 | + for (int redirects = 0; ; redirects++) { |
| 55 | + con = CLIENT.send( |
| 56 | + REQUEST_BUILDER.uri(uri).build(), |
| 57 | + info -> HttpResponse.BodySubscribers.ofInputStream() |
| 58 | + ); |
| 59 | + |
| 60 | + int res = con.statusCode(); |
| 61 | + if (res == HttpURLConnection.HTTP_MOVED_PERM || res == HttpURLConnection.HTTP_MOVED_TEMP) { |
| 62 | + var header = con.headers().firstValue("Location"); |
| 63 | + if (header.isEmpty()) |
| 64 | + throw new IOException(String.format( |
| 65 | + "No location header found in redirect response: %s -- previous redirections: [%s]", |
| 66 | + uri, String.join(", ", redirections) |
| 67 | + )); |
| 68 | + |
| 69 | + var location = header.get(); |
| 70 | + redirections.add(location); |
| 71 | + |
| 72 | + if (redirects == MAX_REDIRECTS - 1) { |
| 73 | + throw new IOException(String.format( |
| 74 | + "Too many redirects: %s -- redirections: [%s]", |
| 75 | + address, String.join(", ", redirections) |
| 76 | + )); |
| 77 | + } else { |
| 78 | + uri = uri.resolve(location); |
| 79 | + } |
| 80 | + } else if (res == HttpURLConnection.HTTP_NOT_FOUND) { |
| 81 | + throw new FileNotFoundException("Returned 404: " + address); |
| 82 | + } else { |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return con.body(); |
| 88 | + } |
| 89 | + |
| 90 | + static byte[] readInputStream(InputStream stream) throws IOException { |
| 91 | + return stream.readAllBytes(); |
| 92 | + } |
| 93 | +} |
0 commit comments