pull/45/head
kunfei 5 years ago
parent 8b89e95e48
commit 26eff62778
  1. 64
      app/src/main/java/io/legado/app/help/http/AjaxWebView.kt
  2. 31
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  3. 5
      app/src/main/java/io/legado/app/help/http/RequestMethod.kt
  4. 25
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@ -1,7 +1,6 @@
package io.legado.app.help.http package io.legado.app.help.http
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context
import android.net.http.SslError import android.net.http.SslError
import android.os.Build import android.os.Build
import android.os.Handler import android.os.Handler
@ -9,6 +8,7 @@ import android.os.Looper
import android.os.Message import android.os.Message
import android.text.TextUtils import android.text.TextUtils
import android.webkit.* import android.webkit.*
import io.legado.app.App
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.* import java.util.*
@ -49,7 +49,7 @@ class AjaxWebView {
@SuppressLint("SetJavaScriptEnabled", "JavascriptInterface") @SuppressLint("SetJavaScriptEnabled", "JavascriptInterface")
fun createAjaxWebView(params: AjaxParams, handler: Handler): WebView { fun createAjaxWebView(params: AjaxParams, handler: Handler): WebView {
val webView = WebView(params.context.applicationContext) val webView = WebView(App.INSTANCE)
val settings = webView.settings val settings = webView.settings
settings.javaScriptEnabled = true settings.javaScriptEnabled = true
settings.domStorageEnabled = true settings.domStorageEnabled = true
@ -63,7 +63,7 @@ class AjaxWebView {
} }
when (params.requestMethod) { when (params.requestMethod) {
RequestMethod.POST -> webView.postUrl(params.url, params.postData) RequestMethod.POST -> webView.postUrl(params.url, params.postData)
RequestMethod.GET, RequestMethod.DEFAULT -> webView.loadUrl( RequestMethod.GET -> webView.loadUrl(
params.url, params.url,
params.headerMap params.headerMap
) )
@ -75,29 +75,24 @@ class AjaxWebView {
mWebView?.destroy() mWebView?.destroy()
mWebView = null mWebView = null
} }
} }
fun ajax(params: AjaxParams) { fun load(params: AjaxParams) {
if (params.audioSuffix != "") {
mHandler.obtainMessage(MSG_SNIFF_START, params)
.sendToTarget()
} else {
mHandler.obtainMessage(MSG_AJAX_START, params) mHandler.obtainMessage(MSG_AJAX_START, params)
.sendToTarget() .sendToTarget()
} }
fun sniff(params: AjaxParams) {
mHandler.obtainMessage(MSG_SNIFF_START, params)
.sendToTarget()
} }
fun destroyWebView() { fun destroyWebView() {
mHandler.obtainMessage(DESTROY_WEB_VIEW) mHandler.obtainMessage(DESTROY_WEB_VIEW)
} }
class AjaxParams(val context: Context, private val tag: String) { class AjaxParams(private val tag: String) {
var requestMethod: RequestMethod? = null var requestMethod = RequestMethod.GET
get() {
return field ?: RequestMethod.DEFAULT
}
var url: String? = null var url: String? = null
var postData: ByteArray? = null var postData: ByteArray? = null
var headerMap: Map<String, String>? = null var headerMap: Map<String, String>? = null
@ -112,41 +107,6 @@ class AjaxWebView {
val isSniff: Boolean val isSniff: Boolean
get() = !TextUtils.isEmpty(audioSuffix) get() = !TextUtils.isEmpty(audioSuffix)
fun requestMethod(method: RequestMethod): AjaxParams {
this.requestMethod = method
return this
}
fun url(url: String): AjaxParams {
this.url = url
return this
}
fun postData(postData: ByteArray): AjaxParams {
this.postData = postData
return this
}
fun headerMap(headerMap: Map<String, String>): AjaxParams {
this.headerMap = headerMap
return this
}
fun cookieStore(cookieStore: CookieStore): AjaxParams {
this.cookieStore = cookieStore
return this
}
fun suffix(suffix: String): AjaxParams {
this.audioSuffix = suffix
return this
}
fun javaScript(javaScript: String): AjaxParams {
this.javaScript = javaScript
return this
}
fun setCookie(url: String) { fun setCookie(url: String) {
if (cookieStore != null) { if (cookieStore != null) {
val cookie = CookieManager.getInstance().getCookie(url) val cookie = CookieManager.getInstance().getCookie(url)
@ -300,10 +260,6 @@ class AjaxWebView {
const val DESTROY_WEB_VIEW = 4 const val DESTROY_WEB_VIEW = 4
} }
enum class RequestMethod {
GET, POST, DEFAULT
}
interface CookieStore { interface CookieStore {
fun setCookie(url: String, cookie: String) fun setCookie(url: String, cookie: String)

@ -77,43 +77,22 @@ object HttpHelper {
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
suspend fun ajax(params: AjaxWebView.AjaxParams): String = suspend fun ajax(params: AjaxWebView.AjaxParams): String =
suspendCancellableCoroutine { block -> suspendCancellableCoroutine { block ->
val ajaxWebView = AjaxWebView() val webView = AjaxWebView()
ajaxWebView.callback = object : AjaxWebView.Callback() { webView.callback = object : AjaxWebView.Callback() {
override fun onResult(result: String) { override fun onResult(result: String) {
block.resume(result) { block.resume(result) {
ajaxWebView.destroyWebView() webView.destroyWebView()
} }
} }
override fun onError(error: Throwable) { override fun onError(error: Throwable) {
block.resume(error.localizedMessage) { block.resume(error.localizedMessage) {
ajaxWebView.destroyWebView() webView.destroyWebView()
} }
} }
} }
ajaxWebView.ajax(params) webView.load(params)
}
@ExperimentalCoroutinesApi
suspend fun sniff(params: AjaxWebView.AjaxParams): String =
suspendCancellableCoroutine { block ->
val ajaxWebView = AjaxWebView()
ajaxWebView.callback = object : AjaxWebView.Callback() {
override fun onResult(result: String) {
block.resume(result) {
ajaxWebView.destroyWebView()
}
}
override fun onError(error: Throwable) {
block.resume(error.localizedMessage) {
ajaxWebView.destroyWebView()
}
}
}
ajaxWebView.sniff(params)
} }
} }

@ -0,0 +1,5 @@
package io.legado.app.help.http
enum class RequestMethod {
GET, POST
}

@ -10,7 +10,9 @@ import io.legado.app.data.api.IHttpGetApi
import io.legado.app.data.api.IHttpPostApi import io.legado.app.data.api.IHttpPostApi
import io.legado.app.data.entities.BaseBook import io.legado.app.data.entities.BaseBook
import io.legado.app.help.JsExtensions import io.legado.app.help.JsExtensions
import io.legado.app.help.http.AjaxWebView
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import io.legado.app.help.http.RequestMethod
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.Deferred import kotlinx.coroutines.Deferred
import okhttp3.FormBody import okhttp3.FormBody
@ -55,7 +57,7 @@ class AnalyzeUrl(
private var charset: String? = null private var charset: String? = null
private var bodyTxt: String? = null private var bodyTxt: String? = null
private var body: RequestBody? = null private var body: RequestBody? = null
private var method = Method.GET private var method = RequestMethod.GET
private var webViewJs: String? = null private var webViewJs: String? = null
val postData: ByteArray val postData: ByteArray
@ -173,7 +175,7 @@ class AnalyzeUrl(
if (urlArray.size > 1) { if (urlArray.size > 1) {
val options = GSON.fromJsonObject<Map<String, String>>(urlArray[1]) val options = GSON.fromJsonObject<Map<String, String>>(urlArray[1])
options?.let { options?.let {
options["method"]?.let { if (it.equals("POST", true)) method = Method.POST } options["method"]?.let { if (it.equals("POST", true)) method = RequestMethod.POST }
options["headers"]?.let { headers -> options["headers"]?.let { headers ->
GSON.fromJsonObject<Map<String, String>>(headers)?.let { headerMap.putAll(it) } GSON.fromJsonObject<Map<String, String>>(headers)?.let { headerMap.putAll(it) }
} }
@ -183,14 +185,14 @@ class AnalyzeUrl(
} }
} }
when (method) { when (method) {
Method.GET -> { RequestMethod.GET -> {
urlArray = url.split("?") urlArray = url.split("?")
url = urlArray[0] url = urlArray[0]
if (urlArray.size > 1) { if (urlArray.size > 1) {
analyzeFields(urlArray[1]) analyzeFields(urlArray[1])
} }
} }
Method.POST -> { RequestMethod.POST -> {
bodyTxt?.let { bodyTxt?.let {
if (it.isJson()) { if (it.isJson()) {
body = it.toRequestBody(jsonType) body = it.toRequestBody(jsonType)
@ -250,14 +252,10 @@ class AnalyzeUrl(
return SCRIPT_ENGINE.eval(jsStr, bindings) return SCRIPT_ENGINE.eval(jsStr, bindings)
} }
enum class Method {
GET, POST
}
@Throws(Exception::class) @Throws(Exception::class)
fun getResponse(): Call<String> { fun getResponse(): Call<String> {
return when { return when {
method == Method.POST -> { method == RequestMethod.POST -> {
if (fieldMap.isNotEmpty()) { if (fieldMap.isNotEmpty()) {
HttpHelper HttpHelper
.getApiService<IHttpPostApi>(baseUrl) .getApiService<IHttpPostApi>(baseUrl)
@ -280,7 +278,7 @@ class AnalyzeUrl(
@Throws(Exception::class) @Throws(Exception::class)
fun getResponseAsync(): Deferred<Response<String>> { fun getResponseAsync(): Deferred<Response<String>> {
return when { return when {
method == Method.POST -> { method == RequestMethod.POST -> {
if (fieldMap.isNotEmpty()) { if (fieldMap.isNotEmpty()) {
HttpHelper HttpHelper
.getApiService<IHttpPostApi>(baseUrl) .getApiService<IHttpPostApi>(baseUrl)
@ -299,4 +297,11 @@ class AnalyzeUrl(
.getMapAsync(url, fieldMap, headerMap) .getMapAsync(url, fieldMap, headerMap)
} }
} }
suspend fun getResultByWebView(tag: String): String {
val params = AjaxWebView.AjaxParams(tag)
params.url = url
params.requestMethod = method
return HttpHelper.ajax(params)
}
} }

Loading…
Cancel
Save