{-# LANGUAGE RankNTypes #-}

module Airship.Headers
    ( addResponseHeader
    , modifyResponseHeaders
    ) where

import Airship.Types (Webmachine, ResponseState(..))
import Control.Monad.State.Class (modify)
import Network.HTTP.Types (ResponseHeaders, Header)

-- | Applies the given function to the 'ResponseHeaders' present in this handlers 'ResponseState'.
modifyResponseHeaders :: Monad m => (ResponseHeaders -> ResponseHeaders) -> Webmachine m ()
modifyResponseHeaders :: (ResponseHeaders -> ResponseHeaders) -> Webmachine m ()
modifyResponseHeaders ResponseHeaders -> ResponseHeaders
f = (ResponseState -> ResponseState) -> Webmachine m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ResponseState -> ResponseState
updateHeaders
    where updateHeaders :: ResponseState -> ResponseState
updateHeaders rs :: ResponseState
rs@ResponseState{stateHeaders :: ResponseState -> ResponseHeaders
stateHeaders = ResponseHeaders
h} = ResponseState
rs { stateHeaders :: ResponseHeaders
stateHeaders = ResponseHeaders -> ResponseHeaders
f ResponseHeaders
h }

-- | Adds a given 'Header' to this handler's 'ResponseState'.
addResponseHeader :: Monad m => Header -> Webmachine m ()
addResponseHeader :: Header -> Webmachine m ()
addResponseHeader Header
h = (ResponseHeaders -> ResponseHeaders) -> Webmachine m ()
forall (m :: * -> *).
Monad m =>
(ResponseHeaders -> ResponseHeaders) -> Webmachine m ()
modifyResponseHeaders (Header
h Header -> ResponseHeaders -> ResponseHeaders
forall a. a -> [a] -> [a]
:)