module Network.Wai.Middleware.CleanPath (cleanPath, cleanPathRel, splitPath) where
import Network.Wai
import qualified Data.ByteString.Char8 as B
import Network.URI (unEscapeString)
cleanPathRel :: B.ByteString -> ([String] -> Request -> IO Response) -> Request -> IO Response
cleanPathRel prefix app env =
case splitPath $ pathInfo env of
Right pieces -> app pieces env
Left p -> return
. Response Status301
[(Location, B.concat [prefix, p, suffix])]
$ Right emptyEnum
where
suffix =
case B.uncons $ queryString env of
Nothing -> B.empty
Just ('?', _) -> queryString env
_ -> B.cons '?' $ queryString env
cleanPath :: ([String] -> Request -> IO Response) -> Request -> IO Response
cleanPath = cleanPathRel B.empty
emptyEnum :: Enumerator
emptyEnum = Enumerator $ \_ -> return . Right
splitPath :: B.ByteString -> Either B.ByteString [String]
splitPath s =
let corrected = B.pack $ rts $ ats $ rds $ B.unpack s
in if corrected == s
then Right $ map (unEscapeString . B.unpack)
$ filter (not . B.null)
$ B.split '/' s
else Left corrected
rds :: String -> String
rds [] = []
rds [x] = [x]
rds (a:b:c)
| a == '/' && b == '/' = rds (b:c)
| otherwise = a : rds (b:c)
ats :: String -> String
ats [] = []
ats s =
if last s == '/' || dbs (reverse s)
then s
else s ++ "/"
rts :: String -> String
rts [] = []
rts s =
if last s == '/' && dbs (tail $ reverse s)
then init s
else s
dbs :: String -> Bool
dbs ('/':_) = False
dbs ('.':_) = True
dbs (_:x) = dbs x
dbs [] = False