zio / zio-http

A next-generation Scala framework for building scalable, correct, and efficient HTTP clients and servers
https://zio.dev/zio-http
Apache License 2.0
794 stars 401 forks source link

Resource handlers fail to handle special characters #2525

Open cqlia opened 11 months ago

cqlia commented 11 months ago

Describe the bug Resource related methods such as Handler.getResourceAsFile fail when special URL characters (like spaces) are used in the resource's path.

To Reproduce Steps to reproduce the behaviour:

  1. Create a basic HTTP application
  2. Add a new file in the resources with a space in its name
  3. Define a new route that responds with a Handler resource method: Handler.fromResource("/resource with spaces")
  4. Fetch this route and observe the resulting error - the resource was found by the class but the handler is unable to find it (returns 404 ZIO HTTP Resource /resource with spaces not found)

Expected behaviour The resource should be loaded and sent as the response.

jgoday commented 2 months ago

Tested with the latest zio-http version (3.0.0-RC9), and seems to work correctly.

val routes: Routes[Any, Response] = Routes(
    Method.GET / Root / "test" -> Handler
      .fromResource("sample file.txt")
      .mapError(_ => Response.status(Status.NotFound)),
  )
curl -vi -X GET http://localhost:8080/test

> GET / HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/8.6.0
> Accept: */*
> 
< HTTP/1.1 200 OK

Could the leading slash in the resource name be the problem ?