-- |
--
-- Since 3.0.3
module Network.Wai.Middleware.AddHeaders
    ( addHeaders
    ) where

import Network.HTTP.Types   (Header)
import Network.Wai          (Middleware, modifyResponse, mapResponseHeaders)
import Network.Wai.Internal (Response(..))
import Data.ByteString      (ByteString)

import qualified Data.CaseInsensitive as CI
import Control.Arrow (first)

addHeaders :: [(ByteString, ByteString)] -> Middleware
-- ^ Prepend a list of headers without any checks
--
-- Since 3.0.3

addHeaders :: [(ByteString, ByteString)] -> Middleware
addHeaders [(ByteString, ByteString)]
h = (Response -> Response) -> Middleware
modifyResponse ((Response -> Response) -> Middleware)
-> (Response -> Response) -> Middleware
forall a b. (a -> b) -> a -> b
$ [Header] -> Response -> Response
addHeaders' (((ByteString, ByteString) -> Header)
-> [(ByteString, ByteString)] -> [Header]
forall a b. (a -> b) -> [a] -> [b]
map ((ByteString -> CI ByteString) -> (ByteString, ByteString) -> Header
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first ByteString -> CI ByteString
forall s. FoldCase s => s -> CI s
CI.mk) [(ByteString, ByteString)]
h)

addHeaders' :: [Header] -> Response -> Response
addHeaders' :: [Header] -> Response -> Response
addHeaders' [Header]
h = ([Header] -> [Header]) -> Response -> Response
mapResponseHeaders (\[Header]
hs -> [Header]
h [Header] -> [Header] -> [Header]
forall a. [a] -> [a] -> [a]
++ [Header]
hs)