Scoop API

scoop:scoop.http:0.1.0

src/websocket/handshake.scoop

Selected production source · src/websocket/handshake.scoop

1package scoop.http
3import scoop.collections.emptyList
4import scoop.crypto.*
5import scoop.io.encoding.Base64
6import scoop.url.Url
8/**
9 * The accepted optional subprotocol and permessage-deflate parameters.
10 */
11public class WebSocketNegotiation internal constructor(
12 /**
13 * The selected offered subprotocol, or None.
14 */
15 public val protocol: String?,
16 /**
17 * Effective permessage-deflate negotiation, or None when disabled.
18 */
19 public val deflate: WebSocketDeflateNegotiation?
22/**
23 * A validated HTTP/1 WebSocket upgrade request, nonce key, offered subprotocols, and extensions.
24 * Response validation remains tied to this exact request.
25 */
26public class WebSocketHandshake internal constructor(
27 /**
28 * The validated upgrade request head.
29 */
30 public val request: HttpRequestHead,
31 /**
32 * The validated base64 client nonce used to verify Sec-WebSocket-Accept.
33 */
34 public val key: String,
35 /**
36 * The offered subprotocol tokens in request order.
37 */
38 public val protocols: Array<String>,
39 /**
40 * The parsed offered extensions.
41 */
42 public val extensions: Array<WebSocketExtension>
43) {
44 /**
45 * Validates a server response against this request and returns selected protocol/compression
46 * parameters.
47 */
48 public fun accept(response: HttpResponseHead): WebSocketNegotiation = acceptWebSocketResponse(this, response)
49 /**
50 * Constructs a valid 101 response using a compatible offered subprotocol and compression
51 * selection.
52 */
53 public fun response(protocol: String? = None, deflate: WebSocketDeflate? = None,
54 fields: List<HttpField> = emptyList<HttpField>()): HttpResponseHead = negotiateResponse(protocol, deflate, fields).head
56 /** Constructs the response and retains its checked negotiation for the server session. */
57 public fun negotiateResponse(protocol: String? = None, deflate: WebSocketDeflate? = None,
58 fields: List<HttpField> = emptyList<HttpField>()): WebSocketServerResponse = webSocketResponse(this, protocol, deflate, fields)
60 /**
61 * Creation and validation of HTTP/1 WebSocket upgrade requests.
62 */
63 public companion object {
64 /**
65 * Creates a WebSocket request with a fresh random key, offered protocols/extensions, and
66 * optional proxy absolute-form target.
67 */
68 public fun create(url: Url, protocols: List<String> = emptyList<String>(),
69 deflateOffers: List<WebSocketDeflateOffer> = emptyList<WebSocketDeflateOffer>(),
70 fields: List<HttpField> = emptyList<HttpField>(), absoluteForm: Boolean = false): WebSocketHandshake =
71 createWebSocketHandshake(url, protocols, deflateOffers, fields, absoluteForm)
72 /**
73 * Validates an incoming WebSocket upgrade request and records its nonce and offers.
74 */
75 public fun parse(request: HttpRequestHead): WebSocketHandshake = parseWebSocketHandshake(request)
76 }
79internal fun webSocketAccept(key: String): String =
80 Base64.Default.encode(digest(DigestAlgorithm.Sha1, (key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").toByteArray()))