scoop:scoop.http.asyncio:0.1.0
src/http2_body.scoop
Selected production source · src/http2_body.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.IoClosedException4
import scoop.collections.toMutableList5
import scoop.platform.memory.NativeByteBuffer7
internal suspend fun h2BodyRequest(state: H2State, stream: H2Stream, kind: H2BodyKind,8
buffer: NativeByteBuffer?, count: Long, prepare: () -> Unit): Long {9
val job = httpJob(state.loop, true)10
return suspendCoroutine<Long> { continuation ->11
val actions = ArrayList<() -> Unit>()12
val operation = state.lock.withLock {13
job.ensureActive()14
if (present(stream.failure)) throw stream.failure!!15
if (stream.disposed) throw IoClosedException()16
val reading = kind == H2BodyKind.Read17
if (if (reading) present(stream.reader) else present(stream.writer))18
throw IllegalStateException(Some("HTTP/2 stream direction is busy"))19
if (present(buffer)) httpCount(count, if (reading) buffer!!.writableBytes else buffer!!.readableBytes)20
if (!reading || !stream.closed || !stream.inputEnded) h2Open(state)21
if (kind == H2BodyKind.Write && (!stream.sendStarted || stream.sendEnded))22
throw IllegalStateException(Some("HTTP/2 output body is not open"))23
prepare()24
val wait = H2Wait<Long>(job, continuation)25
val entry = H2BodyOperation(kind, wait, buffer, count)26
if (kind != H2BodyKind.Flush && count == 0L) succeedH2Wait(wait, 0L, actions)27
else if (reading) {28
val ready = readH2Available(state, stream, buffer!!, count)29
if (present(ready)) {30
succeedH2Wait(wait, ready!!, actions)31
retireH2Stream(state, stream, actions)32
} else stream.reader = Some(entry)33
} else if (kind == H2BodyKind.Flush && stream.closed && !state.outputBusy && !state.outputDirty &&34
state.output.readableBytes == 0L && !state.session.hasPendingOutput(stream.id)) {35
succeedH2Wait(wait, 0L, actions)36
} else {37
state.streams[stream.id] = stream38
stream.writer = Some(entry)39
}40
entry41
}42
h2WaitListener(state, operation.wait) { error, ready -> resetH2Stream(state, stream, error, 8U, ready) }43
for (action in actions) action()44
if (kind == H2BodyKind.Flush || count != 0L) driveH2(state)45
}46
}48
internal fun readH2Available(state: H2State, stream: H2Stream, buffer: NativeByteBuffer, count: Long): Long? {49
if (stream.data.size == 0L) return if (stream.inputEnded) Some(0L) else None50
val bytes = stream.data.firstOrNone()!!51
val available = bytes.size - stream.offset52
val copied = if (count < available) count else available53
buffer.writeBytes(bytes, stream.offset, copied)54
stream.offset += copied55
if (stream.offset == bytes.size) { stream.data.removeFirst(); stream.offset = 0L }56
if (!state.closed) state.session.consume(stream.id, copied)57
return Some(copied)58
}60
internal fun finishH2Body(state: H2State, stream: H2Stream, entry: H2BodyOperation,61
count: Long, failure: Throwable?, actions: ArrayList<() -> Unit>) {62
if (entry.kind == H2BodyKind.Read) stream.reader = None else stream.writer = None63
entry.buffer = None64
if (present(failure)) failH2Wait(entry.wait, failure!!, actions) else succeedH2Wait(entry.wait, count, actions)65
retireH2Stream(state, stream, actions)66
}68
internal fun finishH2Operations(state: H2State, actions: ArrayList<() -> Unit>): Boolean {69
var changed = finishH2Accept(state, actions)70
for (stream in state.streams.values.toMutableList()) {71
if (present(stream.responseWaiter) && stream.responses.size != 0L) {72
val wait = stream.responseWaiter!!73
stream.responseWaiter = None74
val head = stream.responses.removeFirst()75
stream.finalResponseTaken = head.status >= 200U76
succeedH2Wait(wait, head, actions)77
changed = true78
}79
if (present(stream.reader)) {80
val entry = stream.reader!!81
val count = readH2Available(state, stream, entry.buffer!!, entry.count)82
if (present(count)) { finishH2Body(state, stream, entry, count!!, None, actions); changed = true }83
}84
if (present(stream.writer) && stream.writer!!.kind == H2BodyKind.Write) {85
val entry = stream.writer!!86
try {87
val count = state.session.writeBody(stream.id, entry.buffer!!, entry.count)88
if (count != 0L) { finishH2Body(state, stream, entry, count, None, actions); changed = true }89
} catch (error: Throwable) { finishH2Body(state, stream, entry, 0L, Some(error), actions); changed = true }90
}91
retireH2Stream(state, stream, actions)92
}93
return changed94
}