1
0
mirror of https://github.com/SKCraft/Launcher.git synced 2024-11-27 12:46:22 +01:00

Fix bad URL decoding & reformatting in HttpRequest

This commit is contained in:
Henry Le Grys 2021-03-28 03:22:44 +01:00
parent 9b1bae32d9
commit c3b1e51a51
2 changed files with 10 additions and 50 deletions

View File

@ -13,7 +13,10 @@ import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.net.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -116,7 +119,7 @@ public class HttpRequest implements Closeable, ProgressObservable {
throw new IOException("Too many redirects!");
}
HttpURLConnection conn = (HttpURLConnection) reformat(url).openConnection();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Java) SKMCLauncher");
conn.setInstanceFollowRedirects(false);
@ -154,7 +157,6 @@ public class HttpRequest implements Closeable, ProgressObservable {
case 307:
case 308:
String location = conn.getHeaderField("Location");
location = URLDecoder.decode(location, "UTF-8");
redirectCount++;
return runRequest(new URL(this.url, location));
@ -358,27 +360,6 @@ public class HttpRequest implements Closeable, ProgressObservable {
}
}
/**
* URL may contain spaces and other nasties that will cause a failure.
*
* @param existing the existing URL to transform
* @return the new URL, or old one if there was a failure
*/
private static URL reformat(URL existing) {
try {
URL url = new URL(existing.toString());
URI uri = new URI(
url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(),
url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
return url;
} catch (MalformedURLException e) {
return existing;
} catch (URISyntaxException e) {
return existing;
}
}
/**
* Used with {@link #bodyForm(Form)}.
*/

View File

@ -17,7 +17,10 @@ import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.net.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
import static com.skcraft.launcher.LauncherUtils.checkInterrupted;
@ -132,7 +135,7 @@ public class HttpRequest implements Closeable, ProgressObservable {
throw new IOException("Too many redirects!");
}
HttpURLConnection conn = (HttpURLConnection) reformat(url).openConnection();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Java) SKMCLauncher");
conn.setInstanceFollowRedirects(false);
@ -174,7 +177,6 @@ public class HttpRequest implements Closeable, ProgressObservable {
case 307:
case 308:
String location = conn.getHeaderField("Location");
location = URLDecoder.decode(location, "UTF-8");
redirectCount++;
return runRequest(new URL(this.url, location));
@ -433,29 +435,6 @@ public class HttpRequest implements Closeable, ProgressObservable {
}
}
/**
* URL may contain spaces and other nasties that will cause a failure.
*
* @param existing the existing URL to transform
* @return the new URL, or old one if there was a failure
*/
private static URL reformat(URL existing) {
try {
URL url = new URL(existing.toString());
URI uri = new URI(
url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(),
url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
return url;
} catch (MalformedURLException e) {
log.warning("Failed to reformat url " + existing.toString() + ", using unformatted version.");
return existing;
} catch (URISyntaxException e) {
log.warning("Failed to reformat url " + existing.toString() + ", using unformatted version.");
return existing;
}
}
/**
* Used with {@link #bodyForm(Form)}.
*/