scoop:scoop.http:0.1.0
src/websocket/handshake_failure.scoop
Selected production source · src/websocket/handshake_failure.scoop
1
package scoop.http3
/** Distinguishes malformed handshakes from an otherwise valid unsupported request version. */4
public 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
UnsupportedVersion9
}11
/** A handshake failure with its portable kind and optional field context. */12
public 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.Malformed19
) : Exception(Some("WebSocket handshake: " + detail))21
internal fun requireWebSocketVersionSyntax(value: String) {22
val bytes = value.toByteArray()23
var number = 024
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() - 4828
if (digit < 0 || digit > 9)29
throw WebSocketHandshakeException("invalid WebSocket version", Some("Sec-WebSocket-Version"))30
number = number * 10 + digit31
}32
if (number > 255) throw WebSocketHandshakeException("invalid WebSocket version", Some("Sec-WebSocket-Version"))33
}