Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ktor v3.1.1 broke staticFiles index #4706

Open
loxal opened this issue Feb 25, 2025 · 4 comments
Open

Ktor v3.1.1 broke staticFiles index #4706

loxal opened this issue Feb 25, 2025 · 4 comments

Comments

@loxal
Copy link
Contributor

loxal commented Feb 25, 2025

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.

@Stexxe
Copy link
Contributor

Stexxe commented Feb 26, 2025

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.

@loxal
Copy link
Contributor Author

loxal commented Feb 26, 2025

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
    ]
  }
}

@Stexxe
Copy link
Contributor

Stexxe commented Feb 26, 2025

Can you please share the complete routing block?

@loxal
Copy link
Contributor Author

loxal commented Feb 26, 2025

@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 /.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants