scoop:scoop.http.client:0.1.0
src/transport/h2c.scoop
Selected production source · src/transport/h2c.scoop
1
package scoop.http.client3
import scoop.asyncio.readAllBytes4
import scoop.http.*5
import scoop.platform.memory.NativeByteBuffer7
internal class ClientH2cExchange(val call: ClientCall, val entry: ClientPoolEntry, val original: ClientH1Exchange) : ClientExchange {8
private var current: ClientExchange = original9
private var settled: Boolean = false10
public override val version: HttpVersion get() = current.version11
public override val isReadComplete: Boolean get() = current.isReadComplete12
public override val trailers: Array<HttpField>? get() = current.trailers14
public override suspend fun receiveResponse(): HttpResponseHead {15
val head = current.receiveResponse()16
if (settled || (head.status < 200U && head.status != 101U)) return head17
settled = true18
if (!http2UpgradeAccepted(head)) {19
val required = call.client.config.versionPolicy == HttpVersionPolicy.Http2Only20
finishClientH2cOffer(call.client, entry, required)21
if (required) throw HttpNegotiationException("upgrade HTTP client", "server declined required h2c upgrade")22
return head23
}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
}49
}51
internal fun finishClientH2cOffer(state: ClientState, entry: ClientPoolEntry, failed: Boolean) {52
state.lock.withLock {53
entry.h2cPending = false54
if (failed) entry.draining = true55
Unit56
}57
wakeClient(state)58
}