pull/977/head
gedoor 4 years ago
parent 9069cbd357
commit 272163ea70
  1. 8
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  2. 45
      app/src/main/java/io/legado/app/help/http/OkHttpHelper.kt
  3. 78
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@ -4,9 +4,7 @@ import android.net.Uri
import android.util.Base64 import android.util.Base64
import androidx.annotation.Keep import androidx.annotation.Keep
import io.legado.app.constant.AppConst.dateFormat import io.legado.app.constant.AppConst.dateFormat
import io.legado.app.help.http.CookieStore import io.legado.app.help.http.*
import io.legado.app.help.http.SSLHelper
import io.legado.app.help.http.StrResponse
import io.legado.app.model.Debug import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.QueryTTF import io.legado.app.model.analyzeRule.QueryTTF
@ -16,8 +14,6 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.jsoup.Connection import org.jsoup.Connection
import org.jsoup.Jsoup import org.jsoup.Jsoup
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toByteArray
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
import java.net.URLEncoder import java.net.URLEncoder
@ -292,7 +288,7 @@ interface JsExtensions {
str.isAbsUrl() -> runBlocking { str.isAbsUrl() -> runBlocking {
var x = CacheManager.getByteArray(key) var x = CacheManager.getByteArray(key)
if (x == null) { if (x == null) {
x = RxHttp.get(str).toByteArray().await() x = okHttpClient.newCall { url(str) }.bytes()
x.let { x.let {
CacheManager.put(key, it) CacheManager.put(key, it)
} }

@ -6,6 +6,9 @@ import io.legado.app.utils.EncodingDetect
import io.legado.app.utils.UTF8BOMFighter import io.legado.app.utils.UTF8BOMFighter
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.* import okhttp3.*
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException import java.io.IOException
import java.net.InetSocketAddress import java.net.InetSocketAddress
import java.net.Proxy import java.net.Proxy
@ -105,6 +108,17 @@ suspend fun OkHttpClient.newCall(builder: Request.Builder.() -> Unit): ResponseB
return response.body!! return response.body!!
} }
suspend fun OkHttpClient.newCallStrResponse(builder: Request.Builder.() -> Unit): StrResponse {
val requestBuilder = Request.Builder()
requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent)
requestBuilder.apply(builder)
val response = this.newCall(requestBuilder.build()).await()
if (!response.isSuccessful) {
throw IOException("服务器没有响应。")
}
return StrResponse(response, response.body!!.text())
}
suspend fun Call.await(): Response = suspendCancellableCoroutine { block -> suspend fun Call.await(): Response = suspendCancellableCoroutine { block ->
block.invokeOnCancellation { block.invokeOnCancellation {
@ -140,3 +154,34 @@ fun ResponseBody.text(encode: String? = null): String {
charsetName = EncodingDetect.getHtmlEncode(responseBytes) charsetName = EncodingDetect.getHtmlEncode(responseBytes)
return String(responseBytes, Charset.forName(charsetName)) return String(responseBytes, Charset.forName(charsetName))
} }
fun Request.Builder.get(url: String, queryMap: Map<String, String>, encoded: Boolean = false) {
val httpBuilder = url.toHttpUrl().newBuilder()
queryMap.forEach {
if (encoded) {
httpBuilder.addEncodedQueryParameter(it.key, it.value)
} else {
httpBuilder.addQueryParameter(it.key, it.value)
}
}
url(httpBuilder.build())
}
fun Request.Builder.postForm(form: Map<String, String>, encoded: Boolean = false) {
val formBody = FormBody.Builder()
form.forEach {
if (encoded) {
formBody.addEncoded(it.key, it.value)
} else {
formBody.add(it.key, it.value)
}
}
post(formBody.build())
}
fun Request.Builder.postJson(json: String?) {
json?.let {
val requestBody = json.toRequestBody("application/json; charset=UTF-8".toMediaType())
post(requestBody)
}
}

@ -16,9 +16,6 @@ import io.legado.app.help.CacheManager
import io.legado.app.help.JsExtensions import io.legado.app.help.JsExtensions
import io.legado.app.help.http.* import io.legado.app.help.http.*
import io.legado.app.utils.* import io.legado.app.utils.*
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toByteArray
import rxhttp.wrapper.param.toStrResponse
import java.net.URLEncoder import java.net.URLEncoder
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
@ -307,60 +304,45 @@ class AnalyzeUrl(
params.tag = tag params.tag = tag
return HttpHelper.ajax(params) return HttpHelper.ajax(params)
} }
return when (method) { return getProxyClient(proxy).newCallStrResponse {
RequestMethod.POST -> { removeHeader(UA_NAME)
if (fieldMap.isNotEmpty() || body.isNullOrBlank()) { headerMap.forEach {
RxHttp.postForm(url) addHeader(it.key, it.value)
.setAssemblyEnabled(false) }
.setOkClient(getProxyClient(proxy)) when (method) {
.addAllEncoded(fieldMap) RequestMethod.POST -> {
.addAllHeader(headerMap) url(url)
.toStrResponse().await() if (fieldMap.isNotEmpty() || body.isNullOrBlank()) {
} else { postForm(fieldMap, true)
RxHttp.postJson(url) } else {
.setAssemblyEnabled(false) postJson(body)
.setOkClient(getProxyClient(proxy)) }
.addAll(body)
.addAllHeader(headerMap)
.toStrResponse().await()
} }
else -> get(url, fieldMap, true)
} }
else -> RxHttp.get(url)
.setAssemblyEnabled(false)
.setOkClient(getProxyClient(proxy))
.addAllEncoded(fieldMap)
.addAllHeader(headerMap)
.toStrResponse().await()
} }
} }
suspend fun getByteArray(tag: String? = null): ByteArray { suspend fun getByteArray(tag: String? = null): ByteArray {
setCookie(tag) setCookie(tag)
return when (method) { @Suppress("BlockingMethodInNonBlockingContext")
RequestMethod.POST -> { return getProxyClient(proxy).newCall {
if (fieldMap.isNotEmpty() || body.isNullOrBlank()) { removeHeader(UA_NAME)
RxHttp.postForm(url) headerMap.forEach {
.setAssemblyEnabled(false) addHeader(it.key, it.value)
.setOkClient(getProxyClient(proxy)) }
.addAllEncoded(fieldMap) when (method) {
.addAllHeader(headerMap) RequestMethod.POST -> {
.toByteArray().await() url(url)
} else { if (fieldMap.isNotEmpty() || body.isNullOrBlank()) {
RxHttp.postJson(url) postForm(fieldMap, true)
.setAssemblyEnabled(false) } else {
.setOkClient(getProxyClient(proxy)) postJson(body)
.addAll(body) }
.addAllHeader(headerMap)
.toByteArray().await()
} }
else -> get(url, fieldMap, true)
} }
else -> RxHttp.get(url) }.bytes()
.setAssemblyEnabled(false)
.setOkClient(getProxyClient(proxy))
.addAllEncoded(fieldMap)
.addAllHeader(headerMap)
.toByteArray().await()
}
} }
private fun setCookie(tag: String?) { private fun setCookie(tag: String?) {

Loading…
Cancel
Save