Skip to content

Commit

Permalink
Merge pull request #6 from itkacher/feature/content-type
Browse files Browse the repository at this point in the history
Feature/content type
  • Loading branch information
itkacher authored Nov 12, 2018
2 parents 5653d70 + 3f52725 commit 7185d72
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.google.code.gson:gson:2.8.5'
// implementation project(path: ':okhttpprofiler')
implementation 'com.itkacher.okhttpprofiler:okhttpprofiler:1.0.2'
implementation 'com.itkacher.okhttpprofiler:okhttpprofiler:1.0.4'
}
4 changes: 2 additions & 2 deletions okhttpprofiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion 16
targetSdkVersion 28
versionCode 2
versionName "1.0.3"
versionName "1.0.4"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -46,7 +46,7 @@ ext {
siteUrl = 'https://github.com/itkacher/OkHttpProfiler'
gitUrl = 'https://github.com/itkacher/OkHttpProfiler.git'

libraryVersion = '1.0.2'
libraryVersion = '1.0.4'

developerId = 'itkacher'
developerName = 'Ievgenii Tkachenko'
Expand Down
2 changes: 1 addition & 1 deletion okhttpprofiler/jcenter.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'

version '1.0.3'
version '1.0.4'

publishing {
publications {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.itkacher.okhttpprofiler.transfer.LogDataTransfer;
import com.itkacher.okhttpprofiler.transfer.DataTransfer;

import okhttp3.*;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.itkacher.okhttpprofiler.transfer;

import java.io.IOException;

import okhttp3.Request;
import okhttp3.Response;

public interface DataTransfer {
void sendRequest(String id, Request request);
void sendResponse(String id, Response response);
void sendRequest(String id, Request request) throws IOException;

void sendResponse(String id, Response response) throws IOException;

void sendException(String id, Exception response);

void sendDuration(String id, long duration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;

import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
Expand All @@ -24,10 +25,13 @@ public class LogDataTransfer implements DataTransfer {
private static final int BODY_BUFFER_SIZE = 1024 * 1024 * 10;
private static final String LOG_PREFIX = "OKPRFL";
private static final String DELIMITER = "_";
private static final String HEADER_DELIMITER = ":";
private static final Character HEADER_DELIMITER = ':';
private static final Character SPACE = ' ';
private static final String KEY_TAG = "TAG";
private static final String KEY_VALUE = "VALUE";
private static final String KEY_PARTS_COUNT = "PARTS_COUNT";
private static final String CONTENT_TYPE = "Content-Type";
private static final String CONTENT_LENGTH = "Content-Length";
private final Handler mHandler;

public LogDataTransfer() {
Expand All @@ -37,39 +41,46 @@ public LogDataTransfer() {
}

@Override
public void sendRequest(String id, Request request) {
public void sendRequest(String id, Request request) throws IOException {
fastLog(id, MessageType.REQUEST_METHOD, request.method());
String url = request.url().toString();
fastLog(id, MessageType.REQUEST_URL, url);
fastLog(id, MessageType.REQUEST_TIME, String.valueOf(System.currentTimeMillis()));

final Request copy = request.newBuilder().build();
final Buffer buffer = new Buffer();
RequestBody body = copy.body();

if (body != null) {
MediaType type = body.contentType();
if (type != null) {
fastLog(id, MessageType.REQUEST_HEADER, CONTENT_TYPE + HEADER_DELIMITER + SPACE + type.toString());
}
long contentLength = body.contentLength();
if (contentLength != -1) {
fastLog(id, MessageType.REQUEST_HEADER, CONTENT_LENGTH + HEADER_DELIMITER + SPACE + contentLength);
}
}

Headers headers = request.headers();
if (headers != null) {
int headersSize = headers.size();
for (String name : headers.names()) {
logWithHandler(id, MessageType.REQUEST_HEADER, name + ":" + headers.get(name), headersSize);
//We have logged them before
if (CONTENT_TYPE.equalsIgnoreCase(name) || CONTENT_LENGTH.equalsIgnoreCase(name)) continue;
fastLog(id, MessageType.REQUEST_HEADER, name + HEADER_DELIMITER + SPACE + headers.get(name));
}
}

try {
final Request copy = request.newBuilder().build();
final Buffer buffer = new Buffer();
RequestBody body = copy.body();
if (body != null) {
body.writeTo(buffer);
largeLog(id, MessageType.REQUEST_BODY, buffer.readUtf8());
}
} catch (final IOException ignored) {
if (body != null) {
body.writeTo(buffer);
largeLog(id, MessageType.REQUEST_BODY, buffer.readUtf8());
}
}

@Override
public void sendResponse(String id, Response response) {
try {
ResponseBody responseBodyCopy = response.peekBody(BODY_BUFFER_SIZE);
largeLog(id, MessageType.RESPONSE_BODY, responseBodyCopy.string());
} catch (IOException e) {
e.printStackTrace();
}
public void sendResponse(String id, Response response) throws IOException {
ResponseBody responseBodyCopy = response.peekBody(BODY_BUFFER_SIZE);
largeLog(id, MessageType.RESPONSE_BODY, responseBodyCopy.string());

Headers headers = response.headers();
logWithHandler(id, MessageType.RESPONSE_STATUS, String.valueOf(response.code()), 0);
Expand All @@ -94,7 +105,7 @@ public void sendDuration(String id, long duration) {
@SuppressLint("LogNotTimber")
private void fastLog(String id, MessageType type, String message) {
String tag = LOG_PREFIX + DELIMITER + id + DELIMITER + type.name;
if(message != null) {
if (message != null) {
Log.v(tag, message);
}
}
Expand Down Expand Up @@ -137,9 +148,9 @@ private LogBodyHandler(Looper looper) {
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
if(bundle != null) {
if (bundle != null) {
int partsCount = bundle.getInt(KEY_PARTS_COUNT, 0);
if(partsCount > SLOW_DOWN_PARTS_AFTER) {
if (partsCount > SLOW_DOWN_PARTS_AFTER) {
try {
Thread.sleep(5L);
} catch (InterruptedException e) {
Expand All @@ -148,7 +159,7 @@ public void handleMessage(Message msg) {
}
String data = bundle.getString(KEY_VALUE);
String key = bundle.getString(KEY_TAG);
if(data != null && key != null) {
if (data != null && key != null) {
Log.v(key, data);
}
}
Expand Down

0 comments on commit 7185d72

Please sign in to comment.