(Client) Clean up Android FlipperOkHttpInterceptor
Summary: This is the change made by Pascal at D19813495. to clean up the file Shouldn't really change how it works but makes it safer and brings it in line with our coding standards. Reviewed By: passy Differential Revision: D20474258 fbshipit-source-id: 67d77316a5b7efd7258072b4f35a2d9c50de408e
This commit is contained in:
committed by
Facebook GitHub Bot
parent
d27e45d7bb
commit
79141f5fd2
@@ -42,9 +42,9 @@ public class FlipperOkhttpInterceptor
|
|||||||
// By default, limit body size (request or response) reporting to 100KB to avoid OOM
|
// 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 static final long DEFAULT_MAX_BODY_BYTES = 100 * 1024;
|
||||||
|
|
||||||
private long maxBodyBytes = DEFAULT_MAX_BODY_BYTES;
|
private final long mMaxBodyBytes;
|
||||||
|
|
||||||
public @Nullable NetworkFlipperPlugin plugin;
|
private final NetworkFlipperPlugin mPlugin;
|
||||||
|
|
||||||
private static class PartialRequestInfo extends Pair<String, String> {
|
private static class PartialRequestInfo extends Pair<String, String> {
|
||||||
PartialRequestInfo(String url, String method) {
|
PartialRequestInfo(String url, String method) {
|
||||||
@@ -54,20 +54,15 @@ public class FlipperOkhttpInterceptor
|
|||||||
|
|
||||||
// pair of request url and method
|
// pair of request url and method
|
||||||
private Map<PartialRequestInfo, ResponseInfo> mMockResponseMap = new HashMap<>(0);
|
private Map<PartialRequestInfo, ResponseInfo> mMockResponseMap = new HashMap<>(0);
|
||||||
private boolean mIsMockResponseSupported = false;
|
private boolean mIsMockResponseSupported;
|
||||||
|
|
||||||
public FlipperOkhttpInterceptor() {
|
|
||||||
this.plugin = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FlipperOkhttpInterceptor(NetworkFlipperPlugin plugin) {
|
public FlipperOkhttpInterceptor(NetworkFlipperPlugin plugin) {
|
||||||
this.plugin = plugin;
|
this(plugin, DEFAULT_MAX_BODY_BYTES, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** If you want to change the number of bytes displayed for the body, use this constructor */
|
/** If you want to change the number of bytes displayed for the body, use this constructor */
|
||||||
public FlipperOkhttpInterceptor(NetworkFlipperPlugin plugin, long maxBodyBytes) {
|
public FlipperOkhttpInterceptor(NetworkFlipperPlugin plugin, long maxBodyBytes) {
|
||||||
this.plugin = plugin;
|
this(plugin, maxBodyBytes, false);
|
||||||
this.maxBodyBytes = maxBodyBytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,57 +70,54 @@ public class FlipperOkhttpInterceptor
|
|||||||
* short circuit: https://square.github.io/okhttp/interceptors/ *
|
* short circuit: https://square.github.io/okhttp/interceptors/ *
|
||||||
*/
|
*/
|
||||||
public FlipperOkhttpInterceptor(NetworkFlipperPlugin plugin, boolean isMockResponseSupported) {
|
public FlipperOkhttpInterceptor(NetworkFlipperPlugin plugin, boolean isMockResponseSupported) {
|
||||||
this.plugin = plugin;
|
this(plugin, DEFAULT_MAX_BODY_BYTES, isMockResponseSupported);
|
||||||
mIsMockResponseSupported = isMockResponseSupported;
|
|
||||||
if (isMockResponseSupported) {
|
|
||||||
this.plugin.setConnectionListener(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public FlipperOkhttpInterceptor(
|
public FlipperOkhttpInterceptor(
|
||||||
NetworkFlipperPlugin plugin, long maxBodyBytes, boolean isMockResponseSupported) {
|
NetworkFlipperPlugin plugin, long maxBodyBytes, boolean isMockResponseSupported) {
|
||||||
this.plugin = plugin;
|
mPlugin = plugin;
|
||||||
this.maxBodyBytes = maxBodyBytes;
|
mMaxBodyBytes = maxBodyBytes;
|
||||||
mIsMockResponseSupported = isMockResponseSupported;
|
mIsMockResponseSupported = isMockResponseSupported;
|
||||||
if (isMockResponseSupported) {
|
if (isMockResponseSupported) {
|
||||||
this.plugin.setConnectionListener(this);
|
mPlugin.setConnectionListener(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Response intercept(Interceptor.Chain chain) throws IOException {
|
public Response intercept(Interceptor.Chain chain) throws IOException {
|
||||||
Request request = chain.request();
|
final Request request = chain.request();
|
||||||
String identifier = UUID.randomUUID().toString();
|
final String identifier = UUID.randomUUID().toString();
|
||||||
plugin.reportRequest(convertRequest(request, identifier));
|
mPlugin.reportRequest(convertRequest(request, identifier));
|
||||||
|
|
||||||
// Check if there is a mock response
|
// Check if there is a mock response
|
||||||
Response mockResponse = mIsMockResponseSupported ? getMockResponse(request) : null;
|
final Response mockResponse = mIsMockResponseSupported ? getMockResponse(request) : null;
|
||||||
Response response = mockResponse != null ? mockResponse : chain.proceed(request);
|
final Response response = mockResponse != null ? mockResponse : chain.proceed(request);
|
||||||
|
final ResponseBody body = response.body();
|
||||||
ResponseBody body = response.body();
|
final ResponseInfo responseInfo = convertResponse(response, body, identifier);
|
||||||
ResponseInfo responseInfo = convertResponse(response, body, identifier);
|
|
||||||
responseInfo.isMock = mockResponse != null;
|
responseInfo.isMock = mockResponse != null;
|
||||||
plugin.reportResponse(responseInfo);
|
mPlugin.reportResponse(responseInfo);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] bodyToByteArray(final Request request, final long maxBodyBytes)
|
private static byte[] bodyToByteArray(final Request request, final long maxBodyBytes)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
final Buffer buffer = new Buffer();
|
final Buffer buffer = new Buffer();
|
||||||
request.body().writeTo(buffer);
|
if (request.body() != null) {
|
||||||
|
request.body().writeTo(buffer);
|
||||||
|
}
|
||||||
return buffer.readByteArray(Math.min(buffer.size(), maxBodyBytes));
|
return buffer.readByteArray(Math.min(buffer.size(), maxBodyBytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
private RequestInfo convertRequest(Request request, String identifier) throws IOException {
|
private RequestInfo convertRequest(Request request, String identifier) throws IOException {
|
||||||
List<NetworkReporter.Header> headers = convertHeader(request.headers());
|
final List<NetworkReporter.Header> headers = convertHeader(request.headers());
|
||||||
RequestInfo info = new RequestInfo();
|
final RequestInfo info = new RequestInfo();
|
||||||
info.requestId = identifier;
|
info.requestId = identifier;
|
||||||
info.timeStamp = System.currentTimeMillis();
|
info.timeStamp = System.currentTimeMillis();
|
||||||
info.headers = headers;
|
info.headers = headers;
|
||||||
info.method = request.method();
|
info.method = request.method();
|
||||||
info.uri = request.url().toString();
|
info.uri = request.url().toString();
|
||||||
if (request.body() != null) {
|
if (request.body() != null) {
|
||||||
info.body = bodyToByteArray(request, maxBodyBytes);
|
info.body = bodyToByteArray(request, mMaxBodyBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
return info;
|
return info;
|
||||||
@@ -133,24 +125,32 @@ public class FlipperOkhttpInterceptor
|
|||||||
|
|
||||||
private ResponseInfo convertResponse(Response response, ResponseBody body, String identifier)
|
private ResponseInfo convertResponse(Response response, ResponseBody body, String identifier)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
List<NetworkReporter.Header> headers = convertHeader(response.headers());
|
final List<NetworkReporter.Header> headers = convertHeader(response.headers());
|
||||||
ResponseInfo info = new ResponseInfo();
|
final 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;
|
||||||
BufferedSource source = body.source();
|
Buffer buffer = null;
|
||||||
source.request(maxBodyBytes);
|
try {
|
||||||
Buffer buffer = source.buffer().clone();
|
final BufferedSource source = body.source();
|
||||||
info.body = buffer.readByteArray();
|
source.request(mMaxBodyBytes);
|
||||||
|
buffer = source.buffer().clone();
|
||||||
|
info.body = buffer.readByteArray();
|
||||||
|
} finally {
|
||||||
|
if (buffer != null) {
|
||||||
|
buffer.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<NetworkReporter.Header> convertHeader(Headers headers) {
|
private static List<NetworkReporter.Header> convertHeader(Headers headers) {
|
||||||
List<NetworkReporter.Header> list = new ArrayList<>();
|
final List<NetworkReporter.Header> list = new ArrayList<>(headers.size());
|
||||||
|
|
||||||
Set<String> keys = headers.names();
|
final Set<String> keys = headers.names();
|
||||||
for (String key : keys) {
|
for (final String key : keys) {
|
||||||
list.add(new NetworkReporter.Header(key, headers.get(key)));
|
list.add(new NetworkReporter.Header(key, headers.get(key)));
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
|
|||||||
Reference in New Issue
Block a user