scoop:scoop.http.asyncio:0.1.0
src/http1_transfer.scoop
Selected production source · src/http1_transfer.scoop
1
package scoop.http.asyncio3
import scoop.platform.io.WriteZeroException4
import scoop.platform.memory.NativeByteBuffer5
import scoop.http.Http1BodyChunk7
internal suspend fun writeH1Wire(state: H1State, bytes: Array<Byte>, flush: Boolean = true) {8
var offset = 0L9
while (offset < bytes.size) {10
state.output.clear()11
val count =12
if (bytes.size - offset < state.output.writableBytes)13
bytes.size - offset14
else15
state.output.writableBytes16
state.output.writeBytes(bytes, offset, count)17
while (state.output.readableBytes > 0L) {18
val pending = state.output.readableBytes19
beginH1Io(state, false)20
val written = state.transport.writeFrom(state.output, pending)21
httpProgress(written, pending)22
if (written == 0L)23
throw WriteZeroException()24
progressH1Io(state, false, true)25
}26
offset += count27
}28
if (flush)29
flushH1Transport(state)30
}32
internal suspend fun writeH1Body(33
state: H1State,34
chunk: Http1BodyChunk,35
buffer: NativeByteBuffer,36
count: Long37
) {38
writeH1Wire(state, chunk.prefix, false)39
var remaining = count40
while (remaining > 0L) {41
beginH1Io(state, false)42
val before = buffer.readableBytes43
val written = state.transport.writeFrom(buffer, remaining)44
httpProgress(written, remaining)45
if (before - buffer.readableBytes != written)46
throw IllegalStateException(Some("invalid HTTP body transport progress"))47
if (written == 0L)48
throw WriteZeroException()49
remaining -= written50
progressH1Io(state, false, true)51
}52
writeH1Wire(state, chunk.suffix, false)53
flushH1Transport(state)54
}56
internal suspend fun flushH1Transport(state: H1State) {57
beginH1Io(state, false)58
state.transport.flush()59
progressH1Io(state, false, false)60
}