scoop:scoop.http.client:0.1.0
src/resume/storage.scoop
Selected production source · src/resume/storage.scoop
1
package scoop.http.client3
import scoop.asyncio.*4
import scoop.platform.fs.Path5
import scoop.platform.memory.NativeByteBuffer6
import scoop.platform.system.PlatformErrorKind7
import scoop.platform.system.PlatformException9
internal suspend fun loadClientResumeRecord(options: HttpResumeOptions): ClientResumeRecord? {10
try {11
return currentEventLoop().openFile(options.metadataFile).useAsync suspend {12
file -> Some(decodeClientResumeRecord(file.readAllBytes(options.maxMetadataBytes)))13
}14
} catch (error: PlatformException) {15
if (error.kind == PlatformErrorKind.NotFound)16
return None17
throw error18
} catch (error: ReadLimitException) {19
resumeMetadataError("resume metadata exceeds its limit", Some(error))20
}21
}23
internal suspend fun saveClientResumeRecord(24
bytes: Array<Byte>,25
options: HttpResumeOptions,26
sync: Boolean27
) {28
val loop = currentEventLoop()29
val temporary =30
loop.createTempFile(31
Some(options.metadataFile.parent ?: Path.fromString(".")),32
"pending-http-resume-"33
)34
var failed: Throwable? = None35
try {36
NativeByteBuffer.allocate(bytes.size).use suspend {37
buffer ->38
buffer.writeBytes(bytes);39
temporary.file.writeFully(buffer)40
}41
if (sync)42
temporary.file.syncAll()43
temporary.close()44
ensureActive()45
nonCancellableScope<Unit> { loop.moveFile(temporary.path, options.metadataFile, true) }46
} catch (error: Throwable) {47
failed = Some(error)48
}49
finally {50
try {51
closeClientResumeTemporary(temporary)52
}53
catch (error: Throwable) {54
failed = appendClientCleanup(failed, error)55
}56
}57
if (clientPresent(failed))58
throw failed!!59
}61
private suspend fun closeClientResumeTemporary(temporary: TemporaryFile) {62
nonCancellableScope<Unit> {63
var failed: Throwable? = None64
try {65
temporary.close()66
} catch (error: Throwable) {67
failed = Some(error)68
}69
try {70
deleteClientCacheFile(temporary.path)71
}72
catch (error: Throwable) {73
failed = appendClientCleanup(failed, error)74
}75
if (clientPresent(failed))76
throw failed!!77
}78
}80
internal suspend fun selectClientResumeRange(81
original: HttpRequest,82
offset: Long,83
options: HttpResumeOptions84
): ClientResumeRange? {85
if (offset == 0L)86
return None87
val record = loadClientResumeRecord(options)88
val reason: HttpResumeFailureReason? =89
when (record) {90
case None -> Some(HttpResumeFailureReason.MissingMetadata)91
case Some(val value) -> {92
if (value.originalUrl != clientCacheUrl(original.url))93
Some(HttpResumeFailureReason.RequestChanged)94
else95
if (!clientPresent(value.etag))96
Some(HttpResumeFailureReason.UnreliableValidator)97
else98
if (clientPresent(value.length) && offset > value.length!!)99
Some(HttpResumeFailureReason.InvalidRange)100
else101
None102
}103
}104
if (clientPresent(reason)) {105
if (options.mismatch == HttpResumeMismatch.Fail)106
throw HttpResumeException(107
reason!!,108
"partial download cannot be resumed with its saved metadata"109
)110
return None111
}112
return Some(ClientResumeRange(record!!, offset, offset))113
}