scoop:scoop.http.server:0.1.0
src/writer.scoop
Selected production source · src/writer.scoop
1
package scoop.http.server3
import scoop.asyncio.*4
import scoop.http.HttpField5
import scoop.http.HttpResponseHead6
import scoop.platform.memory.NativeByteBuffer8
internal interface ResponseOutput {9
suspend fun begin(head: HttpResponseHead, length: Long?): AsyncByteSink10
suspend fun finish(trailers: List<HttpField>)11
suspend fun abort()12
}14
/** A borrowed output view valid only while the framework executes one response body. */15
public class HttpBodyWriter internal constructor(16
private val sink: AsyncByteSink,17
private val length: Long?18
): AsyncByteSink {19
private val busy: AtomicBoolean = AtomicBoolean(false)20
private val ended: AtomicBoolean = AtomicBoolean(false)21
private var written: Long = 0L22
internal val writtenBytes: Long get() = written24
private fun enter() {25
if (!busy.compareAndSet(false, true))26
throw IllegalStateException(Some("concurrent response writes"))27
if (ended.load()) {28
busy.store(false);29
throw IllegalStateException(Some("response writer is no longer active"))30
}31
}33
/** Accepts a possibly short prefix, enforcing the declared original-content length. */34
public override suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long {35
enter()36
try {37
ensureActive()38
if (count < 0L || count > buffer.readableBytes)39
throw IndexOutOfBoundsException()40
if (present(length) && count > length!! - written)41
throw IllegalStateException(Some("response exceeds its declared length"))42
val before = buffer.readableBytes43
val progress = sink.writeFrom(buffer, count)44
if (progress < 0L || progress > count || before - buffer.readableBytes != progress)45
throw IllegalStateException(Some("invalid response sink progress"))46
if (progress > Long.MAX_VALUE - written)47
throw ArithmeticException()48
written += progress49
return progress50
} finally {51
busy.store(false)52
}53
}55
/** Flushes accepted bytes without finishing the response. */56
public override suspend fun flush() {57
enter()58
try {59
ensureActive();60
sink.flush()61
}62
finally {63
busy.store(false)64
}65
}67
/** Writes an entire byte sequence through a bounded buffer. */68
public suspend fun writeAll(value: List<Byte>) {69
val capacity =70
if (value.size < 65536L)71
value.size72
else73
65536L74
NativeByteBuffer.allocate(capacity).use suspend {75
buffer ->76
var index = 0L77
while (index < value.size) {78
buffer.clear()79
val count =80
if (value.size - index < capacity)81
value.size - index82
else83
capacity84
buffer.writeBytes(value, index, count)85
writeFully(buffer)86
index += count87
}88
}89
}91
/** Writes UTF-8 text through the same bounded output path. */92
public suspend fun writeText(value: String) {93
writeAll(value.toByteArray())94
}96
internal fun complete() {97
enter()98
try {99
if (present(length) && length!! != written)100
throw IllegalStateException(Some("response is shorter than its declared length"))101
ended.store(true)102
} finally {103
busy.store(false)104
}105
}107
internal fun invalidate() {108
ended.store(true)109
}110
}