X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Rakka%2FResource.hs;h=5cbf188e46c8a468b16b5bdbfcbd5f1722f8d7c6;hb=f1016753ef45a4c25745ccb6e81e5acbc085cc42;hp=adbd706d6e9d07c6eb79a3f7bec776be9064b9d4;hpb=e2fd35989e9765281523fd4ce05dcd0199bdbbad;p=Rakka.git diff --git a/Rakka/Resource.hs b/Rakka/Resource.hs index adbd706..5cbf188 100644 --- a/Rakka/Resource.hs +++ b/Rakka/Resource.hs @@ -1,10 +1,14 @@ module Rakka.Resource ( runIdempotentA , runXmlA + , getEntityType , outputXmlPage + , outputXmlPage' + , getUserID ) where +import qualified Codec.Binary.UTF8.String as UTF8 import Control.Arrow import Control.Arrow.ArrowList import Control.Monad @@ -12,8 +16,10 @@ import Control.Monad.Trans import Network.HTTP.Lucu import Network.HTTP.Lucu.Utils import Network.URI hiding (path) +import Rakka.Authorization import Rakka.Environment import Rakka.Validation +import System.Log.Logger import Text.XML.HXT.Arrow.ReadDocument import Text.XML.HXT.Arrow.WriteDocument import Text.XML.HXT.Arrow.XmlIOStateArrow @@ -21,6 +27,10 @@ import Text.XML.HXT.DOM.TypeDefs import Text.XML.HXT.DOM.XmlKeywords +logger :: String +logger = "Rakka.Resource" + + -- "/" ==> "/" -- "/foo" ==> "/foo.html" -- "/foo/" ==> "/foo.html" @@ -100,10 +110,11 @@ getInputReader where getXmlReader = do req <- input defaultLimit + liftIO $ debugM logger req return $ readString [ (a_validate , v_0) , (a_check_namespaces , v_1) , (a_remove_whitespace, v_0) - ] req + ] (UTF8.decodeString req) getFailingReader code headers msg = return $ proc _ -> abortA -< (code, (headers, msg)) @@ -118,18 +129,18 @@ getEntityType where extMap :: [(String, MIMEType)] extMap = [ ("html", read "application/xhtml+xml") + , ( "rdf", read "application/rss+xml" ) , ( "xml", read "text/xml" ) ] -outputXmlPage :: XmlTree -> IOSArrow XmlTree XmlTree -> Resource () -outputXmlPage tree toXHTML +outputXmlPage :: XmlTree -> [(MIMEType, IOSArrow XmlTree XmlTree)] -> Resource () +outputXmlPage tree formatters = do mType <- getEntityType setContentType mType - let formatter = case mType of - MIMEType "application" "xhtml+xml" _ -> toXHTML - MIMEType "text" "xml" _ -> this - _ -> undefined + let formatter = case lookup mType formatters of + Just f -> f + Nothing -> this [resultStr] <- liftIO $ runX ( setErrorMsgHandler False fail >>> constA tree @@ -138,4 +149,22 @@ outputXmlPage tree toXHTML >>> writeDocumentToString [ (a_indent, v_1) ] ) - output resultStr \ No newline at end of file + output resultStr + + +outputXmlPage' :: XmlTree -> IOSArrow XmlTree XmlTree -> Resource () +outputXmlPage' tree toXHTML + = outputXmlPage tree [(MIMEType "application" "xhtml+xml" [], toXHTML)] + + +getUserID :: Environment -> Resource (Maybe String) +getUserID env + = do auth <- getAuthorization + case auth of + Just (BasicAuthCredential userID password) + -> do valid <- isValidPair (envAuthDB env) userID password + if valid then + return (Just userID) + else + return Nothing + _ -> return Nothing