scoop:scoop.http.client:0.1.0
src/transport/upload.scoop
Selected production source · src/transport/upload.scoop
1
package scoop.http.client3
import scoop.asyncio.AsyncByteSink4
import scoop.http.HttpVersion5
import scoop.platform.memory.NativeByteBuffer7
private class ClientUploadStoppedException : Exception(Some("final response stopped further request body output"))9
internal suspend fun sendClientBody(call: ClientCall, exchange: ClientExchange) {10
var complete = false11
try {12
if (!awaitClientContinue(call)) return13
val sink = RequestBodySink(ClientUploadSink(call, exchange), call.request.contentLength)14
try {15
call.lock.withLock { call.bodyStarted = true }16
val result = call.request.body.writeWithTrailersTo(sink)17
sink.finish()18
try { exchange.finish(result.trailers) }19
catch (error: Throwable) { recordClientUploadIoFailure(call, error, exchange.version == HttpVersion.Http2); throw error }20
complete = true21
} finally { sink.end() }22
} catch (_: ClientUploadStoppedException) {23
} catch (error: Throwable) {24
val transport = call.lock.withLock { call.uploadIoFailure }25
failClientCall(call, error, !clientPresent(transport) || !sameClientFailure(transport!!, error))26
}27
finally {28
val finished = complete29
call.lock.withLock { call.uploadDone = true; call.uploadComplete = finished }30
wakeClientCall(call)31
}32
}34
private class ClientUploadSink(val call: ClientCall, val destination: ClientExchange) : AsyncByteSink {35
private fun requireSending() {36
requireClientCall(call)37
if (call.lock.withLock { call.stopUpload }) throw ClientUploadStoppedException()38
}39
public override suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long {40
requireSending()41
return sendClientBytes(call, destination, buffer, count)42
}43
public override suspend fun flush() {44
requireSending()45
try { destination.flush() }46
catch (error: Throwable) { recordClientUploadIoFailure(call, error, destination.version == HttpVersion.Http2); throw error }47
}48
}