3

Haskell新手在这里!

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

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

谢谢!

4

1 回答 1

3

您可以定义自己的中间件,将此标头添加到每个响应中(所有必要的工具都在 中可用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
于 2017-01-19T19:27:50.380 回答