scoop:scoop.http.server:0.1.0
src/http1_io.scoop
Selected production source · src/http1_io.scoop
1
package scoop.http.server3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.http.asyncio.*6
import scoop.platform.memory.NativeByteBuffer8
internal class RequestExpectations(private val fields: Array<HttpField>) {9
private val lock: ServerLock = ServerLock()10
private var parsed: HttpExpectations? = None12
fun requireSupported(): Boolean =13
lock.withLock {14
if (!present(parsed)) {15
try {16
parsed = Some(parseHttpExpectations(fields))17
}18
catch (_: HttpFieldParseException) {19
reject(400U, "invalid Expect field")20
}21
}22
if (parsed!!.hasUnsupported)23
reject(417U, "unsupported expectation")24
parsed!!.continueRequested25
}26
}28
internal open class Http1ResponseOutput(29
val connection: AsyncHttp1Connection,30
val incoming: Http1IncomingRequest31
): ResponseOutput {32
val started: AtomicBoolean = AtomicBoolean(false)33
private var writer: AsyncHttp1Writer? = None35
private fun prepareHead(head: HttpResponseHead): HttpResponseHead {36
if (!started.compareAndSet(false, true))37
throw IllegalStateException(Some("HTTP/1 response already started"))38
val closing = shouldCloseAfterResponse()39
if (!closing)40
return head41
val fields = ArrayList<HttpField>()42
for (value in head.fields)43
if (!value.hasName("Connection".toByteArray()))44
fields.add(value)45
fields.add(field("Connection", "close"))46
val reason: List<Byte>? =47
when (head.reason) {48
case None -> None;49
case Some(val value) -> Some(value)50
}51
return HttpResponseHead(head.version, head.status, reason, fields)52
}54
internal suspend fun sendBuffered(55
head: HttpResponseHead,56
bytes: Array<Byte>,57
count: Long,58
progress: Http1ResponseProgress59
) {60
connection.sendResponse(prepareHead(head), bytes, count, progress = Some(progress))61
}63
public override suspend fun begin(head: HttpResponseHead, length: Long?): AsyncByteSink {64
val prepared = prepareHead(head)65
val content = httpResponseAllowsContent(head, incoming.head.method)66
val framing =67
if (!content)68
Http1BodyFraming.Empty69
else70
if (present(length))71
Http1BodyFraming.Length(length!!)72
else73
if (head.version == HttpVersion.Http10)74
Http1BodyFraming.Eof75
else76
Http1BodyFraming.Chunked77
val output = connection.sendResponse(prepared, framing)78
writer = Some(output)79
return output80
}82
public override suspend fun finish(trailers: List<HttpField>) {83
writer!!.finish(trailers)84
if (!connection.isClosed)85
writer!!.flush()86
}88
public override suspend fun abort() {89
connection.close()90
}92
protected open fun shouldCloseAfterResponse(): Boolean = !incoming.body.isComplete93
}95
/** Adds the owned server's drain and upgrade state to the ordinary H1 output adapter. */96
internal class ServerHttp1ResponseOutput(97
val state: ServerState,98
val entry: ServerConnection,99
connection: AsyncHttp1Connection,100
incoming: Http1IncomingRequest101
): Http1ResponseOutput(connection, incoming) {102
protected override fun shouldCloseAfterResponse(): Boolean =103
super.shouldCloseAfterResponse() || !state.running()104
}106
internal class Http1RequestInput(107
private val connection: AsyncHttp1Connection,108
private val incoming: Http1IncomingRequest,109
private val output: Http1ResponseOutput,110
private val expectations: RequestExpectations111
): AsyncByteSource {112
private var readStarted: Boolean = false114
public override suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long {115
if (count > 0L && !readStarted) {116
readStarted = true117
if (118
!incoming.body.isComplete &&119
expectations.requireSupported() &&120
incoming.head.version ==121
HttpVersion.Http11122
) {123
if (output.started.load())124
throw IllegalStateException(125
Some("cannot send Continue after the final response")126
)127
val continuing = connection.sendResponse(HttpResponseHead(HttpVersion.Http11, 100U))128
continuing.finish()129
continuing.flush()130
}131
}132
return incoming.body.readInto(buffer, count)133
}134
}