packages feed

wai-middleware-route 0.6.0 → 0.7.0

raw patch · 3 files changed

+29/−10 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Network.Wai.Middleware.Route: dispatch_ :: Dispatch Application -> Application -> Application
- Network.Wai.Middleware.Route: dispatch :: Dispatch Application -> Application -> Application
+ Network.Wai.Middleware.Route: dispatch :: Bool -> Dispatch Application -> Application -> Application

Files

src/Network/Wai/Middleware/Route.hs view
@@ -15,7 +15,8 @@     mkRoute,
     
     -- * Middleware
-    dispatch
+    dispatch,
+    dispatch_
 ) where
 
 import Control.Applicative ((<$>), (<*>))
@@ -140,7 +141,7 @@ -- Middleware.
 -----------------------------------------------------------------------------
 
--- | Dispatch function.
+-- | Dispatch 'Middleware'. 
 -- 
 -- > rs :: Dispatch Application
 -- > rs = toDispatch . mkRoutes [
@@ -152,21 +153,36 @@ -- >    ]
 -- > 
 -- > app :: Application
--- > app = dispatch rs (error "Not dispatched")
-
+-- > app = dispatch True rs (error "Not dispatched")
 dispatch :: 
-       D.Dispatch Application   
+       Bool 
+            -- ^ Squash empty 'pathInfo' chunks. It often appear in the 
+            --   presence of double slashes or \"Ending slash\" in URL.
+    -> D.Dispatch Application   
             -- ^ Dispatch function. 
-            --   Use 'D.toDispatch' and route helpers below.
+            --   Use 'D.toDispatch' and route helpers.
     -> Application 
             -- ^ Default (@404@) application. 
     -> Application
-dispatch mappings defApp req = 
+dispatch squash mappings defApp req = 
     case mappings . needle $ req of
         Nothing -> defApp req
         (Just app) -> app req 
   where
-    needle = (:) <$> decodeUtf8 . requestMethod <*> pathInfo
+    needle = (:) <$> decodeUtf8 . requestMethod <*> path squash
+    path False = pathInfo
+    path True = filter (/="") . pathInfo
+
+-- | Dispatch 'Middleware' with auto-squash empty path pieces. Equiwalent to
+-- > dispatch True
+dispatch_ ::
+       D.Dispatch Application   
+            -- ^ Dispatch function. 
+            --   Use 'D.toDispatch' and route helpers.
+    -> Application 
+            -- ^ Default (@404@) application. 
+    -> Application
+dispatch_ = dispatch True
 
 
 
test/Main.hs view
@@ -45,6 +45,9 @@     testOne "last/bar/baz"          "#/#/#"
     testOne "will/never/be/routed"  "noroute"
     
+    -- Weird \"Last slash\" behaviour
+    testOne "foo/bar/"              "foo/bar"
+    
     -- test @POST@
     rPost <- request $ setRawPathInfo 
             defaultRequest {requestMethod="POST"} "foo/bar" 
@@ -58,7 +61,7 @@ -- | Routed application
 routedApp :: Application
 routedApp = 
-    dispatch mappings $ testApp "noroute"
+    dispatch_ mappings $ testApp "noroute"
   where
     mappings = mkRoutes' [
           Get ""            $ testApp ""
wai-middleware-route.cabal view
@@ -1,5 +1,5 @@ name:           wai-middleware-route
-version:        0.6.0
+version:        0.7.0
 cabal-version:  >= 1.8
 build-type:     Simple
 synopsis:       Wai dispatch middleware