Scoop API

scoop:scoop.http.asyncio:0.1.0

src/multipart/reader.scoop

Selected production source · src/multipart/reader.scoop

1package scoop.http.asyncio
3import scoop.asyncio.*
4import scoop.http.*
5import scoop.platform.memory.NativeByteBuffer
7internal class MultipartReader(source: AsyncByteSource, owner: AsyncCloseable?, loop: EventLoop, private val state: MultipartReadState) {
8 val finished: Boolean get() = state.finished
9 val io: MessageIo<AsyncByteSource> = MessageIo<AsyncByteSource>(source, owner, loop) { state.close() }
11 suspend fun nextPart(): MultipartPart? = io.run({
12 if (present(state.current)) throw IllegalStateException(Some("current multipart body must be completed first"))
13 }) suspend { source ->
14 if (finished) None
15 else when (nextEvent(source)) {
16 case PartStarted(val head) -> {
17 val body = MultipartBody(this)
18 state.current = Some(body)
19 Some(MultipartPart(head, body))
20 }
21 case End -> { state.finished = true; None }
22 else -> throw IllegalStateException(Some("unexpected multipart event between parts"))
23 }
24 }
26 suspend fun read(body: MultipartBody, buffer: NativeByteBuffer, count: Long): Long = io.run({
27 requireBody(body)
28 httpCount(count, buffer.writableBytes)
29 }) suspend { source -> readBody(source, body, buffer, count) }
31 fun requireBody(body: MultipartBody) {
32 if (body.closed) throw IoClosedException()
33 if (!body.complete && (!present(state.current) || state.current!! !== body))
34 throw IllegalStateException(Some("multipart body is not current"))
35 }
37 suspend fun readBody(source: AsyncByteSource, body: MultipartBody, buffer: NativeByteBuffer, count: Long): Long {
38 if (count == 0L || body.complete) return 0L
39 while (true) {
40 ensureActive()
41 val remaining = body.data.size - body.offset
42 if (remaining > 0L) {
43 val copied = if (remaining > count) count else remaining
44 buffer.writeBytes(body.data, body.offset, copied)
45 body.offset += copied
46 if (body.offset == body.data.size) body.clear()
47 return copied
48 }
49 when (nextEvent(source)) {
50 case Data(val bytes) -> { body.data = bytes; body.offset = 0L }
51 case PartEnded -> { body.complete = true; state.current = None; return 0L }
52 else -> throw IllegalStateException(Some("unexpected multipart event inside a part"))
53 }
54 }
55 }
57 private suspend fun nextEvent(source: AsyncByteSource): MultipartEvent {
58 val decoder = state.decoder
59 val input = state.input
60 while (true) {
61 ensureActive()
62 val event = decoder.nextEvent()
63 if (present(event)) return event!!
64 if (decoder.isFinished) throw IllegalStateException(Some("multipart event stream has ended"))
65 if (input.readableBytes > 0L) decoder.receive(input)
66 else {
67 input.clear()
68 val requested = input.writableBytes
69 val count = source.readInto(input, requested)
70 httpProgress(count, requested)
71 if (input.writePosition != count || input.readPosition != 0L)
72 throw IllegalStateException(Some("inconsistent multipart source progress"))
73 if (count == 0L) decoder.endInput()
74 }
75 }
76 }