1
0
mirror of https://github.com/SKCraft/Launcher.git synced 2024-12-03 13:44:18 +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,31 +96,7 @@ 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);
if (body != null) {
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Content-Length", Integer.toString(body.length));
conn.setDoInput(true);
}
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
conn.setRequestMethod(method);
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setReadTimeout(READ_TIMEOUT);
conn.connect();
if (body != null) {
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(body);
out.flush();
out.close();
}
inputStream = conn.getResponseCode() == HttpURLConnection.HTTP_OK ? inputStream = conn.getResponseCode() == HttpURLConnection.HTTP_OK ?
conn.getInputStream() : conn.getErrorStream(); conn.getInputStream() : conn.getErrorStream();
@ -134,6 +111,60 @@ public class HttpRequest implements Closeable, ProgressObservable {
return this; 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) {
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Content-Length", Integer.toString(body.length));
conn.setDoInput(true);
}
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
conn.setRequestMethod(method);
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setReadTimeout(READ_TIMEOUT);
conn.connect();
if (body != null) {
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(body);
out.flush();
out.close();
}
switch (conn.getResponseCode()) {
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++;
return runRequest(new URL(this.url, location));
default:
break;
}
return conn;
}
/** /**
* Require that the response code is one of the given response codes. * Require that the response code is one of the given response codes.
* *

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,32 +113,7 @@ 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);
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Java) SKMCLauncher");
if (body != null) {
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Content-Length", Integer.toString(body.length));
conn.setDoInput(true);
}
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
conn.setRequestMethod(method);
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setReadTimeout(READ_TIMEOUT);
conn.connect();
if (body != null) {
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(body);
out.flush();
out.close();
}
inputStream = conn.getResponseCode() == HttpURLConnection.HTTP_OK ? inputStream = conn.getResponseCode() == HttpURLConnection.HTTP_OK ?
conn.getInputStream() : conn.getErrorStream(); conn.getInputStream() : conn.getErrorStream();
@ -149,6 +128,60 @@ public class HttpRequest implements Closeable, ProgressObservable {
return this; 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) {
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Content-Length", Integer.toString(body.length));
conn.setDoInput(true);
}
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
conn.setRequestMethod(method);
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setReadTimeout(READ_TIMEOUT);
conn.connect();
if (body != null) {
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(body);
out.flush();
out.close();
}
switch (conn.getResponseCode()) {
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++;
return runRequest(new URL(this.url, location));
default:
break;
}
return conn;
}
/** /**
* Require that the response code is one of the given response codes. * Require that the response code is one of the given response codes.
* *