Trailing slashes for non-root apps #325

This commit is contained in:
Michael Snoyman 2015-01-14 12:20:45 +02:00
parent fdd315d7db
commit 000490af66
6 changed files with 31 additions and 7 deletions

View File

@ -1,3 +1,7 @@
## 3.0.0.6
Fix trailing slashes for `UrlMap` and other non-root setups [#325](https://github.com/yesodweb/wai/issues/325)
## 3.0.0.4
Add missing trailing slashes [#312](https://github.com/yesodweb/wai/issues/312)

View File

@ -52,6 +52,7 @@ import Network.Mime (MimeType)
data StaticResponse =
-- | Just the etag hash or Nothing for no etag hash
Redirect Pieces (Maybe ByteString)
| RawRedirect ByteString
| NotFound
| FileResponse File H.ResponseHeaders
| NotModified
@ -79,15 +80,15 @@ serveFolder ss@StaticSettings {..} pieces req folder@Folder {..} =
let pieces' = setLast pieces index in
case () of
() | ssRedirectToIndex -> return $ Redirect pieces' Nothing
| noTrailingSlash pieces, Just trailing <- toPiece "" ->
return $ Redirect (pieces ++ [trailing]) Nothing
| Just path <- addTrailingSlash req ->
return $ RawRedirect path
| otherwise ->
-- start the checking process over, with a new set
checkPieces ss pieces' req
Nothing ->
case ssListing of
Just _ | noTrailingSlash pieces, Just trailing <- toPiece "" ->
return $ Redirect (pieces ++ [trailing]) Nothing
Just _ | Just path <- addTrailingSlash req ->
return $ RawRedirect path
Just listing -> do
-- directory listings turned on, display it
builder <- listing pieces folder
@ -104,6 +105,15 @@ serveFolder ss@StaticSettings {..} pieces req folder@Folder {..} =
| fromPiece t == "" = [x]
setLast (a:b) x = a : setLast b x
addTrailingSlash :: W.Request -> Maybe ByteString
addTrailingSlash req
| S8.null rp = Just "/"
| rp == "/" = Nothing
| S8.last rp == '/' = Nothing
| otherwise = Just $ S8.snoc rp '/'
where
rp = W.rawPathInfo req
noTrailingSlash :: Pieces -> Bool
noTrailingSlash [] = False
noTrailingSlash [x] = fromPiece x /= ""
@ -250,6 +260,12 @@ staticAppPieces ss rawPieces req sendResponse = liftIO $ do
, ("Location", S8.append loc $ H.renderQuery True qString)
] "Redirect"
response (RawRedirect path) =
sendResponse $ W.responseLBS H.status301
[ ("Content-Type", "text/plain")
, ("Location", path)
] "Redirect"
response NotFound = sendResponse $ W.responseLBS H.status404
[ ("Content-Type", "text/plain")
] "File not found"

View File

@ -1,5 +1,5 @@
name: wai-app-static
version: 3.0.0.5
version: 3.0.0.6
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>

View File

@ -1,3 +1,7 @@
## 3.0.4.2
* `UrlMap`: do not modify `rawPathInfo`, see [#325](https://github.com/yesodweb/wai/issues/325)
## 3.0.4.1
Fix compilation failure on Windows [#321](https://github.com/yesodweb/wai/issues/321)

View File

@ -88,7 +88,7 @@ instance ToApplication UrlMap where
case try (pathInfo req) (unUrlMap urlMap) of
Just (newPath, app) ->
app (req { pathInfo = newPath
, rawPathInfo = makeRaw newPath
--, rawPathInfo = makeRaw newPath
}) sendResponse
Nothing ->
sendResponse $ responseLBS

View File

@ -1,5 +1,5 @@
Name: wai-extra
Version: 3.0.4.1
Version: 3.0.4.2
Synopsis: Provides some basic WAI handlers and middleware.
description: API docs and the README are available at <http://www.stackage.org/package/wai-extra>.
License: MIT