scoop:scoop.http.server:0.1.0
src/context.scoop
Selected production source · src/context.scoop
1
package scoop.http.server3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.time.Duration7
/** One request's explicit context; it is also bound by exact type while its pipeline executes. */8
public class HttpContext internal constructor(9
/** The original request and its single-consumer body. */10
public val request: HttpRequest,11
internal val output: ResponseOutput,12
/** The identifier supplied by the server or the caller of single-request execution. */13
public val requestId: String,14
/** The structured job that owns this request's work. */15
public val requestJob: Job,16
/** Immutable socket and negotiated protocol facts for this request. */17
public val connection: HttpConnectionInfo,18
private val deadline: RequestDeadline19
) {20
/** Pending response mutations and submitted response state. */21
public val response: HttpResponse = HttpResponse()22
private val responding: AtomicBoolean = AtomicBoolean(false)23
private val aborting: AtomicBoolean = AtomicBoolean(false)24
internal var selectedRoute: HttpRouteInfo? = None25
internal var parameters: PathParameters = PathParameters([], [])26
internal var mappedFailure: Boolean = false27
internal var observedFailure: Throwable? = None28
internal var responseBytes: Long = 0L29
internal var encodedResponseBytes: Long = 0L30
internal var compressionOptions: ResponseCompressionOptions? = None31
internal var acceptEncoding: HttpAcceptEncoding? = None32
internal var acceptEncodingFailure: HttpFieldParseException? = None34
/** The selected endpoint metadata, absent before routing and for fallback results. */35
public val route: HttpRouteInfo? get() = selectedRoute36
/** Captured parameters for the selected endpoint or fallback group. */37
public val pathParams: PathParameters get() = parameters39
/** Overrides the total time since pipeline entry, before commit; None disables this deadline. */40
public fun setDeadline(duration: Duration?) {41
requestJob.ensureActive()42
if (requestJob.isCompleted || response.isCommitted || response.isAborted)43
throw IllegalStateException(44
Some("request deadline can only change before response commit")45
)46
deadline.set(duration)47
}49
/** Executes a final response, waiting for its producer and protocol completion. */50
public suspend fun respond(result: HttpResult) {51
sendResult(result, false)52
}54
internal suspend fun sendResult(result: HttpResult, minimal: Boolean) {55
if (!responding.compareAndSet(false, true))56
throw IllegalStateException(Some("response is already executing"))57
try {58
requestJob.ensureActive()59
when (result.body) {60
case HttpBody.File(val value) -> executeFileResponse(this, result, value, minimal)61
case HttpBody.WebSocket(val value) -> executeWebSocketResponse(62
this,63
result,64
value,65
minimal66
)67
else -> {68
executeResponse(this, prepareRequestResponse(this, result, minimal))69
}70
}71
} catch (error: Throwable) {72
response.releasePreparation()73
throw error74
} finally {75
responding.store(false)76
}77
}79
internal suspend fun abort() {80
if (aborting.compareAndSet(false, true)) {81
if (!response.isFinished)82
response.abort()83
output.abort()84
}85
}86
}