scoop:scoop.http.server:0.1.0
src/files/execution.scoop
Selected production source · src/files/execution.scoop
1
package scoop.http.server3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.platform.fs.*6
import scoop.platform.io.UnexpectedEofException7
import scoop.platform.memory.NativeByteBuffer8
import scoop.platform.system.*9
import scoop.platform.time.SystemClock11
internal suspend fun executeFileResponse(12
ctx: HttpContext,13
result: HttpResult,14
description: HttpFileBody,15
minimal: Boolean16
) {17
val(path, opened) =18
responseFileInput<(Path, AsyncFile)> suspend { openResponseFile(description) }19
opened.useAsync suspend {20
source ->21
val metadata = responseFileInput<FileMetadata> suspend { source.metadata() }22
if (metadata.kind != FileKind.Regular)23
reject(404U, "file not found")24
val now = SystemClock.now()25
val initial = fileResponseFields(result, path, description.options, metadata, now)26
val draft = ctx.response.prepare(initial, minimal)27
val plan =28
prepareFileResponse(29
ctx,30
source,31
description.options,32
metadata.size,33
now,34
draft,35
minimal36
)37
executeResponse(ctx, plan)38
}39
}41
private suspend fun openResponseFile(description: HttpFileBody): (Path, AsyncFile) {42
try {43
val path = selectedResponseFile(description)44
return (path, openResponseFilePath(path))45
} catch (error: PlatformException) {46
if (47
!present(description.fallbackFile) ||48
(49
error.kind !=50
PlatformErrorKind.NotFound &&51
error.kind !=52
PlatformErrorKind.NotDirectory53
)54
)55
throw error56
}57
val fallback = description.fallbackFile!!58
return (fallback, openResponseFilePath(fallback))59
}61
private suspend fun openResponseFilePath(path: Path): AsyncFile {62
try {63
return currentEventLoop().openFile(path)64
}65
catch (error: PlatformException) {66
if (error.kind == PlatformErrorKind.PermissionDenied) {67
val metadata =68
try {69
Some(currentEventLoop().metadata(path))70
}71
catch (_: PlatformException) {72
None73
}74
if (present(metadata) && metadata!!.kind == FileKind.Directory)75
reject(404U, "file not found")76
}77
throw error78
}79
}81
private suspend fun selectedResponseFile(description: HttpFileBody): Path {82
if (!present(description.indexDocument) && !description.directoryRequired)83
return description.path84
val metadata = currentEventLoop().metadata(description.path)85
if (description.directoryRequired && metadata.kind != FileKind.Directory)86
reject(404U, "directory not found")87
if (metadata.kind != FileKind.Directory)88
return description.path89
val index = description.indexDocument ?: reject(404U, "directory index is disabled")90
return description.path.resolve(Path.fromString(index))91
}93
private suspend fun <T> responseFileInput(block: suspend () -> T): T {94
try {95
return block()96
}97
catch (error: PlatformException) {98
val status =99
when (error.kind) {100
case PlatformErrorKind.NotFound -> 404U101
case PlatformErrorKind.NotDirectory -> 404U102
case PlatformErrorKind.PermissionDenied -> 403U103
else -> throw error104
}105
throw HttpRejection(106
text(107
if (status == 404U)108
"file not found"109
else110
"file access denied",111
status112
),113
Some<Throwable>(error)114
)115
}116
}118
internal suspend fun copyResponseFile(119
source: AsyncFile,120
offset: Long,121
length: Long,122
bufferSize: Long,123
writer: HttpBodyWriter124
) {125
if (offset != 0L)126
source.seek(offset)127
NativeByteBuffer.allocate(bufferSize).use suspend {128
buffer ->129
var remaining = length130
while (remaining > 0L) {131
buffer.clear()132
val count =133
source.readInto(134
buffer,135
if (remaining < bufferSize)136
remaining137
else138
bufferSize139
)140
if (count == 0L)141
throw UnexpectedEofException()142
writer.writeFully(buffer, count)143
remaining -= count144
}145
}146
}