module Network.Wai.Middleware.CleanPath (cleanPath, splitPath) where
import Network.Wai
import qualified Data.ByteString.Char8 as B
import Network.URI (unEscapeString)
cleanPath :: ([String] -> Request -> IO Response)
-> Request
-> IO Response
cleanPath app env =
case splitPath $ pathInfo env of
Left p -> do
let suffix = case B.uncons $ queryString env of
Nothing -> B.empty
Just ('?', _) -> queryString env
_ -> B.cons '?' $ queryString env
return $ Response Status303 [(Location, B.append p suffix)]
$ Right emptyEnum
Right pieces -> app pieces env
emptyEnum :: Enumerator
emptyEnum = Enumerator $ \_ -> return . Right
splitPath :: B.ByteString -> Either B.ByteString [String]
splitPath s =
let corrected = B.pack $ 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 ++ "/"
dbs :: String -> Bool
dbs ('/':_) = False
dbs ('.':_) = True
dbs (_:x) = dbs x
dbs [] = False