3

Suppose I have a serving of static files, but they are without extension. I want to set header "Content-Type: image/png" for all of them which are serving from "some/" (1st rule). How to do it in this code?

import Network.Wai.Middleware.Static
import Web.Scotty

routes :: Scotty M ()
routes = do
  ...
  middleware $ staticPolicy $
    contains "some/" >-> (addBase "/something/files/")
    <|>
    addBase "/elsewhere/"

I tried this:

setContentType :: Middleware
setContentType = modifyResponse $ mapResponseHeaders 
(("Content-Type", "image/png"):)

reqInfixedWith :: Text -> Request -> Bool
reqInfixedWith str req =
  isInfixOf str $ convertString $ rawQueryString req

...

-- in routes
  middleware $ ifRequest (reqInfixedWith "some/") setContentType

and checked with Debug.Trace request's path, query string - all are empty, while actual request is "...:8080/some/somefile".

What is the correct way to do it?

RandomB
  • 3,367
  • 19
  • 30

1 Answers1

1

You want the addHeader function from Web.Scotty:

http://hackage.haskell.org/package/scotty-0.11.3/docs/Web-Scotty.html

addHeader :: Text -> Text -> ActionM ()

Example:

{-#Language OverloadedStrings#-}
import           Network.Wai.Middleware.Static
import           Web.Scotty

main :: IO ()
main = do
  scotty 3000 $ do
    middleware static
    get "/some/:file" $ do
      f <- param "file"
      addHeader "Content-Type" "image/png"
      file f

A request to http://localhost:3000/some/image yields a file named "image" with a content type of image/png:

enter image description here

Nick Acosta
  • 1,890
  • 17
  • 19