scoop:scoop.http.server:0.1.0
src/request_body.scoop
Selected production source · src/request_body.scoop
1
package scoop.http.server3
import scoop.asyncio.AsyncByteSource4
import scoop.http.HttpField5
import scoop.json.Json6
import scoop.url.*8
/** A request body with one explicit consumption path and bounded input and decoding. */9
public class HttpRequestBody internal constructor(10
source: AsyncByteSource,11
limit: Long? = Some(8388608L),12
private val fields: Array<HttpField> = [],13
/** The server-configured default limit used by aggregate helpers. */14
public val bufferedBodyLimit: Long = 2097152L15
) {16
private val state: RequestBodyState = RequestBodyState(source, limit, fields)17
internal val receivedBytes: Long get() = state.receivedBytes18
internal val deliveredBytes: Long get() = state.deliveredBytes20
/** Whether the selected source has reached its normal, validated EOF. */21
public val isComplete: Boolean get() = state.isComplete22
/** Selected decoding and length facts, or None before a consumer is chosen. */23
public val view: HttpRequestBodyView? get() = state.view25
/** Claims the body once, applying an installed request decoding policy. */26
public fun openStream(): AsyncByteSource = state.open(false)27
/** Claims the encoded body once, bypassing any installed content decoding. */28
public fun openRawStream(): AsyncByteSource = state.open(true)30
/** Selects one lazy form-data reader, whose local resources also belong to this request. */31
public fun multipart(32
options: RequestMultipartOptions = requestMultipartOptions()33
): RequestMultipart = state.multipart(requestMultipartBoundary(fields), options)35
internal fun takeViewsToClose(): RequestBodyViews? = state.takeViewsToClose()36
internal fun configureDecoding(options: RequestDecompressionOptions) {37
state.configure(options)38
}40
/** Overrides the encoded input limit before selecting a consumption path; None disables it. */41
public fun setLimit(maxBytes: Long?) {42
state.setLimit(maxBytes)43
}45
/** Reads one complete body into a bounded independent snapshot. */46
public suspend fun bytes(maxBytes: Long = bufferedBodyLimit): Array<Byte> =47
aggregate(maxBytes, false)49
private suspend fun aggregate(maxBytes: Long, structured: Boolean): Array<Byte> {50
if (maxBytes < 0L)51
throw IllegalArgumentException(Some("negative aggregate body limit"))52
return aggregateRequestBody(state.open(false, structured), maxBytes)53
}55
/** Reads a complete, bounded, strictly UTF-8 request body. */56
public suspend fun text(maxBytes: Long = bufferedBodyLimit): String =57
inputText(bytes(maxBytes), "request body")59
/** Decodes one bounded UTF-8 JSON body using an explicit codec. */60
public suspend fun <T> json(codec: ValueDecoder<T>, maxBytes: Long = bufferedBodyLimit): T {61
requireStructuredMedia(fields, true)62
val value = inputText(aggregate(maxBytes, true), "request body")63
try {64
return Json.decodeFromString(value, codec)65
}66
catch (_: DecodingException) {67
reject(400U, "invalid JSON request body")68
}69
}71
/** Decodes one bounded JSON body using the type's companion codec. */72
public suspend fun <T> json(maxBytes: Long = bufferedBodyLimit): T73
where T.Companion: ValueDecoder<T> = json(T.Companion, maxBytes)75
/** Decodes a bounded form body, where plus represents a space. */76
public suspend fun form(maxBytes: Long = bufferedBodyLimit): RequestQuery {77
requireStructuredMedia(fields, false)78
val value = inputText(aggregate(maxBytes, true), "request body")79
try {80
return RequestQuery(FormUrlEncoded.parse(value))81
}82
catch (_: UrlParseException) {83
reject(400U, "invalid form request body")84
}85
}86
}