From 6cd097fc8f320f81c4011b45c9d2c8f69d25b6b9 Mon Sep 17 00:00:00 2001 From: cpacm Date: Wed, 8 May 2024 16:41:46 +0800 Subject: [PATCH] fix giokit http interceptor null point exceptioon (#11) --- .../giokit/hook/GioHttpCaptureInterceptor.kt | 18 +++++++++--------- gradle/libs.versions.toml | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/giokit/src/main/java/com/growingio/giokit/hook/GioHttpCaptureInterceptor.kt b/giokit/src/main/java/com/growingio/giokit/hook/GioHttpCaptureInterceptor.kt index 0e6fd9c..7a1958d 100644 --- a/giokit/src/main/java/com/growingio/giokit/hook/GioHttpCaptureInterceptor.kt +++ b/giokit/src/main/java/com/growingio/giokit/hook/GioHttpCaptureInterceptor.kt @@ -70,19 +70,19 @@ class GioHttpCaptureInterceptor @JvmOverloads constructor() : Interceptor { log("--> END ${request.method()} (encoded body omitted)") } else if (bodyHasSnappyEncoding(request.headers())) { val buffer = Buffer() - requestBody!!.writeTo(buffer) + requestBody?.writeTo(buffer) log("") val byteArray = buffer.readByteArray() val compressedOut = XORUtils.encrypt(byteArray, (gioHttp.httpTime and 0xFF).toInt()) gioHttp.requestBody = String(Snappy.uncompress(compressedOut, 0, compressedOut.size)) log(gioHttp.requestBody) - log("--> END ${request.method()} (${requestBody.contentLength()}-byte body)") + log("--> END ${request.method()} (${requestBody?.contentLength() ?: 0}-byte body)") } else { val buffer = Buffer() - requestBody!!.writeTo(buffer) + requestBody?.writeTo(buffer) - val contentType = requestBody.contentType() + val contentType = requestBody?.contentType() val charset: Charset = contentType?.charset(StandardCharsets.UTF_8) ?: StandardCharsets.UTF_8 @@ -90,11 +90,11 @@ class GioHttpCaptureInterceptor @JvmOverloads constructor() : Interceptor { if (buffer.isProbablyUtf8()) { gioHttp.requestBody = buffer.readString(charset) log(gioHttp.requestBody) - log("--> END ${request.method()} (${requestBody.contentLength()}-byte body)") + log("--> END ${request.method()} (${requestBody?.contentLength() ?: 0}-byte body)") } else { gioHttp.requestBody = buffer.readString(charset) //gioHttp.requestBody = "encoded body omitted (e.g. protobuf)" - log("--> END ${request.method()} (binary ${requestBody.contentLength()}-byte body omitted. e.g. protobuf)") + log("--> END ${request.method()} (binary ${requestBody?.contentLength() ?: 0}-byte body omitted. e.g. protobuf)") } } @@ -113,8 +113,8 @@ class GioHttpCaptureInterceptor @JvmOverloads constructor() : Interceptor { val tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs) gioHttp.httpCost = tookMs - val responseBody = response.body()!! - val contentLength = responseBody.contentLength() + val responseBody = response.body() + val contentLength = responseBody?.contentLength() ?: 0 val bodySize = if (contentLength != -1L) "$contentLength-byte" else "unknown-length" gioHttp.responseCode = response.code() gioHttp.responseSize = contentLength @@ -137,7 +137,7 @@ class GioHttpCaptureInterceptor @JvmOverloads constructor() : Interceptor { if (bodyHasUnknownEncoding(response.headers())) { gioHttp.responseBody = "encoded body omitted" log("<-- END HTTP (encoded body omitted)") - } else { + } else if (responseBody != null) { val source = responseBody.source() source.request(Long.MAX_VALUE) // Buffer the entire body. var buffer = source.buffer() diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f04bafe..7a889b0 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -34,8 +34,8 @@ powermock = "2.0.9" # !!! SDK VERSION !!! growingio = "4.1.0" growingioPlugin = "4.1.0" -giokit = "2.1.1" -giokitCode = "21" +giokit = "2.1.2" +giokitCode = "22" [plugins] android-application = { id = "com.android.application", version.ref = "pluginGradle" }