Просмотр исходного кода

新增输出MultiPart参数日志

drake 3 лет назад
Родитель
Сommit
552a5d6897

+ 33 - 7
net/src/main/java/com/drake/net/body/BodyExtension.kt

@@ -1,6 +1,7 @@
 package com.drake.net.body
 
 import com.drake.net.interfaces.ProgressListener
+import okhttp3.MultipartBody
 import okhttp3.RequestBody
 import okhttp3.ResponseBody
 import okhttp3.ResponseBody.Companion.asResponseBody
@@ -22,9 +23,9 @@ fun ResponseBody.toNetResponseBody(
  */
 @JvmName("peekString")
 fun RequestBody?.peekString(byteCount: Long = 1024 * 1024, discard: Boolean = false): String? {
-    return when {
-        this == null -> return null
-        this is NetRequestBody -> peekString(byteCount, discard)
+    return when (this) {
+        null -> return null
+        is NetRequestBody -> peekString(byteCount, discard)
         else -> {
             val buffer = Buffer()
             writeTo(buffer)
@@ -39,10 +40,9 @@ fun RequestBody?.peekString(byteCount: Long = 1024 * 1024, discard: Boolean = fa
  * 复制一段指定长度的字符串内容
  * @param byteCount 复制的字节长度. 如果-1则返回完整的字符串内容
  */
-fun ResponseBody?.peekString(byteCount: Long = 1024 * 1024 * 4, discard: Boolean = false): String? {
-    return when {
-        this == null -> return null
-        this is NetResponseBody -> peekString(byteCount, discard)
+fun ResponseBody.peekString(byteCount: Long = 1024 * 1024 * 4, discard: Boolean = false): String {
+    return when (this) {
+        is NetResponseBody -> peekString(byteCount, discard)
         else -> {
             val peeked = source().peek()
             val buffer = Buffer()
@@ -54,4 +54,30 @@ fun ResponseBody?.peekString(byteCount: Long = 1024 * 1024 * 4, discard: Boolean
             buffer.asResponseBody(contentType(), byteCountFinal).string()
         }
     }
+}
+
+/**
+ * 通过判断[okhttp3.Headers]里面的Content-Disposition是否存在filename属性来确定是否为文件类型[MultipartBody.Part]
+ */
+fun MultipartBody.Part.isFile(): Boolean {
+    val contentDisposition = headers?.get("Content-Disposition") ?: return false
+    return ";\\s${"filename"}=\"(.+?)\"".toRegex().find(contentDisposition)?.groupValues?.getOrNull(1) != null
+}
+
+/**
+ * 返回Content-Disposition里面的字段名称
+ */
+fun MultipartBody.Part.name(): String? {
+    val contentDisposition = headers?.get("Content-Disposition") ?: return null
+    return ";\\s${"name"}=\"(.+?)\"".toRegex().find(contentDisposition)?.groupValues?.getOrNull(1) ?: ""
+}
+
+/**
+ * 将[MultipartBody.Part.body]作为字符串返回
+ * 如果[MultipartBody.Part]是文件类型则返回的是文件名称, 确定文件类型请参考[MultipartBody.Part.isFile]
+ */
+fun MultipartBody.Part.value(): String? {
+    val contentDisposition = headers?.get("Content-Disposition") ?: return null
+    return ";\\s${"filename"}=\"(.+?)\"".toRegex().find(contentDisposition)?.groupValues?.getOrNull(1)
+        ?: body.peekString()
 }

+ 20 - 10
net/src/main/java/com/drake/net/request/RequestExtension.kt

@@ -17,14 +17,13 @@
 package com.drake.net.request
 
 import com.drake.net.NetConfig
+import com.drake.net.body.name
 import com.drake.net.body.peekString
+import com.drake.net.body.value
 import com.drake.net.convert.NetConverter
 import com.drake.net.interfaces.ProgressListener
 import com.drake.net.tag.NetLabel
-import okhttp3.FormBody
-import okhttp3.Headers
-import okhttp3.OkHttpUtils
-import okhttp3.Request
+import okhttp3.*
 import java.net.URLDecoder
 import java.util.concurrent.ConcurrentLinkedQueue
 import kotlin.reflect.KType
@@ -370,16 +369,27 @@ fun Request.Builder.setConverter(converter: NetConverter) = apply {
  * @param urlDecode 是否进行 UTF-8 URLDecode
  */
 fun Request.logString(byteCount: Long = 1024 * 1024, urlDecode: Boolean = true): String? {
-    val mediaType = body?.contentType() ?: return null
-    val bodyString =
-        if (body is FormBody || mediaType.type == "text" || mediaType.subtype == "json") {
-            body?.peekString(byteCount)
-        } else "Not support this type $mediaType"
+    val requestBody = body ?: return null
+    val mediaType = requestBody.contentType()
+    val supportSubtype = arrayOf("plain", "json", "xml", "html").contains(mediaType?.subtype)
+    val bodyString = when {
+        requestBody is MultipartBody -> {
+            val params = mutableListOf<String>()
+            requestBody.parts.forEach {
+                params.add("${it.name()}=${it.value()}")
+            }
+            params.joinToString("&")
+        }
+        requestBody is FormBody || supportSubtype -> requestBody.peekString(byteCount)
+        else -> "$mediaType does not support output logs"
+    }
     return if (urlDecode) {
         try {
             URLDecoder.decode(bodyString, "UTF-8")
         } catch (e: Exception) {
             bodyString
         }
-    } else bodyString
+    } else {
+        bodyString
+    }
 }

+ 6 - 4
net/src/main/java/com/drake/net/response/ResponseExtension.kt

@@ -133,11 +133,13 @@ fun Response.file(): File? {
  * 只会输出 application/json, text/`*` 响应体类型日志
  */
 fun Response.logString(byteCount: Long = 1024 * 1024 * 4): String? {
-    val mediaType = body?.contentType() ?: return null
-    return if (mediaType.subtype == "json" || mediaType.type == "text") {
-        body?.peekString(byteCount)
+    val requestBody = body ?: return null
+    val mediaType = requestBody.contentType()
+    val supportSubtype = arrayOf("plain", "json", "xml", "html").contains(mediaType?.subtype)
+    return if (supportSubtype) {
+        requestBody.peekString(byteCount)
     } else {
-        "Not support this type $mediaType"
+        "$mediaType does not support output logs"
     }
 }