scoop:scoop.http.client:0.1.0
src/transport/http1.scoop
Selected production source · src/transport/http1.scoop
1
package scoop.http.client3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.http.asyncio.*6
import scoop.platform.memory.NativeByteBuffer7
import scoop.time.Duration9
internal class ClientH1Exchange(val call: ClientCall, val connection: AsyncHttp1Connection, val output: AsyncHttp1Writer,10
public override val version: HttpVersion, val readIdle: Duration?) : ClientExchange {11
private var input: AsyncHttp1Body? = None12
public override val isReadComplete: Boolean get() = clientPresent(input) && input!!.isComplete13
public override val trailers: Array<HttpField>? get() = when (input) { case Some(val body) -> body.trailers; case None -> None }14
public override suspend fun receiveResponse(): HttpResponseHead {15
val response = try { connection.receiveResponse(readIdle = readIdle) }16
catch (error: Throwable) {17
if (clientPresent(call.observation)) observeClientFirstByte(call, connection.pendingResponseFirstByteAt)18
recordClientIoFailure(call, error); throw error19
}20
observeClientFirstByte(call, response.firstByteAt)21
if (response.head.status < 200U && response.head.status != 101U) {22
response.body.useAsync suspend { body -> body.readAllBytes(0L); Unit }23
} else input = Some(response.body)24
return response.head25
}26
public override suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long = try { input!!.readInto(buffer, count) }27
catch (error: Throwable) { recordClientIoFailure(call, error); throw error }28
public override suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long = output.writeFrom(buffer, count)29
public override suspend fun flush() { output.flush() }30
public override suspend fun finish(trailers: List<HttpField>) { output.finish(trailers) }31
public override suspend fun close() {32
var failure: Throwable? = None33
try { if (clientPresent(input)) input!!.close() }34
catch (error: Throwable) { failure = Some(error) }35
try { output.close() }36
catch (error: Throwable) { failure = Some(clientCombined(failure, error)) }37
if (clientPresent(failure)) throw failure!!38
}39
}