scoop:scoop.http.server:0.1.0
src/result.scoop
Selected production source · src/result.scoop
1
package scoop.http.server3
import scoop.http.HttpField4
import scoop.json.Json6
/** The response body; streams acquire and release their own resources during execution. */7
public enum HttpBody {8
/** No response content. */9
Empty,10
/** An immutable byte snapshot. */11
Bytes(Array<Byte>),12
/** A producer executed once inside the request pipeline. */13
Stream(HttpStreamBody),14
/** A file opened and closed by the response executor, including for metadata-only results. */15
File(HttpFileBody),16
/** An explicit HTTP/1 upgrade whose session starts after request cleanup. */17
WebSocket(HttpWebSocketBody)18
}20
/** A suspending producer and its optional original-content length and final trailers. */21
public class HttpStreamBody public constructor(22
/** The producer, invoked only when the request and status permit content. */23
public val produce: suspend (HttpBodyWriter) -> Unit,24
/** The exact original-content length, if known without executing the producer. */25
public val length: Long? = None,26
/** Trailers collected after successful production; never called for HEAD. */27
public val trailers: (() -> List<HttpField>)? = None28
) {29
init {30
if (present(length) && length!! < 0L)31
throw IllegalArgumentException(Some("negative response length"))32
}33
}35
/** An immutable ordinary response description; it does not own an already-open source. */36
public class HttpResult internal constructor(37
/** The final response status, 200 through 599 or 101 for an explicit WebSocket upgrade. */38
public val status: UInt = 200U,39
fields: List<HttpField> = ArrayList<HttpField>(),40
/** The response content, executed within the request scope. */41
public val body: HttpBody = HttpBody.Empty,42
/** Whether an installed compression policy may dynamically transform this representation. */43
public val allowCompression: Boolean = true44
) {45
/** Ordered response fields, including independently preserved Set-Cookie entries. */46
public val fields: Array<HttpField> = fields.toArray()47
internal val preparedBytes: Atomic<PreparedByteResponse?> = Atomic<PreparedByteResponse?>(None)49
init {50
val upgrading =51
when (body) {52
case HttpBody.WebSocket(_) -> true;53
else -> false54
}55
if (56
if (upgrading)57
status != 101U58
else59
status < 200U || status > 599U60
)61
throw IllegalArgumentException(Some("invalid final response status"))62
}63
}65
/** Builds an empty response with the supplied final status and fields. */66
public fun empty(67
status: UInt = 204U,68
fields: List<HttpField> = ArrayList<HttpField>()69
): HttpResult =70
httpResult {71
this.status = status72
this.fields = fields73
}75
/** Copies bytes into a response with an explicit media type. */76
public fun bytes(77
value: List<Byte>,78
contentType: String = "application/octet-stream",79
status: UInt = 200U80
): HttpResult =81
httpResult {82
this.status = status83
this.fields = [field("Content-Type", contentType)]84
this.body = HttpBody.Bytes(value.toArray())85
}87
/** Builds a UTF-8 text response. */88
public fun text(value: String, status: UInt = 200U): HttpResult =89
bytes(value.toByteArray(), "text/plain; charset=utf-8", status)91
/** Encodes a JSON response using an explicit codec. */92
public fun <T> json(value: T, codec: ValueEncoder<T>, status: UInt = 200U): HttpResult =93
bytes(Json.encodeToString(value, codec).toByteArray(), "application/json", status)95
/** Encodes a JSON response using the type's companion codec. */96
public fun <T> json(value: T, status: UInt = 200U): HttpResult97
where T.Companion: ValueEncoder<T> = json(value, T.Companion, status)99
/** Builds a redirect without acquiring any response resource. */100
public fun redirect(location: String, status: UInt = 303U): HttpResult {101
if (status != 301U && status != 302U && status != 303U && status != 307U && status != 308U)102
throw IllegalArgumentException(Some("invalid redirect status"))103
return empty(status, [field("Location", location)])104
}106
/** Builds a streaming response whose producer remains inside all enclosing middleware scopes. */107
public fun stream(108
contentType: String,109
produce: suspend (HttpBodyWriter) -> Unit,110
length: Long? = None,111
status: UInt = 200U,112
trailers: (() -> List<HttpField>)? = None113
): HttpResult =114
httpResult {115
this.status = status116
this.fields = [field("Content-Type", contentType)]117
this.body = HttpBody.Stream(HttpStreamBody(produce, length, trailers))118
}120
internal fun bodyLength(body: HttpBody): Long? =121
when (body) {122
case HttpBody.Empty -> Some(0L)123
case HttpBody.Bytes(val value) -> Some(value.size)124
case HttpBody.Stream(val value) -> value.length125
case HttpBody.File(_) -> None126
case HttpBody.WebSocket(_) -> None127
}