zio-http icon indicating copy to clipboard operation
zio-http copied to clipboard

Smart content-type response headers for static files

Open ghostdogpr opened this issue 4 years ago • 4 comments

When serving static files, it would be nice if the content-type header was automatically filled for common file types (ex: .js files get application/javascript, etc).

http4s does this here: https://github.com/http4s/http4s/blob/main/core/src/main/scala/org/http4s/StaticFile.scala#L240 https://github.com/http4s/http4s/blob/main/core/src/main/scala/org/http4s/MimeDB.scala

When the content-type is not filled, browsers might refuse to load javascript files.

ghostdogpr avatar May 26 '21 05:05 ghostdogpr

The links are broken. Stable links:

ayakovlenko avatar Aug 18 '21 02:08 ayakovlenko

We currently serve static files by converting the file content into ZStream of content. Great if we have a middleware that can attach content-type header smartly as @ghostdogpr mentioned

API suggestion app @@ smartContentType(url, headerValue)

Spec

  • Add middleware smartContentType
  • Modfiy FileStreaming example to showcase usage of smartContentType header

kaushik143 avatar Oct 29 '21 13:10 kaushik143

I'm interested in taking a stab at this. How should this be implemented as a middleware? The middleware would need access to the file to parse the mimetype, which is not supported as far as I can tell.

Why don't we add a method on HttpApp instead that parses the mimetype and returns the file content as a ZStream?

olivierdeckers avatar Nov 17 '21 19:11 olivierdeckers

I just realized we don't need to get the mime type from the file if we base ourselves on the file extension in the url.

Does this resemble what you had in mind? https://github.com/olivierdeckers/zio-http/commit/8abfa9274fd222c59e7c2bdde6bff18ed31c723a

olivierdeckers avatar Nov 17 '21 19:11 olivierdeckers

I think this is done (Http.fromFile etc are using MimeDb to set the content type)

vigoo avatar Jan 21 '23 14:01 vigoo