Add supports for redirects when doing mod version checks (#4826)

This commit is contained in:
Take Weiland 2018-05-13 20:39:13 +02:00 committed by LexManos
parent c5d95cb3a9
commit 38c5cb6b94
1 changed files with 40 additions and 1 deletions

View File

@ -21,8 +21,11 @@ package net.minecraftforge.common;
import static net.minecraftforge.common.ForgeVersion.Status.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -68,6 +71,8 @@ public class ForgeVersion
private static final Logger log = LogManager.getLogger(MOD_ID + ".VersionCheck");
private static final int MAX_HTTP_REDIRECTS = Integer.getInteger("http.maxRedirects", 20);
public static int getMajorVersion()
{
return majorVersion;
@ -202,6 +207,40 @@ public class ForgeVersion
}
}
/**
* Opens stream for given URL while following redirects
*/
private InputStream openUrlStream(URL url) throws IOException
{
URL currentUrl = url;
for (int redirects = 0; redirects < MAX_HTTP_REDIRECTS; redirects++)
{
URLConnection c = currentUrl.openConnection();
if (c instanceof HttpURLConnection)
{
HttpURLConnection huc = (HttpURLConnection) c;
huc.setInstanceFollowRedirects(false);
int responseCode = huc.getResponseCode();
if (responseCode >= 300 && responseCode <= 399)
{
try
{
String loc = huc.getHeaderField("Location");
currentUrl = new URL(currentUrl, loc);
continue;
}
finally
{
huc.disconnect();
}
}
}
return c.getInputStream();
}
throw new IOException("Too many redirects while trying to fetch " + url);
}
private void process(ModContainer mod, URL url)
{
try
@ -210,7 +249,7 @@ public class ForgeVersion
Status status = PENDING;
ComparableVersion target = null;
InputStream con = url.openStream();
InputStream con = openUrlStream(url);
String data = new String(ByteStreams.toByteArray(con), "UTF-8");
con.close();