scoop:scoop.http:0.1.0
src/websocket/frames/message_validator.scoop
Selected production source · src/websocket/frames/message_validator.scoop
1
package scoop.http3
/**4
* Validates the final message byte stream against a size limit and, for text, incremental UTF-85
* validity. Feed decompressed bytes when compression is used.6
*/7
public class WebSocketMessageValidator public constructor(8
/**9
* Whether the validated message is text or binary.10
*/11
public val type: WebSocketMessageType,12
/**13
* The maximum accepted message size in bytes.14
*/15
public val maxBytes: Long) : Closeable {16
private val busy: AtomicBoolean = AtomicBoolean(false)17
private var validator: WebSocketUtf8? = None18
private var closed: Boolean = false19
private var finished: Boolean = false20
private var count: Long = 0L21
private var error: Throwable? = None23
init {24
if (maxBytes < 0L) throw IllegalArgumentException(Some("negative WebSocket message limit"))25
if (type == WebSocketMessageType.Text) validator = Some(WebSocketUtf8.create())26
}28
/**29
* The bytes successfully accepted so far.30
*/31
public val byteCount: Long get() = count32
/**33
* Whether validation resources have been released.34
*/35
public val isClosed: Boolean get() = closed36
/**37
* Whether final message validation completed successfully.38
*/39
public val isFinished: Boolean get() = finished40
/**41
* The retained validation or cleanup failure.42
*/43
public val failure: Throwable? get() = error45
/**46
* Accepts the next uncompressed message bytes, checking the byte limit and incremental UTF-847
* when required.48
*/49
public fun receive(data: List<Byte>) {50
withUse {51
requireActive()52
val bytes = data.toArray()53
try {54
if (bytes.size > maxBytes - count) throw WebSocketFrameException(1009U, "message exceeds its byte limit")55
when (validator) { case Some(val value) -> value.receive(bytes); case None -> {} }56
count += bytes.size57
} catch (failure: Throwable) { fail(failure) }58
Unit59
}60
}62
/**63
* Validates final UTF-8 completion and releases the validator; no more message data may64
* follow.65
*/66
public fun finish() {67
withUse {68
requireActive()69
try {70
when (validator) { case Some(val value) -> value.finish(); case None -> {} }71
closeValidator()72
finished = true73
} catch (failure: Throwable) { fail(failure) }74
Unit75
}76
}78
/**79
* Releases validation resources without asserting successful message completion.80
*/81
public override fun close() { withUse { if (!closed) { closed = true; closeValidator() }; Unit } }83
private fun closeValidator() {84
val owned = validator85
validator = None86
when (owned) { case Some(val value) -> value.close(); case None -> {} }87
}89
private fun requireActive() {90
if (closed || finished) throw IllegalStateException(Some("WebSocket message validator is closed or finished"))91
when (error) { case Some(_) -> throw IllegalStateException(Some("WebSocket message validator failed")); case None -> {} }92
}94
private fun fail(failure: Throwable): Nothing {95
var result = failure96
try { closeValidator() } catch (cleanup: Throwable) { result = ResourceUseException(failure, cleanup) }97
error = Some(result)98
throw result99
}101
private fun <T> withUse(block: () -> T): T {102
if (!busy.compareAndSet(false, true)) throw IllegalStateException(Some("WebSocket message validator is already in use"))103
try { return block() } finally { busy.store(false) }104
}105
}