on master help sign in
Make ssIndices work with ssLookupFile and trailing slashes
6b06875Sönke Hahn
wai-app-static/Network/Wai/Application/Static.hs+1−1
@@ -119,7 +119,7 @@ checkPieces ss@StaticSettings {..} pieces req = do
119 case nonIndexResult of
119 case nonIndexResult of
120 LRFile{} -> return $ Right nonIndexResult
120 LRFile{} -> return $ Right nonIndexResult
121 _ -> do
121 _ -> do
122− indexResult <- lookupIndices (map (\ index -> pieces ++ [index]) ssIndices)
122+ indexResult <- lookupIndices (map (\ index -> dropLastIfNull pieces ++ [index]) ssIndices)
123 return $ case indexResult of
123 return $ case indexResult of
124 LRNotFound -> Right nonIndexResult
124 LRNotFound -> Right nonIndexResult
125 LRFile file | ssRedirectToIndex ->
125 LRFile file | ssRedirectToIndex ->
wai-app-static/Util.hs+8−1
@@ -1,9 +1,10 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE OverloadedStrings, ViewPatterns #-}
2module Util
2module Util
3 ( relativeDirFromPieces
3 ( relativeDirFromPieces
4 , defaultMkRedirect
4 , defaultMkRedirect
5 , replace
5 , replace
6 , remove
6 , remove
7+ , dropLastIfNull
7 ) where
8 ) where
8
9
9import WaiAppStatic.Types
10import WaiAppStatic.Types
@@ -36,3 +37,9 @@ defaultMkRedirect pieces newPath
36 | otherwise = relDir `S8.append` S8.tail newPath
37 | otherwise = relDir `S8.append` S8.tail newPath
37 where
38 where
38 relDir = TE.encodeUtf8 (relativeDirFromPieces pieces)
39 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
47 , EmbeddableEntry "index.html"
47 , EmbeddableEntry "index.html"
48 "text/html"
48 "text/html"
49 (Right [| return ("" :: T.Text, "index file") |] )
49 (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") |] )
50 ]
55 ]
wai-app-static/test/WaiAppEmbeddedTest.hs+10−1
@@ -32,7 +32,7 @@ embSpec = do
32 { requestHeaders = [("If-None-Match", "Etag 1")] }
32 { requestHeaders = [("If-None-Match", "Etag 1")] }
33 assertStatus 304 req
33 assertStatus 304 req
34
34
35− it "ssIndices works" $ do
35+ it "ssIndices work" $ do
36 let testSettings = $(mkSettings mkEntries){
36 let testSettings = $(mkSettings mkEntries){
37 ssIndices = [unsafeToPiece "index.html"]
37 ssIndices = [unsafeToPiece "index.html"]
38 }
38 }
@@ -41,6 +41,15 @@ embSpec = do
41 assertStatus 200 req
41 assertStatus 200 req
42 assertBody "index file" req
42 assertBody "index file" req
43
43
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+
44 describe "embedded, uncompressed entry" $ do
53 describe "embedded, uncompressed entry" $ do
45 it "too short" $ embed $ do
54 it "too short" $ embed $ do
46 req <- request (setRawPathInfo defRequest "e2.txt")
55 req <- request (setRawPathInfo defRequest "e2.txt")