Scoop API

scoop:scoop.http:0.1.0

src/websocket/handshake_failure.scoop

Selected production source · src/websocket/handshake_failure.scoop

1package scoop.http
3/** Distinguishes malformed handshakes from an otherwise valid unsupported request version. */
4public enum WebSocketHandshakeFailureKind {
5 /** Invalid handshake fields, protocol conditions or response negotiation. */
6 Malformed,
7 /** An otherwise valid request advertises a version other than 13. */
8 UnsupportedVersion
9}
11/** A handshake failure with its portable kind and optional field context. */
12public class WebSocketHandshakeException public constructor(
13 /** The protocol failure explanation. */
14 public val detail: String,
15 /** The handshake field involved, when known. */
16 public val field: String? = None,
17 /** The failure category; ordinary handshake failures are malformed. */
18 public val kind: WebSocketHandshakeFailureKind = WebSocketHandshakeFailureKind.Malformed
19) : Exception(Some("WebSocket handshake: " + detail))
21internal fun requireWebSocketVersionSyntax(value: String) {
22 val bytes = value.toByteArray()
23 var number = 0
24 if (bytes.size == 0L || bytes.size > 3L || (bytes.size > 1L && bytes[0L] == 48.toByte()))
25 throw WebSocketHandshakeException("invalid WebSocket version", Some("Sec-WebSocket-Version"))
26 for (byte in bytes) {
27 val digit = byte.toUByte().toInt() - 48
28 if (digit < 0 || digit > 9)
29 throw WebSocketHandshakeException("invalid WebSocket version", Some("Sec-WebSocket-Version"))
30 number = number * 10 + digit
31 }
32 if (number > 255) throw WebSocketHandshakeException("invalid WebSocket version", Some("Sec-WebSocket-Version"))