Scoop API

scoop:scoop.http:0.1.0

src/websocket/frames/message_validator.scoop

Selected production source · src/websocket/frames/message_validator.scoop

1package scoop.http
3/**
4 * Validates the final message byte stream against a size limit and, for text, incremental UTF-8
5 * validity. Feed decompressed bytes when compression is used.
6 */
7public 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? = None
18 private var closed: Boolean = false
19 private var finished: Boolean = false
20 private var count: Long = 0L
21 private var error: Throwable? = None
23 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() = count
32 /**
33 * Whether validation resources have been released.
34 */
35 public val isClosed: Boolean get() = closed
36 /**
37 * Whether final message validation completed successfully.
38 */
39 public val isFinished: Boolean get() = finished
40 /**
41 * The retained validation or cleanup failure.
42 */
43 public val failure: Throwable? get() = error
45 /**
46 * Accepts the next uncompressed message bytes, checking the byte limit and incremental UTF-8
47 * 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.size
57 } catch (failure: Throwable) { fail(failure) }
58 Unit
59 }
60 }
62 /**
63 * Validates final UTF-8 completion and releases the validator; no more message data may
64 * 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 = true
73 } catch (failure: Throwable) { fail(failure) }
74 Unit
75 }
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 = validator
85 validator = None
86 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 = failure
96 try { closeValidator() } catch (cleanup: Throwable) { result = ResourceUseException(failure, cleanup) }
97 error = Some(result)
98 throw result
99 }
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 }