diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpConstants.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpConstants.java index 3850cd3f..b86534a3 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpConstants.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpConstants.java @@ -51,6 +51,14 @@ public class HttpConstants { public static final String ACCEPT_ENCODING_IDENTITY = "identity"; public static final String OC_FILE_REMOTE_ID = "OC-FileId"; + /*********************************************************************************************************** + ************************************************ CONTENT TYPES ******************************************** + ***********************************************************************************************************/ + + public static final String CONTENT_TYPE_XML = "application/xml"; + public static final String CONTENT_TYPE_JSON = "application/json"; + public static final String CONTENT_TYPE_WWW_FORM = "application/x-www-form-urlencoded"; + /*********************************************************************************************************** ************************************************ STATUS CODES ********************************************* ***********************************************************************************************************/ diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogBuilder.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogBuilder.kt index c85f4fd5..c0ca4fb0 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogBuilder.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogBuilder.kt @@ -22,6 +22,10 @@ */ package com.owncloud.android.lib.common.http +import com.owncloud.android.lib.common.http.HttpConstants.CONTENT_TYPE_JSON +import com.owncloud.android.lib.common.http.HttpConstants.CONTENT_TYPE_WWW_FORM +import com.owncloud.android.lib.common.http.HttpConstants.CONTENT_TYPE_XML +import okhttp3.MediaType import timber.log.Timber import java.util.Locale @@ -45,3 +49,17 @@ enum class NetworkNode { override fun toString(): String = super.toString().toLowerCase(Locale.ROOT) } + +/** + * Check whether a media type is loggable. + * + * @return true if its type is text, xml, json, or x-www-form-urlencoded. + */ +fun MediaType?.isLoggable(): Boolean = + this?.let { mediaType -> + val mediaTypeString = mediaType.toString() + (mediaType.type == "text" || + mediaTypeString.contains(CONTENT_TYPE_XML) || + mediaTypeString.contains(CONTENT_TYPE_JSON) || + mediaTypeString.contains(CONTENT_TYPE_WWW_FORM)) + } ?: false diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogInterceptor.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogInterceptor.kt index 30860f54..a6f5dd74 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogInterceptor.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogInterceptor.kt @@ -99,7 +99,7 @@ class LogInterceptor : Interceptor { logHttp(REQUEST, BODY, requestId, "Type: ${requestBody.contentType()}") logHttp(REQUEST, BODY, requestId, "--> Body start for request") - if (buffer.isProbablyUtf8()) { + if (contentType.isLoggable()) { if (requestBody.contentLength() < LIMIT_BODY_LOG) { logHttp(REQUEST, BODY, requestId, buffer.readString(charset)) } else { @@ -137,7 +137,25 @@ class LogInterceptor : Interceptor { source.request(LIMIT_BODY_LOG) val buffer = source.buffer - if (!buffer.isProbablyUtf8()) { + if (contentType.isLoggable()) { + + if (responseBody.contentLength() < LIMIT_BODY_LOG) { + logHttp(RESPONSE, BODY, requestId, buffer.clone().readString(charset)) + } else { + logHttp(RESPONSE, BODY, requestId, buffer.clone().readString(LIMIT_BODY_LOG, charset)) + } + logHttp( + RESPONSE, + BODY, + requestId, + "<-- Body end for response -- Omitted: ${ + max( + 0, + responseBody.contentLength() - LIMIT_BODY_LOG + ) + } bytes" + ) + } else { logHttp( RESPONSE, BODY, @@ -145,18 +163,6 @@ class LogInterceptor : Interceptor { "<-- Body end for response -- Binary -- Omitted: ${responseBody.contentLength()} bytes" ) } - - if (responseBody.contentLength() < LIMIT_BODY_LOG) { - logHttp(RESPONSE, BODY, requestId, buffer.clone().readString(charset)) - } else { - logHttp(RESPONSE, BODY, requestId, buffer.clone().readString(LIMIT_BODY_LOG, charset)) - } - logHttp( - RESPONSE, - BODY, - requestId, - "<-- Body end for response -- Omitted: ${max(0, responseBody.contentLength() - LIMIT_BODY_LOG)} bytes" - ) } ?: logHttp(RESPONSE, BODY, requestId, "Empty body") } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/utf8.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/utf8.kt deleted file mode 100644 index 9591e425..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/utf8.kt +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2015 Square, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.owncloud.android.lib.common.http - -import java.io.EOFException -import okio.Buffer - -/** - * Returns true if the body in question probably contains human readable text. Uses a small - * sample of code points to detect unicode control characters commonly used in binary file - * signatures. - */ -internal fun Buffer.isProbablyUtf8(): Boolean { - try { - val prefix = Buffer() - val byteCount = size.coerceAtMost(64) - copyTo(prefix, 0, byteCount) - for (i in 0 until 16) { - if (prefix.exhausted()) { - break - } - val codePoint = prefix.readUtf8CodePoint() - if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) { - return false - } - } - return true - } catch (_: EOFException) { - return false // Truncated UTF-8 sequence. - } -}