From 2d4696337675c352f49af3d308990dfbadb9c613 Mon Sep 17 00:00:00 2001 From: David Wursteisen Date: Sun, 26 Nov 2023 15:17:55 +0100 Subject: [PATCH] Fix issue with tiny-cli serve returning HEAD OK with missing resource --- .../minigdx/tiny/cli/command/ServeCommand.kt | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/tiny-cli/src/jvmMain/kotlin/com/github/minigdx/tiny/cli/command/ServeCommand.kt b/tiny-cli/src/jvmMain/kotlin/com/github/minigdx/tiny/cli/command/ServeCommand.kt index f12f12ba..ebd0440c 100644 --- a/tiny-cli/src/jvmMain/kotlin/com/github/minigdx/tiny/cli/command/ServeCommand.kt +++ b/tiny-cli/src/jvmMain/kotlin/com/github/minigdx/tiny/cli/command/ServeCommand.kt @@ -9,6 +9,7 @@ import com.github.ajalt.clikt.parameters.types.int import io.ktor.http.ContentType import io.ktor.http.HttpStatusCode import io.ktor.server.application.Application +import io.ktor.server.application.ApplicationCall import io.ktor.server.application.call import io.ktor.server.engine.embeddedServer import io.ktor.server.netty.Netty @@ -17,6 +18,7 @@ import io.ktor.server.response.respondBytes import io.ktor.server.routing.get import io.ktor.server.routing.head import io.ktor.server.routing.routing +import io.ktor.util.pipeline.PipelineContext import java.io.FileInputStream import java.util.zip.ZipInputStream @@ -57,18 +59,14 @@ class ServeCommand : CliktCommand(name = "serve", help = "Run your game as a web val method = fun Application.() { routing { head("/{...}") { - call.respond(HttpStatusCode.OK) + if (resources.containsKey(resourceKey())) { + call.respond(HttpStatusCode.OK) + } else { + call.respond(HttpStatusCode.NotFound) + } } get("/{...}") { - val key = call.request.local.uri.let { k -> - // Small hack as the engine add a /. - // Need to fix it... - if (k.startsWith("/")) { - k.drop(1) - } else { - k - } - } + val key = resourceKey() if (resources.containsKey(key)) { val value = resources[key] if (value != null) { @@ -113,4 +111,17 @@ class ServeCommand : CliktCommand(name = "serve", help = "Run your game as a web // start a browser to the address // route to files from the zip. } + + private fun PipelineContext.resourceKey(): String { + val key = call.request.local.uri.let { k -> + // Small hack as the engine add a /. + // Need to fix it... + if (k.startsWith("/")) { + k.drop(1) + } else { + k + } + } + return key + } }