Scoop API

scoop:scoop.http.client:0.1.0

src/response/input.scoop

Selected production source · src/response/input.scoop

1package scoop.http.client
3import scoop.asyncio.*
4import scoop.http.HttpResponseHead
5import scoop.platform.memory.NativeByteBuffer
7internal class ClientResponseInput(val call: ClientCall, val plan: ClientResponseDecoding,
8 head: HttpResponseHead) : AsyncByteSource, AsyncCloseable {
9 private val closed: AtomicBoolean = AtomicBoolean(false)
10 private val codec: ClientResponseCodec = ClientResponseCodec(call, plan, head)
11 private var reading: Boolean = false
12 private var readCompletion: Continuation<Unit>? = None
13 val isComplete: Boolean get() = call.lock.withLock { call.readDone }
15 public override suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long {
16 if (closed.load()) throw IoClosedException()
17 if (count < 0L) throw IllegalArgumentException(Some("negative HTTP response read count"))
18 if (count > buffer.writableBytes) throw IndexOutOfBoundsException()
19 checkClientDeadlines(call)
20 val failure = observeClientCallFailure(call)
21 if (clientPresent(failure)) throw failure!!
22 if (isComplete || count == 0L) return 0L
23 val scope = call.lock.withLock {
24 if (call.ending || call.completed) throw (call.failure ?: IoClosedException())
25 val saved = call.scope ?: throw IoClosedException()
26 reading = true
27 saved
28 }
29 val job = scope.job
30 var failed: Throwable? = None
31 val result = try {
32 val result = context(job) { receiveClientBytes(call, codec, buffer, count, false) }
33 checkClientDeadlines(call)
34 val expired = observeClientCallFailure(call)
35 if (clientPresent(expired)) throw expired!!
36 if (result > 0L && clientPresent(call.observation)) context(job) { emitClientEvent(call, HttpCallEventKind.ResponseProgress) }
37 if (result == 0L || (plan.formats.size == 0L && call.responseStream!!.isReadComplete)) {
38 context(job) { commitClientCache(call) }
39 requireClientCall(call)
40 call.lock.withLock { call.readDone = true }
41 wakeClientCall(call)
42 }
43 result
44 } catch (error: Throwable) {
45 failClientCall(call, error)
46 failed = Some(error)
47 0L
48 } finally { finishRead() }
49 if (clientPresent(failed)) {
50 awaitClientResponseClose(call)
51 throw (observeClientCallFailure(call) ?: failed!!)
52 }
53 return result
54 }
56 suspend fun closeCodecs() { codec.close() }
58 suspend fun awaitReadCompletion() {
59 suspendCoroutine<Unit> { continuation ->
60 val done = call.lock.withLock { if (reading) { readCompletion = Some(continuation); false } else true }
61 if (done) continuation.resume(Unit)
62 }
63 }
65 private fun finishRead() {
66 val waiter = call.lock.withLock {
67 reading = false
68 val saved = readCompletion; readCompletion = None; saved
69 }
70 waiter?.resume(Unit)
71 }
73 public override suspend fun close() {
74 checkClientObservedResponseClose(call)
75 if (call.lock.withLock { call.completed }) {
76 if (closed.compareAndSet(false, true)) reportClientResponseCloseFailure(call)
77 return
78 }
79 clientJob(call.client.loop, false)
80 closed.store(true)
81 call.lock.withLock { call.abandoned = true; call.ending = true }
82 cancelClientCall(call, CancellationException(), true)
83 awaitClientResponseClose(call)
84 reportClientResponseCloseFailure(call)
85 }
88private fun reportClientResponseCloseFailure(call: ClientCall) {
89 observeClosingJobCancellation(call)
90 val failure = if (call.lock.withLock { call.failureObserved }) unobservedClientCleanup(call)
91 else clientCallFailure(call)
92 if (clientPresent(failure)) throw failure!!
95private fun observeClosingJobCancellation(call: ClientCall) {
96 val job = currentJobOrNone() ?: return
97 val cancellation = try { job.ensureActive(); return } catch (error: CancellationException) { error }
98 call.lock.withLock {
99 val primary = call.failure
100 if (clientPresent(primary) && primary!! === cancellation) call.failureObserved = true
101 Unit
102 }
105private suspend fun awaitClientResponseClose(call: ClientCall) {
106 suspendCoroutine<Unit> { continuation ->
107 val done = call.lock.withLock {
108 if (call.completed) true else { call.closeWaiters.add(continuation); false }
109 }
110 if (done) continuation.resume(Unit)
111 }