|
@@ -14,7 +14,6 @@ import okhttp3.CacheControl
|
|
import okhttp3.Call
|
|
import okhttp3.Call
|
|
import okhttp3.Interceptor
|
|
import okhttp3.Interceptor
|
|
import okhttp3.Response
|
|
import okhttp3.Response
|
|
-import okhttp3.internal.cache.CacheRequest
|
|
|
|
import okhttp3.internal.discard
|
|
import okhttp3.internal.discard
|
|
import okhttp3.internal.http.ExchangeCodec
|
|
import okhttp3.internal.http.ExchangeCodec
|
|
import okhttp3.internal.http.RealResponseBody
|
|
import okhttp3.internal.http.RealResponseBody
|
|
@@ -47,17 +46,17 @@ object NetOkHttpInterceptor : Interceptor {
|
|
when (request.tagOf<CacheMode>()) {
|
|
when (request.tagOf<CacheMode>()) {
|
|
CacheMode.READ -> cache.get(request) ?: throw NoCacheException(request)
|
|
CacheMode.READ -> cache.get(request) ?: throw NoCacheException(request)
|
|
CacheMode.READ_THEN_REQUEST -> cache.get(request) ?: chain.proceed(request).run {
|
|
CacheMode.READ_THEN_REQUEST -> cache.get(request) ?: chain.proceed(request).run {
|
|
- cacheWritingResponse(cache.put(this), this)
|
|
|
|
|
|
+ cacheWritingResponse(cache, this)
|
|
}
|
|
}
|
|
CacheMode.REQUEST_THEN_READ -> try {
|
|
CacheMode.REQUEST_THEN_READ -> try {
|
|
chain.proceed(request).run {
|
|
chain.proceed(request).run {
|
|
- cacheWritingResponse(cache.put(this), this)
|
|
|
|
|
|
+ cacheWritingResponse(cache, this)
|
|
}
|
|
}
|
|
} catch (e: Exception) {
|
|
} catch (e: Exception) {
|
|
cache.get(request) ?: throw NoCacheException(request)
|
|
cache.get(request) ?: throw NoCacheException(request)
|
|
}
|
|
}
|
|
CacheMode.WRITE -> chain.proceed(request).run {
|
|
CacheMode.WRITE -> chain.proceed(request).run {
|
|
- cacheWritingResponse(cache.put(this), this)
|
|
|
|
|
|
+ cacheWritingResponse(cache, this)
|
|
}
|
|
}
|
|
else -> chain.proceed(request)
|
|
else -> chain.proceed(request)
|
|
}
|
|
}
|
|
@@ -104,9 +103,10 @@ object NetOkHttpInterceptor : Interceptor {
|
|
|
|
|
|
/** 缓存网络响应 */
|
|
/** 缓存网络响应 */
|
|
@Throws(IOException::class)
|
|
@Throws(IOException::class)
|
|
- private fun cacheWritingResponse(cacheRequest: CacheRequest?, response: Response): Response {
|
|
|
|
|
|
+ private fun cacheWritingResponse(cache: ForceCache, response: Response): Response {
|
|
// Some apps return a null body; for compatibility we treat that like a null cache request.
|
|
// Some apps return a null body; for compatibility we treat that like a null cache request.
|
|
- if (cacheRequest == null) return response
|
|
|
|
|
|
+ if (!response.isSuccessful) return response
|
|
|
|
+ val cacheRequest = cache.put(response) ?: return response
|
|
val cacheBodyUnbuffered = cacheRequest.body()
|
|
val cacheBodyUnbuffered = cacheRequest.body()
|
|
|
|
|
|
val source = response.body!!.source()
|
|
val source = response.body!!.source()
|