Search code examples
haskellscottyhaskell-wai

Haskell-Scotty: Set custom headers (x-frame-options)


Haskell newbie here!

In my haskell side project, I am using scotty to serve some dynamically generated html pages. The problem is that the pages can not be opened inside an iframe, due to the "x-frame-options" header set to "SAMEORIGIN".

How can I change that header to something different? I'd like to set that header for all responses. Is there a Middleware that can do that?

Thanks!


Solution

  • You can define your own middleware that adds this header to each response (all the tools neccesary are available in 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") :))
    

    Then use it in your scotty application:

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

    And with curl we can see that it is included in the response:

    > 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