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

@ -77,43 +77,22 @@ object HttpHelper {
@ExperimentalCoroutinesApi
suspend fun ajax(params: AjaxWebView.AjaxParams): String =
suspendCancellableCoroutine { block ->
val ajaxWebView = AjaxWebView()
ajaxWebView.callback = object : AjaxWebView.Callback() {
val webView = AjaxWebView()
webView.callback = object : AjaxWebView.Callback() {
override fun onResult(result: String) {
block.resume(result) {
ajaxWebView.destroyWebView()
webView.destroyWebView()
}
}
override fun onError(error: Throwable) {
block.resume(error.localizedMessage) {
ajaxWebView.destroyWebView()
webView.destroyWebView()
}
}
}
ajaxWebView.ajax(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)
webView.load(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.entities.BaseBook
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.RequestMethod
import io.legado.app.utils.*
import kotlinx.coroutines.Deferred
import okhttp3.FormBody
@ -55,7 +57,7 @@ class AnalyzeUrl(
private var charset: String? = null
private var bodyTxt: String? = null
private var body: RequestBody? = null
private var method = Method.GET
private var method = RequestMethod.GET
private var webViewJs: String? = null
val postData: ByteArray
@ -173,7 +175,7 @@ class AnalyzeUrl(
if (urlArray.size > 1) {
val options = GSON.fromJsonObject<Map<String, String>>(urlArray[1])
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 ->
GSON.fromJsonObject<Map<String, String>>(headers)?.let { headerMap.putAll(it) }
}
@ -183,14 +185,14 @@ class AnalyzeUrl(
}
}
when (method) {
Method.GET -> {
RequestMethod.GET -> {
urlArray = url.split("?")
url = urlArray[0]
if (urlArray.size > 1) {
analyzeFields(urlArray[1])
}
}
Method.POST -> {
RequestMethod.POST -> {
bodyTxt?.let {
if (it.isJson()) {
body = it.toRequestBody(jsonType)
@ -250,14 +252,10 @@ class AnalyzeUrl(
return SCRIPT_ENGINE.eval(jsStr, bindings)
}
enum class Method {
GET, POST
}
@Throws(Exception::class)
fun getResponse(): Call<String> {
return when {
method == Method.POST -> {
method == RequestMethod.POST -> {
if (fieldMap.isNotEmpty()) {
HttpHelper
.getApiService<IHttpPostApi>(baseUrl)
@ -280,7 +278,7 @@ class AnalyzeUrl(
@Throws(Exception::class)
fun getResponseAsync(): Deferred<Response<String>> {
return when {
method == Method.POST -> {
method == RequestMethod.POST -> {
if (fieldMap.isNotEmpty()) {
HttpHelper
.getApiService<IHttpPostApi>(baseUrl)
@ -299,4 +297,11 @@ class AnalyzeUrl(
.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