scoop:scoop.http:0.1.0
src/websocket/frames/model.scoop
Selected production source · src/websocket/frames/model.scoop
1
package scoop.http3
/**4
* The data or control operation encoded by a WebSocket frame.5
*/6
public enum WebSocketOpcode {7
/**8
* Continues an already-started fragmented data message.9
*/10
Continuation,11
/**12
* Starts a UTF-8 text message.13
*/14
Text,15
/**16
* Starts a binary message.17
*/18
Binary,19
/**20
* Carries the connection close status and reason.21
*/22
Close,23
/**24
* Carries a ping control payload.25
*/26
Ping,27
/**28
* Carries a pong control payload.29
*/30
Pong }31
/**32
* The logical type of a complete or fragmented data message.33
*/34
public enum WebSocketMessageType {35
/**36
* Message bytes must form valid UTF-8 after decompression.37
*/38
Text,39
/**40
* Message bytes have no implicit text encoding.41
*/42
Binary }44
/**45
* Frame opcode, nonnegative payload length, FIN flag, and compressed-message flag.46
*/47
public class WebSocketFrameHead public constructor(48
/**49
* The frame operation.50
*/51
public val opcode: WebSocketOpcode,52
/**53
* The exact nonnegative payload byte count.54
*/55
public val payloadLength: Long,56
/**57
* The FIN bit, ending a data message when true.58
*/59
public val isFinal: Boolean = true,60
/**61
* The RSV1 compressed-message marker, legal only in negotiated message positions.62
*/63
public val isCompressed: Boolean = false64
) {65
init { if (payloadLength < 0L) throw IllegalArgumentException(Some("negative WebSocket frame length")) }66
}68
/**69
* Nonnegative per-frame and per-message byte limits with a positive emitted chunk size.70
*/71
public class WebSocketFrameLimits public constructor(72
/**73
* Maximum accepted frame payload bytes.74
*/75
public val maxFrameBytes: Long = 16777216L,76
/**77
* Maximum accepted message bytes under the frame parser's message accounting.78
*/79
public val maxMessageBytes: Long = 67108864L,80
/**81
* Maximum emitted data-chunk bytes; must be positive.82
*/83
public val dataChunkBytes: Long = 8192L84
) {85
init {86
if (maxFrameBytes < 0L || maxMessageBytes < 0L || dataChunkBytes <= 0L)87
throw IllegalArgumentException(Some("invalid WebSocket frame limits"))88
}89
}91
/**92
* Parsed frame boundaries, data chunks, complete control payloads, and terminal input state.93
*/94
public enum WebSocketFrameEvent {95
/**96
* Begins a data frame with its decoded head.97
*/98
FrameStarted(WebSocketFrameHead),99
/**100
* An owned, unmasked data payload chunk; compressed bytes still require inflation.101
*/102
Data(Array<Byte>),103
/**104
* The announced frame payload has ended.105
*/106
FrameEnded,107
/**108
* A complete ping control payload.109
*/110
Ping(Array<Byte>),111
/**112
* A complete pong control payload.113
*/114
Pong(Array<Byte>),115
/**116
* A validated close-frame payload.117
*/118
Close(WebSocketClose),119
/**120
* The decoder reached its terminal input state.121
*/122
End123
}125
/**126
* A framing, message, or UTF-8 failure with the appropriate WebSocket close code and optional byte127
* offset.128
*/129
public class WebSocketFrameException public constructor(130
/**131
* The WebSocket close code appropriate for this failure.132
*/133
public val closeCode: UInt,134
/**135
* The protocol failure explanation.136
*/137
public val detail: String,138
/**139
* The zero-based input byte position, when available.140
*/141
public val byteOffset: Long? = None,142
/**143
* The underlying failure, when available.144
*/145
public val cause: Throwable? = None146
) : Exception(Some("WebSocket frame: " + detail))148
internal fun webSocketControl(opcode: WebSocketOpcode): Boolean = when (opcode) {149
case Close -> true150
case Ping -> true151
case Pong -> true152
else -> false153
}155
internal fun webSocketFrameError(detail: String): Nothing = throw WebSocketFrameException(1002U, detail)