1
0
mirror of https://github.com/SKCraft/Launcher.git synced 2024-12-02 13:33:40 +01:00

Add support for HTTPS redirects

This commit is contained in:
Henry Le Grys 2021-01-06 03:21:40 +00:00
parent 92048fbd3a
commit 7707e5e45e
2 changed files with 118 additions and 54 deletions

View File

@ -33,13 +33,14 @@ public class HttpRequest implements Closeable, ProgressObservable {
private static final int READ_BUFFER_SIZE = 1024 * 8; private static final int READ_BUFFER_SIZE = 1024 * 8;
private final Map<String, String> headers = new HashMap<String, String>(); private final Map<String, String> headers = new HashMap<String, String>();
private final String method; private String method;
@Getter @Getter
private final URL url; private final URL url;
private String contentType; private String contentType;
private byte[] body; private byte[] body;
private HttpURLConnection conn; private HttpURLConnection conn;
private InputStream inputStream; private InputStream inputStream;
private int redirectCount;
private long contentLength = -1; private long contentLength = -1;
private long readBytes = 0; private long readBytes = 0;
@ -95,7 +96,29 @@ public class HttpRequest implements Closeable, ProgressObservable {
throw new IllegalArgumentException("Connection already executed"); throw new IllegalArgumentException("Connection already executed");
} }
conn = (HttpURLConnection) reformat(url).openConnection(); conn = this.runRequest(url);
inputStream = conn.getResponseCode() == HttpURLConnection.HTTP_OK ?
conn.getInputStream() : conn.getErrorStream();
successful = true;
} finally {
if (!successful) {
close();
}
}
return this;
}
private HttpURLConnection runRequest(URL url) throws IOException {
if (redirectCount > 20) {
throw new IOException("Too many redirects!");
}
HttpURLConnection conn = (HttpURLConnection) reformat(url).openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Java) SKMCLauncher");
conn.setInstanceFollowRedirects(false);
if (body != null) { if (body != null) {
conn.setRequestProperty("Content-Type", contentType); conn.setRequestProperty("Content-Type", contentType);
@ -121,17 +144,25 @@ public class HttpRequest implements Closeable, ProgressObservable {
out.close(); out.close();
} }
inputStream = conn.getResponseCode() == HttpURLConnection.HTTP_OK ? switch (conn.getResponseCode()) {
conn.getInputStream() : conn.getErrorStream(); case HttpURLConnection.HTTP_SEE_OTHER:
method = "GET";
body = null;
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
case HttpURLConnection.HTTP_ACCEPTED:
case 307:
case 308:
String location = conn.getHeaderField("Location");
location = URLDecoder.decode(location, "UTF-8");
redirectCount++;
successful = true; return runRequest(new URL(this.url, location));
} finally { default:
if (!successful) { break;
close();
}
} }
return this; return conn;
} }
/** /**

View File

@ -17,7 +17,10 @@ import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller; import javax.xml.bind.Unmarshaller;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.skcraft.launcher.LauncherUtils.checkInterrupted; import static com.skcraft.launcher.LauncherUtils.checkInterrupted;
import static org.apache.commons.io.IOUtils.closeQuietly; import static org.apache.commons.io.IOUtils.closeQuietly;
@ -34,13 +37,14 @@ public class HttpRequest implements Closeable, ProgressObservable {
private final ObjectMapper mapper = new ObjectMapper(); private final ObjectMapper mapper = new ObjectMapper();
private final Map<String, String> headers = new HashMap<String, String>(); private final Map<String, String> headers = new HashMap<String, String>();
private final String method; private String method;
@Getter @Getter
private final URL url; private final URL url;
private String contentType; private String contentType;
private byte[] body; private byte[] body;
private HttpURLConnection conn; private HttpURLConnection conn;
private InputStream inputStream; private InputStream inputStream;
private int redirectCount;
private long contentLength = -1; private long contentLength = -1;
private long readBytes = 0; private long readBytes = 0;
@ -109,8 +113,29 @@ public class HttpRequest implements Closeable, ProgressObservable {
throw new IllegalArgumentException("Connection already executed"); throw new IllegalArgumentException("Connection already executed");
} }
conn = (HttpURLConnection) reformat(url).openConnection(); conn = this.runRequest(url);
inputStream = conn.getResponseCode() == HttpURLConnection.HTTP_OK ?
conn.getInputStream() : conn.getErrorStream();
successful = true;
} finally {
if (!successful) {
close();
}
}
return this;
}
private HttpURLConnection runRequest(URL url) throws IOException {
if (redirectCount > 20) {
throw new IOException("Too many redirects!");
}
HttpURLConnection conn = (HttpURLConnection) reformat(url).openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Java) SKMCLauncher"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Java) SKMCLauncher");
conn.setInstanceFollowRedirects(false);
if (body != null) { if (body != null) {
conn.setRequestProperty("Content-Type", contentType); conn.setRequestProperty("Content-Type", contentType);
@ -136,17 +161,25 @@ public class HttpRequest implements Closeable, ProgressObservable {
out.close(); out.close();
} }
inputStream = conn.getResponseCode() == HttpURLConnection.HTTP_OK ? switch (conn.getResponseCode()) {
conn.getInputStream() : conn.getErrorStream(); case HttpURLConnection.HTTP_SEE_OTHER:
method = "GET";
body = null;
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
case HttpURLConnection.HTTP_ACCEPTED:
case 307:
case 308:
String location = conn.getHeaderField("Location");
location = URLDecoder.decode(location, "UTF-8");
redirectCount++;
successful = true; return runRequest(new URL(this.url, location));
} finally { default:
if (!successful) { break;
close();
}
} }
return this; return conn;
} }
/** /**