{-# LANGUAGE FlexibleContexts, CPP #-}
module Happstack.Server.Monads
(
ServerPartT
, ServerPart
, Happstack
, ServerMonad(..)
, mapServerPartT
, mapServerPartT'
, UnWebT
, filterFun
, FilterMonad(..)
, ignoreFilters
, addHeaderM
, getHeaderM
, setHeaderM
, neverExpires
, WebMonad(..)
, escape
, escape'
, require
, requireM
, escapeHTTP
) where
import Control.Applicative (Alternative, Applicative)
import Control.Monad (MonadPlus(mzero))
#if !MIN_VERSION_transformers(0,6,0)
import Control.Monad.Trans.Error (Error, ErrorT)
#endif
import Control.Monad.Trans (MonadIO(..),MonadTrans(lift))
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Reader (ReaderT)
import qualified Control.Monad.Writer.Lazy as Lazy (WriterT)
import qualified Control.Monad.Writer.Strict as Strict (WriterT)
import qualified Control.Monad.State.Lazy as Lazy (StateT)
import qualified Control.Monad.State.Strict as Strict (StateT)
import qualified Control.Monad.RWS.Lazy as Lazy (RWST)
import qualified Control.Monad.RWS.Strict as Strict (RWST)
import qualified Data.ByteString.Char8 as B
import Data.Monoid (Monoid)
import Happstack.Server.Internal.Monads
import Happstack.Server.Types (Response, addHeader, getHeader, setHeader)
import Happstack.Server.RqData (HasRqData)
class ( ServerMonad m, WebMonad Response m, FilterMonad Response m
, MonadIO m, MonadPlus m, HasRqData m, Monad m, Functor m
, Applicative m, Alternative m) => Happstack m
instance (Functor m, Monad m, MonadPlus m
, MonadIO m) => Happstack (ServerPartT m)
instance (Happstack m) => Happstack (Lazy.StateT s m)
instance (Happstack m) => Happstack (Strict.StateT s m)
instance (Happstack m) => Happstack (ReaderT r m)
instance (Happstack m, Monoid w) => Happstack (Lazy.WriterT w m)
instance (Happstack m, Monoid w) => Happstack (Strict.WriterT w m)
instance (Happstack m, Monoid w) => Happstack (Lazy.RWST r w s m)
instance (Happstack m, Monoid w) => Happstack (Strict.RWST r w s m)
#if !MIN_VERSION_transformers(0,6,0)
instance (Happstack m, Error e) => Happstack (ErrorT e m)
#endif
instance (Happstack m, Monoid e) => Happstack (ExceptT e m)
getHeaderM :: (ServerMonad m) => String -> m (Maybe B.ByteString)
String
a = forall (m :: * -> *). ServerMonad m => m Request
askRq forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall r. HasHeaders r => String -> r -> Maybe ByteString
getHeader String
a)
addHeaderM :: (FilterMonad Response m) => String -> String -> m ()
String
a String
v = forall a (m :: * -> *). FilterMonad a m => (a -> a) -> m ()
composeFilter forall a b. (a -> b) -> a -> b
$ \Response
res-> forall r. HasHeaders r => String -> String -> r -> r
addHeader String
a String
v Response
res
setHeaderM :: (FilterMonad Response m) => String -> String -> m ()
String
a String
v = forall a (m :: * -> *). FilterMonad a m => (a -> a) -> m ()
composeFilter forall a b. (a -> b) -> a -> b
$ \Response
res -> forall r. HasHeaders r => String -> String -> r -> r
setHeader String
a String
v Response
res
neverExpires :: (FilterMonad Response m) => m ()
neverExpires :: forall (m :: * -> *). FilterMonad Response m => m ()
neverExpires = forall (m :: * -> *).
FilterMonad Response m =>
String -> String -> m ()
setHeaderM String
"Expires" String
"Mon, 31 Dec 2035 12:00:00 GMT"
require :: (MonadIO m, MonadPlus m) => IO (Maybe a) -> (a -> m r) -> m r
require :: forall (m :: * -> *) a r.
(MonadIO m, MonadPlus m) =>
IO (Maybe a) -> (a -> m r) -> m r
require IO (Maybe a)
fn a -> m r
handle = do
Maybe a
mbVal <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO (Maybe a)
fn
case Maybe a
mbVal of
Maybe a
Nothing -> forall (m :: * -> *) a. MonadPlus m => m a
mzero
Just a
a -> a -> m r
handle a
a
requireM :: (MonadTrans t, Monad m, MonadPlus (t m)) => m (Maybe a) -> (a -> t m r) -> t m r
requireM :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
(MonadTrans t, Monad m, MonadPlus (t m)) =>
m (Maybe a) -> (a -> t m r) -> t m r
requireM m (Maybe a)
fn a -> t m r
handle = do
Maybe a
mbVal <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Maybe a)
fn
case Maybe a
mbVal of
Maybe a
Nothing -> forall (m :: * -> *) a. MonadPlus m => m a
mzero
Just a
a -> a -> t m r
handle a
a