feat: support gzip encoding requests & use GZIPInputStream (#1104)

This commit is contained in:
anasofiagribeiro 2021-04-09 03:26:23 +01:00 committed by GitHub
parent 9071d5131a
commit c774bf3311
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,6 +41,7 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.util.Locale; import java.util.Locale;
import java.util.zip.GZIPInputStream;
/** /**
* What this class provides: * What this class provides:
@ -286,13 +287,19 @@ public class CordovaResourceApi {
case URI_TYPE_HTTP: case URI_TYPE_HTTP:
case URI_TYPE_HTTPS: { case URI_TYPE_HTTPS: {
HttpURLConnection conn = (HttpURLConnection)new URL(uri.toString()).openConnection(); HttpURLConnection conn = (HttpURLConnection)new URL(uri.toString()).openConnection();
conn.setRequestProperty("Accept-Encoding", "gzip");
conn.setDoInput(true); conn.setDoInput(true);
String mimeType = conn.getHeaderField("Content-Type"); String mimeType = conn.getHeaderField("Content-Type");
if (mimeType != null) { if (mimeType != null) {
mimeType = mimeType.split(";")[0]; mimeType = mimeType.split(";")[0];
} }
int length = conn.getContentLength(); int length = conn.getContentLength();
InputStream inputStream = conn.getInputStream(); InputStream inputStream;
if ("gzip".equals(conn.getContentEncoding())) {
inputStream = new GZIPInputStream(conn.getInputStream());
} else {
inputStream = conn.getInputStream();
}
return new OpenForReadResult(uri, inputStream, mimeType, length, null); return new OpenForReadResult(uri, inputStream, mimeType, length, null);
} }
case URI_TYPE_PLUGIN: { case URI_TYPE_PLUGIN: {