ktor icon indicating copy to clipboard operation
ktor copied to clipboard

Ktor v3.1.1 broke staticFiles index

Open loxal opened this issue 9 months ago • 4 comments

staticFiles(remotePath = "/", dir = File("static"), index = "index.html") // worked in Ktor v3.1.0 and does not work in Ktor v3.1.1

Instead of the index.html I get a 404 for / now.

loxal avatar Feb 25 '25 22:02 loxal

Unfortunately, I cannot reproduce the problem by making a GET / request to the following server:

embeddedServer(Netty, port = 8080) {
    routing {
        staticFiles(remotePath = "/", dir = File("static"), index = "index.html")
    }
}.start(wait = true)

Here is the file structure:

static
└── index.html

Please share a code snippet to reproduce the bug.

Stexxe avatar Feb 26 '25 09:02 Stexxe

The code snippet is pretty much what you have tried but the context might be different:

Image

curl -v http://localhost:1180/ # gives me a 404 curl -v http://localhost:1180/index.html # gives me a 200

Below is my HUtils.kt prelude:

fun Application.hutils() {
// I've commented everything BELOW  <<<<<<<<<<<<<<<<<
    mapper.registerModule(JavaTimeModule())
    install(StatusPages) { 
        status(
            HttpStatusCode.InternalServerError,
            HttpStatusCode.BadGateway,
            HttpStatusCode.ServiceUnavailable,
            HttpStatusCode.Unauthorized,
            HttpStatusCode.NotFound,
        ) { call, status ->
            if (call.request.host() == "www.domain.com") {
                val oopsPageContent = File("static/pups.html").readText()
                call.respondText(
                    text = oopsPageContent,
                    contentType = ContentType.Text.Html,
                    status = status,
                )
            }
        }
        exception<Exception> { call, cause ->
            redirectToErrorPage(call, HttpStatusCode.InternalServerError)
        }
    }
// I've commented everything ABOVE but this did not solve the issue <<<<<<<<<<<<<<<<<
    install(WebSockets) {
        pingPeriodMillis = Duration.ofMinutes(1).toMillis()
    }
    routing {

My application.conf:

ktor {
  development = true
  deployment {
    environment = prod
    port = 1180

    autoreload = true
    watch = ["classes"]
  }


  application {
    modules = [
      net.loxal.hutils.HUtilsKt.hutils
      net.loxal.hutils.RedirectsKt.redirects
    ]
  }
}

loxal avatar Feb 26 '25 11:02 loxal

Can you please share the complete routing block?

Stexxe avatar Feb 26 '25 12:02 Stexxe

@Stexxe thanks for essentially helping me to debug my callee code. I had a lot of routing implementation and commented them out one by one...

        get {
            // some blub
        }

...was the culprit which was basically a catch-all for all GET requests. It turns out it was not working as expected in the past and the latest update just made the the issue visible. Removing the "catch-all" GET implementation

get {}

...fixed the issue. The get {} was hijacking the requests heading towards /.

loxal avatar Feb 26 '25 12:02 loxal