X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FAbortion.hs;h=db32c1d95292975a4185d6297407f87f123296fd;hb=fffa09842d060c7d738084125dea07783d84aefe;hp=b9e4b116250cedee3692aedb9a082ece6d812420;hpb=34b0ad7f2ffc5e7ca67fa3468d01c5551edc8a8b;p=Lucu.git diff --git a/Network/HTTP/Lucu/Abortion.hs b/Network/HTTP/Lucu/Abortion.hs index b9e4b11..db32c1d 100644 --- a/Network/HTTP/Lucu/Abortion.hs +++ b/Network/HTTP/Lucu/Abortion.hs @@ -1,75 +1,55 @@ +{-# LANGUAGE + FlexibleContexts + , UnicodeSyntax + #-} +-- |Aborting the computation of 'Network.HTTP.Lucu.Rsrc' in any 'IO' +-- monads. module Network.HTTP.Lucu.Abortion - ( Abortion(..) + ( Abortion + , mkAbortion + , mkAbortion' + , abort - , abortSTM - , abortA - , abortPage ) 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 => a (StatusCode, ([ (String, String) ], Maybe String)) c -abortA - = arrIO3 abort - - --- aboMessage が Just なら單に mkDefaultPage に渡すだけで良いので樂だが、 --- Nothing の場合は getDefaultPage を使ってデフォルトのメッセージを得な --- ければならない。 -abortPage :: Config -> Maybe Request -> Response -> Abortion -> String -abortPage conf reqM res abo - = case aboMessage abo of - Just msg - -> let [html] = unsafePerformIO - $ runX ( mkDefaultPage conf (aboStatus abo) (txt msg) - >>> - writeDocumentToString [(a_indent, v_1)] - ) - in - html - Nothing - -> let res' = res { resStatus = aboStatus abo } - res'' = foldl (.) id [setHeader name value - | (name, value) <- aboHeaders abo] - $ res' - in - getDefaultPage conf reqM res'' +import Control.Exception +import Control.Monad.Trans +import Data.Ascii (Ascii, CIAscii) +import Data.Collections +import Data.Monoid.Unicode +import Data.Text (Text) +import Network.HTTP.Lucu.Abortion.Internal +import Network.HTTP.Lucu.Response +import Prelude.Unicode + +-- |Construct an 'Abortion' with additional headers and an optional +-- message text. +mkAbortion ∷ (StatusCode sc, Foldable f (CIAscii, Ascii)) + ⇒ sc + → f + → Maybe Text + → Abortion +{-# INLINE mkAbortion #-} +mkAbortion sc hdrs msg + = Abortion { + aboStatus = fromStatusCode sc + , aboHeaders = fromFoldable hdrs + , 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.Rsrc' monad. +abort ∷ MonadIO m ⇒ Abortion → m a +{-# INLINE abort #-} +abort = liftIO ∘ throwIO