mirror of
https://github.com/SKCraft/Launcher.git
synced 2024-11-30 13:13:58 +01:00
Add support for HTTPS redirects
This commit is contained in:
parent
92048fbd3a
commit
7707e5e45e
@ -33,13 +33,14 @@ public class HttpRequest implements Closeable, ProgressObservable {
|
||||
private static final int READ_BUFFER_SIZE = 1024 * 8;
|
||||
|
||||
private final Map<String, String> headers = new HashMap<String, String>();
|
||||
private final String method;
|
||||
private String method;
|
||||
@Getter
|
||||
private final URL url;
|
||||
private String contentType;
|
||||
private byte[] body;
|
||||
private HttpURLConnection conn;
|
||||
private InputStream inputStream;
|
||||
private int redirectCount;
|
||||
|
||||
private long contentLength = -1;
|
||||
private long readBytes = 0;
|
||||
@ -95,7 +96,29 @@ public class HttpRequest implements Closeable, ProgressObservable {
|
||||
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) {
|
||||
conn.setRequestProperty("Content-Type", contentType);
|
||||
@ -121,17 +144,25 @@ public class HttpRequest implements Closeable, ProgressObservable {
|
||||
out.close();
|
||||
}
|
||||
|
||||
inputStream = conn.getResponseCode() == HttpURLConnection.HTTP_OK ?
|
||||
conn.getInputStream() : conn.getErrorStream();
|
||||
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++;
|
||||
|
||||
successful = true;
|
||||
} finally {
|
||||
if (!successful) {
|
||||
close();
|
||||
}
|
||||
return runRequest(new URL(this.url, location));
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return this;
|
||||
return conn;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,7 +17,10 @@ import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import java.io.*;
|
||||
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 org.apache.commons.io.IOUtils.closeQuietly;
|
||||
@ -34,13 +37,14 @@ public class HttpRequest implements Closeable, ProgressObservable {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final Map<String, String> headers = new HashMap<String, String>();
|
||||
private final String method;
|
||||
private String method;
|
||||
@Getter
|
||||
private final URL url;
|
||||
private String contentType;
|
||||
private byte[] body;
|
||||
private HttpURLConnection conn;
|
||||
private InputStream inputStream;
|
||||
private int redirectCount;
|
||||
|
||||
private long contentLength = -1;
|
||||
private long readBytes = 0;
|
||||
@ -109,8 +113,29 @@ public class HttpRequest implements Closeable, ProgressObservable {
|
||||
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) {
|
||||
conn.setRequestProperty("Content-Type", contentType);
|
||||
@ -136,17 +161,25 @@ public class HttpRequest implements Closeable, ProgressObservable {
|
||||
out.close();
|
||||
}
|
||||
|
||||
inputStream = conn.getResponseCode() == HttpURLConnection.HTTP_OK ?
|
||||
conn.getInputStream() : conn.getErrorStream();
|
||||
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++;
|
||||
|
||||
successful = true;
|
||||
} finally {
|
||||
if (!successful) {
|
||||
close();
|
||||
}
|
||||
return runRequest(new URL(this.url, location));
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return this;
|
||||
return conn;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user