gpt4 book ai didi

haskell - 如何在 Scotty 中使用静态中间件设置 header ?

转载 作者:行者123 更新时间:2023-12-02 13:39:36 47 4
gpt4 key购买 nike

假设我有一份静态文件,但它们没有扩展名。我想为所有从“some/”提供服务的标题设置标题“Content-Type:image/png”(第一条规则)。如何在这段代码中做到这一点?

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

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

我尝试过这个:

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

并使用Debug.Trace检查请求的路径、查询字符串 - 全部为空,而实际请求为“...:8080/some/somefile”。

正确的做法是什么?

最佳答案

您需要 Web.Scotty 的 addHeader 函数:

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 的文件:

enter image description here

关于haskell - 如何在 Scotty 中使用静态中间件设置 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55240176/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com