Haskell-Scotty:设置自定义标头(x-frame-options)



Haskell新手在这里!

在我的 haskell 副项目中,我使用 scotty 来提供一些动态生成的 html 页面。问题是页面无法在iframe内打开,因为"x-frame-options"标题设置为"SAMEORIGIN"。

如何将该标题更改为其他标题?我想为所有响应设置该标题。有没有中间件可以做到这一点?

谢谢!

您可以定义自己的中间件,将此标头添加到每个响应中(所有必需的工具都可以在Network.Wai中找到):

{-# LANGUAGE OverloadedStrings #-}
import Network.Wai -- from the wai package
import Web.Scotty hiding (options)
addFrameHeader :: Middleware
addFrameHeader =
  modifyResponse (mapResponseHeaders (("X-Frame-Options", "whatever") :))

然后在您的 scotty 应用程序中使用它:

main = scotty 6000 $ do
  middleware addFrameHeader
  get "/" (text "hello")

通过curl我们可以看到它包含在响应中:

> curl --include localhost:6000
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Date: Thu, 19 Jan 2017 19:22:57 GMT
Server: Warp/3.2.8
X-Frame-Options: whatever
Content-Type: text/plain; charset=utf-8
hello

相关内容

最新更新