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

OkHTTP does not unzip Gzipped content when using Authenticator #1927

Closed
tyvsmith opened this issue Oct 15, 2015 · 9 comments
Closed

OkHTTP does not unzip Gzipped content when using Authenticator #1927

tyvsmith opened this issue Oct 15, 2015 · 9 comments
Labels
bug Bug in existing code
Milestone

Comments

@tyvsmith
Copy link

Using OkHttp 2.5

I'm using an Authenticator to catch 401's and refresh my OAuth token. Upon retrying the requests, GSON fails at parsing a compressed String. Since OkHTTP silently adds the "Accept-encoding: gzip" header after the first try, upon retrying with the Authenticator it sees the existing header for "Accept-encoding" and does not do the unzipping.

10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: <--- HTTP 200 (775ms)
10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: Content-Encoding: gzip
10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: Content-Type: application/json; charset=utf-8
10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: Date: Thu, 15 Oct 2015 20:03:42 GMT
10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: Server: nginx
10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: Status: 200 OK
10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: X-Runtime: 0.079625
10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: X-UA-Compatible: IE=Edge,chrome=1
10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: Content-Length: 146
10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: Connection: keep-alive
10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: OkHttp-Selected-Protocol: http/1.1
10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: OkHttp-Sent-Millis: 1444939422732
10-15 16:03:42.900 18864-18889/com.example.dev D/Retrofit: OkHttp-Received-Millis: 1444939422900
10-15 16:03:42.901 18864-18889/com.example.dev D/Retrofit: ����������������]�A
10-15 16:03:42.901 18864-18889/com.example.dev D/Retrofit: � �D��׶�F+z��D�����5�"x�j7-��̛Y� �S�j�n�19JF)��L ��
10-15 16:03:42.901 18864-18889/com.example.dev D/Retrofit: ���c1m|���'&�]'�N�d�����e��?�J�����p� ��r��?4�w `����������_�7�3�'�������
10-15 16:03:42.901 18864-18889/com.example.dev D/Retrofit: <--- END HTTP (146-byte body)
    private Request refreshAndSign(Response response, AccessToken token) {
        AccessToken freshToken = service.refreshAccessToken(token.refreshToken);
        accessTokenState.setAccessToken(freshToken);

        Request.Builder builder = response.networkResponse().request().newBuilder();
        setAuthHeader(builder, freshToken);
        return builder.build();
    }

This is worked around by explicitly removing the header when resigning.

builder.removeHeader("Accept-Encoding");
@swankjesse
Copy link
Collaborator

Eep. I thought we'd fixed all of these.

@swankjesse swankjesse added the bug Bug in existing code label Oct 15, 2015
@JakeWharton
Copy link
Collaborator

Did you try OkHttp 2.5? OkHttp 2.0 is really, really old.

On Thu, Oct 15, 2015 at 5:54 PM Jesse Wilson notifications@github.com
wrote:

Eep. I thought we'd fixed all of these.


Reply to this email directly or view it on GitHub
#1927 (comment).

@tyvsmith
Copy link
Author

@JakeWharton I edited it to 2.5 after the first post.

@swankjesse swankjesse added this to the 2.6 milestone Oct 31, 2015
@alexeimoisseev
Copy link

It seems that response body isn't ungzipped automatically every time you specify "Accept-Encoding: gzip" header manually in a request.

@JakeWharton
Copy link
Collaborator

By adding that header you are informing OkHttp that you can accept
Gzip-encoded bodies at the application layer.

On Wed, Nov 18, 2015 at 2:21 PM Alexei Moisseev notifications@github.com
wrote:

It seems that response body isn't ungzipped automatically every time you
specify "Accept-Encoding: gzip" header manually in a request.


Reply to this email directly or view it on GitHub
#1927 (comment).

alexeimoisseev added a commit to alexeimoisseev/okhttp that referenced this issue Nov 18, 2015
@alexeimoisseev
Copy link

@tyvsmith By the way, is it really necessary for you to use raw headers (networkResponse)?
Seems like not.
It will work properly if you set auth header this way:

Request.Builder builder = response.request().newBuilder();
setAuthHeader(builder, freshToken);

@tyvsmith
Copy link
Author

@alexeimoisseev At first glance that seems accurate, however, it appears like already existing headers only exist on the networkResponse.request(). Using the builder you recommended would lose other headers set.

@swankjesse swankjesse modified the milestones: 2.7, 2.6 Nov 22, 2015
@swankjesse
Copy link
Collaborator

@tyvsmith I think the right fix is to avoid using networkResponse() when you're rebuilding the request.

- Request.Builder builder = response.networkResponse().request().newBuilder();
+ Request.Builder builder = response.request().newBuilder();

@tyvsmith
Copy link
Author

@swankjesse unfortunately, by using response.request(), all headers are stripped, including ones I injected with interceptors. Is this the desired behavior or is there a recommended alternative approach?

evaluate_expression_and_genymotion_-google_nexus_5-5_1_0-api_22-1080x1920__1080x1920__480dpi_-_192_168_58_101

evaluate_expression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug in existing code
Projects
None yet
Development

No branches or pull requests

4 participants