scoop:scoop.http.asyncio:0.1.0
src/http1_body.scoop
Selected production source · src/http1_body.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.platform.io.UnexpectedEofException6
import scoop.platform.memory.NativeByteBuffer8
/**9
* The single-pass body of an incoming HTTP/1 message. Completing it establishes framing completion10
* and makes trailers available; abandoning it can prevent connection reuse.11
*/12
public class AsyncHttp1Body internal constructor(13
private val state: H1State,14
internal val body: H1Input15
)16
: AsyncByteSource, AsyncCloseable {17
/**18
* Whether the incoming message body has been fully consumed.19
*/20
public val isComplete: Boolean get() = state.lock.withLock { body.life.completed }21
/**22
* The complete trailer collection after body completion, or None before completion.23
*/24
public val trailers: Array<HttpField>? get() = state.lock.withLock { body.trailers }26
/**27
* Reads available body bytes into the writable region. A positive-size read returns zero at28
* the completed body boundary.29
*/30
public override suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long {31
httpJob(state.loop, true)32
val completed =33
state.lock.withLock {34
if (body.life.disposed)35
throw IoClosedException()36
httpCount(count, buffer.writableBytes)37
body.life.completed38
}39
if (completed)40
return 0L41
val operation =42
beginH1Operation<Unit>(43
state,44
true,45
{46
if (body.life.disposed)47
throw IoClosedException()48
if (49
!body.life.completed &&50
(!present(state.incoming) || state.incoming!! !== body)51
)52
throw IllegalStateException(Some("this is not the active HTTP/1 body"))53
httpCount(count, buffer.writableBytes)54
},55
body.readIdle56
)57
val progress =58
try {59
operation.job.ensureActive()60
if (count == 0L)61
0L62
else63
readH1Body(state, body, buffer, count)64
} catch (error: Throwable) {65
failH1Operation(operation, error)66
}67
endH1Operation(operation)68
if (count != 0L)69
closeAfterH1Input(state, body)70
return progress71
}73
/**74
* Closes the body; abandoning an incomplete body closes the HTTP/1 connection instead of75
* silently consuming arbitrary input.76
*/77
public override suspend fun close() {78
httpJob(state.loop, false)79
val incomplete =80
state.lock.withLock {81
if (body.life.disposed)82
false83
else {84
body.life.disposed = true;85
!body.life.completed86
}87
}88
if (incomplete)89
closeH1(state)90
}91
}93
private suspend fun readH1Body(94
state: H1State,95
body: H1Input,96
buffer: NativeByteBuffer,97
count: Long98
): Long {99
while (true) {100
if (body.life.completed)101
return 0L102
when (body.bytes) {103
case Some(val bytes) -> {104
val available = bytes.size - body.offset105
val copied =106
if (count < available)107
count108
else109
available110
buffer.writeBytes(bytes, body.offset, copied)111
body.offset += copied112
if (body.offset == bytes.size) {113
body.bytes = None;114
body.offset = 0L;115
advanceH1End(state, body)116
}117
if (copied != 0L)118
return copied119
}120
case None -> {121
val next = nextH1Event(state)122
if (!present(next))123
throw UnexpectedEofException()124
when (next!!) {125
case Data(val bytes) -> body.bytes = Some(bytes)126
case Trailers(val fields) -> state.lock.withLock {127
body.trailers = Some(fields)128
}129
case End(val keepAlive) -> {130
val listener =131
state.lock.withLock {132
body.keepAlive = keepAlive133
body.life.completed = true134
resumeH1AfterRejection(state, body)135
state.incoming = None136
completeH1BodyLocked(body.life)137
}138
if (present(listener))139
listener!!.dispose()140
}141
else -> throw IllegalStateException(Some("unexpected event in HTTP/1 body"))142
}143
}144
}145
}146
}