scoop:scoop.http.client:0.1.0
src/response/input.scoop
Selected production source · src/response/input.scoop
1
package scoop.http.client3
import scoop.asyncio.*4
import scoop.http.HttpResponseHead5
import scoop.platform.memory.NativeByteBuffer7
internal 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 = false12
private var readCompletion: Continuation<Unit>? = None13
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 0L23
val scope = call.lock.withLock {24
if (call.ending || call.completed) throw (call.failure ?: IoClosedException())25
val saved = call.scope ?: throw IoClosedException()26
reading = true27
saved28
}29
val job = scope.job30
var failed: Throwable? = None31
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
result44
} catch (error: Throwable) {45
failClientCall(call, error)46
failed = Some(error)47
0L48
} finally { finishRead() }49
if (clientPresent(failed)) {50
awaitClientResponseClose(call)51
throw (observeClientCallFailure(call) ?: failed!!)52
}53
return result54
}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 = false68
val saved = readCompletion; readCompletion = None; saved69
}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
return78
}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
}86
}88
private 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!!93
}95
private fun observeClosingJobCancellation(call: ClientCall) {96
val job = currentJobOrNone() ?: return97
val cancellation = try { job.ensureActive(); return } catch (error: CancellationException) { error }98
call.lock.withLock {99
val primary = call.failure100
if (clientPresent(primary) && primary!! === cancellation) call.failureObserved = true101
Unit102
}103
}105
private 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
}112
}