From 082e088eb4941456a2bba0a4645223c155ecfc9b Mon Sep 17 00:00:00 2001 From: 131 <11222509+Netrvin@users.noreply.github.com> Date: Sat, 12 Feb 2022 20:29:12 +0800 Subject: [PATCH] =?UTF-8?q?Data=20URI=20=E6=94=AF=E6=8C=81=EF=BC=88TXT?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/analyzeRule/AnalyzeUrl.kt | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 79cd6319a..51488c7b4 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -1,6 +1,7 @@ package io.legado.app.model.analyzeRule import android.annotation.SuppressLint +import android.util.Base64 import androidx.annotation.Keep import com.bumptech.glide.load.model.GlideUrl import com.bumptech.glide.load.model.LazyHeaders @@ -461,30 +462,39 @@ class AnalyzeUrl( * 访问网站,返回ByteArray */ suspend fun getByteArrayAwait(): ByteArray { + val dataUriRegex = Regex("data:[\\w/\\-\\.]+;base64,(.*)") + val dataUriFindResult = dataUriRegex.find(urlNoQuery) val concurrentRecord = fetchStart() setCookie(source?.getKey()) @Suppress("BlockingMethodInNonBlockingContext") - val byteArray = getProxyClient(proxy).newCallResponseBody(retry) { - addHeaders(headerMap) - when (method) { - RequestMethod.POST -> { - url(urlNoQuery) - val contentType = headerMap["Content-Type"] - val body = body - if (fieldMap.isNotEmpty() || body.isNullOrBlank()) { - postForm(fieldMap, true) - } else if (!contentType.isNullOrBlank()) { - val requestBody = body.toRequestBody(contentType.toMediaType()) - post(requestBody) - } else { - postJson(body) + if (dataUriFindResult != null){ + val dataUriBase64 = dataUriFindResult.groupValues?.get(1) + val byteArray = Base64.decode(dataUriBase64, Base64.DEFAULT) + fetchEnd(concurrentRecord) + return byteArray + }else { + val byteArray = getProxyClient(proxy).newCallResponseBody(retry) { + addHeaders(headerMap) + when (method) { + RequestMethod.POST -> { + url(urlNoQuery) + val contentType = headerMap["Content-Type"] + val body = body + if (fieldMap.isNotEmpty() || body.isNullOrBlank()) { + postForm(fieldMap, true) + } else if (!contentType.isNullOrBlank()) { + val requestBody = body.toRequestBody(contentType.toMediaType()) + post(requestBody) + } else { + postJson(body) + } } + else -> get(urlNoQuery, fieldMap, true) } - else -> get(urlNoQuery, fieldMap, true) - } - }.bytes() - fetchEnd(concurrentRecord) - return byteArray + }.bytes() + fetchEnd(concurrentRecord) + return byteArray + } } fun getByteArray(): ByteArray {