in ktor-server/ktor-server-host-common/jvm/src/io/ktor/server/engine/BaseApplicationResponse.kt [101:144]
protected open suspend fun respondOutgoingContent(content: OutgoingContent) {
when (content) {
is OutgoingContent.ProtocolUpgrade -> {
commitHeaders(content)
return respondUpgrade(content)
}
// ByteArrayContent is most efficient
is OutgoingContent.ByteArrayContent -> {
// First call user code to acquire bytes, because it could fail
val bytes = content.bytes()
// If bytes are fine, commit headers and send data
commitHeaders(content)
return respondFromBytes(bytes)
}
// WriteChannelContent is more efficient than ReadChannelContent
is OutgoingContent.WriteChannelContent -> {
// First set headers
commitHeaders(content)
// need to be in external function to keep tail suspend call
return respondWriteChannelContent(content)
}
// Pipe is the least efficient
is OutgoingContent.ReadChannelContent -> {
// First call user code to acquire read channel, because it could fail
val readChannel = content.readFrom()
try {
// If channel is fine, commit headers and pipe data
commitHeaders(content)
return respondFromChannel(readChannel)
} finally {
readChannel.cancel()
}
}
// Do nothing, but maintain `when` exhaustiveness
is OutgoingContent.NoContent -> {
commitHeaders(content)
return respondNoContent(content)
}
}
}