Scoop API

scoop:scoop.http.client:0.1.0

src/transport/h2c.scoop

Selected production source · src/transport/h2c.scoop

1package scoop.http.client
3import scoop.asyncio.readAllBytes
4import scoop.http.*
5import scoop.platform.memory.NativeByteBuffer
7internal class ClientH2cExchange(val call: ClientCall, val entry: ClientPoolEntry, val original: ClientH1Exchange) : ClientExchange {
8 private var current: ClientExchange = original
9 private var settled: Boolean = false
10 public override val version: HttpVersion get() = current.version
11 public override val isReadComplete: Boolean get() = current.isReadComplete
12 public override val trailers: Array<HttpField>? get() = current.trailers
14 public override suspend fun receiveResponse(): HttpResponseHead {
15 val head = current.receiveResponse()
16 if (settled || (head.status < 200U && head.status != 101U)) return head
17 settled = true
18 if (!http2UpgradeAccepted(head)) {
19 val required = call.client.config.versionPolicy == HttpVersionPolicy.Http2Only
20 finishClientH2cOffer(call.client, entry, required)
21 if (required) throw HttpNegotiationException("upgrade HTTP client", "server declined required h2c upgrade")
22 return head
23 }
24 original.readAllBytes(0L)
25 val upload = call.lock.withLock { call.upload!! }
26 upload.join()
27 requireClientCall(call)
28 if (!call.lock.withLock { call.uploadComplete })
29 throw HttpNegotiationException("upgrade HTTP client", "request upload did not complete")
30 original.close()
31 val connection = awaitClientUpgrade(call, entry, original.connection)
32 val stream = connection.upgradedStream!!
33 current = ClientH2Exchange(call, stream)
34 bindClientHttp2Stream(call, stream)
35 return current.receiveResponse()
36 }
38 public override suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long = current.readInto(buffer, count)
39 public override suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long = original.writeFrom(buffer, count)
40 public override suspend fun flush() { original.flush() }
41 public override suspend fun finish(trailers: List<HttpField>) { original.finish(trailers) }
42 public override suspend fun close() {
43 try { current.close() }
44 finally {
45 val unfinished = call.client.lock.withLock { entry.h2cPending }
46 finishClientH2cOffer(call.client, entry, unfinished)
47 }
48 }
51internal fun finishClientH2cOffer(state: ClientState, entry: ClientPoolEntry, failed: Boolean) {
52 state.lock.withLock {
53 entry.h2cPending = false
54 if (failed) entry.draining = true
55 Unit
56 }
57 wakeClient(state)