scoop:scoop.http.client:0.1.0
src/cache/disk_entry.scoop
Selected production source · src/cache/disk_entry.scoop
1
package scoop.http.client3
import scoop.asyncio.*4
import scoop.bytes.ByteOrder5
import scoop.http.*6
import scoop.platform.fs.Path7
import scoop.platform.io.UnexpectedEofException8
import scoop.platform.memory.NativeByteBuffer10
internal class ClientDiskCacheEntry(val path: Path, val record: HttpCacheRecord, val bodyLength: Long,11
val fileSize: Long, val trailers: Array<HttpField>) {12
var readers: Long = 0L13
var retired: Boolean = false14
}16
internal suspend fun readClientDiskCacheEntry(path: Path, maxMetadata: Long): ClientDiskCacheEntry? {17
return currentEventLoop().openFile(path).useAsync suspend { file ->18
val size = file.size()19
if (size < 16L) throw HttpCacheFormatException("truncated cache file")20
val length = NativeByteBuffer.allocate(16L).use suspend { footer ->21
file.seek(size - 16L); file.readFully(footer)22
if (footer.readUInt64(ByteOrder.BigEndian) != clientCacheFileMagic)23
throw HttpCacheFormatException("invalid cache file version")24
footer.readInt64(ByteOrder.BigEndian)25
}26
if (length < 0L || length > size - 16L)27
throw HttpCacheFormatException("invalid cache metadata length")28
if (length > maxMetadata) return@useAsync None29
val bodyLength = size - length - 16L30
val (record, trailers) = NativeByteBuffer.allocate(length).use suspend { metadata ->31
file.seek(bodyLength); file.readFully(metadata)32
decodeClientCacheMetadata(metadata.readBytes(length))33
}34
val expected = parseHttpContentLength(record.head.fields)35
if (clientPresent(expected) && expected!! != bodyLength)36
throw HttpCacheFormatException("cached body length does not match Content-Length")37
Some(ClientDiskCacheEntry(path, record, bodyLength, size, trailers))38
}39
}41
internal class ClientDiskCacheSnapshot(val owner: DiskHttpCache, val entry: ClientDiskCacheEntry, val file: AsyncFile) : HttpCacheSnapshot {42
public override val record: HttpCacheRecord get() = entry.record43
public override val bodyLength: Long get() = entry.bodyLength44
public override val trailers: Array<HttpField> get() = entry.trailers45
private val source: ClientDiskCacheSource = ClientDiskCacheSource(file, entry.bodyLength)46
private val closing: ClientCacheClosure = ClientCacheClosure()47
public override val body: AsyncByteSource get() = source48
public override suspend fun close() {49
closing.close(suspend {50
source.closed.store(true)51
var failure: Throwable? = None52
try { file.close() }53
catch (error: Throwable) { failure = Some(error) }54
try { owner.release(entry) }55
catch (error: Throwable) { failure = Some(clientCombined(failure, error)) }56
if (clientPresent(failure)) throw failure!!57
})58
}59
}61
private class ClientDiskCacheSource(val file: AsyncFile, var remaining: Long) : AsyncByteSource {62
val closed: AtomicBoolean = AtomicBoolean(false)63
private val busy: AtomicBoolean = AtomicBoolean(false)64
public override suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long {65
if (closed.load()) throw IoClosedException()66
if (count < 0L) throw IllegalArgumentException(Some("negative cache read count"))67
if (count > buffer.writableBytes) throw IndexOutOfBoundsException()68
if (!busy.compareAndSet(false, true)) throw IllegalStateException(Some("cache read is active"))69
try {70
if (remaining == 0L || count == 0L) return 0L71
val before = buffer.writePosition72
val amount = try { file.readInto(buffer, if (count < remaining) count else remaining) }73
finally { remaining -= buffer.writePosition - before }74
if (amount == 0L) throw UnexpectedEofException()75
return amount76
} finally { busy.store(false) }77
}78
}