on master help sign in
support CGI programs not at site's root url

Add cleanPathRel Redirect with 301

commita73a60e9595132ea67a2cbb12690a93d25de25d5 authorYair Chuchem <yairc@tiny.local> dateFri 25 Jun 2010, 17:29 (16 years ago) parentd2f1908
2 files changed+16−15
Network/Wai/Middleware/CleanPath.hs+15−14 wai-extra.cabal+1−1
Network/Wai/Middleware/CleanPath.hs+15−14
@@ -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
wai-extra.cabal+1−1
@@ -1,5 +1,5 @@
11Name: wai-extra
2−Version: 0.1.2.1
2+Version: 0.1.3
33Synopsis: Provides some basic WAI handlers and middleware.
44Description: The goal here is to provide common features without many dependencies.
55License: BSD3