|
|
@ -2,7 +2,7 @@ package io.legado.app.help.http.cronet |
|
|
|
|
|
|
|
|
|
|
|
import io.legado.app.help.http.okHttpClient |
|
|
|
import io.legado.app.help.http.okHttpClient |
|
|
|
import io.legado.app.utils.DebugLog |
|
|
|
import io.legado.app.utils.DebugLog |
|
|
|
import io.legado.app.utils.rethrowAsIOException |
|
|
|
import io.legado.app.utils.asIOException |
|
|
|
import okhttp3.* |
|
|
|
import okhttp3.* |
|
|
|
import okhttp3.EventListener |
|
|
|
import okhttp3.EventListener |
|
|
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull |
|
|
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull |
|
|
@ -27,12 +27,24 @@ abstract class AbsCallBack( |
|
|
|
|
|
|
|
|
|
|
|
var mResponse: Response |
|
|
|
var mResponse: Response |
|
|
|
|
|
|
|
|
|
|
|
var mException: IOException? = null |
|
|
|
|
|
|
|
private var followCount = 0 |
|
|
|
private var followCount = 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Throws(IOException::class) |
|
|
|
abstract fun waitForDone(urlRequest: UrlRequest): Response |
|
|
|
abstract fun waitForDone(urlRequest: UrlRequest): Response |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 当发生错误时,通知子类终止阻塞抛出错误 |
|
|
|
|
|
|
|
* @param error |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
abstract fun onError(error: IOException) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 请求成功后,通知子类结束阻塞,返回response |
|
|
|
|
|
|
|
* @param response |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
abstract fun onSuccess(response: Response) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onRedirectReceived( |
|
|
|
override fun onRedirectReceived( |
|
|
|
request: UrlRequest, |
|
|
|
request: UrlRequest, |
|
|
@ -41,10 +53,13 @@ abstract class AbsCallBack( |
|
|
|
) { |
|
|
|
) { |
|
|
|
if (followCount > MAX_FOLLOW_COUNT) { |
|
|
|
if (followCount > MAX_FOLLOW_COUNT) { |
|
|
|
request.cancel() |
|
|
|
request.cancel() |
|
|
|
mException = IOException("Too many redirect") |
|
|
|
onError(IOException("Too many redirect")) |
|
|
|
|
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
if (mCall.isCanceled()) { |
|
|
|
if (mCall.isCanceled()) { |
|
|
|
mException = IOException("Request Canceled") |
|
|
|
onError(IOException("Request Canceled")) |
|
|
|
|
|
|
|
request.cancel() |
|
|
|
|
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
followCount += 1 |
|
|
|
followCount += 1 |
|
|
|
val client = okHttpClient |
|
|
|
val client = okHttpClient |
|
|
@ -55,7 +70,7 @@ abstract class AbsCallBack( |
|
|
|
} else if (okHttpClient.followRedirects) { |
|
|
|
} else if (okHttpClient.followRedirects) { |
|
|
|
request.followRedirect() |
|
|
|
request.followRedirect() |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
mException = IOException("Too many redirect") |
|
|
|
onError(IOException("Too many redirect")) |
|
|
|
request.cancel() |
|
|
|
request.cancel() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -73,7 +88,7 @@ abstract class AbsCallBack( |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Throws(Exception::class) |
|
|
|
@Throws(IOException::class) |
|
|
|
override fun onReadCompleted( |
|
|
|
override fun onReadCompleted( |
|
|
|
request: UrlRequest, |
|
|
|
request: UrlRequest, |
|
|
|
info: UrlResponseInfo, |
|
|
|
info: UrlResponseInfo, |
|
|
@ -83,7 +98,7 @@ abstract class AbsCallBack( |
|
|
|
|
|
|
|
|
|
|
|
if (mCall.isCanceled()) { |
|
|
|
if (mCall.isCanceled()) { |
|
|
|
request.cancel() |
|
|
|
request.cancel() |
|
|
|
mException = IOException("Request Canceled") |
|
|
|
onError(IOException("Request Canceled")) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
byteBuffer.flip() |
|
|
|
byteBuffer.flip() |
|
|
@ -91,9 +106,9 @@ abstract class AbsCallBack( |
|
|
|
try { |
|
|
|
try { |
|
|
|
buffer.write(byteBuffer) |
|
|
|
buffer.write(byteBuffer) |
|
|
|
} catch (e: IOException) { |
|
|
|
} catch (e: IOException) { |
|
|
|
DebugLog.i(javaClass.name, "IOException during ByteBuffer read. Details: ", e) |
|
|
|
DebugLog.e(javaClass.name, "IOException during ByteBuffer read. Details: ", e) |
|
|
|
mException = IOException("IOException during ByteBuffer read. Details:", e) |
|
|
|
onError(IOException("IOException during ByteBuffer read. Details:", e)) |
|
|
|
throw e |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
byteBuffer.clear() |
|
|
|
byteBuffer.clear() |
|
|
|
request.read(byteBuffer) |
|
|
|
request.read(byteBuffer) |
|
|
@ -108,7 +123,8 @@ abstract class AbsCallBack( |
|
|
|
buffer.asResponseBody(contentType) |
|
|
|
buffer.asResponseBody(contentType) |
|
|
|
val newRequest = originalRequest.newBuilder().url(info.url).build() |
|
|
|
val newRequest = originalRequest.newBuilder().url(info.url).build() |
|
|
|
this.mResponse = this.mResponse.newBuilder().body(responseBody).request(newRequest).build() |
|
|
|
this.mResponse = this.mResponse.newBuilder().body(responseBody).request(newRequest).build() |
|
|
|
DebugLog.i(javaClass.simpleName, "end[${info.negotiatedProtocol}]${info.url}") |
|
|
|
onSuccess(this.mResponse) |
|
|
|
|
|
|
|
//DebugLog.i(javaClass.simpleName, "end[${info.negotiatedProtocol}]${info.url}") |
|
|
|
|
|
|
|
|
|
|
|
eventListener?.callEnd(mCall) |
|
|
|
eventListener?.callEnd(mCall) |
|
|
|
if (responseCallback != null) { |
|
|
|
if (responseCallback != null) { |
|
|
@ -123,8 +139,8 @@ abstract class AbsCallBack( |
|
|
|
|
|
|
|
|
|
|
|
//UrlResponseInfo可能为null |
|
|
|
//UrlResponseInfo可能为null |
|
|
|
override fun onFailed(request: UrlRequest, info: UrlResponseInfo?, error: CronetException) { |
|
|
|
override fun onFailed(request: UrlRequest, info: UrlResponseInfo?, error: CronetException) { |
|
|
|
DebugLog.i(javaClass.name, error.message.toString()) |
|
|
|
DebugLog.e(javaClass.name, error.message.toString()) |
|
|
|
mException = error.rethrowAsIOException() |
|
|
|
onError(error.asIOException()) |
|
|
|
this.eventListener?.callFailed(mCall, error) |
|
|
|
this.eventListener?.callFailed(mCall, error) |
|
|
|
responseCallback?.onFailure(mCall, error) |
|
|
|
responseCallback?.onFailure(mCall, error) |
|
|
|
} |
|
|
|
} |
|
|
@ -132,7 +148,7 @@ abstract class AbsCallBack( |
|
|
|
override fun onCanceled(request: UrlRequest?, info: UrlResponseInfo?) { |
|
|
|
override fun onCanceled(request: UrlRequest?, info: UrlResponseInfo?) { |
|
|
|
super.onCanceled(request, info) |
|
|
|
super.onCanceled(request, info) |
|
|
|
this.eventListener?.callEnd(mCall) |
|
|
|
this.eventListener?.callEnd(mCall) |
|
|
|
mException = IOException("Cronet Request Canceled") |
|
|
|
onError(IOException("Cronet Request Canceled")) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|