Use keep-available when processing ALI

This commit is contained in:
yushijinhun 2020-08-13 21:10:17 +08:00
parent 059dc2641d
commit d3da27444c
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4

View File

@ -25,6 +25,7 @@ import static moe.yushi.authlibinjector.util.IOUtils.asString;
import static moe.yushi.authlibinjector.util.IOUtils.removeNewLines; import static moe.yushi.authlibinjector.util.IOUtils.removeNewLines;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.lang.instrument.Instrumentation; import java.lang.instrument.Instrumentation;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
@ -243,7 +244,16 @@ public final class AuthlibInjector {
if (ali != null) { if (ali != null) {
URL absoluteAli = new URL(connection.getURL(), ali); URL absoluteAli = new URL(connection.getURL(), ali);
if (!urlEqualsIgnoreSlash(apiRoot, absoluteAli.toString())) { if (!urlEqualsIgnoreSlash(apiRoot, absoluteAli.toString())) {
connection.disconnect();
// usually the URL that ALI points to is on the same host
// so the TCP connection can be reused
// we need to consume the response to make the connection reusable
try (InputStream in = connection.getInputStream()) {
while (in.read() != -1)
;
} catch (IOException e) {
}
Logging.CONFIG.info("Redirect to: " + absoluteAli); Logging.CONFIG.info("Redirect to: " + absoluteAli);
apiRoot = absoluteAli.toString(); apiRoot = absoluteAli.toString();
warnIfHttp(apiRoot); warnIfHttp(apiRoot);
@ -251,10 +261,8 @@ public final class AuthlibInjector {
} }
} }
try { try (InputStream in = connection.getInputStream()) {
metadataResponse = asString(asBytes(connection.getInputStream())); metadataResponse = asString(asBytes(in));
} finally {
connection.disconnect();
} }
} catch (IOException e) { } catch (IOException e) {
Logging.CONFIG.severe("Failed to fetch metadata: " + e); Logging.CONFIG.severe("Failed to fetch metadata: " + e);