From 1ed68d47ed0f7a016d264edd3ced87eb6dc83758 Mon Sep 17 00:00:00 2001 From: Nathanael Anderson Date: Tue, 17 Jul 2018 10:20:08 -0500 Subject: [PATCH] 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. --- .../java/org/nativescript/widgets/Async.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/android/widgets/src/main/java/org/nativescript/widgets/Async.java b/android/widgets/src/main/java/org/nativescript/widgets/Async.java index bf444cc67..e1ce838f6 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/Async.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/Async.java @@ -29,6 +29,7 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.zip.GZIPInputStream; public class Async { @@ -296,10 +297,20 @@ public class Async { return; } + boolean hasAcceptHeader = false; 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 { 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)