Scoop API

scoop:scoop.http.client:0.1.0

src/websocket/request.scoop

Selected production source · src/websocket/request.scoop

1package scoop.http.client
3import scoop.http.asyncio.webSocketSessionOptions
5import scoop.collections.emptyList
6import scoop.http.*
7import scoop.http.asyncio.WebSocketSessionOptions
8import scoop.url.Url
10/**
11 * A ws/wss upgrade request without URL fragment or request body. Handshake-specific fields are
12 * managed by the client.
13 */
14public class HttpWebSocketRequest internal constructor(
15 /**
16 * The validated ws or wss URL without a fragment.
17 */
18 public val url: Url,
19 fields: List<HttpField> = emptyList<HttpField>(),
20 /**
21 * HTTP call policy used during the handshake.
22 */
23 public val options: HttpCallOptions = httpCallOptions()
24) {
25 /**
26 * Copied caller fields for the upgrade request.
27 */
28 public val fields: Array<HttpField> = fields.toArray()
29 internal val httpUrl: Url = clientWebSocketHttpUrl(url)
30 init {
31 if (url.scheme != Some("ws") && url.scheme != Some("wss"))
32 throw IllegalArgumentException(Some("WebSocket request requires ws or wss"))
33 if (options.duplex || options.expectContinue)
34 throw IllegalArgumentException(Some("WebSocket handshake has an empty request body"))
35 if (
36 options.cacheMode ==
37 HttpCacheMode.OnlyIfCached ||
38 HttpCacheControl.parseFields(this.fields).contains("only-if-cached")
39 )
40 throw IllegalArgumentException(Some("WebSocket handshake cannot use only-if-cached"))
41 for (field in this.fields)
42 if (field.hasName("Expect".toByteArray()))
43 throw IllegalArgumentException(Some("WebSocket handshake cannot wait for Continue"))
44 }
47/**
48 * Offered subprotocols, permessage-deflate offers, and the asynchronous session's buffering/close
49 * policy.
50 */
51public class HttpWebSocketOptions internal constructor(
52 protocols: List<String> = emptyList<String>(),
53 deflateOffers: List<WebSocketDeflateOffer> = emptyList<WebSocketDeflateOffer>(),
54 /**
55 * Post-upgrade streaming, limits, and close policy.
56 */
57 public val session: WebSocketSessionOptions = webSocketSessionOptions()
58) {
59 /**
60 * Subprotocol tokens offered in order.
61 */
62 public val protocols: Array<String> = protocols.toArray()
63 /**
64 * Explicit permessage-deflate offers.
65 */
66 public val deflateOffers: Array<WebSocketDeflateOffer> = deflateOffers.toArray()
69internal fun clientWebSocketHttpUrl(url: Url): Url {
70 if (url.encodedFragment != None)
71 throw IllegalArgumentException(Some("WebSocket URL cannot contain a fragment"))
72 val scheme =
73 when (url.scheme) {
74 case Some("ws") -> "http"
75 case Some("http") -> "http"
76 case Some("wss") -> "https"
77 case Some("https") -> "https"
78 else -> throw IllegalArgumentException(
79 Some("WebSocket handshake requires an HTTP or WebSocket URL")
80 )
81 }
82 val result = url.copy { this.scheme = Some(scheme) }
83 result.httpOrigin()
84 return result
87internal fun clientWebSocketUrl(url: Url): Url =
88 url.copy {
89 scheme =
90 Some(
91 if (url.scheme == Some("http"))
92 "ws"
93 else
94 "wss"
95 )
96 }
98internal fun clientWebSocketOptions(options: HttpCallOptions): HttpCallOptions =
99 httpCallOptions {
100 this.decoding = options.decoding
101 this.timeouts = options.timeouts
102 this.onInformational = options.onInformational
103 this.redirects = options.redirects
104 this.retries = options.retries
105 this.authentication = options.authentication
106 this.proxy = options.proxy
107 this.useCookies = options.useCookies
108 this.cookieContext = options.cookieContext
109 this.cacheMode = HttpCacheMode.Bypass
110 this.observer = options.observer
111 }