X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Rakka%2FResource.hs;h=a1c4d90af52b3847b7896ae5904a5690d04e09fe;hb=88747f2463963ff2895a597b3054b12b2288530e;hp=5cbf188e46c8a468b16b5bdbfcbd5f1722f8d7c6;hpb=f1016753ef45a4c25745ccb6e81e5acbc085cc42;p=Rakka.git diff --git a/Rakka/Resource.hs b/Rakka/Resource.hs index 5cbf188..a1c4d90 100644 --- a/Rakka/Resource.hs +++ b/Rakka/Resource.hs @@ -1,14 +1,14 @@ module Rakka.Resource ( runIdempotentA + , runIdempotentA' , runXmlA , getEntityType , outputXmlPage , outputXmlPage' + , outputXml , getUserID ) where - -import qualified Codec.Binary.UTF8.String as UTF8 import Control.Arrow import Control.Arrow.ArrowList import Control.Monad @@ -19,10 +19,10 @@ import Network.URI hiding (path) import Rakka.Authorization import Rakka.Environment import Rakka.Validation +import System.FilePath.Posix import System.Log.Logger import Text.XML.HXT.Arrow.ReadDocument import Text.XML.HXT.Arrow.WriteDocument -import Text.XML.HXT.Arrow.XmlIOStateArrow import Text.XML.HXT.DOM.TypeDefs import Text.XML.HXT.DOM.XmlKeywords @@ -36,27 +36,28 @@ logger = "Rakka.Resource" -- "/foo/" ==> "/foo.html" -- "/foo.bar/" ==> "/foo.bar" -- "/foo.bar" ==> "/foo.bar" -canonicalizeURI :: Resource () -canonicalizeURI - = do uri <- getRequestURI - let newURI = uri { uriPath = "/" ++ joinWith "/" newPath } - newPath = case [x | x <- splitBy (== '/') (uriPath uri), x /= ""] of - [] -> [] - path -> case break (== '.') $ last path of - (_, "") -> let basePieces = reverse $ tail $ reverse path - lastPiece = last path - in - basePieces ++ [lastPiece ++ ".html"] - (_, _) -> path - when (uri /= newURI) +canonicalizeURI :: URI -> Resource () +canonicalizeURI baseURI + = do rPath <- return . uriPath =<< getRequestURI + let newURI = baseURI { uriPath = uriPath baseURI newPath } + newPath = foldl () "/" newPath' + newPath' = case [x | x <- splitBy (== '/') rPath, x /= ""] of + [] -> [] + path -> case break (== '.') $ last path of + (_, "") -> let basePieces = reverse $ tail $ reverse path + lastPiece = last path + in + basePieces ++ [lastPiece ++ ".html"] + (_, _) -> path + when (rPath /= newPath) $ abort MovedPermanently [("Location", uriToString id newURI $ "")] Nothing -runIdempotentA :: IOSArrow () (Resource c) -> Resource c -runIdempotentA a - = do canonicalizeURI +runIdempotentA :: URI -> IOSArrow () (Resource c) -> Resource c +runIdempotentA baseURI a + = do canonicalizeURI baseURI [rsrc] <- liftIO $ runX ( setErrorMsgHandler False fail >>> constA () @@ -66,6 +67,17 @@ runIdempotentA a rsrc +runIdempotentA' :: IOSArrow () (Resource c) -> Resource c +runIdempotentA' a + = do [rsrc] <- liftIO $ runX ( setErrorMsgHandler False fail + >>> + constA () + >>> + a + ) + rsrc + + runXmlA :: Environment -> FilePath -> IOSArrow XmlTree (Resource c) -> Resource c runXmlA env schemaPath a = do inputA <- getInputXmlA env schemaPath @@ -147,7 +159,9 @@ outputXmlPage tree formatters >>> formatter >>> - writeDocumentToString [ (a_indent, v_1) ] + writeDocumentToString [ (a_indent , v_1 ) + , (a_output_encoding, utf8) + , (a_no_xml_pi , v_0 ) ] ) output resultStr @@ -157,6 +171,20 @@ outputXmlPage' tree toXHTML = outputXmlPage tree [(MIMEType "application" "xhtml+xml" [], toXHTML)] +outputXml :: XmlTree -> Resource () +outputXml tree + = do setContentType (MIMEType "text" "xml" []) + [xmlStr] <- liftIO $ runX ( setErrorMsgHandler False fail + >>> + constA tree + >>> + writeDocumentToString [ (a_indent , v_1 ) + , (a_output_encoding, utf8) + , (a_no_xml_pi , v_0 ) ] + ) + output xmlStr + + getUserID :: Environment -> Resource (Maybe String) getUserID env = do auth <- getAuthorization