Scoop API

scoop:scoop.http.asyncio:0.1.0

src/http1_transfer.scoop

Selected production source · src/http1_transfer.scoop

1package scoop.http.asyncio
3import scoop.platform.io.WriteZeroException
4import scoop.platform.memory.NativeByteBuffer
5import scoop.http.Http1BodyChunk
7internal suspend fun writeH1Wire(state: H1State, bytes: Array<Byte>, flush: Boolean = true) {
8 var offset = 0L
9 while (offset < bytes.size) {
10 state.output.clear()
11 val count =
12 if (bytes.size - offset < state.output.writableBytes)
13 bytes.size - offset
14 else
15 state.output.writableBytes
16 state.output.writeBytes(bytes, offset, count)
17 while (state.output.readableBytes > 0L) {
18 val pending = state.output.readableBytes
19 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 += count
27 }
28 if (flush)
29 flushH1Transport(state)
32internal suspend fun writeH1Body(
33 state: H1State,
34 chunk: Http1BodyChunk,
35 buffer: NativeByteBuffer,
36 count: Long
37) {
38 writeH1Wire(state, chunk.prefix, false)
39 var remaining = count
40 while (remaining > 0L) {
41 beginH1Io(state, false)
42 val before = buffer.readableBytes
43 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 -= written
50 progressH1Io(state, false, true)
51 }
52 writeH1Wire(state, chunk.suffix, false)
53 flushH1Transport(state)
56internal suspend fun flushH1Transport(state: H1State) {
57 beginH1Io(state, false)
58 state.transport.flush()
59 progressH1Io(state, false, false)