scoop:scoop.http.asyncio:0.1.0
src/http1_input.scoop
Selected production source · src/http1_input.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.platform.io.UnexpectedEofException6
import scoop.platform.time.MonotonicTimeSource7
import scoop.time.Duration9
internal suspend fun nextH1Event(state: H1State): Http1Event? {10
while (true) {11
val(ready, ended) =12
state.lock.withLock {13
val event = state.events.removeFirstOrNone()14
if (present(event))15
return@withLock (event, false)16
h1Open(state)17
if (state.decoder.isSwitched)18
throw IllegalStateException(Some("HTTP/1 transport has switched protocols"))19
(event, state.inputEnded)20
}21
if (present(ready))22
return ready23
if (ended)24
return None25
val received: Long? =26
if (state.input.readableBytes == 0L) {27
state.input.clear()28
val requested = state.input.writableBytes29
beginH1Io(state, true)30
val count = state.transport.readInto(state.input, requested)31
httpProgress(count, requested)32
progressH1Io(state, true, count > 0L)33
Some(count)34
} else35
None36
val next =37
state.lock.withLock {38
h1Open(state)39
if (present(received)) {40
if (received!! > 0L && state.captureTimings)41
state.inputReceivedAt = Some(MonotonicTimeSource.markNow())42
if (received!! == 0L) {43
state.inputEnded = true;44
state.decoder.endInput()45
}46
}47
if (state.input.readableBytes != 0L) {48
if (state.captureTimings) {49
val received = state.decoder.receiveWithProgress(state.input)50
for (_ in 0L ..< received.startedMessages)51
state.responseStarts!!.addLast(state.inputReceivedAt!!)52
} else53
state.decoder.receive(state.input)54
}55
while (true) {56
val event = state.decoder.nextEvent()57
if (!present(event))58
break59
state.events.addLast(event!!)60
}61
state.events.removeFirstOrNone()62
}63
if (present(next))64
return next65
}66
}68
internal fun advanceH1End(state: H1State, body: H1Input) {69
val listener = state.lock.withLock { advanceH1EndLocked(state, body) }70
if (present(listener))71
listener!!.dispose()72
}74
private fun advanceH1EndLocked(state: H1State, body: H1Input): Disposable? {75
if (present(body.bytes))76
return None77
while (state.events.size != 0L) {78
when (state.events.firstOrNone()!!) {79
case Trailers(val fields) -> {80
state.events.removeFirst()81
body.trailers = Some(fields)82
}83
case End(val keepAlive) -> {84
state.events.removeFirst()85
body.keepAlive = keepAlive86
body.life.completed = true87
resumeH1AfterRejection(state, body)88
state.incoming = None89
return completeH1BodyLocked(body.life)90
}91
else -> return None92
}93
}94
return None95
}97
internal fun resumeH1AfterRejection(state: H1State, body: H1Input) {98
if (99
!body.responseFinished ||100
!body.life.completed ||101
!state.decoder.isSwitched ||102
state.encoder.isSwitched103
)104
return105
state.decoder.resumeHttp1()106
if (state.events.size != 0L) {107
val switched =108
when (state.events.firstOrNone()!!) {109
case Switch -> true;110
else -> false111
}112
if (switched)113
state.events.removeFirst()114
}115
}117
private fun requireH1Head(state: H1State, role: HttpRole) {118
if (state.decoder.role != role)119
throw IllegalStateException(Some("wrong HTTP/1 receive role"))120
if (present(state.incoming))121
throw IllegalStateException(Some("previous HTTP/1 body is not complete"))122
if (role == HttpRole.Client && state.clientPending == 0L)123
throw IllegalStateException(Some("no HTTP/1 request is awaiting a response"))124
}126
private fun incomingH1(127
state: H1State,128
readIdle: Duration?,129
finalResponse: Boolean = false130
): H1Input {131
val body = H1Input(finalResponse, readIdle)132
val listener =133
state.lock.withLock {134
state.incoming = Some(body)135
advanceH1EndLocked(state, body)136
}137
if (present(listener))138
listener!!.dispose()139
if (!body.life.completed)140
listenH1Body(state, body.life, httpJob(state.loop, true))141
return body142
}144
internal suspend fun receiveH1Request(state: H1State, readIdle: Duration?): Http1IncomingRequest? {145
val operation =146
beginH1Operation<Unit>(state, true, { requireH1Head(state, HttpRole.Server) }, readIdle)147
val result =148
try {149
operation.job.ensureActive()150
val next = nextH1Event(state)151
if (!present(next))152
None153
else154
when (next!!) {155
case Request(val head) -> {156
val body = incomingH1(state, readIdle)157
state.lock.withLock {158
state.requests.addLast(H1RequestContext(head.method, body))159
}160
Some(Http1IncomingRequest(head, AsyncHttp1Body(state, body)))161
}162
else -> throw IllegalStateException(Some("expected an HTTP/1 request head"))163
}164
} catch (error: Throwable) {165
failH1Operation(operation, error)166
}167
endH1Operation(operation)168
return result169
}171
internal suspend fun receiveH1Response(state: H1State, readIdle: Duration?): Http1IncomingResponse {172
val operation =173
beginH1Operation<Unit>(state, true, { requireH1Head(state, HttpRole.Client) }, readIdle)174
val result =175
try {176
operation.job.ensureActive()177
val next = nextH1Event(state)178
if (!present(next))179
throw UnexpectedEofException()180
when (next!!) {181
case Response(val head) -> {182
if (head.status >= 200U || head.status == 101U)183
state.lock.withLock { state.clientPending -= 1L }184
val firstByteAt =185
state.lock.withLock {186
if (state.captureTimings)187
Some(state.responseStarts!!.removeFirst())188
else189
None190
}191
Http1IncomingResponse(192
head,193
AsyncHttp1Body(194
state,195
incomingH1(state, readIdle, head.status >= 200U || head.status == 101U)196
),197
firstByteAt198
)199
}200
else -> throw IllegalStateException(Some("expected an HTTP/1 response head"))201
}202
} catch (error: Throwable) {203
failH1Operation(operation, error)204
}205
endH1Operation(operation)206
return result207
}209
internal suspend fun closeAfterH1Input(state: H1State, body: H1Input) {210
val close = state.lock.withLock { closeAfterH1InputLocked(state, body) }211
if (close)212
closeH1(state)213
}215
internal fun closeAfterH1InputLocked(state: H1State, body: H1Input): Boolean {216
if (state.closed || !body.life.completed || state.decoder.isSwitched)217
return false218
if (state.decoder.role == HttpRole.Client) {219
if (body.finalResponse && (!body.keepAlive || state.encoder.mustClose))220
state.closeAfterOutput = true221
return state.closeAfterOutput && !present(state.outgoing)222
}223
return body.responseFinished && !body.keepAlive224
}