on master help sign in
update rewriter to set rawPathInfo
commit166e45e82219377849d5dac14e42e8072b54ee0d authorGreg Weber <greg@gregweber.info> dateThu 13 Oct 2011, 06:43 (15 years ago) parent63f0860
1 file changed+13−7
wai-extra/Network/Wai/Middleware/Rewrite.hs+13−7
wai-extra/Network/Wai/Middleware/Rewrite.hs+13−7
@@ -7,21 +7,27 @@ import Network.Wai
77import System.Directory (doesFileExist)
88import Control.Monad.IO.Class (liftIO)
99import Data.Text (Text, unpack, pack)
10+import qualified Data.Text.Encoding as TE
11+import qualified Data.Text as T
12+import Network.HTTP.Types as H
13+
1014
1115-- | rewrite based on your own conversion rules
1216-- Example usage: rewrite (autoHtmlRewrite "static")
13−rewrite :: ([Text] -> IO [Text]) -> Middleware
17+rewrite :: ([Text] -> H.RequestHeaders -> IO [Text]) -> Middleware
1418rewrite convert app req = do
15− newPathInfo <- liftIO $ convert $ pathInfo req
16− app req { pathInfo = newPathInfo }
19+ newPathInfo <- liftIO $ convert (pathInfo req) (requestHeaders req)
20+ let rawPInfo = TE.encodeUtf8 $ T.intercalate "/" newPathInfo
21+ app req { pathInfo = newPathInfo, rawPathInfo = rawPInfo }
1722
1823-- | example rewriter
19−-- implements 2 rules for static html re-writes
24+-- We don't recommend normally checking the file system on every request - this is just an example.
25+-- Implements 2 rules for static html re-writes
2026-- 1) for a directory foo/, check for foo/index.html
2127-- 2) for a non-directory bar, check for bar.html
22−-- if the file exists, do the rewrite
23−autoHtmlRewrite :: String -> [Text] -> IO [Text]
24−autoHtmlRewrite staticDir pieces' = do
28+-- Do the rewrite only if the html file exists.
29+autoHtmlRewrite :: String -> [Text] -> H.RequestHeaders -> IO [Text]
30+autoHtmlRewrite staticDir pieces' _ = do
2531 fe <- doesFileExist $ staticDir ++ "/" ++ reWritePath
2632 return $ if fe then map pack reWritePieces else pieces'
2733 where