Skip to content

Commit

Permalink
Fix Stream Closed IOException
Browse files Browse the repository at this point in the history
Summary:
Original Issue: #993

The exception occurs when OkHttp and we try to read request body twice, while it can be only read once.

Hence, we will read request body after it is processed by OkHttp.

Tradeoff for this is requests on Flipper will not appear immediately after fired, but they will appear together with their responses.

There are ways we can get rid of the tradeoff. For example, as demonstrated in D21167308, OkHttp ^3.14.0 contains method `isOneShot`, which can be used to check if we can read request body more than once. Another example is to change server side to accept nullable variable so that we can send request body and others separately.

Reviewed By: passy

Differential Revision: D21175341

fbshipit-source-id: 053789a2c2f28cd8149ea1bb36fd0cfe1c668df7
  • Loading branch information
cekkaewnumchai authored and facebook-github-bot committed Apr 22, 2020
1 parent 1a692cc commit f3edfb3
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,23 @@ public FlipperOkhttpInterceptor(
public Response intercept(Interceptor.Chain chain) throws IOException {
final Request request = chain.request();
final String identifier = UUID.randomUUID().toString();
mPlugin.reportRequest(convertRequest(request, identifier));
final RequestInfo requestInfo = convertRequestWithoutBody(request, identifier);

// Check if there is a mock response
final Response mockResponse = mIsMockResponseSupported ? getMockResponse(request) : null;
final Response response = mockResponse != null ? mockResponse : chain.proceed(request);
final ResponseBody body = response.body();
final ResponseInfo responseInfo = convertResponse(response, body, identifier);
responseInfo.isMock = mockResponse != null;
// Add request body
try {
if (request.body() != null) {
requestInfo.body = bodyToByteArray(request, mMaxBodyBytes);
}
} catch (IOException e) {
// We can safely ignore this as some requests don't allow their body to be read more than once
}
mPlugin.reportRequest(requestInfo);
mPlugin.reportResponse(responseInfo);
return response;
}
Expand All @@ -108,17 +117,15 @@ private static byte[] bodyToByteArray(final Request request, final long maxBodyB
return buffer.readByteArray(Math.min(buffer.size(), maxBodyBytes));
}

private RequestInfo convertRequest(Request request, String identifier) throws IOException {
private RequestInfo convertRequestWithoutBody(Request request, String identifier)
throws IOException {
final List<NetworkReporter.Header> headers = convertHeader(request.headers());
final RequestInfo info = new RequestInfo();
info.requestId = identifier;
info.timeStamp = System.currentTimeMillis();
info.headers = headers;
info.method = request.method();
info.uri = request.url().toString();
if (request.body() != null) {
info.body = bodyToByteArray(request, mMaxBodyBytes);
}

return info;
}
Expand Down

0 comments on commit f3edfb3

Please sign in to comment.