From ed0a2de09fc91fbd25c3ee82a722ef88793f2a8f Mon Sep 17 00:00:00 2001 From: pho Date: Fri, 22 Feb 2008 22:33:52 +0900 Subject: [PATCH] Fixed canonicalization bug darcs-hash:20080222133352-62b54-d093ba1700e9adcf13a1e81dfcfa70fe05388386.gz --- Rakka/Resource.hs | 34 ++++++++++++++++++---------------- Rakka/Resource/PageEntity.hs | 27 ++++++++++++++------------- Rakka/Resource/Search.hs | 3 ++- js/Makefile | 2 +- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/Rakka/Resource.hs b/Rakka/Resource.hs index 5cbf188..9f3af22 100644 --- a/Rakka/Resource.hs +++ b/Rakka/Resource.hs @@ -19,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 @@ -36,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 () diff --git a/Rakka/Resource/PageEntity.hs b/Rakka/Resource/PageEntity.hs index 8f4bd9c..e354004 100644 --- a/Rakka/Resource/PageEntity.hs +++ b/Rakka/Resource/PageEntity.hs @@ -55,19 +55,20 @@ fallbackPageEntity env path handleGet :: Environment -> PageName -> Resource () handleGet env name - = runIdempotentA $ proc () - -> do pageM <- getPageA (envStorage env) -< (name, Nothing) - case pageM of - Nothing - -> do items <- getDirContentsA (envStorage env) -< (name, Nothing) - case items of - [] -> handlePageNotFound env -< name - _ -> handleGetPageListing env -< (name, items) - Just page - -> if isEntity page then - handleGetEntity env -< page - else - handleRedirect env -< page + = do BaseURI baseURI <- getSysConf (envSysConf env) + runIdempotentA baseURI $ proc () + -> do pageM <- getPageA (envStorage env) -< (name, Nothing) + case pageM of + Nothing + -> do items <- getDirContentsA (envStorage env) -< (name, Nothing) + case items of + [] -> handlePageNotFound env -< name + _ -> handleGetPageListing env -< (name, items) + Just page + -> if isEntity page then + handleGetEntity env -< page + else + handleRedirect env -< page {- diff --git a/Rakka/Resource/Search.hs b/Rakka/Resource/Search.hs index e4456e8..7c2acae 100644 --- a/Rakka/Resource/Search.hs +++ b/Rakka/Resource/Search.hs @@ -77,7 +77,8 @@ handleSearch env let to' = min (from + length (srPages result)) to - runIdempotentA $ proc () + BaseURI baseURI <- getSysConf (envSysConf env) + runIdempotentA baseURI $ proc () -> do tree <- ( eelem "/" += ( eelem "searchResult" += sattr "query" query diff --git a/js/Makefile b/js/Makefile index f776451..6db118d 100644 --- a/js/Makefile +++ b/js/Makefile @@ -24,7 +24,7 @@ build: ../Rakka/Resource/JavaScript.hs packed.js: $(SOURCES) $(COMPRESSOR) cat $(SOURCES) > $@ -# cat $(SOURCES) | $(COMPRESS) --warn -o $@ +# cat $(SOURCES) | $(COMPRESS) -o $@ ../Rakka/Resource/JavaScript.hs: packed.js -- 2.40.0