Skip to content

Commit

Permalink
safely truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
jgilfelt committed Feb 22, 2017
1 parent 2977e36 commit 75da679
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.util.Log;

import com.readystatesoftware.chuck.internal.data.ChuckContentProvider;
import com.readystatesoftware.chuck.internal.data.HttpTransaction;
Expand Down Expand Up @@ -69,6 +70,7 @@ public enum Period {
FOREVER
}

private static final String LOG_TAG = "ChuckInterceptor";
private static final Period DEFAULT_RETENTION = Period.ONE_WEEK;
private static final Charset UTF8 = Charset.forName("UTF-8");

Expand Down Expand Up @@ -145,11 +147,10 @@ public ChuckInterceptor retainDataFor(Period period) {
}
}

transaction.setRequestBodyIsPlainText(!bodyEncoded(request.headers()));
transaction.setRequestBodyIsPlainText(!bodyHasUnsupportedEncoding(request.headers()));
if (hasRequestBody && transaction.requestBodyIsPlainText()) {
BufferedSource source = getNativeSource(new Buffer(), bodyGzipped(request.headers()));
Buffer buffer = source.buffer();
//Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
Charset charset = UTF8;
MediaType contentType = requestBody.contentType();
Expand Down Expand Up @@ -191,10 +192,9 @@ public ChuckInterceptor retainDataFor(Period period) {
}
transaction.setResponseHeaders(response.headers());

transaction.setResponseBodyIsPlainText(!bodyEncoded(response.headers()));
transaction.setResponseBodyIsPlainText(!bodyHasUnsupportedEncoding(response.headers()));
if (HttpHeaders.hasBody(response) && transaction.responseBodyIsPlainText()) {
BufferedSource source = getNativeSource(response, bodyGzipped(response.headers()));
//BufferedSource source = responseBody.source();
BufferedSource source = getNativeSource(response);
source.request(Long.MAX_VALUE);
Buffer buffer = source.buffer();
Charset charset = UTF8;
Expand Down Expand Up @@ -264,7 +264,7 @@ private boolean isPlaintext(Buffer buffer) {
}
}

private boolean bodyEncoded(Headers headers) {
private boolean bodyHasUnsupportedEncoding(Headers headers) {
String contentEncoding = headers.get("Content-Encoding");
return contentEncoding != null &&
!contentEncoding.equalsIgnoreCase("identity") &&
Expand Down Expand Up @@ -300,11 +300,15 @@ private BufferedSource getNativeSource(BufferedSource input, boolean isGzipped)
}
}

private BufferedSource getNativeSource(Response response, boolean isGzipped) throws IOException {
if (isGzipped) {
return getNativeSource(response.peekBody(maxContentLength).source(), true);
} else {
return response.body().source();
private BufferedSource getNativeSource(Response response) throws IOException {
if (bodyGzipped(response.headers())) {
BufferedSource source = response.peekBody(maxContentLength).source();
if (source.buffer().size() < maxContentLength) {
return getNativeSource(source, true);
} else {
Log.w(LOG_TAG, "gzip encoded response was too long");
}
}
return response.body().source();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onClick(View view) {
private OkHttpClient getClient(Context context) {
return new OkHttpClient.Builder()
// Add a ChuckInterceptor instance to your OkHttp client
.addNetworkInterceptor(new ChuckInterceptor(context))
.addInterceptor(new ChuckInterceptor(context))
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
}
Expand Down

0 comments on commit 75da679

Please sign in to comment.