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

@ -6,6 +6,9 @@ import io.legado.app.utils.EncodingDetect
import io.legado.app.utils.UTF8BOMFighter
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.*
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Proxy
@ -105,6 +108,17 @@ suspend fun OkHttpClient.newCall(builder: Request.Builder.() -> Unit): ResponseB
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 ->
block.invokeOnCancellation {
@ -139,4 +153,35 @@ fun ResponseBody.text(encode: String? = null): String {
//根据内容判断
charsetName = EncodingDetect.getHtmlEncode(responseBytes)
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.http.*
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.util.*
import java.util.regex.Pattern
@ -307,60 +304,45 @@ class AnalyzeUrl(
params.tag = tag
return HttpHelper.ajax(params)
}
return when (method) {
RequestMethod.POST -> {
if (fieldMap.isNotEmpty() || body.isNullOrBlank()) {
RxHttp.postForm(url)
.setAssemblyEnabled(false)
.setOkClient(getProxyClient(proxy))
.addAllEncoded(fieldMap)
.addAllHeader(headerMap)
.toStrResponse().await()
} else {
RxHttp.postJson(url)
.setAssemblyEnabled(false)
.setOkClient(getProxyClient(proxy))
.addAll(body)
.addAllHeader(headerMap)
.toStrResponse().await()
return getProxyClient(proxy).newCallStrResponse {
removeHeader(UA_NAME)
headerMap.forEach {
addHeader(it.key, it.value)
}
when (method) {
RequestMethod.POST -> {
url(url)
if (fieldMap.isNotEmpty() || body.isNullOrBlank()) {
postForm(fieldMap, true)
} else {
postJson(body)
}
}
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 {
setCookie(tag)
return when (method) {
RequestMethod.POST -> {
if (fieldMap.isNotEmpty() || body.isNullOrBlank()) {
RxHttp.postForm(url)
.setAssemblyEnabled(false)
.setOkClient(getProxyClient(proxy))
.addAllEncoded(fieldMap)
.addAllHeader(headerMap)
.toByteArray().await()
} else {
RxHttp.postJson(url)
.setAssemblyEnabled(false)
.setOkClient(getProxyClient(proxy))
.addAll(body)
.addAllHeader(headerMap)
.toByteArray().await()
@Suppress("BlockingMethodInNonBlockingContext")
return getProxyClient(proxy).newCall {
removeHeader(UA_NAME)
headerMap.forEach {
addHeader(it.key, it.value)
}
when (method) {
RequestMethod.POST -> {
url(url)
if (fieldMap.isNotEmpty() || body.isNullOrBlank()) {
postForm(fieldMap, true)
} else {
postJson(body)
}
}
else -> get(url, fieldMap, true)
}
else -> RxHttp.get(url)
.setAssemblyEnabled(false)
.setOkClient(getProxyClient(proxy))
.addAllEncoded(fieldMap)
.addAllHeader(headerMap)
.toByteArray().await()
}
}.bytes()
}
private fun setCookie(tag: String?) {

Loading…
Cancel
Save