Initial commit 🎉

fbshipit-source-id: b6fc29740c6875d2e78953b8a7123890a67930f2
Co-authored-by: Sebastian McKenzie <sebmck@fb.com>
Co-authored-by: John Knox <jknox@fb.com>
Co-authored-by: Emil Sjölander <emilsj@fb.com>
Co-authored-by: Pritesh Nandgaonkar <prit91@fb.com>
This commit is contained in:
Daniel Büchele
2018-04-13 08:38:06 -07:00
committed by Daniel Buchele
commit fbbf8cf16b
659 changed files with 87130 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.sonar.plugins.network;
import java.util.ArrayList;
import java.util.List;
public interface NetworkReporter {
void reportRequest(RequestInfo requestInfo);
void reportResponse(ResponseInfo responseInfo);
public class Header {
public final String name;
public final String value;
public Header(final String name, final String value) {
this.name = name;
this.value = value;
}
@Override
public String toString() {
return "Header{" + name + ": " + value + "}";
}
}
public class RequestInfo {
public String requestId;
public long timeStamp;
public List<Header> headers = new ArrayList<>();
public String method;
public String uri;
public byte[] body;
public Header getFirstHeader(final String name) {
for (Header header : headers) {
if (name.equalsIgnoreCase(header.name)) {
return header;
}
}
return null;
}
}
public class ResponseInfo {
public String requestId;
public long timeStamp;
public int statusCode;
public String statusReason;
public List<Header> headers = new ArrayList<>();
public byte[] body;
public Header getFirstHeader(final String name) {
for (Header header : headers) {
if (name.equalsIgnoreCase(header.name)) {
return header;
}
}
return null;
}
}
}

View File

@@ -0,0 +1,22 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.sonar.plugins.network;
import com.facebook.sonar.plugins.network.NetworkReporter.ResponseInfo;
public interface NetworkResponseFormatter {
interface OnCompletionListener {
void onCompletion(String json);
}
boolean shouldFormat(ResponseInfo response);
void format(ResponseInfo response, OnCompletionListener onCompletionListener);
}

View File

@@ -0,0 +1,122 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.sonar.plugins.network;
import android.util.Base64;
import com.facebook.sonar.core.ErrorReportingRunnable;
import com.facebook.sonar.core.SonarArray;
import com.facebook.sonar.core.SonarObject;
import com.facebook.sonar.plugins.common.BufferingSonarPlugin;
import java.util.List;
public class NetworkSonarPlugin extends BufferingSonarPlugin implements NetworkReporter {
public static final String ID = "Network";
private final List<NetworkResponseFormatter> mFormatters;
public NetworkSonarPlugin() {
this(null);
}
public NetworkSonarPlugin(List<NetworkResponseFormatter> formatters) {
this.mFormatters = formatters;
}
@Override
public String getId() {
return ID;
}
@Override
public void reportRequest(RequestInfo requestInfo) {
final SonarObject request =
new SonarObject.Builder()
.put("id", requestInfo.requestId)
.put("timestamp", requestInfo.timeStamp)
.put("method", requestInfo.method)
.put("url", requestInfo.uri)
.put("headers", toSonarObject(requestInfo.headers))
.put("data", toBase64(requestInfo.body))
.build();
send("newRequest", request);
}
@Override
public void reportResponse(final ResponseInfo responseInfo) {
final Runnable job =
new ErrorReportingRunnable(getConnection()) {
@Override
protected void runOrThrow() throws Exception {
if (shouldStripResponseBody(responseInfo)) {
responseInfo.body = null;
}
final SonarObject response =
new SonarObject.Builder()
.put("id", responseInfo.requestId)
.put("timestamp", responseInfo.timeStamp)
.put("status", responseInfo.statusCode)
.put("reason", responseInfo.statusReason)
.put("headers", toSonarObject(responseInfo.headers))
.put("data", toBase64(responseInfo.body))
.build();
send("newResponse", response);
}
};
if (mFormatters != null) {
for (NetworkResponseFormatter formatter : mFormatters) {
if (formatter.shouldFormat(responseInfo)) {
formatter.format(
responseInfo,
new NetworkResponseFormatter.OnCompletionListener() {
@Override
public void onCompletion(final String json) {
responseInfo.body = json.getBytes();
job.run();
}
});
return;
}
}
}
job.run();
}
private String toBase64(byte[] bytes) {
if (bytes == null) {
return null;
}
return new String(Base64.encode(bytes, Base64.DEFAULT));
}
private SonarArray toSonarObject(List<Header> headers) {
final SonarArray.Builder list = new SonarArray.Builder();
for (Header header : headers) {
list.put(new SonarObject.Builder().put("key", header.name).put("value", header.value));
}
return list.build();
}
private static boolean shouldStripResponseBody(ResponseInfo responseInfo) {
final Header contentType = responseInfo.getFirstHeader("content-type");
if (contentType == null) {
return false;
}
return contentType.value.contains("image/")
|| contentType.value.contains("video/")
|| contentType.value.contains("application/zip");
}
}

View File

@@ -0,0 +1,106 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.sonar.plugins.network;
import android.util.Log;
import com.facebook.sonar.plugins.network.NetworkReporter.RequestInfo;
import com.facebook.sonar.plugins.network.NetworkReporter.ResponseInfo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import javax.annotation.Nullable;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
public class SonarOkhttpInterceptor implements Interceptor {
public @Nullable NetworkSonarPlugin plugin;
public SonarOkhttpInterceptor() {
this.plugin = null;
}
public SonarOkhttpInterceptor(NetworkSonarPlugin plugin) {
this.plugin = plugin;
}
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
int randInt = randInt(1, Integer.MAX_VALUE);
plugin.reportRequest(convertRequest(request, randInt));
Response response = chain.proceed(request);
ResponseBody body = response.body();
ResponseInfo responseInfo = convertResponse(response, body, randInt);
plugin.reportResponse(responseInfo);
// Creating new response as can't used response.body() more than once
return response
.newBuilder()
.body(ResponseBody.create(body.contentType(), responseInfo.body))
.build();
}
private static byte[] bodyToByteArray(final Request request) {
try {
final Request copy = request.newBuilder().build();
final Buffer buffer = new Buffer();
copy.body().writeTo(buffer);
return buffer.readByteArray();
} catch (final IOException e) {
return e.getMessage().getBytes();
}
}
private RequestInfo convertRequest(Request request, int identifier) {
List<NetworkReporter.Header> headers = convertHeader(request.headers());
RequestInfo info = new RequestInfo();
info.requestId = String.valueOf(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);
}
return info;
}
private ResponseInfo convertResponse(Response response, ResponseBody body, int identifier) {
List<NetworkReporter.Header> headers = convertHeader(response.headers());
ResponseInfo info = new ResponseInfo();
info.requestId = String.valueOf(identifier);
info.timeStamp = response.receivedResponseAtMillis();
info.statusCode = response.code();
info.headers = headers;
try {
info.body = body.bytes();
} catch (IOException e) {
Log.e("Sonar", e.toString());
}
return info;
}
private List<NetworkReporter.Header> convertHeader(Headers headers) {
List<NetworkReporter.Header> list = new ArrayList<>();
Set<String> keys = headers.names();
for (String key : keys) {
list.add(new NetworkReporter.Header(key, headers.get(key)));
}
return list;
}
private int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}