Scoop API

scoop:scoop.http.client:0.1.0

src/cache/disk_entry.scoop

Selected production source · src/cache/disk_entry.scoop

1package scoop.http.client
3import scoop.asyncio.*
4import scoop.bytes.ByteOrder
5import scoop.http.*
6import scoop.platform.fs.Path
7import scoop.platform.io.UnexpectedEofException
8import scoop.platform.memory.NativeByteBuffer
10internal class ClientDiskCacheEntry(val path: Path, val record: HttpCacheRecord, val bodyLength: Long,
11 val fileSize: Long, val trailers: Array<HttpField>) {
12 var readers: Long = 0L
13 var retired: Boolean = false
16internal 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 None
29 val bodyLength = size - length - 16L
30 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 }
41internal class ClientDiskCacheSnapshot(val owner: DiskHttpCache, val entry: ClientDiskCacheEntry, val file: AsyncFile) : HttpCacheSnapshot {
42 public override val record: HttpCacheRecord get() = entry.record
43 public override val bodyLength: Long get() = entry.bodyLength
44 public override val trailers: Array<HttpField> get() = entry.trailers
45 private val source: ClientDiskCacheSource = ClientDiskCacheSource(file, entry.bodyLength)
46 private val closing: ClientCacheClosure = ClientCacheClosure()
47 public override val body: AsyncByteSource get() = source
48 public override suspend fun close() {
49 closing.close(suspend {
50 source.closed.store(true)
51 var failure: Throwable? = None
52 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 }
61private 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 0L
71 val before = buffer.writePosition
72 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 amount
76 } finally { busy.store(false) }
77 }