修复因大小写引发的user agent重复的bug

pull/1814/head
kunfei 3 years ago
parent 4dd58c1e98
commit 8edc4b0282
  1. 15
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  2. 7
      app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt

@ -1,5 +1,6 @@
package io.legado.app.help.http package io.legado.app.help.http
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
@ -36,12 +37,14 @@ val okHttpClient: OkHttpClient by lazy {
.followSslRedirects(true) .followSslRedirects(true)
.addInterceptor(Interceptor { chain -> .addInterceptor(Interceptor { chain ->
val request = chain.request() val request = chain.request()
.newBuilder() val builder = request.newBuilder()
.addHeader("Keep-Alive", "300") if (request.header(AppConst.UA_NAME) == null) {
.addHeader("Connection", "Keep-Alive") builder.addHeader(AppConst.UA_NAME, AppConfig.userAgent)
.addHeader("Cache-Control", "no-cache") }
.build() builder.addHeader("Keep-Alive", "300")
chain.proceed(request) builder.addHeader("Connection", "Keep-Alive")
builder.addHeader("Cache-Control", "no-cache")
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(null))

@ -1,7 +1,5 @@
package io.legado.app.help.http package io.legado.app.help.http
import io.legado.app.constant.AppConst
import io.legado.app.help.config.AppConfig
import io.legado.app.utils.EncodingDetect import io.legado.app.utils.EncodingDetect
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.Utf8BomUtils import io.legado.app.utils.Utf8BomUtils
@ -25,7 +23,6 @@ suspend fun OkHttpClient.newCallResponse(
): Response { ): Response {
return withContext(IO) { return withContext(IO) {
val requestBuilder = Request.Builder() val requestBuilder = Request.Builder()
requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent)
requestBuilder.apply(builder) requestBuilder.apply(builder)
var response: Response? = null var response: Response? = null
for (i in 0..retry) { for (i in 0..retry) {
@ -94,10 +91,6 @@ fun ResponseBody.text(encode: String? = null): String {
fun Request.Builder.addHeaders(headers: Map<String, String>) { fun Request.Builder.addHeaders(headers: Map<String, String>) {
headers.forEach { headers.forEach {
if (it.key == AppConst.UA_NAME) {
//防止userAgent重复
removeHeader(AppConst.UA_NAME)
}
addHeader(it.key, it.value) addHeader(it.key, it.value)
} }
} }

Loading…
Cancel
Save