scoop:scoop.http.asyncio:0.1.0
src/http2_requests.scoop
Selected production source · src/http2_requests.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.IoClosedException4
import scoop.http.*6
internal suspend fun acceptH2(state: H2State): Http2IncomingRequest? {7
val job = httpJob(state.loop, true)8
return suspendCoroutine<Http2IncomingRequest?> { continuation ->9
val wait = state.lock.withLock {10
job.ensureActive(); h2Open(state)11
if (state.session.role != HttpRole.Server) throw IllegalStateException(Some("only HTTP/2 servers accept requests"))12
if (present(state.accepting)) throw IllegalStateException(Some("HTTP/2 accept is busy"))13
val result = H2Wait<Http2IncomingRequest?>(job, continuation)14
state.accepting = Some(result)15
result16
}17
h2WaitListener(state, wait) { error, actions ->18
state.accepting = None19
failH2Wait(wait, error, actions)20
}21
driveH2(state)22
}23
}25
internal suspend fun receiveH2Response(state: H2State, stream: H2Stream): HttpResponseHead {26
val job = httpJob(state.loop, true)27
return suspendCoroutine<HttpResponseHead> { continuation ->28
val actions = ArrayList<() -> Unit>()29
val waiting = state.lock.withLock {30
job.ensureActive()31
if (state.session.role != HttpRole.Client) throw IllegalStateException(Some("only HTTP/2 clients receive responses"))32
if (present(stream.failure)) throw stream.failure!!33
if (stream.disposed) throw IoClosedException()34
if (stream.finalResponseTaken) throw IllegalStateException(Some("final HTTP/2 response has already been received"))35
if (present(stream.responseWaiter)) throw IllegalStateException(Some("HTTP/2 response receive is busy"))36
val wait = H2Wait<HttpResponseHead>(job, continuation)37
if (stream.responses.size != 0L) {38
val head = stream.responses.removeFirst()39
stream.finalResponseTaken = head.status >= 200U40
succeedH2Wait(wait, head, actions)41
retireH2Stream(state, stream, actions)42
} else {43
h2StreamOpen(state, stream)44
stream.responseWaiter = Some(wait)45
}46
wait47
}48
h2WaitListener(state, waiting) { error, ready -> resetH2Stream(state, stream, error, 8U, ready) }49
for (action in actions) action()50
driveH2(state)51
}52
}54
internal fun finishH2Accept(state: H2State, actions: ArrayList<() -> Unit>): Boolean {55
val wait = state.accepting ?: return false56
if (state.requests.size == 0L && !state.acceptingEnded) return false57
state.accepting = None58
if (state.requests.size == 0L) succeedH2Wait(wait, None, actions)59
else {60
val incoming = state.requests.removeAt(0L)61
actions.add { listenH2Stream(state, incoming.stream.stream, wait.job) }62
succeedH2Wait(wait, Some(incoming), actions)63
}64
return true65
}67
internal suspend fun flushH2Connection(state: H2State, prepare: () -> Unit) {68
val job = httpJob(state.loop, true)69
suspendCoroutine<Unit> { continuation ->70
val wait = state.lock.withLock {71
job.ensureActive(); h2Open(state); prepare()72
val result = H2Wait<Unit>(job, continuation)73
state.connectionFlush.add(result)74
result75
}76
h2WaitListener(state, wait) { error, actions ->77
var index = state.connectionFlush.size78
while (index > 0L) {79
index -= 1L80
if (state.connectionFlush[index] === wait) state.connectionFlush.removeAt(index)81
}82
failH2Wait(wait, error, actions)83
}84
driveH2(state)85
}86
}