diff --git a/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt b/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt index c14423c8b..b57409671 100644 --- a/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt +++ b/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt @@ -160,7 +160,7 @@ class AjaxWebView { if (it.isNotEmpty() && it != "null") { val content = StringEscapeUtils.unescapeJson(it) .replace("^\"|\"$".toRegex(), "") - handler.obtainMessage(MSG_SUCCESS, StrResponse.success(content, url)) + handler.obtainMessage(MSG_SUCCESS, StrResponse(url, content)) .sendToTarget() handler.removeCallbacks(this) return@evaluateJavascript @@ -188,7 +188,7 @@ class AjaxWebView { if (url.matches(it.toRegex())) { handler.obtainMessage( MSG_SUCCESS, - StrResponse.success(url, view.url ?: params.url) + StrResponse(view.url ?: params.url, url) ).sendToTarget() } } diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index 0b83c0616..31e8dfecf 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -111,7 +111,7 @@ object HttpHelper { override fun onError(error: Throwable) { if (!block.isCompleted) - block.resume(StrResponse.success(error.msg, params.url)) + block.resume(StrResponse(params.url, error.msg)) } } webView.load(params) diff --git a/app/src/main/java/io/legado/app/help/http/StrResponse.kt b/app/src/main/java/io/legado/app/help/http/StrResponse.kt index 19255c47e..c7ccad9a1 100644 --- a/app/src/main/java/io/legado/app/help/http/StrResponse.kt +++ b/app/src/main/java/io/legado/app/help/http/StrResponse.kt @@ -2,17 +2,36 @@ package io.legado.app.help.http import okhttp3.* import okhttp3.Response.Builder -import java.util.* /** * An HTTP response. */ @Suppress("unused", "MemberVisibilityCanBePrivate") -class StrResponse private constructor( - val rawResponse: Response, - val body: String?, - val errorBody: ResponseBody? -) { +class StrResponse { + var rawResponse: Response + var body: String? = null + var errorBody: ResponseBody? = null + + constructor(rawResponse: Response, body: String?) { + this.rawResponse = rawResponse + this.body = body + } + + constructor(url: String, body: String?) { + rawResponse = Builder() + .code(200) + .message("OK") + .protocol(Protocol.HTTP_1_1) + .request(Request.Builder().url(url).build()) + .build() + this.body = body + } + + constructor(rawResponse: Response, errorBody: ResponseBody?) { + this.rawResponse = rawResponse + this.errorBody = errorBody + } + fun raw() = rawResponse fun url(): String { @@ -44,79 +63,4 @@ class StrResponse private constructor( return rawResponse.toString() } - companion object { - fun success(code: Int, body: String): StrResponse { - require(!(code < 200 || code >= 300)) { "code < 200 or >= 300: $code" } - return success( - body, - Builder() // - .code(code) - .message("Response.success()") - .protocol(Protocol.HTTP_1_1) - .request(Request.Builder().url("http://localhost/").build()) - .build() - ) - } - - fun success(body: String, headers: Headers): StrResponse { - return success( - body, - Builder() // - .code(200) - .message("OK") - .protocol(Protocol.HTTP_1_1) - .headers(headers) - .request(Request.Builder().url("http://localhost/").build()) - .build() - ) - } - - fun success(body: String, url: String): StrResponse { - return success( - body, - Builder() // - .code(200) - .message("OK") - .protocol(Protocol.HTTP_1_1) - .request(Request.Builder().url(url).build()) - .build() - ) - } - - @JvmOverloads - fun success( - body: String?, rawResponse: Response = - Builder() // - .code(200) - .message("OK") - .protocol(Protocol.HTTP_1_1) - .request(Request.Builder().url("http://localhost/").build()) - .build() - ): StrResponse { - Objects.requireNonNull(rawResponse, "rawResponse == null") - require(rawResponse.isSuccessful) { "rawResponse must be successful response" } - return StrResponse(rawResponse, body, null) - } - - fun error(code: Int, body: ResponseBody?): StrResponse { - Objects.requireNonNull(body, "body == null") - require(code >= 400) { "code < 400: $code" } - return error( - body, - Builder() // - .code(code) - .message("Response.error()") - .protocol(Protocol.HTTP_1_1) - .request(Request.Builder().url("http://localhost/").build()) - .build() - ) - } - - fun error(body: ResponseBody?, rawResponse: Response): StrResponse { - Objects.requireNonNull(body, "body == null") - Objects.requireNonNull(rawResponse, "rawResponse == null") - require(!rawResponse.isSuccessful) { "rawResponse should not be successful response" } - return StrResponse(rawResponse, null, body) - } - } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt b/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt index 53ead9d26..9868b9dc8 100644 --- a/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt +++ b/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt @@ -9,11 +9,12 @@ import rxhttp.wrapper.exception.HttpStatusCodeException import java.nio.charset.Charset @Parser(name = "StrResponse") -class StrResponseParser(val encode: String? = null) : rxhttp.wrapper.parse.Parser { +class StrResponseParser(private val encode: String? = null) : + rxhttp.wrapper.parse.Parser { override fun onParse(response: Response): StrResponse { val body = getString(response) - return StrResponse.success(body, response) + return StrResponse(response, body) } private fun getString(response: Response): String { 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 00b2e4b95..9991a49f5 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 @@ -280,7 +280,7 @@ class AnalyzeUrl( sourceRegex: String? = null, ): StrResponse { if (type != null) { - return StrResponse.success(StringUtils.byteToHexString(getByteArray(tag)), url) + return StrResponse(url, StringUtils.byteToHexString(getByteArray(tag))) } setCookie(tag) if (useWebView) {