1
0
mirror of https://github.com/square/okhttp.git synced 2025-08-01 16:06:56 +03:00

Cover more of the OkHttp API for source compatibility

This commit is contained in:
Jesse Wilson
2019-04-06 13:20:41 -04:00
parent 631d6457b4
commit 2e947a2202
4 changed files with 273 additions and 110 deletions

View File

@ -44,7 +44,7 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
@Volatile private var headersToRedact = emptySet<String>()
@Volatile private var level = Level.NONE
@Volatile @set:JvmName("-deprecated_setLevel") var level = Level.NONE
enum class Level {
/** No logs. */
@ -109,6 +109,14 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
fun log(message: String)
companion object {
// This lambda conversion is for Kotlin callers expecting a Java SAM (single-abstract-method).
@JvmName("-deprecated_Logger")
inline operator fun invoke(
crossinline block: (message: String) -> Unit
): Logger = object : Logger {
override fun log(message: String) = block(message)
}
/** A [Logger] defaults output appropriate for the current platform. */
@JvmField
val DEFAULT: Logger = object : Logger {
@ -126,11 +134,11 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
headersToRedact = newHeadersToRedact
}
/** Change the level at which this interceptor logs. */
fun setLevel(level: Level) = apply {
this.level = level
}
@JvmName("-deprecated_getLevel")
fun getLevel(): Level = level
@Throws(IOException::class)
@ -193,7 +201,7 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
val charset: Charset = contentType?.charset(UTF8) ?: UTF8
logger.log("")
if (isPlaintext(buffer)) {
if (buffer.isUtf8()) {
logger.log(buffer.readString(charset))
logger.log("--> END ${request.method()} (${requestBody.contentLength()}-byte body)")
} else {
@ -247,7 +255,7 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
val contentType = responseBody.contentType()
val charset: Charset = contentType?.charset(UTF8) ?: UTF8
if (!isPlaintext(buffer)) {
if (!buffer.isUtf8()) {
logger.log("")
logger.log("<-- END HTTP (binary ${buffer.size}-byte body omitted)")
return response
@ -278,16 +286,16 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
private val UTF8 = StandardCharsets.UTF_8
/**
* 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.
* 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.
*/
@JvmStatic
fun isPlaintext(buffer: Buffer): Boolean {
internal fun Buffer.isUtf8(): Boolean {
try {
val prefix = Buffer()
val byteCount = if (buffer.size < 64) buffer.size else 64
buffer.copyTo(prefix, 0, byteCount)
for (i in 0..15) {
val byteCount = if (size < 64) size else 64
copyTo(prefix, 0, byteCount)
for (i in 0 until 16) {
if (prefix.exhausted()) {
break
}

View File

@ -0,0 +1,33 @@
/*
* 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 okhttp3.logging
import okhttp3.logging.HttpLoggingInterceptor.Companion.isUtf8
import okio.Buffer
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
class HttpLoggingInterceptorKotlinTest {
@Test fun isPlaintext() {
assertThat(Buffer().isUtf8()).isTrue()
assertThat(Buffer().writeUtf8("abc").isUtf8()).isTrue()
assertThat(Buffer().writeUtf8("new\r\nlines").isUtf8()).isTrue()
assertThat(Buffer().writeUtf8("white\t space").isUtf8()).isTrue()
assertThat(Buffer().writeByte(0x80).isUtf8()).isTrue()
assertThat(Buffer().writeByte(0x00).isUtf8()).isFalse()
assertThat(Buffer().writeByte(0xc0).isUtf8()).isFalse()
}
}

View File

@ -644,16 +644,6 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs();
}
@Test public void isPlaintext() {
assertThat(HttpLoggingInterceptor.isPlaintext(new Buffer())).isTrue();
assertThat(HttpLoggingInterceptor.isPlaintext(new Buffer().writeUtf8("abc"))).isTrue();
assertThat(HttpLoggingInterceptor.isPlaintext(new Buffer().writeUtf8("new\r\nlines"))).isTrue();
assertThat(HttpLoggingInterceptor.isPlaintext(new Buffer().writeUtf8("white\t space"))).isTrue();
assertThat(HttpLoggingInterceptor.isPlaintext(new Buffer().writeByte(0x80))).isTrue();
assertThat(HttpLoggingInterceptor.isPlaintext(new Buffer().writeByte(0x00))).isFalse();
assertThat(HttpLoggingInterceptor.isPlaintext(new Buffer().writeByte(0xc0))).isFalse();
}
@Test public void responseBodyIsBinary() throws IOException {
setLevel(Level.BODY);
Buffer buffer = new Buffer();