From ebcda8e4d390ce78a17d012d9f2bc7c5efd03986 Mon Sep 17 00:00:00 2001 From: Reinaldo Antonio Camargo Rauch Date: Tue, 11 Sep 2018 10:09:19 -0300 Subject: [PATCH] fix: add support for gzipped content in http error stream (#134) When the HTTP server responded with an error (statusCode >= 400), the readResponseStream didn't decode the inStream as gzip even if the request sended had the Content-Encoding header. My fix moves the code that parses Content-Encoded header and decodes gzipped body out the test to get the input stream to the point where we are certain that we have a inStream that we can decode. --- .../java/org/nativescript/widgets/Async.java | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) 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 88c4a382a..8cac1b8ee 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/Async.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/Async.java @@ -388,23 +388,10 @@ public class Async { int contentLength = connection.getContentLength(); - InputStream inStream; - if (this.statusCode >= 400) - { - inStream = connection.getErrorStream(); - } - 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); - } - } - } + InputStream inStream = + this.statusCode >= 400 + ? connection.getErrorStream() + : connection.getInputStream(); if (inStream == null) { @@ -413,6 +400,13 @@ public class Async return; } + // 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 + String encodingHeader = connection.getHeaderField("Content-Encoding"); + if (encodingHeader != null && encodingHeader.toLowerCase().contains("gzip")) { + inStream = new GZIPInputStream(inStream); + } + openedStreams.push(inStream); BufferedInputStream buffer = new BufferedInputStream(inStream, 4096);