|
| 1 | +package com.httparena.fishcake |
| 2 | + |
| 3 | +import org.codegreen.api.MemoryView |
| 4 | +import org.codegreen.api.content.HandlerBuilder |
| 5 | +import org.codegreen.api.protocol.Request |
| 6 | +import org.codegreen.api.protocol.Response |
| 7 | +import org.codegreen.api.protocol.getEntry |
| 8 | +import org.codegreen.modules.io.Handler |
| 9 | +import org.codegreen.modules.io.Resource |
| 10 | +import org.codegreen.modules.io.guessContentType |
| 11 | +import org.codegreen.modules.io.streaming.ResourceContent |
| 12 | +import java.io.File |
| 13 | + |
| 14 | +/** |
| 15 | + * Serves a directory of static files (the HttpArena "static" profile) with pre-compressed |
| 16 | + * variant selection: when the request carries `Accept-Encoding` and a sibling `<file>.br` or |
| 17 | + * `<file>.gz` exists on disk, that variant is served with the matching `Content-Encoding` and the |
| 18 | + * original file's `Content-Type`. Mirrors GenHTTP's pre-compressed assets (#840) — the bytes are |
| 19 | + * served straight off disk, so no runtime compression is needed here. A missing file yields |
| 20 | + * `null`, which the surrounding layout turns into a 404. |
| 21 | + */ |
| 22 | +object StaticFiles { |
| 23 | + |
| 24 | + // Brotli before gzip: Brotli has the higher priority in GenHTTP's compression set, so when the |
| 25 | + // client accepts both we serve the smaller payload. |
| 26 | + private val PRECOMPRESSED = listOf("br" to ".br", "gzip" to ".gz") |
| 27 | + |
| 28 | + fun from(directory: String): HandlerBuilder { |
| 29 | + val root = File(directory).canonicalFile |
| 30 | + return Handler.from { request -> serve(request, root) } |
| 31 | + } |
| 32 | + |
| 33 | + private fun serve(request: Request, root: File): Response? { |
| 34 | + val relative = request.header.target.asString(decode = true, remainingOnly = true).trimStart('/') |
| 35 | + if (relative.isEmpty() || relative.endsWith("/")) return null |
| 36 | + |
| 37 | + val file = File(root, relative) |
| 38 | + // Containment guard: never serve outside the configured root (e.g. via ".."). |
| 39 | + if (!file.canonicalFile.path.startsWith(root.path + File.separator)) return null |
| 40 | + |
| 41 | + // Content-Type comes from the ORIGINAL name, not the .br/.gz variant. |
| 42 | + val contentType = relative.guessContentType() |
| 43 | + |
| 44 | + val accepted = acceptedEncodings(request) |
| 45 | + for ((algorithm, extension) in PRECOMPRESSED) { |
| 46 | + if (algorithm in accepted) { |
| 47 | + val variant = File(root, relative + extension) |
| 48 | + if (variant.isFile) { |
| 49 | + return request.respond() |
| 50 | + .content(ResourceContent(Resource.fromFile(variant).build(), contentType, MemoryView.ofAscii(algorithm))) |
| 51 | + .build() |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (!file.isFile) return null |
| 57 | + |
| 58 | + return request.respond() |
| 59 | + .content(ResourceContent(Resource.fromFile(file).build(), contentType)) |
| 60 | + .build() |
| 61 | + } |
| 62 | + |
| 63 | + private fun acceptedEncodings(request: Request): Set<String> { |
| 64 | + val header = request.header.headers.getEntry("Accept-Encoding") ?: return emptySet() |
| 65 | + return header.split(',') |
| 66 | + .mapNotNull { part -> part.substringBefore(';').trim().lowercase().takeIf(String::isNotEmpty) } |
| 67 | + .toSet() |
| 68 | + } |
| 69 | +} |
0 commit comments