mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 07:56:19 +00:00
Determine if a body is loggable based on its content-type
This commit is contained in:
parent
460f85f2e1
commit
5e86e9f0e4
@ -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 *********************************************
|
||||
***********************************************************************************************************/
|
||||
|
@ -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
|
||||
|
@ -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")
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user