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?

4

1 回答 1

1

你想要addHeader来自 Web.Scotty 的函数:

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

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

例子:

{-#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

对http://localhost:3000/some/image的请求会生成一个名为“image”的文件,其内容类型为image/png

在此处输入图像描述

于 2019-03-20T06:22:03.807 回答