diff --git a/util/src/main/java/eu/darkbot/util/http/Http.java b/util/src/main/java/eu/darkbot/util/http/Http.java index 008eb5f9..36e5871a 100644 --- a/util/src/main/java/eu/darkbot/util/http/Http.java +++ b/util/src/main/java/eu/darkbot/util/http/Http.java @@ -12,6 +12,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.io.PushbackInputStream; import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.URL; @@ -22,6 +23,7 @@ import java.util.List; import java.util.Map; import java.util.function.Consumer; +import java.util.zip.GZIPInputStream; /** * Utility for HTTP connections. @@ -291,7 +293,22 @@ public void closeInputStream() throws IOException { * @throws IOException of {@link Http#getConnection()} */ public InputStream getInputStream() throws IOException { - return getConnection().getInputStream(); + PushbackInputStream in = new PushbackInputStream(getConnection().getInputStream(), 2); + + byte[] header = new byte[2]; + int count = 0; + while (count < header.length) { + int read = in.read(header, count, header.length - count); + if (read == -1) break; + count += read; + } + if (count > 0) in.unread(header, 0, count); + + // Response may be gzip-compressed even without a Content-Encoding header + if (count == 2 && (header[0] & 0xff) == 0x1f && (header[1] & 0xff) == 0x8b) { + return new GZIPInputStream(in); + } + return in; } /**