scoop:scoop.http.server:0.1.0
src/files/options.scoop
Selected production source · src/files/options.scoop
1
package scoop.http.server3
import scoop.http.*4
import scoop.platform.fs.Path6
/** Metadata and bounded copying for one explicitly selected file representation. */7
public class FileResponseOptions internal constructor(8
/** Explicit media type; None uses the resolved file's extension. */9
public val contentType: String? = None,10
/** An application-maintained tag for these exact representation bytes. */11
public val entityTag: HttpEntityTag? = None,12
/** Actual content coding of a pre-encoded file; None means identity. */13
public val contentEncoding: String? = None,14
/** Whether the application guarantees Last-Modified is a strong validator. */15
public val lastModifiedIsStrong: Boolean = false,16
/** Maximum bytes read and copied per iteration. */17
public val bufferSize: Long = 8192L18
) {19
init {20
if (bufferSize <= 0L)21
throw IllegalArgumentException(Some("invalid file response buffer size"))22
if (present(contentType)) {23
try {24
HttpMediaType.parse(contentType!!)25
}26
catch (_: HttpFieldParseException) {27
throw IllegalArgumentException(Some("invalid file response media type"))28
}29
}30
if (31
present(contentEncoding) &&32
(!isHttpToken(contentEncoding!!) || contentEncoding!! == "*")33
)34
throw IllegalArgumentException(Some("invalid file content coding"))35
}36
}38
/** A deferred file description, without an open handle or acquired resource. */39
public class HttpFileBody internal constructor(40
/** The application-selected native path. */41
public val path: Path,42
/** Metadata and copying options for this representation. */43
public val options: FileResponseOptions,44
internal val indexDocument: String? = None,45
internal val directoryRequired: Boolean = false,46
internal val fallbackFile: Path? = None47
)49
/** Describes a file to open, inspect, send and close during response execution. */50
public fun file(path: Path, options: FileResponseOptions = fileResponseOptions()): HttpResult =51
httpResult {52
this.body = HttpBody.File(HttpFileBody(path, options))53
this.allowCompression = false54
}56
/** Explicit wildcard mapping, directory indexes and an optional missing-file response. */57
public class StaticFileOptions internal constructor(58
/** Name of the route's terminal wildcard parameter. */59
public val pathParameter: String = "path",60
/** A fixed index filename, attempted only for directories; None disables indexes. */61
public val indexDocument: String? = None,62
/** Options shared by the files served by this handler. */63
public val fileOptions: FileResponseOptions = fileResponseOptions(),64
/** File to serve once for missing GET/HEAD paths; relative paths use the process working directory. */65
public val fallbackFile: Path? = None66
) {67
init {68
if (69
pathParameter.charLength ==70
0L ||71
(present(indexDocument) && !validStaticFileName(indexDocument!!.toByteArray()))72
)73
throw IllegalArgumentException(Some("invalid static file mapping options"))74
}75
}