Fix OkHttp Interceptor closing response on IOException. (#313)
Summary: The returned response body should not be closed, so that users can read the response body. Before, when [this call](db833d36e9/android/src/main/java/com/facebook/flipper/plugins/network/FlipperOkhttpInterceptor.java (L94)) threw an exception, the body would be closed, and then [this](db833d36e9/android/src/main/java/com/facebook/flipper/plugins/network/FlipperOkhttpInterceptor.java (L54)) would return a closed response. Pull Request resolved: https://github.com/facebook/flipper/pull/313 Reviewed By: jknoxville Differential Revision: D12881111 Pulled By: passy fbshipit-source-id: e76a732af6bdb527be6b90bbb02b3a5f45a1ec10
This commit is contained in:
committed by
Facebook Github Bot
parent
1dd5a72ab6
commit
c6aed22a25
@@ -7,7 +7,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.facebook.flipper.plugins.network;
|
package com.facebook.flipper.plugins.network;
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
import com.facebook.flipper.plugins.network.NetworkReporter.RequestInfo;
|
import com.facebook.flipper.plugins.network.NetworkReporter.RequestInfo;
|
||||||
import com.facebook.flipper.plugins.network.NetworkReporter.ResponseInfo;
|
import com.facebook.flipper.plugins.network.NetworkReporter.ResponseInfo;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -22,6 +21,7 @@ import okhttp3.Request;
|
|||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
import okhttp3.ResponseBody;
|
||||||
import okio.Buffer;
|
import okio.Buffer;
|
||||||
|
import okio.BufferedSource;
|
||||||
|
|
||||||
public class FlipperOkhttpInterceptor implements Interceptor {
|
public class FlipperOkhttpInterceptor implements Interceptor {
|
||||||
|
|
||||||
@@ -44,30 +44,16 @@ public class FlipperOkhttpInterceptor implements Interceptor {
|
|||||||
ResponseBody body = response.body();
|
ResponseBody body = response.body();
|
||||||
ResponseInfo responseInfo = convertResponse(response, body, identifier);
|
ResponseInfo responseInfo = convertResponse(response, body, identifier);
|
||||||
plugin.reportResponse(responseInfo);
|
plugin.reportResponse(responseInfo);
|
||||||
// Creating new response as can't used response.body() more than once
|
|
||||||
if (responseInfo.body != null) {
|
|
||||||
return response
|
|
||||||
.newBuilder()
|
|
||||||
.body(ResponseBody.create(body.contentType(), responseInfo.body))
|
|
||||||
.build();
|
|
||||||
} else {
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static byte[] bodyToByteArray(final Request request) {
|
private static byte[] bodyToByteArray(final Request request) throws IOException {
|
||||||
|
|
||||||
try {
|
|
||||||
final Request copy = request.newBuilder().build();
|
|
||||||
final Buffer buffer = new Buffer();
|
final Buffer buffer = new Buffer();
|
||||||
copy.body().writeTo(buffer);
|
request.body().writeTo(buffer);
|
||||||
return buffer.readByteArray();
|
return buffer.readByteArray();
|
||||||
} catch (final IOException e) {
|
|
||||||
return e.getMessage().getBytes();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private RequestInfo convertRequest(Request request, String identifier) {
|
private RequestInfo convertRequest(Request request, String identifier) throws IOException {
|
||||||
List<NetworkReporter.Header> headers = convertHeader(request.headers());
|
List<NetworkReporter.Header> headers = convertHeader(request.headers());
|
||||||
RequestInfo info = new RequestInfo();
|
RequestInfo info = new RequestInfo();
|
||||||
info.requestId = identifier;
|
info.requestId = identifier;
|
||||||
@@ -82,19 +68,18 @@ public class FlipperOkhttpInterceptor implements Interceptor {
|
|||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResponseInfo convertResponse(Response response, ResponseBody body, String identifier) {
|
private ResponseInfo convertResponse(Response response, ResponseBody body, String identifier)
|
||||||
|
throws IOException {
|
||||||
List<NetworkReporter.Header> headers = convertHeader(response.headers());
|
List<NetworkReporter.Header> headers = convertHeader(response.headers());
|
||||||
ResponseInfo info = new ResponseInfo();
|
ResponseInfo info = new ResponseInfo();
|
||||||
info.requestId = identifier;
|
info.requestId = identifier;
|
||||||
info.timeStamp = response.receivedResponseAtMillis();
|
info.timeStamp = response.receivedResponseAtMillis();
|
||||||
info.statusCode = response.code();
|
info.statusCode = response.code();
|
||||||
info.headers = headers;
|
info.headers = headers;
|
||||||
try {
|
BufferedSource source = body.source();
|
||||||
info.body = body.bytes();
|
source.request(Long.MAX_VALUE);
|
||||||
} catch (IOException e) {
|
Buffer buffer = source.buffer().clone();
|
||||||
Log.e("Flipper", e.toString());
|
info.body = buffer.readByteArray();
|
||||||
}
|
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user