Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode gzip body if not handled by okhttp #21187

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.ByteString;
import okio.GzipSource;
import okio.Okio;

/**
* Implements the XMLHttpRequest JavaScript interface.
Expand Down Expand Up @@ -454,8 +456,26 @@ public void onResponse(Call call, Response response) throws IOException {
translateHeaders(response.headers()),
response.request().url().toString());

ResponseBody responseBody = response.body();
try {
// OkHttp implements something called transparent gzip, which mean that it will
// automatically add the Accept-Encoding gzip header and handle decoding internally.
// The issue is that it won't handle decoding if the user provides a Accept-Encoding
// header. This is also undesirable considering that iOS does handle the decoding even
// when the header is provided. To make sure this works in all cases, handle gzip body
// here also. This works fine since OKHttp will remove the Content-Encoding header if
// it used transparent gzip.
// See https://github.com/square/okhttp/blob/5b37cda9e00626f43acf354df145fd452c3031f1/okhttp/src/main/java/okhttp3/internal/http/BridgeInterceptor.java#L76-L111
ResponseBody responseBody = response.body();
if ("gzip".equalsIgnoreCase(response.header("Content-Encoding")) &&
responseBody != null) {
GzipSource gzipSource = new GzipSource(responseBody.source());
String contentType = response.header("Content-Type");
responseBody = ResponseBody.create(
contentType != null ? MediaType.parse(contentType) : null,
-1L,
Okio.buffer(gzipSource));
}

// Check if a handler is registered
for (ResponseHandler handler : mResponseHandlers) {
if (handler.supports(responseType)) {
Expand Down