Scoop API

scoop:scoop.http.server:0.1.0

src/websocket/options.scoop

Selected production source · src/websocket/options.scoop

1package scoop.http.server
3import scoop.collections.HashSet
4import scoop.http.*
5import scoop.http.asyncio.*
7/** Explicit HTTP/1 upgrade policy and independent session configuration. */
8public class WebSocketOptions internal constructor(
9 protocols: List<String> = ArrayList<String>(),
10 /** Rejects an upgrade when none of the configured protocols was offered. */
11 public val requireProtocol: Boolean = false,
12 /** Receives every original Origin field; absence, duplicates and invalid values remain explicit. */
13 public val originAllowed: (List<HttpField>) -> Boolean = { _ -> true },
14 /** Bounded frame buffers and the finite WebSocket close deadline. */
15 public val session: WebSocketSessionOptions = webSocketSessionOptions(),
16 /** Optional compression response policy; unmatched offers leave compression disabled. */
17 public val deflate: WebSocketDeflate? = None
18) {
19 /** Case-sensitive subprotocol tokens in server preference order. */
20 public val protocols: Array<String> = protocols.toArray()
21 init {
22 val seen = HashSet<String>()
23 for (protocol in this.protocols)
24 if (!isHttpToken(protocol) || !seen.add(protocol))
25 throw IllegalArgumentException(Some("invalid or duplicate WebSocket subprotocol"))
26 if (present(deflate) && deflate!!.serverMaxWindowBits == Some(8))
27 throw IllegalArgumentException(
28 Some("WebSocket server compression requires window bits 9 through 15")
29 )
30 }
33/** Deferred upgrade policy and a callback that acquires its own session resources. */
34public class HttpWebSocketBody internal constructor(
35 /** The endpoint's explicit negotiation and session options. */
36 public val options: WebSocketOptions,
37 /** Runs after HTTP cleanup, outside the request Context and deadline. */
38 public val onSession: suspend (WebSocketContext, AsyncWebSocketSession) -> Unit
41/** Requests an HTTP/1.1 upgrade; the session callback starts only after the HTTP pipeline exits. */
42public fun webSocket(
43 options: WebSocketOptions = webSocketOptions(),
44 onSession: suspend (WebSocketContext, AsyncWebSocketSession) -> Unit
45): HttpResult =
46 httpResult {
47 this.status = 101U
48 this.body = HttpBody.WebSocket(HttpWebSocketBody(options, onSession))
49 this.allowCompression = false
50 }