@@ -1,24 +1,25 @@
1−module Network.Wai.Middleware.CleanPath (cleanPath, splitPath) where
1+module Network.Wai.Middleware.CleanPath (cleanPath, cleanPathRel, splitPath) where
22
33import Network.Wai
44import qualified Data.ByteString.Char8 as B
55import Network.URI (unEscapeString)
66
77-- | Performs redirects as per 'splitPath'.
8−cleanPath :: ([String] -> Request -> IO Response)
9− -> Request
10− -> IO Response
11−cleanPath app env =
12− case splitPath $ pathInfo env of
13− Left p -> do
14− -- include the query string if present
15− let suffix = case B.uncons $ queryString env of
16− Nothing -> B.empty
17− Just ('?', _) -> queryString env
18− _ -> B.cons '?' $ queryString env
19− return $ Response Status303 [(Location, B.append p suffix)]
20− $ Right emptyEnum
8+cleanPathRel :: B.ByteString -> ([String] -> Request -> IO Response) -> Request -> IO Response
9+cleanPathRel prefix app env =
10+ case splitPath (prefix `B.append` (pathInfo env)) of
2111 Right pieces -> app pieces env
12+ Left p -> return . Response Status301 [(Location, B.append p suffix)] $ Right emptyEnum
13+ where
14+ -- include the query string if present
15+ suffix =
16+ case B.uncons $ queryString env of
17+ Nothing -> B.empty
18+ Just ('?', _) -> queryString env
19+ _ -> B.cons '?' $ queryString env
20+
21+cleanPath :: ([String] -> Request -> IO Response) -> Request -> IO Response
22+cleanPath = cleanPathRel B.empty
2223
2324emptyEnum :: Enumerator
2425emptyEnum = Enumerator $ \_ -> return . Right