Fix response closing on mirror switch

pull/343/head
Koitharu 3 years ago
parent ac8283d4af
commit 648ee3c763
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -4,6 +4,8 @@ import dagger.Lazy
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import okhttp3.ResponseBody
import okhttp3.ResponseBody.Companion.toResponseBody
import okhttp3.internal.canParseAsIpAddress import okhttp3.internal.canParseAsIpAddress
import okhttp3.internal.closeQuietly import okhttp3.internal.closeQuietly
import okhttp3.internal.publicsuffix.PublicSuffixDatabase import okhttp3.internal.publicsuffix.PublicSuffixDatabase
@ -23,9 +25,11 @@ class MirrorSwitchInterceptor @Inject constructor(
return try { return try {
val response = chain.proceed(request) val response = chain.proceed(request)
if (response.isFailed) { if (response.isFailed) {
val responseCopy = response.copy()
response.closeQuietly()
trySwitchMirror(request, chain)?.also { trySwitchMirror(request, chain)?.also {
response.close() responseCopy.closeQuietly()
} ?: response } ?: responseCopy
} else { } else {
response response
} }
@ -85,4 +89,14 @@ class MirrorSwitchInterceptor @Inject constructor(
val domain = PublicSuffixDatabase.get().getEffectiveTldPlusOne(host) ?: return null val domain = PublicSuffixDatabase.get().getEffectiveTldPlusOne(host) ?: return null
return host.removeSuffix(domain) + newDomain return host.removeSuffix(domain) + newDomain
} }
private fun Response.copy(): Response {
return newBuilder()
.body(body?.copy())
.build()
}
private fun ResponseBody.copy(): ResponseBody {
return source().readByteArray().toResponseBody(contentType())
}
} }

Loading…
Cancel
Save