module Network.Wai.Routing.Predicate.Cookie
( Cookie
, HasCookie
, cookie
, hasCookie
) where
import Data.ByteString (ByteString)
import Data.ByteString.From
import Data.Monoid
import Network.HTTP.Types.Status
import Network.Wai.Routing.Error
import Network.Wai.Routing.Internal
import Network.Wai.Routing.Predicate.Predicate
import Network.Wai.Routing.Request
newtype Cookie a = Cookie ByteString
cookie :: ByteString -> Cookie a
cookie = Cookie
instance (FromByteString a) => Predicate (Cookie a) Req where
type FVal (Cookie a) = Error
type TVal (Cookie a) = a
apply (Cookie x) =
rqApply (lookupCookie x) readValues (err status400 (msg x))
newtype HasCookie = HasCookie ByteString
hasCookie :: ByteString -> HasCookie
hasCookie = HasCookie
instance Predicate HasCookie Req where
type FVal HasCookie = Error
type TVal HasCookie = ()
apply (HasCookie x) r =
if null (lookupCookie x r)
then F (err status400 (msg x))
else T 0 ()
msg :: ByteString -> ByteString
msg x = "Missing cookie '" <> x <> "'."