Scoop API

scoop:scoop.http.server:0.1.0

src/files/execution.scoop

Selected production source · src/files/execution.scoop

1package scoop.http.server
3import scoop.asyncio.*
4import scoop.http.*
5import scoop.platform.fs.*
6import scoop.platform.io.UnexpectedEofException
7import scoop.platform.memory.NativeByteBuffer
8import scoop.platform.system.*
9import scoop.platform.time.SystemClock
11internal suspend fun executeFileResponse(
12 ctx: HttpContext,
13 result: HttpResult,
14 description: HttpFileBody,
15 minimal: Boolean
16) {
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 minimal
36 )
37 executeResponse(ctx, plan)
38 }
41private 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.NotDirectory
53 )
54 )
55 throw error
56 }
57 val fallback = description.fallbackFile!!
58 return (fallback, openResponseFilePath(fallback))
61private 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 None
73 }
74 if (present(metadata) && metadata!!.kind == FileKind.Directory)
75 reject(404U, "file not found")
76 }
77 throw error
78 }
81private suspend fun selectedResponseFile(description: HttpFileBody): Path {
82 if (!present(description.indexDocument) && !description.directoryRequired)
83 return description.path
84 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.path
89 val index = description.indexDocument ?: reject(404U, "directory index is disabled")
90 return description.path.resolve(Path.fromString(index))
93private 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 -> 404U
101 case PlatformErrorKind.NotDirectory -> 404U
102 case PlatformErrorKind.PermissionDenied -> 403U
103 else -> throw error
104 }
105 throw HttpRejection(
106 text(
107 if (status == 404U)
108 "file not found"
109 else
110 "file access denied",
111 status
112 ),
113 Some<Throwable>(error)
114 )
115 }
118internal suspend fun copyResponseFile(
119 source: AsyncFile,
120 offset: Long,
121 length: Long,
122 bufferSize: Long,
123 writer: HttpBodyWriter
124) {
125 if (offset != 0L)
126 source.seek(offset)
127 NativeByteBuffer.allocate(bufferSize).use suspend {
128 buffer ->
129 var remaining = length
130 while (remaining > 0L) {
131 buffer.clear()
132 val count =
133 source.readInto(
134 buffer,
135 if (remaining < bufferSize)
136 remaining
137 else
138 bufferSize
139 )
140 if (count == 0L)
141 throw UnexpectedEofException()
142 writer.writeFully(buffer, count)
143 remaining -= count
144 }
145 }