Skip to content

Commit 9c73e1d

Browse files
egsavagechingor13
authored andcommitted
fix: HttpResponse GZip content encoding equality change (#843)
* Issue #842 - HttpResponse GZip content encoding equality change * Issue #842 - Added unit test, PR updates * Issue #842 - Adjusted spacing, dropped ignoreCase on comparison * Issue #842 - Enforce locale on encoding comparison, add unit tests * Issue #842 - Removed final, fixed spelling * Issue #842 - test updates
1 parent cc2ea16 commit 9c73e1d

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.OutputStream;
2727
import java.lang.reflect.Type;
2828
import java.nio.charset.Charset;
29+
import java.util.Locale;
2930
import java.util.logging.Level;
3031
import java.util.logging.Logger;
3132
import java.util.zip.GZIPInputStream;
@@ -80,6 +81,12 @@ public final class HttpResponse {
8081
/** Whether {@link #getContent()} should return raw input stream. */
8182
private final boolean returnRawInputStream;
8283

84+
/** Content encoding for GZip */
85+
private static final String CONTENT_ENCODING_GZIP = "gzip";
86+
87+
/** Content encoding for GZip (legacy) */
88+
private static final String CONTENT_ENCODING_XGZIP = "x-gzip";
89+
8390
/**
8491
* Determines the limit to the content size that will be logged during {@link #getContent()}.
8592
*
@@ -327,12 +334,12 @@ public InputStream getContent() throws IOException {
327334
boolean contentProcessed = false;
328335
try {
329336
// gzip encoding (wrap content with GZipInputStream)
330-
String contentEncoding = this.contentEncoding;
331-
if (!returnRawInputStream
332-
&& contentEncoding != null
333-
&& contentEncoding.contains("gzip")) {
334-
lowLevelResponseContent =
335-
new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent));
337+
if (!returnRawInputStream && this.contentEncoding != null) {
338+
String oontentencoding = this.contentEncoding.trim().toLowerCase(Locale.ENGLISH);
339+
if (CONTENT_ENCODING_GZIP.equals(oontentencoding) || CONTENT_ENCODING_XGZIP.equals(oontentencoding)) {
340+
lowLevelResponseContent =
341+
new ConsumingInputStream(new GZIPInputStream(lowLevelResponseContent));
342+
}
336343
}
337344
// logging (wrap content with LoggingInputStream)
338345
Logger logger = HttpTransport.LOGGER;

google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.nio.charset.StandardCharsets;
3030
import java.text.NumberFormat;
3131
import java.util.Arrays;
32+
import java.util.Locale;
3233
import java.util.logging.Level;
3334
import java.util.zip.GZIPInputStream;
3435
import java.util.zip.GZIPOutputStream;
@@ -461,17 +462,37 @@ public LowLevelHttpResponse execute() throws IOException {
461462
}
462463

463464
public void testGetContent_gzipEncoding_finishReading() throws IOException {
465+
do_testGetContent_gzipEncoding_finishReading("gzip");
466+
}
467+
468+
public void testGetContent_gzipEncoding_finishReadingWithUppercaseContentEncoding() throws IOException {
469+
do_testGetContent_gzipEncoding_finishReading("GZIP");
470+
}
471+
472+
public void testGetContent_gzipEncoding_finishReadingWithDifferentDefaultLocaleAndUppercaseContentEncoding() throws IOException {
473+
Locale originalDefaultLocale = Locale.getDefault();
474+
try {
475+
Locale.setDefault(Locale.forLanguageTag("tr-TR"));
476+
do_testGetContent_gzipEncoding_finishReading("GZIP");
477+
} finally {
478+
Locale.setDefault(originalDefaultLocale);
479+
}
480+
}
481+
482+
private void do_testGetContent_gzipEncoding_finishReading(String contentEncoding) throws IOException {
464483
byte[] dataToCompress = "abcd".getBytes(StandardCharsets.UTF_8);
465484
byte[] mockBytes;
466-
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream(dataToCompress.length)) {
467-
GZIPOutputStream zipStream = new GZIPOutputStream((byteStream));
485+
try (
486+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(dataToCompress.length);
487+
GZIPOutputStream zipStream = new GZIPOutputStream((byteStream))
488+
) {
468489
zipStream.write(dataToCompress);
469490
zipStream.close();
470491
mockBytes = byteStream.toByteArray();
471492
}
472493
final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse();
473494
mockResponse.setContent(mockBytes);
474-
mockResponse.setContentEncoding("gzip");
495+
mockResponse.setContentEncoding(contentEncoding);
475496
mockResponse.setContentType("text/plain");
476497

477498
HttpTransport transport =
@@ -490,9 +511,35 @@ public LowLevelHttpResponse execute() throws IOException {
490511
HttpRequest request =
491512
transport.createRequestFactory().buildHeadRequest(HttpTesting.SIMPLE_GENERIC_URL);
492513
HttpResponse response = request.execute();
493-
TestableByteArrayInputStream output = (TestableByteArrayInputStream) mockResponse.getContent();
494-
assertFalse(output.isClosed());
514+
try (TestableByteArrayInputStream output = (TestableByteArrayInputStream) mockResponse.getContent()) {
515+
assertFalse(output.isClosed());
516+
assertEquals("abcd", response.parseAsString());
517+
assertTrue(output.isClosed());
518+
}
519+
}
520+
521+
public void testGetContent_otherEncodingWithgzipInItsName_GzipIsNotUsed() throws IOException {
522+
final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse();
523+
mockResponse.setContent("abcd");
524+
mockResponse.setContentEncoding("otherEncodingWithgzipInItsName");
525+
mockResponse.setContentType("text/plain");
526+
527+
HttpTransport transport =
528+
new MockHttpTransport() {
529+
@Override
530+
public LowLevelHttpRequest buildRequest(String method, final String url)
531+
throws IOException {
532+
return new MockLowLevelHttpRequest() {
533+
@Override
534+
public LowLevelHttpResponse execute() throws IOException {
535+
return mockResponse;
536+
}
537+
};
538+
}
539+
};
540+
HttpRequest request = transport.createRequestFactory().buildHeadRequest(HttpTesting.SIMPLE_GENERIC_URL);
541+
// If gzip was used on this response, an exception would be thrown
542+
HttpResponse response = request.execute();
495543
assertEquals("abcd", response.parseAsString());
496-
assertTrue(output.isClosed());
497544
}
498545
}

0 commit comments

Comments
 (0)