Skip to content

Commit

Permalink
Add Policy choice operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Farmer committed Jun 8, 2012
1 parent fe0fa27 commit 8e3097b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
29 changes: 19 additions & 10 deletions Network/Wai/Middleware/Static.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,34 +12,35 @@ module Network.Wai.Middleware.Static
( -- * Middlewares ( -- * Middlewares
static, staticPolicy static, staticPolicy
, -- * Policies , -- * Policies
Policy, (>->) Policy, (>->), (<|>)
, addBase, addSlash, noDots, only , addBase, addSlash, hasExtension, noDots, only
) where ) where


import Control.Monad.Trans (liftIO) import Control.Monad.Trans (liftIO)
import Data.List (isInfixOf) import Data.List (isInfixOf, isSuffixOf)
import qualified Data.Map as M import qualified Data.Map as M
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import Data.Monoid
import qualified Data.Text as T import qualified Data.Text as T


import Network.HTTP.Types (status200, Ascii) import Network.HTTP.Types (status200, Ascii)
import System.Directory (doesFileExist) import System.Directory (doesFileExist)
import System.FilePath import qualified System.FilePath as FP


import Network.Wai import Network.Wai


-- | Take an incoming URI and optionally modify or filter it. -- | Take an incoming URI and optionally modify or filter it.
-- The result will be treated as a filepath. -- The result will be treated as a filepath.
type Policy = String -> Maybe String type Policy = String -> Maybe String


-- | Combine two policies. They are run from left to right. -- | Sequence two policies. They are run from left to right.
infixr 5 >->
(>->) :: Policy -> Policy -> Policy (>->) :: Policy -> Policy -> Policy
p1 >-> p2 = maybe Nothing p2 . p1 p1 >-> p2 = maybe Nothing p2 . p1


-- | Filter URIs containing \"..\" -- | Choose between two policies. If the first returns Nothing, run the second.
noDots :: Policy infixr 4 <|>
noDots s = if ".." `isInfixOf` s then Nothing else Just s (<|>) :: Policy -> Policy -> Policy
p1 <|> p2 = \s -> maybe (p2 s) Just (p1 s)


-- | Add a base path to the URI -- | Add a base path to the URI
-- --
Expand All @@ -48,7 +49,7 @@ noDots s = if ".." `isInfixOf` s then Nothing else Just s
-- GET \"foo\/bar\" looks for \"\/home\/user\/files\/foo\/bar\" -- GET \"foo\/bar\" looks for \"\/home\/user\/files\/foo\/bar\"
-- --
addBase :: String -> Policy addBase :: String -> Policy
addBase b = Just . (b </>) addBase b = Just . (b FP.</>)


-- | Add an initial slash to to the URI, if not already present. -- | Add an initial slash to to the URI, if not already present.
-- --
Expand All @@ -59,6 +60,14 @@ addSlash :: Policy
addSlash s@('/':_) = Just s addSlash s@('/':_) = Just s
addSlash s = Just ('/':s) addSlash s = Just ('/':s)


-- | Filter URIs based on extension
hasExtension :: String -> Policy
hasExtension suf s = if suf `isSuffixOf` s then Just s else Nothing

-- | Filter URIs containing \"..\"
noDots :: Policy
noDots s = if ".." `isInfixOf` s then Nothing else Just s

-- | Filter any URIs not in a specific list, mapping to a filepath. -- | Filter any URIs not in a specific list, mapping to a filepath.
-- --
-- > staticPolicy (only [("foo/bar", "/home/user/files/bar")]) -- > staticPolicy (only [("foo/bar", "/home/user/files/bar")])
Expand Down
2 changes: 1 addition & 1 deletion examples/json.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $(deriveJSON Prelude.id ''Foo)
main :: IO () main :: IO ()
main = scotty 3000 $ do main = scotty 3000 $ do
middleware logStdoutDev middleware logStdoutDev
middleware $ staticPolicy (addBase "static") middleware $ staticPolicy (noDots >-> addBase "static")


get "/" $ do get "/" $ do
html $ wrapper $ do html $ wrapper $ do
Expand Down
2 changes: 1 addition & 1 deletion wai-middleware-static/wai-middleware-static.cabal
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: wai-middleware-static Name: wai-middleware-static
Version: 0.2.0 Version: 0.2.2
Synopsis: WAI middleware that intercepts requests to static files. Synopsis: WAI middleware that intercepts requests to static files.
Homepage: https://github.com/xich/scotty Homepage: https://github.com/xich/scotty
Bug-reports: https://github.com/xich/scotty/issues Bug-reports: https://github.com/xich/scotty/issues
Expand Down

0 comments on commit 8e3097b

Please sign in to comment.