Fix response closing on mirror switch
This commit is contained in:
@@ -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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user