pull/1391/head
gedoor 3 years ago
parent 358b3e26f4
commit d46d35d085
  1. 22
      app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt
  2. 24
      app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt

@ -18,7 +18,7 @@ import java.util.concurrent.Executors
val executor: ExecutorService by lazy { Executors.newCachedThreadPool() } val executor: ExecutorService by lazy { Executors.newCachedThreadPool() }
val cronetEngine: ExperimentalCronetEngine by lazy { val cronetEngine: ExperimentalCronetEngine? by lazy {
if (AppConfig.isGooglePlay) { if (AppConfig.isGooglePlay) {
CronetProviderInstaller.installProvider(appCtx) CronetProviderInstaller.installProvider(appCtx)
} else { } else {
@ -35,18 +35,22 @@ val cronetEngine: ExperimentalCronetEngine by lazy {
enablePublicKeyPinningBypassForLocalTrustAnchors(true) enablePublicKeyPinningBypassForLocalTrustAnchors(true)
enableBrotli(true)//Brotli压缩 enableBrotli(true)//Brotli压缩
} }
val engine = builder.build() try {
Timber.d("Cronet Version:" + engine.versionString) val engine = builder.build()
//这会导致Jsoup的网络请求出现问题,暂时不接管系统URL Timber.d("Cronet Version:" + engine.versionString)
//URL.setURLStreamHandlerFactory(CronetURLStreamHandlerFactory(engine)) //这会导致Jsoup的网络请求出现问题,暂时不接管系统URL
return@lazy engine //URL.setURLStreamHandlerFactory(CronetURLStreamHandlerFactory(engine))
return@lazy engine
} catch (e: Exception) {
return@lazy null
}
} }
fun buildRequest(request: Request, callback: UrlRequest.Callback): UrlRequest { fun buildRequest(request: Request, callback: UrlRequest.Callback): UrlRequest? {
val url = request.url.toString() val url = request.url.toString()
val headers: Headers = request.headers val headers: Headers = request.headers
val requestBody = request.body val requestBody = request.body
return cronetEngine.newUrlRequestBuilder(url, callback, executor).apply { return cronetEngine?.newUrlRequestBuilder(url, callback, executor)?.apply {
setHttpMethod(request.method)//设置 setHttpMethod(request.method)//设置
allowDirectExecutor() allowDirectExecutor()
headers.forEachIndexed { index, _ -> headers.forEachIndexed { index, _ ->
@ -68,7 +72,7 @@ fun buildRequest(request: Request, callback: UrlRequest.Callback): UrlRequest {
} }
}.build() }?.build()
} }

@ -23,13 +23,16 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor {
builder.header("Cookie", cookieStr) builder.header("Cookie", cookieStr)
} }
val new = builder.build() val new = builder.build()
val response: Response = proceedWithCronet(new, chain.call()) proceedWithCronet(new, chain.call())?.let { response ->
//从Response 中保存Cookie到CookieJar //从Response 中保存Cookie到CookieJar
cookieJar?.saveFromResponse(new.url, Cookie.parseAll(new.url, response.headers)) cookieJar?.saveFromResponse(new.url, Cookie.parseAll(new.url, response.headers))
response response
} ?: chain.proceed(original)
} catch (e: Exception) { } catch (e: Exception) {
//遇到Cronet处理有问题时的情况,如证书过期等等,回退到okhttp处理 //遇到Cronet处理有问题时的情况,如证书过期等等,回退到okhttp处理
if (e.message.toString().contains("ERR_CERT_", true)||e.message.toString().contains("ERR_SSL_",true)) { if (e.message.toString().contains("ERR_CERT_", true)
|| e.message.toString().contains("ERR_SSL_", true)
) {
chain.proceed(original) chain.proceed(original)
} else { } else {
throw e throw e
@ -41,12 +44,13 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor {
} }
@Throws(IOException::class) @Throws(IOException::class)
private fun proceedWithCronet(request: Request, call: Call): Response { private fun proceedWithCronet(request: Request, call: Call): Response? {
val callback = CronetRequestCallback(request, call) val callback = CronetRequestCallback(request, call)
val urlRequest = buildRequest(request, callback) buildRequest(request, callback)?.let {
urlRequest.start() it.start()
return callback.waitForDone(urlRequest) return callback.waitForDone(it)
}
return null
} }
private fun getCookie(url: HttpUrl): String { private fun getCookie(url: HttpUrl): String {

Loading…
Cancel
Save