scoop:scoop.http:0.1.0
src/multipart/decoder.scoop
Selected production source · src/multipart/decoder.scoop
1
package scoop.http3
import scoop.platform.memory.NativeByteBuffer5
/**6
* An incremental multipart parser with bounded headers, parts, and body events. Drain an event7
* before feeding more input; calls on one instance must not overlap.8
*/9
public class MultipartDecoder public constructor(10
/**11
* The validated delimiter boundary used by this codec.12
*/13
public val boundary: MultipartBoundary,14
limits: MultipartLimits = multipartLimits()15
): Closeable {16
private val guard: MultipartGuard = MultipartGuard()17
private val state: MultipartState = MultipartState(boundary, limits)18
private var closed: Boolean = false19
private var parseFailure: Throwable? = None20
/**21
* Whether local parser state has been closed.22
*/23
public val isClosed: Boolean get() = closed24
/**25
* Whether the closing multipart boundary has been recognized.26
*/27
public val isComplete: Boolean get() = state.complete28
/**29
* Whether the codec has completed its terminal operation and, for a decoder, drained pending30
* events.31
*/32
public val isFinished: Boolean get() = state.ended && !state.hasPending33
/**34
* The retained parse failure, if the decoder has failed.35
*/36
public val failure: Throwable? get() = parseFailure38
/**39
* Consumes a prefix of the readable buffer and advances by the returned count. A pending event40
* can stop progress; drain events before supplying more bytes.41
*/42
public fun receive(buffer: NativeByteBuffer, count: Long = buffer.readableBytes): Long =43
guard.withUse {44
requireOpen()45
if (state.ended)46
throw IllegalStateException(Some("multipart input has ended"))47
if (count < 0L)48
throw IllegalArgumentException(Some("negative multipart input count"))49
if (count > buffer.readableBytes)50
throw IndexOutOfBoundsException()51
if (count == 0L || state.hasPending)52
return@withUse 0L53
@Unsafe54
{55
val borrow = buffer.borrowReadable(count)56
var consumed = 0L57
try {58
val pointer = borrow.pointer()!!59
while (consumed < count && !state.hasPending) {60
val byte = pointer.load(consumed)61
consumed += 1L62
state.accept(byte)63
}64
state.flushBody(true)65
return@withUse consumed66
} catch (error: Throwable) {67
failed(error)68
throw error69
} finally {70
borrow.complete(consumed)71
}72
}73
}75
/**76
* Removes the next parsed event, or returns None when no event is ready.77
*/78
public fun nextEvent(): MultipartEvent? =79
guard.withUse {80
requireOpen()81
try {82
state.take()83
}84
catch (error: Throwable) {85
failed(error);86
throw error87
}88
}89
/**90
* Reports EOF, validates completion, and rejects a truncated multipart message.91
*/92
public fun endInput() {93
guard.withUse {94
requireOpen()95
try {96
state.endInput()97
}98
catch (error: Throwable) {99
failed(error);100
throw error101
}102
}103
}105
/**106
* Releases local state without emitting bytes or finishing an incomplete message.107
*/108
public override fun close() {109
guard.withUse {110
closed = true;111
state.discard();112
Unit113
}114
}115
private fun failed(error: Throwable) {116
parseFailure = Some(error);117
state.discard()118
}119
private fun requireOpen() {120
if (closed)121
throw IllegalStateException(Some("multipart decoder is closed"))122
when (parseFailure) {123
case Some(_) -> throw IllegalStateException(Some("multipart decoder has failed"));124
case None -> {}125
}126
}127
}