on master help sign in
Make ssIndices work with ssLookupFile and trailing slashes
commit6b06875d3164f4af35bb2852888cbc409a657123 authorSönke Hahn <soenkehahn@gmail.com> dateSun 24 Jul 2016, 00:13 (10 years ago) parent190a193
4 files changed+24−3
wai-app-static/Network/Wai/Application/Static.hs+1−1 wai-app-static/Util.hs+8−1 wai-app-static/test/EmbeddedTestEntries.hs+5 wai-app-static/test/WaiAppEmbeddedTest.hs+10−1
wai-app-static/Network/Wai/Application/Static.hs+1−1
@@ -119,7 +119,7 @@ checkPieces ss@StaticSettings {..} pieces req = do
119119 case nonIndexResult of
120120 LRFile{} -> return $ Right nonIndexResult
121121 _ -> do
122− indexResult <- lookupIndices (map (\ index -> pieces ++ [index]) ssIndices)
122+ indexResult <- lookupIndices (map (\ index -> dropLastIfNull pieces ++ [index]) ssIndices)
123123 return $ case indexResult of
124124 LRNotFound -> Right nonIndexResult
125125 LRFile file | ssRedirectToIndex ->
wai-app-static/Util.hs+8−1
@@ -1,9 +1,10 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE OverloadedStrings, ViewPatterns #-}
22module Util
33 ( relativeDirFromPieces
44 , defaultMkRedirect
55 , replace
66 , remove
7+ , dropLastIfNull
78 ) where
89
910import WaiAppStatic.Types
@@ -36,3 +37,9 @@ defaultMkRedirect pieces newPath
3637 | otherwise = relDir `S8.append` S8.tail newPath
3738 where
3839 relDir = TE.encodeUtf8 (relativeDirFromPieces pieces)
40+
41+dropLastIfNull :: [Piece] -> [Piece]
42+dropLastIfNull pieces = case pieces of
43+ [fromPiece -> ""] -> []
44+ (a : r) -> a : dropLastIfNull r
45+ [] -> []
wai-app-static/test/EmbeddedTestEntries.hs+5
@@ -47,4 +47,9 @@ mkEntries = return
4747 , EmbeddableEntry "index.html"
4848 "text/html"
4949 (Right [| return ("" :: T.Text, "index file") |] )
50+
51+ -- An index file in a subdir
52+ , EmbeddableEntry "foo/index.html"
53+ "text/html"
54+ (Right [| return ("" :: T.Text, "index file in subdir") |] )
5055 ]
wai-app-static/test/WaiAppEmbeddedTest.hs+10−1
@@ -32,7 +32,7 @@ embSpec = do
3232 { requestHeaders = [("If-None-Match", "Etag 1")] }
3333 assertStatus 304 req
3434
35− it "ssIndices works" $ do
35+ it "ssIndices work" $ do
3636 let testSettings = $(mkSettings mkEntries){
3737 ssIndices = [unsafeToPiece "index.html"]
3838 }
@@ -41,6 +41,15 @@ embSpec = do
4141 assertStatus 200 req
4242 assertBody "index file" req
4343
44+ it "ssIndices work with trailing slashes" $ do
45+ let testSettings = $(mkSettings mkEntries){
46+ ssIndices = [unsafeToPiece "index.html"]
47+ }
48+ embedSettings testSettings $ do
49+ req <- request (setRawPathInfo defRequest "/foo/")
50+ assertStatus 200 req
51+ assertBody "index file in subdir" req
52+
4453 describe "embedded, uncompressed entry" $ do
4554 it "too short" $ embed $ do
4655 req <- request (setRawPathInfo defRequest "e2.txt")