diff --git a/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/FlipperOkhttpInterceptor.java b/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/FlipperOkhttpInterceptor.java index c1d74c004..935f5512f 100644 --- a/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/FlipperOkhttpInterceptor.java +++ b/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/FlipperOkhttpInterceptor.java @@ -25,6 +25,11 @@ import okio.BufferedSource; public class FlipperOkhttpInterceptor implements Interceptor { + // By default, limit body size (request or response) reporting to 100KB to avoid OOM + private static final long DEFAULT_MAX_BODY_BYTES = 100 * 1024; + + private long maxBodyBytes = DEFAULT_MAX_BODY_BYTES; + public @Nullable NetworkFlipperPlugin plugin; public FlipperOkhttpInterceptor() { @@ -35,6 +40,12 @@ public class FlipperOkhttpInterceptor implements Interceptor { this.plugin = plugin; } + /** If you want to change the number of bytes displayed for the body, use this constructor */ + public FlipperOkhttpInterceptor(NetworkFlipperPlugin plugin, long maxBodyBytes) { + this.plugin = plugin; + this.maxBodyBytes = maxBodyBytes; + } + @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request(); @@ -47,10 +58,11 @@ public class FlipperOkhttpInterceptor implements Interceptor { return response; } - private static byte[] bodyToByteArray(final Request request) throws IOException { + private static byte[] bodyToByteArray(final Request request, final long maxBodyBytes) + throws IOException { final Buffer buffer = new Buffer(); request.body().writeTo(buffer); - return buffer.readByteArray(); + return buffer.readByteArray(Math.min(buffer.size(), maxBodyBytes)); } private RequestInfo convertRequest(Request request, String identifier) throws IOException { @@ -62,7 +74,7 @@ public class FlipperOkhttpInterceptor implements Interceptor { info.method = request.method(); info.uri = request.url().toString(); if (request.body() != null) { - info.body = bodyToByteArray(request); + info.body = bodyToByteArray(request, maxBodyBytes); } return info; @@ -77,7 +89,7 @@ public class FlipperOkhttpInterceptor implements Interceptor { info.statusCode = response.code(); info.headers = headers; BufferedSource source = body.source(); - source.request(Long.MAX_VALUE); + source.request(maxBodyBytes); Buffer buffer = source.buffer().clone(); info.body = buffer.readByteArray(); return info;