Merge pull request #1854 from ag2s20150909/master

Cronet support cookieJar
pull/1857/head
kunfei 3 years ago committed by GitHub
commit 541806977f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 37
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  2. 42
      app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt

@ -4,13 +4,7 @@ import io.legado.app.constant.AppConst
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.http.cronet.CronetInterceptor import io.legado.app.help.http.cronet.CronetInterceptor
import io.legado.app.help.http.cronet.CronetLoader import io.legado.app.help.http.cronet.CronetLoader
import okhttp3.ConnectionSpec import okhttp3.*
import okhttp3.Credentials
import okhttp3.CookieJar
import okhttp3.Cookie
import okhttp3.HttpUrl
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import java.net.InetSocketAddress import java.net.InetSocketAddress
import java.net.Proxy import java.net.Proxy
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
@ -20,6 +14,22 @@ private val proxyClientCache: ConcurrentHashMap<String, OkHttpClient> by lazy {
ConcurrentHashMap() ConcurrentHashMap()
} }
val cookieJar by lazy {
object : CookieJar {
override fun loadForRequest(url: HttpUrl): List<Cookie> {
return emptyList()
}
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
cookies.forEach {
CookieStore.replaceCookie(url.toString(), "${it.name}=${it.value}")
}
}
}
}
val okHttpClient: OkHttpClient by lazy { val okHttpClient: OkHttpClient by lazy {
val specs = arrayListOf( val specs = arrayListOf(
ConnectionSpec.MODERN_TLS, ConnectionSpec.MODERN_TLS,
@ -32,16 +42,7 @@ val okHttpClient: OkHttpClient by lazy {
.writeTimeout(15, TimeUnit.SECONDS) .writeTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS)
.callTimeout(60, TimeUnit.SECONDS) .callTimeout(60, TimeUnit.SECONDS)
.cookieJar(object : CookieJar { .cookieJar(cookieJar = cookieJar)
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
cookies.forEach {
CookieStore.replaceCookie(url.toString(), "${it.name}=${it.value}")
}
}
override fun loadForRequest(url: HttpUrl): List<Cookie> {
return ArrayList<Cookie>()
}
})
.sslSocketFactory(SSLHelper.unsafeSSLSocketFactory, SSLHelper.unsafeTrustManager) .sslSocketFactory(SSLHelper.unsafeSSLSocketFactory, SSLHelper.unsafeTrustManager)
.retryOnConnectionFailure(true) .retryOnConnectionFailure(true)
.hostnameVerifier(SSLHelper.unsafeHostnameVerifier) .hostnameVerifier(SSLHelper.unsafeHostnameVerifier)
@ -60,7 +61,7 @@ val okHttpClient: OkHttpClient by lazy {
chain.proceed(builder.build()) chain.proceed(builder.build())
}) })
if (!AppConfig.isGooglePlay && AppConfig.isCronet && CronetLoader.install()) { if (!AppConfig.isGooglePlay && AppConfig.isCronet && CronetLoader.install()) {
builder.addInterceptor(CronetInterceptor(null)) builder.addInterceptor(CronetInterceptor(cookieJar = cookieJar))
} }
builder.build() builder.build()
} }

@ -1,12 +1,12 @@
package io.legado.app.help.http.cronet package io.legado.app.help.http.cronet
import android.os.Build import android.os.Build
import io.legado.app.help.http.CookieStore
import io.legado.app.utils.printOnDebug import io.legado.app.utils.printOnDebug
import okhttp3.* import okhttp3.*
import okhttp3.internal.http.receiveHeaders
class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor { class CronetInterceptor(private val cookieJar: CookieJar = CookieJar.NO_COOKIES) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response { override fun intercept(chain: Interceptor.Chain): Response {
val original: Request = chain.request() val original: Request = chain.request()
@ -18,15 +18,18 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor {
//移除Keep-Alive,手动设置会导致400 BadRequest //移除Keep-Alive,手动设置会导致400 BadRequest
builder.removeHeader("Keep-Alive") builder.removeHeader("Keep-Alive")
builder.removeHeader("Accept-Encoding") builder.removeHeader("Accept-Encoding")
val cookieStr = getCookie(original.url) if (cookieJar != CookieJar.NO_COOKIES) {
//设置Cookie val cookieStr = getCookie(original.url)
if (cookieStr.length > 3) { //设置Cookie
builder.header("Cookie", cookieStr) if (cookieStr.length > 3) {
builder.addHeader("Cookie", cookieStr)
}
} }
val new = builder.build()
proceedWithCronet(new, chain.call())?.let { response -> val newReq = builder.build()
proceedWithCronet(newReq, chain.call())?.let { response ->
//从Response 中保存Cookie到CookieJar //从Response 中保存Cookie到CookieJar
cookieJar?.saveFromResponse(new.url, Cookie.parseAll(new.url, response.headers)) cookieJar.receiveHeaders(newReq.url, response.headers)
response response
} ?: chain.proceed(original) } ?: chain.proceed(original)
} catch (e: Exception) { } catch (e: Exception) {
@ -54,21 +57,14 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor {
return null return null
} }
private fun getCookie(url: HttpUrl): String {
val sb = StringBuilder() /** Returns a 'Cookie' HTTP request header with all cookies, like `a=b; c=d`. */
//处理从 Cookiejar 获取到的Cookies private fun getCookie(url: HttpUrl): String = buildString {
if (cookieJar != null) { val cookies = cookieJar.loadForRequest(url)
val cookies = cookieJar.loadForRequest(url) cookies.forEachIndexed { index, cookie ->
for (cookie in cookies) { if (index > 0) append("; ")
sb.append(cookie.name).append("=").append(cookie.value).append("; ") append(cookie.name).append('=').append(cookie.value)
}
}
//处理自定义的Cookie
val cookie = CookieStore.getCookie(url.toString())
if (cookie.length > 3) {
sb.append(cookie)
} }
return sb.toString()
} }
} }

Loading…
Cancel
Save