Skip to content

Commit

Permalink
Add a User-Agent to the rest of the web requests (GeyserMC#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 authored Jul 11, 2020
1 parent 4daa568 commit 5e5e3b0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -379,7 +380,11 @@ private static byte[] requestImage(String imageUrl, CapeProvider provider) throw
private static BufferedImage downloadImage(String imageUrl, CapeProvider provider) throws IOException {
if (provider == CapeProvider.FIVEZIG)
return readFiveZigCape(imageUrl);
BufferedImage image = ImageIO.read(new URL(imageUrl));

HttpURLConnection con = (HttpURLConnection) new URL(imageUrl).openConnection();
con.setRequestProperty("User-Agent", "Geyser-" + GeyserConnector.getInstance().getPlatformType().toString() + "/" + GeyserConnector.VERSION);

BufferedImage image = ImageIO.read(con.getInputStream());
if (image == null) throw new NullPointerException();
return image;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public static String getBody(String reqURL) {
*/
public static void downloadFile(String reqURL, String fileLocation) {
try {
InputStream in = new URL(reqURL).openStream();
HttpURLConnection con = (HttpURLConnection) new URL(reqURL).openConnection();
con.setRequestProperty("User-Agent", "Geyser-" + GeyserConnector.getInstance().getPlatformType().toString() + "/" + GeyserConnector.VERSION);
InputStream in = con.getInputStream();
Files.copy(in, Paths.get(fileLocation), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
throw new AssertionError("Unable to download and save file: " + fileLocation + " (" + reqURL + ")", e);
Expand All @@ -90,6 +92,7 @@ public static String post(String reqURL, String postContent) throws IOException
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text/plain");
con.setRequestProperty("User-Agent", "Geyser-" + GeyserConnector.getInstance().getPlatformType().toString() + "/" + GeyserConnector.VERSION);
con.setDoOutput(true);

OutputStream out = con.getOutputStream();
Expand Down

0 comments on commit 5e5e3b0

Please sign in to comment.