{-# LANGUAGE DeriveDataTypeable, FlexibleContexts #-}
module Happstack.Server.Cookie
( Cookie(..)
, CookieLife(..)
, SameSite(..)
, mkCookie
, addCookie
, addCookies
, expireCookie
)
where
import Control.Monad.Trans (MonadIO(..))
import Happstack.Server.Internal.Monads (FilterMonad, composeFilter)
import Happstack.Server.Internal.Cookie (Cookie(..), CookieLife(..), SameSite(..), calcLife, mkCookie, mkCookieHeader)
import Happstack.Server.Types (Response, addHeader)
addCookie :: (MonadIO m, FilterMonad Response m) => CookieLife -> Cookie -> m ()
addCookie :: CookieLife -> Cookie -> m ()
addCookie CookieLife
life Cookie
cookie =
do Maybe (Int, UTCTime)
l <- IO (Maybe (Int, UTCTime)) -> m (Maybe (Int, UTCTime))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe (Int, UTCTime)) -> m (Maybe (Int, UTCTime)))
-> IO (Maybe (Int, UTCTime)) -> m (Maybe (Int, UTCTime))
forall a b. (a -> b) -> a -> b
$ CookieLife -> IO (Maybe (Int, UTCTime))
calcLife CookieLife
life
(String -> String -> m ()
forall r (m :: * -> *).
(FilterMonad r m, HasHeaders r) =>
String -> String -> m ()
addHeaderM String
"Set-Cookie") (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$ Maybe (Int, UTCTime) -> Cookie -> String
mkCookieHeader Maybe (Int, UTCTime)
l Cookie
cookie
where
addHeaderM :: String -> String -> m ()
addHeaderM String
a String
v = (r -> r) -> m ()
forall a (m :: * -> *). FilterMonad a m => (a -> a) -> m ()
composeFilter ((r -> r) -> m ()) -> (r -> r) -> m ()
forall a b. (a -> b) -> a -> b
$ \r
res-> String -> String -> r -> r
forall r. HasHeaders r => String -> String -> r -> r
addHeader String
a String
v r
res
addCookies :: (MonadIO m, FilterMonad Response m) => [(CookieLife, Cookie)] -> m ()
addCookies :: [(CookieLife, Cookie)] -> m ()
addCookies = ((CookieLife, Cookie) -> m ()) -> [(CookieLife, Cookie)] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ((CookieLife -> Cookie -> m ()) -> (CookieLife, Cookie) -> m ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry CookieLife -> Cookie -> m ()
forall (m :: * -> *).
(MonadIO m, FilterMonad Response m) =>
CookieLife -> Cookie -> m ()
addCookie)
expireCookie :: (MonadIO m, FilterMonad Response m) => String -> m ()
expireCookie :: String -> m ()
expireCookie String
name = CookieLife -> Cookie -> m ()
forall (m :: * -> *).
(MonadIO m, FilterMonad Response m) =>
CookieLife -> Cookie -> m ()
addCookie CookieLife
Expired (String -> String -> Cookie
mkCookie String
name String
"")