Skip to content

Commit

Permalink
Merge branch 'eschlenz-hotfix/max_content_length'
Browse files Browse the repository at this point in the history
  • Loading branch information
jgilfelt committed Feb 14, 2017
2 parents 8ee9204 + 94f168e commit 206a533
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public ChuckInterceptor showNotification(boolean show) {
return this;
}

public ChuckInterceptor maxContentLength(long max) {
return this;
}

public ChuckInterceptor retainDataFor(Period period) {
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public enum Period {
private NotificationHelper notificationHelper;
private RetentionManager retentionManager;
private boolean showNotification;
private long maxContentLength = 250000L;

/**
* @param context The current Context.
Expand All @@ -96,6 +97,18 @@ public ChuckInterceptor showNotification(boolean show) {
return this;
}

/**
* Set the maximum length for request and response content before it is truncated.
* Warning: setting this value too high may cause unexpected results.
*
* @param max the maximum length (in bytes) for request/response content.
* @return The {@link ChuckInterceptor} instance.
*/
public ChuckInterceptor maxContentLength(long max) {
this.maxContentLength = max;
return this;
}

/**
* Set the retention period for HTTP transaction data captured by this interceptor.
* The default is one week.
Expand Down Expand Up @@ -140,7 +153,7 @@ public ChuckInterceptor retainDataFor(Period period) {
charset = contentType.charset(UTF8);
}
if (isPlaintext(buffer)) {
transaction.setRequestBody(buffer.readString(charset));
transaction.setRequestBody(readFromBuffer(buffer, charset));
} else {
transaction.setResponseBodyIsPlainText(false);
}
Expand Down Expand Up @@ -190,7 +203,7 @@ public ChuckInterceptor retainDataFor(Period period) {
}
}
if (isPlaintext(buffer)) {
transaction.setResponseBody(buffer.clone().readString(charset));
transaction.setResponseBody(readFromBuffer(buffer.clone(), charset));
} else {
transaction.setResponseBodyIsPlainText(false);
}
Expand Down Expand Up @@ -250,4 +263,19 @@ private boolean bodyEncoded(Headers headers) {
String contentEncoding = headers.get("Content-Encoding");
return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity");
}

private String readFromBuffer(Buffer buffer, Charset charset) {
long bufferSize = buffer.size();
long maxBytes = Math.min(bufferSize, maxContentLength);
String body = "";
try {
body = buffer.readString(maxBytes, charset);
} catch (EOFException e) {
body += context.getString(R.string.chuck_body_unexpected_eof);
}
if (bufferSize > maxContentLength) {
body += context.getString(R.string.chuck_body_content_truncated);
}
return body;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

import android.net.Uri;

import com.google.gson.reflect.TypeToken;
import com.readystatesoftware.chuck.internal.support.FormatUtils;
import com.readystatesoftware.chuck.internal.support.JsonConvertor;
import com.google.gson.reflect.TypeToken;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@ public static String getShareCurlCommand(HttpTransaction transaction) {
private static String v(String string) {
return (string != null) ? string : "";
}
}
}
2 changes: 2 additions & 0 deletions library/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@
<string name="chuck_share_as_curl">Share as curl command</string>
<string name="chuck_body_omitted">(encoded or binary body omitted)</string>
<string name="chuck_search">Search</string>
<string name="chuck_body_unexpected_eof">\n\n--- Unexpected end of content ---</string>
<string name="chuck_body_content_truncated">\n\n--- Content truncated ---</string>
</resources>

0 comments on commit 206a533

Please sign in to comment.