mirror of
https://github.com/amir1376/ab-download-manager.git
synced 2025-02-20 11:43:24 +08:00
some servers send wrong content-range headers now handled correctly
This commit is contained in:
parent
bc46e32328
commit
93ff08bc7e
@ -22,12 +22,16 @@ data class ResponseInfo(
|
||||
responseHeaders["content-length"]?.toLongOrNull()?.takeIf { it >= 0L }
|
||||
}
|
||||
|
||||
val contentRange by lazy {
|
||||
getContentRange()
|
||||
}
|
||||
|
||||
//total length of whole file even if it is partial content
|
||||
val totalLength by lazy {
|
||||
val responseLength = contentLength ?: return@lazy null
|
||||
// partial length only valid when we have content-length header
|
||||
if (isPartial) {
|
||||
getContentRange()?.fullSize
|
||||
contentRange?.fullSize ?: responseLength
|
||||
} else responseLength
|
||||
}
|
||||
val requiresAuth by lazy {
|
||||
@ -43,8 +47,8 @@ data class ResponseInfo(
|
||||
}
|
||||
|
||||
val resumeSupport by lazy {
|
||||
// maybe server does not give us content-length, so we ignore resume support
|
||||
isPartial && contentLength != null
|
||||
// maybe server does not give us content-length or content-range, so we ignore resume support
|
||||
isPartial && contentLength != null && contentRange?.fullSize!=null
|
||||
}
|
||||
|
||||
val fileName: String? by lazy {
|
||||
|
@ -7,37 +7,42 @@ data class ContentRangeValue(
|
||||
val fullSize: Long?,
|
||||
)
|
||||
|
||||
fun ResponseInfo.getContentRange(): ContentRangeValue?{
|
||||
val value=responseHeaders["content-range"]?:return null
|
||||
val actualValue=runCatching {
|
||||
fun ResponseInfo.getContentRange(): ContentRangeValue? {
|
||||
val value = responseHeaders["content-range"] ?: return null
|
||||
val actualValue = runCatching {
|
||||
value.substring("bytes ".length)
|
||||
}.getOrNull()?:return null
|
||||
if (actualValue.isBlank()){
|
||||
}.getOrNull() ?: return null
|
||||
if (actualValue.isBlank()) {
|
||||
return null
|
||||
}
|
||||
var from:Long?=null
|
||||
var to:Long?=null
|
||||
var size:Long?=null
|
||||
val (rangeString,sizeString)=actualValue.split("/")
|
||||
if (rangeString!="*"){
|
||||
rangeString.split("-").map {
|
||||
it.toLong()
|
||||
}.let {
|
||||
from=it[0]
|
||||
to=it[1]
|
||||
|
||||
val (rangeString, sizeString) = actualValue
|
||||
.split("/")
|
||||
.takeIf { it.size >= 2 } ?: return null
|
||||
|
||||
val range = try {
|
||||
if (rangeString != "*") {
|
||||
rangeString.split("-").map {
|
||||
it.toLong()
|
||||
}.let {
|
||||
it[0]..it[1]
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
if (sizeString!="*"){
|
||||
size=sizeString.toLong()
|
||||
} catch (e: Exception) {
|
||||
// NumberFormatException or IndexOutOfBoundException
|
||||
return null
|
||||
}
|
||||
|
||||
val size: Long? = if (sizeString != "*") {
|
||||
// some servers not returning * nor integer value.
|
||||
sizeString.toLongOrNull() ?: return null
|
||||
} else null
|
||||
|
||||
return ContentRangeValue(
|
||||
range = from?.let {f->
|
||||
to?.let {t->
|
||||
f..t
|
||||
}
|
||||
},
|
||||
fullSize=size
|
||||
range = range,
|
||||
fullSize = size,
|
||||
)
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user