scoop:scoop.http.server:0.1.0
src/response.scoop
Selected production source · src/response.scoop
1
package scoop.http.server3
import scoop.http.*5
/** A mutable response draft, valid only during its beforeCommit callback sequence. */6
public class HttpResponseDraft internal constructor(initialStatus: UInt, fields: List<HttpField>) {7
private val values: ArrayList<HttpField> = ArrayList<HttpField>()8
private var writable: Boolean = true9
private var currentStatus: UInt = initialStatus10
init {11
for (value in fields)12
values.add(value)13
}15
/** The final response status, writable only during preparation. */16
public var status: UInt17
get() = currentStatus18
set(value) {19
requireMutable();20
currentStatus = value21
}22
/** Returns an independent ordered snapshot of the current draft fields. */23
public val fields: Array<HttpField> get() = values.toArray()24
/** Adds an independent field without combining repeated values. */25
public fun add(value: HttpField) {26
requireMutable();27
values.add(value)28
}29
/** Removes every field with this ASCII case-insensitive name. */30
public fun remove(name: String) {31
requireMutable()32
val bytes = name.toByteArray()33
var index = values.size34
while (index > 0L) {35
index -= 1L36
if (values[index].hasName(bytes))37
values.removeAt(index)38
}39
}40
/** Replaces all fields with the new field's name. */41
public fun set(value: HttpField) {42
remove(String.fromUtf8(value.name));43
add(value)44
}46
private fun requireMutable() {47
if (!writable)48
throw IllegalStateException(Some("response draft is no longer mutable"))49
}50
internal fun seal() {51
writable = false52
}53
}55
internal enum HeaderChange {56
Set(HttpField), Add(HttpField), Remove(String)57
}59
/** Response mutations allowed before execution, with read-only completion facts afterwards. */60
public class HttpResponse internal constructor() {61
/** Appends or expires cookies as independent Set-Cookie fields. */62
public val cookies: ResponseCookies get() = ResponseCookies(this)63
internal val lock: ServerLock = ServerLock()64
private var changes: ArrayList<HeaderChange>? = None65
private var callbacks: ArrayList<(HttpResponseDraft) -> Unit>? = None66
private var claimed: Boolean = false67
private var committed: Boolean = false68
private var finished: Boolean = false69
private var aborted: Boolean = false70
private var finalStatus: UInt? = None72
/** Whether an irreversible final-response send has been attempted. */73
public val isCommitted: Boolean get() = lock.withLock { committed }74
/** Whether the response body and framing finished successfully. */75
public val isFinished: Boolean get() = lock.withLock { finished }76
/** Whether response execution was aborted. */77
public val isAborted: Boolean get() = lock.withLock { aborted }78
/** The submitted final status, or None before submission. */79
public val status: UInt? get() = lock.withLock { finalStatus }81
private fun requireMutable() {82
if (claimed || committed || finished || aborted)83
throw IllegalStateException(Some("response is no longer mutable"))84
}86
/** Replaces all occurrences of the field's name at response preparation. */87
public fun set(value: HttpField) {88
change(HeaderChange.Set(value))89
}90
/** Appends one field at response preparation, preserving duplicates. */91
public fun add(value: HttpField) {92
change(HeaderChange.Add(value))93
}94
/** Removes all matching fields at response preparation. */95
public fun remove(name: String) {96
change(HeaderChange.Remove(name))97
}99
private fun change(value: HeaderChange) {100
lock.withLock {101
requireMutable()102
if (!present(changes))103
changes = Some(ArrayList<HeaderChange>())104
changes!!.add(value)105
}106
}107
/** Registers a non-suspending draft callback; it runs at most once. */108
public fun beforeCommit(callback: (HttpResponseDraft) -> Unit) {109
lock.withLock {110
requireMutable()111
if (!present(callbacks))112
callbacks = Some(ArrayList<(HttpResponseDraft) -> Unit>())113
callbacks!!.add(callback)114
}115
}117
internal fun claimUnchanged(): Boolean =118
lock.withLock {119
requireMutable()120
if (present(changes) || present(callbacks))121
return@withLock false122
claimed = true123
true124
}126
internal fun prepare(result: HttpResult, minimal: Boolean): HttpResponseDraft {127
val snapshot =128
lock.withLock {129
requireMutable()130
claimed = true131
val pending =132
if (present(callbacks))133
callbacks!!.toArray()134
else135
[]136
callbacks = None137
return@withLock (138
if (present(changes))139
changes!!.toArray()140
else141
[],142
pending143
)144
}145
val(edits, beforeCommit) = snapshot146
val draft = HttpResponseDraft(result.status, result.fields)147
if (!minimal) {148
for (change in edits)149
when (change) {150
case HeaderChange.Set(val value) -> draft.set(value)151
case HeaderChange.Add(val value) -> draft.add(value)152
case HeaderChange.Remove(val name) -> draft.remove(name)153
}154
try {155
for (callback in beforeCommit)156
callback(draft)157
}158
catch (error: Throwable) {159
draft.seal();160
throw CommitCallbackFailure(error)161
}162
}163
draft.seal()164
return draft165
}167
internal fun releasePreparation() {168
lock.withLock {169
if (!committed && !aborted)170
claimed = false;171
Unit172
}173
}174
internal fun commit(status: UInt) {175
lock.withLock {176
committed = true;177
finalStatus = Some(status)178
}179
}180
internal fun finish() {181
lock.withLock { finished = true }182
}183
internal fun abort() {184
lock.withLock { aborted = true }185
}186
}188
internal class CommitCallbackFailure(val original: Throwable): Exception(189
Some("beforeCommit failed")190
)