From 5f391350447b234185abfbfeef60208c96734692 Mon Sep 17 00:00:00 2001 From: egsavage Date: Mon, 14 Oct 2019 19:34:58 -0400 Subject: [PATCH 1/6] Issue #842 - HttpResponse GZip content encoding equality change --- .../java/com/google/api/client/http/HttpResponse.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java index 90c3812f0..d429ea183 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java +++ b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java @@ -80,6 +80,12 @@ public final class HttpResponse { /** Whether {@link #getContent()} should return raw input stream. */ private final boolean returnRawInputStream; + /** Content encoding for GZip */ + private static final String CONTENT_ENCODING_GZIP = "gzip"; + + /** Content encoding for GZip HTTP 1.1 */ + private static final String CONTENT_ENCODING_XGZIP = "x-gzip"; + /** * Determines the limit to the content size that will be logged during {@link #getContent()}. * @@ -330,7 +336,8 @@ public InputStream getContent() throws IOException { String contentEncoding = this.contentEncoding; if (!returnRawInputStream && contentEncoding != null - && contentEncoding.contains("gzip")) { + && (CONTENT_ENCODING_GZIP.equalsIgnoreCase(contentEncoding.trim()) + || CONTENT_ENCODING_XGZIP.equalsIgnoreCase(contentEncoding.trim()))) { lowLevelResponseContent = new GZIPInputStream(lowLevelResponseContent); } // logging (wrap content with LoggingInputStream) From af5b46907c0234fb75cf815fdcf6202e8c31b97e Mon Sep 17 00:00:00 2001 From: egsavage Date: Sat, 19 Oct 2019 17:46:47 -0400 Subject: [PATCH 2/6] Issue #842 - Added unit test, PR updates --- .../google/api/client/http/HttpResponse.java | 11 ++++---- .../api/client/http/HttpResponseTest.java | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java index 3348dc158..e9606b23c 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java +++ b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java @@ -83,7 +83,7 @@ public final class HttpResponse { /** Content encoding for GZip */ private static final String CONTENT_ENCODING_GZIP = "gzip"; - /** Content encoding for GZip HTTP 1.1 */ + /** Content encoding for GZip (legacy) */ private static final String CONTENT_ENCODING_XGZIP = "x-gzip"; /** @@ -333,13 +333,12 @@ public InputStream getContent() throws IOException { boolean contentProcessed = false; try { // gzip encoding (wrap content with GZipInputStream) - String contentEncoding = this.contentEncoding; if (!returnRawInputStream - && contentEncoding != null - && (CONTENT_ENCODING_GZIP.equalsIgnoreCase(contentEncoding.trim()) - || CONTENT_ENCODING_XGZIP.equalsIgnoreCase(contentEncoding.trim()))) { + && this.contentEncoding != null + && (CONTENT_ENCODING_GZIP.equalsIgnoreCase(this.contentEncoding.trim()) + || CONTENT_ENCODING_XGZIP.equalsIgnoreCase(this.contentEncoding.trim()))) { lowLevelResponseContent = - new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent)); + new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent)); } // logging (wrap content with LoggingInputStream) Logger logger = HttpTransport.LOGGER; diff --git a/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java b/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java index a611d774a..9a285cf5f 100644 --- a/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java +++ b/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java @@ -495,4 +495,30 @@ public LowLevelHttpResponse execute() throws IOException { assertEquals("abcd", response.parseAsString()); assertTrue(output.isClosed()); } + + public void testGetContent_otherEncodingWithgzipInItsName_GzipIsNotUsed() throws IOException { + final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse(); + mockResponse.setContent("abcd"); + mockResponse.setContentEncoding("otherEncodingWithgzipInItsName"); + mockResponse.setContentType("text/plain"); + + HttpTransport transport = + new MockHttpTransport() { + @Override + public LowLevelHttpRequest buildRequest(String method, final String url) + throws IOException { + return new MockLowLevelHttpRequest() { + @Override + public LowLevelHttpResponse execute() throws IOException { + return mockResponse; + } + }; + } + }; + HttpRequest request = + transport.createRequestFactory().buildHeadRequest(HttpTesting.SIMPLE_GENERIC_URL); + // If gzip was used on this response, an exception would be thrown + HttpResponse response = request.execute(); + assertEquals("abcd", response.parseAsString()); + } } From 2cc4fe7211b2c43849a5e6e964c389ccff65d9c6 Mon Sep 17 00:00:00 2001 From: egsavage Date: Sun, 20 Oct 2019 17:21:34 -0400 Subject: [PATCH 3/6] Issue #842 - Adjusted spacing, dropped ignoreCase on comparison --- .../google/api/client/http/HttpResponse.java | 8 +++---- .../api/client/http/HttpResponseTest.java | 21 +++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java index e9606b23c..f68e20e7d 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java +++ b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java @@ -334,11 +334,11 @@ public InputStream getContent() throws IOException { try { // gzip encoding (wrap content with GZipInputStream) if (!returnRawInputStream - && this.contentEncoding != null - && (CONTENT_ENCODING_GZIP.equalsIgnoreCase(this.contentEncoding.trim()) - || CONTENT_ENCODING_XGZIP.equalsIgnoreCase(this.contentEncoding.trim()))) { + && this.contentEncoding != null + && (CONTENT_ENCODING_GZIP.equals(this.contentEncoding.trim()) + || CONTENT_ENCODING_XGZIP.equals(this.contentEncoding.trim()))) { lowLevelResponseContent = - new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent)); + new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent)); } // logging (wrap content with LoggingInputStream) Logger logger = HttpTransport.LOGGER; diff --git a/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java b/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java index 9a285cf5f..d6b6b900c 100644 --- a/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java +++ b/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java @@ -503,20 +503,19 @@ public void testGetContent_otherEncodingWithgzipInItsName_GzipIsNotUsed() throws mockResponse.setContentType("text/plain"); HttpTransport transport = - new MockHttpTransport() { + new MockHttpTransport() { + @Override + public LowLevelHttpRequest buildRequest(String method, final String url) + throws IOException { + return new MockLowLevelHttpRequest() { @Override - public LowLevelHttpRequest buildRequest(String method, final String url) - throws IOException { - return new MockLowLevelHttpRequest() { - @Override - public LowLevelHttpResponse execute() throws IOException { - return mockResponse; - } - }; + public LowLevelHttpResponse execute() throws IOException { + return mockResponse; } }; - HttpRequest request = - transport.createRequestFactory().buildHeadRequest(HttpTesting.SIMPLE_GENERIC_URL); + } + }; + HttpRequest request = transport.createRequestFactory().buildHeadRequest(HttpTesting.SIMPLE_GENERIC_URL); // If gzip was used on this response, an exception would be thrown HttpResponse response = request.execute(); assertEquals("abcd", response.parseAsString()); From beeac46097791cbbf654dc9b0c89c74368809919 Mon Sep 17 00:00:00 2001 From: egsavage Date: Sun, 27 Oct 2019 11:32:19 -0400 Subject: [PATCH 4/6] Issue #842 - Enforce locale on encoding comparison, add unit tests --- .../google/api/client/http/HttpResponse.java | 13 +++++++------ .../api/client/http/HttpResponseTest.java | 18 +++++++++++++++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java index f68e20e7d..af50776ca 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java +++ b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java @@ -26,6 +26,7 @@ import java.io.OutputStream; import java.lang.reflect.Type; import java.nio.charset.Charset; +import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import java.util.zip.GZIPInputStream; @@ -333,12 +334,12 @@ public InputStream getContent() throws IOException { boolean contentProcessed = false; try { // gzip encoding (wrap content with GZipInputStream) - if (!returnRawInputStream - && this.contentEncoding != null - && (CONTENT_ENCODING_GZIP.equals(this.contentEncoding.trim()) - || CONTENT_ENCODING_XGZIP.equals(this.contentEncoding.trim()))) { - lowLevelResponseContent = - new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent)); + if (!returnRawInputStream && this.contentEncoding != null) { + final String oontentEncoding = this.contentEncoding.trim().toLowerCase(Locale.ENGLISH); + if (CONTENT_ENCODING_GZIP.equals(oontentEncoding) || CONTENT_ENCODING_XGZIP.equals(oontentEncoding)) { + lowLevelResponseContent = + new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent)); + } } // logging (wrap content with LoggingInputStream) Logger logger = HttpTransport.LOGGER; diff --git a/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java b/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java index d6b6b900c..e6fc94a6f 100644 --- a/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java +++ b/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java @@ -29,6 +29,7 @@ import java.nio.charset.StandardCharsets; import java.text.NumberFormat; import java.util.Arrays; +import java.util.Locale; import java.util.logging.Level; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; @@ -461,6 +462,21 @@ public LowLevelHttpResponse execute() throws IOException { } public void testGetContent_gzipEncoding_finishReading() throws IOException { + do_testGetContent_gzipEncoding_finishReading("gzip"); + } + + public void testGetContent_gzipEncoding_finishReadingWithUppercaseContentEncoding() throws IOException { + do_testGetContent_gzipEncoding_finishReading("GZIP"); + } + + public void testGetContent_gzipEncoding_finishReadingWithDifferentDefaultLocaleAndUppercaseContentEncoding() throws IOException { + Locale originalDefaultLocale = Locale.getDefault(); + Locale.setDefault(Locale.JAPAN); + do_testGetContent_gzipEncoding_finishReading("GZIP"); + Locale.setDefault(originalDefaultLocale); + } + + private void do_testGetContent_gzipEncoding_finishReading(String contentEncoding) throws IOException { byte[] dataToCompress = "abcd".getBytes(StandardCharsets.UTF_8); byte[] mockBytes; try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream(dataToCompress.length)) { @@ -471,7 +487,7 @@ public void testGetContent_gzipEncoding_finishReading() throws IOException { } final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse(); mockResponse.setContent(mockBytes); - mockResponse.setContentEncoding("gzip"); + mockResponse.setContentEncoding(contentEncoding); mockResponse.setContentType("text/plain"); HttpTransport transport = From abda56ba6465981f16f5e16b55dccd26d265fa6f Mon Sep 17 00:00:00 2001 From: egsavage Date: Sun, 27 Oct 2019 13:42:37 -0400 Subject: [PATCH 5/6] Issue #842 - Removed final, fixed spelling --- .../main/java/com/google/api/client/http/HttpResponse.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java index af50776ca..5273300f6 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java +++ b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java @@ -335,8 +335,8 @@ public InputStream getContent() throws IOException { try { // gzip encoding (wrap content with GZipInputStream) if (!returnRawInputStream && this.contentEncoding != null) { - final String oontentEncoding = this.contentEncoding.trim().toLowerCase(Locale.ENGLISH); - if (CONTENT_ENCODING_GZIP.equals(oontentEncoding) || CONTENT_ENCODING_XGZIP.equals(oontentEncoding)) { + String oontentencoding = this.contentEncoding.trim().toLowerCase(Locale.ENGLISH); + if (CONTENT_ENCODING_GZIP.equals(oontentencoding) || CONTENT_ENCODING_XGZIP.equals(oontentencoding)) { lowLevelResponseContent = new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent)); } From 80aa6039ac6f5c093967d8c65fb497acf73dd865 Mon Sep 17 00:00:00 2001 From: egsavage Date: Mon, 28 Oct 2019 20:58:24 -0400 Subject: [PATCH 6/6] Issue #842 - test updates --- .../api/client/http/HttpResponseTest.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java b/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java index e6fc94a6f..a3efdb305 100644 --- a/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java +++ b/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java @@ -471,16 +471,21 @@ public void testGetContent_gzipEncoding_finishReadingWithUppercaseContentEncodin public void testGetContent_gzipEncoding_finishReadingWithDifferentDefaultLocaleAndUppercaseContentEncoding() throws IOException { Locale originalDefaultLocale = Locale.getDefault(); - Locale.setDefault(Locale.JAPAN); - do_testGetContent_gzipEncoding_finishReading("GZIP"); - Locale.setDefault(originalDefaultLocale); + try { + Locale.setDefault(Locale.forLanguageTag("tr-TR")); + do_testGetContent_gzipEncoding_finishReading("GZIP"); + } finally { + Locale.setDefault(originalDefaultLocale); + } } private void do_testGetContent_gzipEncoding_finishReading(String contentEncoding) throws IOException { byte[] dataToCompress = "abcd".getBytes(StandardCharsets.UTF_8); byte[] mockBytes; - try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream(dataToCompress.length)) { - GZIPOutputStream zipStream = new GZIPOutputStream((byteStream)); + try ( + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(dataToCompress.length); + GZIPOutputStream zipStream = new GZIPOutputStream((byteStream)) + ) { zipStream.write(dataToCompress); zipStream.close(); mockBytes = byteStream.toByteArray(); @@ -506,10 +511,11 @@ public LowLevelHttpResponse execute() throws IOException { HttpRequest request = transport.createRequestFactory().buildHeadRequest(HttpTesting.SIMPLE_GENERIC_URL); HttpResponse response = request.execute(); - TestableByteArrayInputStream output = (TestableByteArrayInputStream) mockResponse.getContent(); - assertFalse(output.isClosed()); - assertEquals("abcd", response.parseAsString()); - assertTrue(output.isClosed()); + try (TestableByteArrayInputStream output = (TestableByteArrayInputStream) mockResponse.getContent()) { + assertFalse(output.isClosed()); + assertEquals("abcd", response.parseAsString()); + assertTrue(output.isClosed()); + } } public void testGetContent_otherEncodingWithgzipInItsName_GzipIsNotUsed() throws IOException {