Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 5e86e9f

Browse files
committed
Determine if a body is loggable based on its content-type
1 parent 460f85f commit 5e86e9f

File tree

4 files changed

+46
-58
lines changed

4 files changed

+46
-58
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpConstants.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public class HttpConstants {
5151
public static final String ACCEPT_ENCODING_IDENTITY = "identity";
5252
public static final String OC_FILE_REMOTE_ID = "OC-FileId";
5353

54+
/***********************************************************************************************************
55+
************************************************ CONTENT TYPES ********************************************
56+
***********************************************************************************************************/
57+
58+
public static final String CONTENT_TYPE_XML = "application/xml";
59+
public static final String CONTENT_TYPE_JSON = "application/json";
60+
public static final String CONTENT_TYPE_WWW_FORM = "application/x-www-form-urlencoded";
61+
5462
/***********************************************************************************************************
5563
************************************************ STATUS CODES *********************************************
5664
***********************************************************************************************************/

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogBuilder.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
*/
2323
package com.owncloud.android.lib.common.http
2424

25+
import com.owncloud.android.lib.common.http.HttpConstants.CONTENT_TYPE_JSON
26+
import com.owncloud.android.lib.common.http.HttpConstants.CONTENT_TYPE_WWW_FORM
27+
import com.owncloud.android.lib.common.http.HttpConstants.CONTENT_TYPE_XML
28+
import okhttp3.MediaType
2529
import timber.log.Timber
2630
import java.util.Locale
2731

@@ -45,3 +49,17 @@ enum class NetworkNode {
4549

4650
override fun toString(): String = super.toString().toLowerCase(Locale.ROOT)
4751
}
52+
53+
/**
54+
* Check whether a media type is loggable.
55+
*
56+
* @return true if its type is text, xml, json, or x-www-form-urlencoded.
57+
*/
58+
fun MediaType?.isLoggable(): Boolean =
59+
this?.let { mediaType ->
60+
val mediaTypeString = mediaType.toString()
61+
(mediaType.type == "text" ||
62+
mediaTypeString.contains(CONTENT_TYPE_XML) ||
63+
mediaTypeString.contains(CONTENT_TYPE_JSON) ||
64+
mediaTypeString.contains(CONTENT_TYPE_WWW_FORM))
65+
} ?: false

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogInterceptor.kt

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class LogInterceptor : Interceptor {
9999
logHttp(REQUEST, BODY, requestId, "Type: ${requestBody.contentType()}")
100100
logHttp(REQUEST, BODY, requestId, "--> Body start for request")
101101

102-
if (buffer.isProbablyUtf8()) {
102+
if (contentType.isLoggable()) {
103103
if (requestBody.contentLength() < LIMIT_BODY_LOG) {
104104
logHttp(REQUEST, BODY, requestId, buffer.readString(charset))
105105
} else {
@@ -137,26 +137,32 @@ class LogInterceptor : Interceptor {
137137
source.request(LIMIT_BODY_LOG)
138138
val buffer = source.buffer
139139

140-
if (!buffer.isProbablyUtf8()) {
140+
if (contentType.isLoggable()) {
141+
142+
if (responseBody.contentLength() < LIMIT_BODY_LOG) {
143+
logHttp(RESPONSE, BODY, requestId, buffer.clone().readString(charset))
144+
} else {
145+
logHttp(RESPONSE, BODY, requestId, buffer.clone().readString(LIMIT_BODY_LOG, charset))
146+
}
141147
logHttp(
142148
RESPONSE,
143149
BODY,
144150
requestId,
145-
"<-- Body end for response -- Binary -- Omitted: ${responseBody.contentLength()} bytes"
151+
"<-- Body end for response -- Omitted: ${
152+
max(
153+
0,
154+
responseBody.contentLength() - LIMIT_BODY_LOG
155+
)
156+
} bytes"
146157
)
147-
}
148-
149-
if (responseBody.contentLength() < LIMIT_BODY_LOG) {
150-
logHttp(RESPONSE, BODY, requestId, buffer.clone().readString(charset))
151158
} else {
152-
logHttp(RESPONSE, BODY, requestId, buffer.clone().readString(LIMIT_BODY_LOG, charset))
159+
logHttp(
160+
RESPONSE,
161+
BODY,
162+
requestId,
163+
"<-- Body end for response -- Binary -- Omitted: ${responseBody.contentLength()} bytes"
164+
)
153165
}
154-
logHttp(
155-
RESPONSE,
156-
BODY,
157-
requestId,
158-
"<-- Body end for response -- Omitted: ${max(0, responseBody.contentLength() - LIMIT_BODY_LOG)} bytes"
159-
)
160166
} ?: logHttp(RESPONSE, BODY, requestId, "Empty body")
161167
}
162168

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/utf8.kt

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)