Scoop API

scoop:scoop.http.client:0.1.0

src/transport/upload.scoop

Selected production source · src/transport/upload.scoop

1package scoop.http.client
3import scoop.asyncio.AsyncByteSink
4import scoop.http.HttpVersion
5import scoop.platform.memory.NativeByteBuffer
7private class ClientUploadStoppedException : Exception(Some("final response stopped further request body output"))
9internal suspend fun sendClientBody(call: ClientCall, exchange: ClientExchange) {
10 var complete = false
11 try {
12 if (!awaitClientContinue(call)) return
13 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 = true
21 } 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 = complete
29 call.lock.withLock { call.uploadDone = true; call.uploadComplete = finished }
30 wakeClientCall(call)
31 }
34private 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 }