scoop:scoop.http.client:0.1.0
src/websocket/request.scoop
Selected production source · src/websocket/request.scoop
1
package scoop.http.client3
import scoop.http.asyncio.webSocketSessionOptions5
import scoop.collections.emptyList6
import scoop.http.*7
import scoop.http.asyncio.WebSocketSessionOptions8
import scoop.url.Url10
/**11
* A ws/wss upgrade request without URL fragment or request body. Handshake-specific fields are12
* managed by the client.13
*/14
public 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
}45
}47
/**48
* Offered subprotocols, permessage-deflate offers, and the asynchronous session's buffering/close49
* policy.50
*/51
public 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()67
}69
internal 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 result85
}87
internal fun clientWebSocketUrl(url: Url): Url =88
url.copy {89
scheme =90
Some(91
if (url.scheme == Some("http"))92
"ws"93
else94
"wss"95
)96
}98
internal fun clientWebSocketOptions(options: HttpCallOptions): HttpCallOptions =99
httpCallOptions {100
this.decoding = options.decoding101
this.timeouts = options.timeouts102
this.onInformational = options.onInformational103
this.redirects = options.redirects104
this.retries = options.retries105
this.authentication = options.authentication106
this.proxy = options.proxy107
this.useCookies = options.useCookies108
this.cookieContext = options.cookieContext109
this.cacheMode = HttpCacheMode.Bypass110
this.observer = options.observer111
}