X-Git-Url: https://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Rakka%2FResource.hs;h=9f3af22fd6ef2752c0acdece23c4926177427bbc;hb=ed0a2de09fc91fbd25c3ee82a722ef88793f2a8f;hp=d0d9c4866d06665732ab91344ed96dc87dc2ac2a;hpb=f57c5c5ae6c95e68b11400718e7ce5de4ea1317a;p=Rakka.git diff --git a/Rakka/Resource.hs b/Rakka/Resource.hs index d0d9c48..9f3af22 100644 --- a/Rakka/Resource.hs +++ b/Rakka/Resource.hs @@ -8,6 +8,7 @@ module Rakka.Resource ) where +import qualified Codec.Binary.UTF8.String as UTF8 import Control.Arrow import Control.Arrow.ArrowList import Control.Monad @@ -18,6 +19,7 @@ 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 @@ -35,27 +37,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 () @@ -113,7 +116,7 @@ getInputReader 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))