on master help sign in
Trailing slashes for non-root apps #325
000490aMichael Snoyman
wai-app-static/ChangeLog.md+4
@@ -1,3 +1,7 @@
1+## 3.0.0.6
2+
3+Fix trailing slashes for `UrlMap` and other non-root setups [#325](https://github.com/yesodweb/wai/issues/325)
4+
1## 3.0.0.4
5## 3.0.0.4
2
6
3Add missing trailing slashes [#312](https://github.com/yesodweb/wai/issues/312)
7Add missing trailing slashes [#312](https://github.com/yesodweb/wai/issues/312)
wai-app-static/Network/Wai/Application/Static.hs+20−4
@@ -52,6 +52,7 @@ import Network.Mime (MimeType)
52data StaticResponse =
52data StaticResponse =
53 -- | Just the etag hash or Nothing for no etag hash
53 -- | Just the etag hash or Nothing for no etag hash
54 Redirect Pieces (Maybe ByteString)
54 Redirect Pieces (Maybe ByteString)
55+ | RawRedirect ByteString
55 | NotFound
56 | NotFound
56 | FileResponse File H.ResponseHeaders
57 | FileResponse File H.ResponseHeaders
57 | NotModified
58 | NotModified
@@ -79,15 +80,15 @@ serveFolder ss@StaticSettings {..} pieces req folder@Folder {..} =
79 let pieces' = setLast pieces index in
80 let pieces' = setLast pieces index in
80 case () of
81 case () of
81 () | ssRedirectToIndex -> return $ Redirect pieces' Nothing
82 () | ssRedirectToIndex -> return $ Redirect pieces' Nothing
82− | noTrailingSlash pieces, Just trailing <- toPiece "" ->
83+ | Just path <- addTrailingSlash req ->
83− return $ Redirect (pieces ++ [trailing]) Nothing
84+ return $ RawRedirect path
84 | otherwise ->
85 | otherwise ->
85 -- start the checking process over, with a new set
86 -- start the checking process over, with a new set
86 checkPieces ss pieces' req
87 checkPieces ss pieces' req
87 Nothing ->
88 Nothing ->
88 case ssListing of
89 case ssListing of
89− Just _ | noTrailingSlash pieces, Just trailing <- toPiece "" ->
90+ Just _ | Just path <- addTrailingSlash req ->
90− return $ Redirect (pieces ++ [trailing]) Nothing
91+ return $ RawRedirect path
91 Just listing -> do
92 Just listing -> do
92 -- directory listings turned on, display it
93 -- directory listings turned on, display it
93 builder <- listing pieces folder
94 builder <- listing pieces folder
@@ -104,6 +105,15 @@ serveFolder ss@StaticSettings {..} pieces req folder@Folder {..} =
104 | fromPiece t == "" = [x]
105 | fromPiece t == "" = [x]
105 setLast (a:b) x = a : setLast b x
106 setLast (a:b) x = a : setLast b x
106
107
108+ addTrailingSlash :: W.Request -> Maybe ByteString
109+ addTrailingSlash req
110+ | S8.null rp = Just "/"
111+ | rp == "/" = Nothing
112+ | S8.last rp == '/' = Nothing
113+ | otherwise = Just $ S8.snoc rp '/'
114+ where
115+ rp = W.rawPathInfo req
116+
107 noTrailingSlash :: Pieces -> Bool
117 noTrailingSlash :: Pieces -> Bool
108 noTrailingSlash [] = False
118 noTrailingSlash [] = False
109 noTrailingSlash [x] = fromPiece x /= ""
119 noTrailingSlash [x] = fromPiece x /= ""
@@ -250,6 +260,12 @@ staticAppPieces ss rawPieces req sendResponse = liftIO $ do
250 , ("Location", S8.append loc $ H.renderQuery True qString)
260 , ("Location", S8.append loc $ H.renderQuery True qString)
251 ] "Redirect"
261 ] "Redirect"
252
262
263+ response (RawRedirect path) =
264+ sendResponse $ W.responseLBS H.status301
265+ [ ("Content-Type", "text/plain")
266+ , ("Location", path)
267+ ] "Redirect"
268+
253 response NotFound = sendResponse $ W.responseLBS H.status404
269 response NotFound = sendResponse $ W.responseLBS H.status404
254 [ ("Content-Type", "text/plain")
270 [ ("Content-Type", "text/plain")
255 ] "File not found"
271 ] "File not found"
wai-app-static/wai-app-static.cabal+1−1
@@ -1,5 +1,5 @@
1name: wai-app-static
1name: wai-app-static
2−version: 3.0.0.5
2+version: 3.0.0.6
3license: MIT
3license: MIT
4license-file: LICENSE
4license-file: LICENSE
5author: Michael Snoyman <michael@snoyman.com>
5author: Michael Snoyman <michael@snoyman.com>
wai-extra/ChangeLog.md+4
@@ -1,3 +1,7 @@
1+## 3.0.4.2
2+
3+* `UrlMap`: do not modify `rawPathInfo`, see [#325](https://github.com/yesodweb/wai/issues/325)
4+
1## 3.0.4.1
5## 3.0.4.1
2
6
3Fix compilation failure on Windows [#321](https://github.com/yesodweb/wai/issues/321)
7Fix compilation failure on Windows [#321](https://github.com/yesodweb/wai/issues/321)
wai-extra/Network/Wai/UrlMap.hs+1−1
@@ -88,7 +88,7 @@ instance ToApplication UrlMap where
88 case try (pathInfo req) (unUrlMap urlMap) of
88 case try (pathInfo req) (unUrlMap urlMap) of
89 Just (newPath, app) ->
89 Just (newPath, app) ->
90 app (req { pathInfo = newPath
90 app (req { pathInfo = newPath
91− , rawPathInfo = makeRaw newPath
91+ --, rawPathInfo = makeRaw newPath
92 }) sendResponse
92 }) sendResponse
93 Nothing ->
93 Nothing ->
94 sendResponse $ responseLBS
94 sendResponse $ responseLBS
wai-extra/wai-extra.cabal+1−1
@@ -1,5 +1,5 @@
1Name: wai-extra
1Name: wai-extra
2−Version: 3.0.4.1
2+Version: 3.0.4.2
3Synopsis: Provides some basic WAI handlers and middleware.
3Synopsis: Provides some basic WAI handlers and middleware.
4description: API docs and the README are available at <http://www.stackage.org/package/wai-extra>.
4description: API docs and the README are available at <http://www.stackage.org/package/wai-extra>.
5License: MIT
5License: MIT