]> gitweb @ CieloNegro.org - Rakka.git/blobdiff - Rakka/Resource.hs
Fixed canonicalization bug
[Rakka.git] / Rakka / Resource.hs
index 5cbf188e46c8a468b16b5bdbfcbd5f1722f8d7c6..9f3af22fd6ef2752c0acdece23c4926177427bbc 100644 (file)
@@ -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 ()