feat: Add Gzip ability to Android http (#128)

* Add Gzip ability to Android http

* Fix issues from rebasing older version of widgets, and fix case sensitivity

* Fix the string to be lowercased.
This commit is contained in:
Nathanael Anderson
2018-07-17 10:20:08 -05:00
committed by Martin Yankov
parent a7e023cbf9
commit 1ed68d47ed

View File

@@ -29,6 +29,7 @@ import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream;
public class Async public class Async
{ {
@@ -296,10 +297,20 @@ public class Async
{ {
return; return;
} }
boolean hasAcceptHeader = false;
for (KeyValuePair pair : this.headers) for (KeyValuePair pair : this.headers)
{ {
connection.addRequestProperty(pair.key.toString(), pair.value.toString()); String key = pair.key.toString();
connection.addRequestProperty(key, pair.value.toString());
if (key.toLowerCase().contentEquals("accept-encoding")) {
hasAcceptHeader = true;
}
}
// If the user hasn't added an Accept-Encoding header, we add gzip as something we accept
if (!hasAcceptHeader) {
connection.addRequestProperty("Accept-Encoding", "gzip");
} }
} }
@@ -387,6 +398,14 @@ public class Async
else else
{ {
inStream = connection.getInputStream(); inStream = connection.getInputStream();
// In the event we don't have a null stream, and we have gzip as part of the encoding
// then we will use gzip to decode the stream
if (inStream != null) {
String encodingHeader = connection.getHeaderField("Content-Encoding");
if (encodingHeader != null && encodingHeader.toLowerCase().contains("gzip")) {
inStream = new GZIPInputStream(inStream);
}
}
} }
if (inStream == null) if (inStream == null)