hack-middleware-cleanpath (empty) → 0.0.0
raw patch · 4 files changed
+107/−0 lines, 4 filesdep +basedep +bytestringdep +hacksetup-changed
Dependencies added: base, bytestring, hack, split, web-encodings
Files
- Hack/Middleware/CleanPath.hs +54/−0
- LICENSE +25/−0
- Setup.lhs +7/−0
- hack-middleware-cleanpath.cabal +21/−0
+ Hack/Middleware/CleanPath.hs view
@@ -0,0 +1,54 @@+module Hack.Middleware.CleanPath (cleanPath, splitPath) where++import Hack+import qualified Data.ByteString.Lazy as BS+import Data.List+import Web.Encodings+import Data.List.Split++-- | Performs redirects as per 'splitPath'.+cleanPath :: Middleware+cleanPath app env =+ case splitPath $ pathInfo env of+ Left p -> return $! Response 303 [("Location", p)] BS.empty+ Right _ -> app env++-- | Given a certain requested path, return either a corrected path+-- to redirect to or the tokenized path.+--+-- This code corrects for the following issues:+--+-- * It is missing a trailing slash, and there is no period after the+-- last slash.+--+-- * There are any doubled slashes.+splitPath :: String -> Either String [String]+splitPath s =+ let corrected = ats $ rds s+ in if corrected == s+ then Right $ map decodeUrl $ filter (\l -> length l /= 0)+ $ splitOneOf "/" s+ else Left corrected++-- | Remove double slashes+rds :: String -> String+rds [] = []+rds [x] = [x]+rds (a:b:c)+ | a == '/' && b == '/' = rds (b:c)+ | otherwise = a : rds (b:c)++-- | Add a trailing slash if it is missing. Empty string is left alone.+ats :: String -> String+ats [] = []+ats s =+ if last s == '/' || dbs (reverse s)+ then s+ else s ++ "/"++-- | Is there a period before a slash here?+dbs :: String -> Bool+dbs ('/':_) = False+dbs ('.':_) = True+dbs (_:x) = dbs x+dbs [] = False
+ LICENSE view
@@ -0,0 +1,25 @@+The following license covers this documentation, and the source code, except+where otherwise indicated.++Copyright 2008, Michael Snoyman. All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+ list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+ this list of conditions and the following disclaimer in the documentation+ and/or other materials provided with the distribution.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO+EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,+OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.lhs view
@@ -0,0 +1,7 @@+#!/usr/bin/env runhaskell++> module Main where+> import Distribution.Simple++> main :: IO ()+> main = defaultMain
+ hack-middleware-cleanpath.cabal view
@@ -0,0 +1,21 @@+name: hack-middleware-cleanpath+version: 0.0.0+license: BSD3+license-file: LICENSE+author: Michael Snoyman <michael@snoyman.com>+maintainer: Michael Snoyman <michael@snoyman.com>+synopsis: Applies some basic redirect rules to get cleaner paths.+description: Forces request to have a trailing slash (unless there is a+ file extension) and ensures there are no doubled slashes.+ Takes the idea from Django.+category: Web+stability: unstable+cabal-version: >= 1.2+build-type: Simple+homepage: http://github.com/snoyberg/hack-middleware-cleanpath/tree/master++library+ build-depends: base >= 3, split >= 0.1.1, web-encodings,+ bytestring >= 0.9.1.4, hack >= 2009.5.19+ exposed-modules: Hack.Middleware.CleanPath+ ghc-options: -Wall