X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=Lucu.git;a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FAbortion.hs;h=064a97ebd8671232b9053a6382d93c85927df318;hp=6c03e8b6732bf5332bbf14792edfda23631a03aa;hb=51eda5b;hpb=a44a96d95b5fcbaf24a21c0336046ce0c3bab614 diff --git a/Network/HTTP/Lucu/Abortion.hs b/Network/HTTP/Lucu/Abortion.hs index 6c03e8b..064a97e 100644 --- a/Network/HTTP/Lucu/Abortion.hs +++ b/Network/HTTP/Lucu/Abortion.hs @@ -1,81 +1,50 @@ +{-# LANGUAGE + UnicodeSyntax + #-} +-- |Aborting the computation of 'Network.HTTP.Lucu.Resource.Resource' +-- in any 'Prelude.IO' monads or arrows. module Network.HTTP.Lucu.Abortion - ( Abortion(..) - , abort -- MonadIO m => StatusCode -> [ (String, String) ] -> Maybe String -> m a - , abortSTM -- StatusCode -> [ (String, String) ] -> Maybe String -> STM a - , abortA -- ArrowIO a => StatusCode -> [ (String, String) ] -> Maybe String -> a b c - , abortPage -- Config -> Maybe Request -> Maybe Response -> Abortion -> String + ( Abortion + , mkAbortion + , mkAbortion' + + , abort ) where - -import Control.Arrow -import Control.Arrow.ArrowIO -import Control.Concurrent.STM -import Control.Exception -import Control.Monad.Trans -import GHC.Conc (unsafeIOToSTM) -import Data.Dynamic -import Network.HTTP.Lucu.Config -import Network.HTTP.Lucu.DefaultPage -import Network.HTTP.Lucu.Headers -import Network.HTTP.Lucu.HttpVersion -import Network.HTTP.Lucu.Request -import Network.HTTP.Lucu.Response -import System.IO.Unsafe -import Text.XML.HXT.Arrow.WriteDocument -import Text.XML.HXT.Arrow.XmlArrow -import Text.XML.HXT.Arrow.XmlIOStateArrow -import Text.XML.HXT.DOM.XmlKeywords - - -data Abortion = Abortion { - aboStatus :: StatusCode - , aboHeaders :: Headers - , aboMessage :: Maybe String - } deriving (Show, Typeable) - - -abort :: MonadIO m => StatusCode -> [ (String, String) ] -> Maybe String -> m a -abort status headers msg - = let abo = Abortion status headers msg - exc = DynException (toDyn abo) - in - liftIO $ throwIO exc - - -abortSTM :: StatusCode -> [ (String, String) ] -> Maybe String -> STM a -abortSTM status headers msg - = unsafeIOToSTM $ abort status headers msg - - -abortA :: ArrowIO a => StatusCode -> [ (String, String) ] -> Maybe String -> a b c -abortA status headers msg - = arrIO0 $ abort status headers msg - - --- aboMessage が Just なら單に mkDefaultPage に渡すだけで良いので樂だが、 --- Nothing の場合は getDefaultPage を使ってデフォルトのメッセージを得な --- ければならない。しかもその時は resM から Response を捏造までする必要 --- がある。 -abortPage :: Config -> Maybe Request -> Maybe Response -> Abortion -> String -abortPage conf reqM resM abo - = let msg = case aboMessage abo of - Just msg -> msg - Nothing -> let res' = case resM of - Just res -> res { resStatus = aboStatus abo } - Nothing -> Response { - resVersion = HttpVersion 1 1 - , resStatus = aboStatus abo - , resHeaders = [] - } - res = foldl (.) id [setHeader name value - | (name, value) <- aboHeaders abo] - $ res' - in - getDefaultPage conf reqM res - [html] = unsafePerformIO - $ runX ( mkDefaultPage conf (aboStatus abo) (txt msg) - >>> - writeDocumentToString [(a_indent, v_1)] - ) - in - html +import Control.Exception +import Control.Monad.Trans +import Data.Ascii (Ascii, CIAscii) +import Data.Monoid.Unicode +import Data.Text (Text) +import Network.HTTP.Lucu.Abortion.Internal +import Network.HTTP.Lucu.Headers +import Network.HTTP.Lucu.Response +import Prelude.Unicode + +-- |Construct an 'Abortion' with additional headers and an optional +-- message text. +mkAbortion ∷ StatusCode sc ⇒ sc → [(CIAscii, Ascii)] → Maybe Text → Abortion +{-# INLINE mkAbortion #-} +mkAbortion sc hdr msg + = Abortion { + aboStatus = fromStatusCode sc + , aboHeaders = toHeaders hdr + , aboMessage = msg + } + +-- |Construct an 'Abortion' without any additional headers but with a +-- message text. +mkAbortion' ∷ StatusCode sc ⇒ sc → Text → Abortion +{-# INLINE mkAbortion' #-} +mkAbortion' sc msg + = Abortion { + aboStatus = fromStatusCode sc + , aboHeaders = (∅) + , aboMessage = Just msg + } + +-- |Throw an 'Abortion' in a 'MonadIO', including the very +-- 'Network.HTTP.Lucu.Resource.Resource' monad. +abort ∷ MonadIO m ⇒ Abortion → m a +{-# INLINE abort #-} +abort = liftIO ∘ throwIO