on master help sign in
added a RedirectToIndex setting

When a directory is requested and a request file exists, the previous version would have redirected to the request file. This setting enables the user to set RedirectToIndex which does that. Otherwise, it just serves the index file without any redirect. RedirectToIndex=True: http://example.com/ redirected to http://example.com/index.html RedirectToIndex=False: http://example.com/ remains same. Index.html is served.

commite54e9ad041181088fd6d2ae08087356d61117f0c authorAbdulsattar Mohammed <codingtales@gmail.com> dateMon 20 Feb 2012, 12:32 (15 years ago) parent24f77c0
2 files changed+12−3
wai-app-static/Network/Wai/Application/Static.hs+11−3 warp-static/warp.hs+1
wai-app-static/Network/Wai/Application/Static.hs+11−3
@@ -261,8 +261,9 @@ checkPieces :: (Pieces -> IO FileLookup) -- ^ file lookup function
261261 -> W.Request
262262 -> MaxAge
263263 -> Bool -- ^ use hash?
264+ -> Bool -- ^ Redirect to Index?
264265 -> IO CheckPieces
265−checkPieces fileLookup indices pieces req maxAge useHash
266+checkPieces fileLookup indices pieces req maxAge useHash redirectToIndex
266267 | any unsafe pieces = return Forbidden
267268 | any nullFilePath $ safeInit pieces =
268269 return $ Redirect (filterButLast (not . nullFilePath) pieces) Nothing
@@ -281,7 +282,11 @@ checkPieces fileLookup indices pieces req maxAge useHash
281282 (Just Right{}, False) -> return $ Redirect (init pieces) Nothing
282283 (Just (Left folder@(Folder _ contents)), _) -> do
283284 case checkIndices $ map fileName $ rights contents of
284− Just index -> return $ Redirect (setLast pieces index) Nothing
285+ Just index ->
286+ if redirectToIndex then
287+ return $ Redirect (setLast pieces index) Nothing
288+ else
289+ checkPieces fileLookup indices (setLast pieces index) req maxAge useHash redirectToIndex
285290 Nothing ->
286291 if isFolder
287292 then return $ DirectoryResponse folder
@@ -392,6 +397,7 @@ data StaticSettings = StaticSettings
392397 , ssGetMimeType :: File -> IO MimeType
393398 , ssListing :: Maybe Listing
394399 , ssIndices :: [T.Text] -- index.html
400+ , ssRedirectToIndex :: Bool
395401 , ssMaxAge :: MaxAge
396402 , ssUseHash :: Bool
397403 }
@@ -420,6 +426,7 @@ defaultWebAppSettings = StaticSettings
420426 , ssMaxAge = MaxAgeForever
421427 , ssListing = Nothing
422428 , ssIndices = []
429+ , ssRedirectToIndex = False
423430 , ssUseHash = True
424431 }
425432
@@ -431,6 +438,7 @@ defaultFileServerSettings = StaticSettings
431438 , ssMaxAge = MaxAgeSeconds $ 60 * 60
432439 , ssListing = Just defaultListing
433440 , ssIndices = ["index.html", "index.htm"]
441+ , ssRedirectToIndex = False
434442 , ssUseHash = False
435443 }
436444
@@ -598,7 +606,7 @@ staticAppPieces ss pieces req = liftIO $ do
598606 let indices = ssIndices ss
599607 case checkSpecialDirListing pieces of
600608 Just res -> response res
601− Nothing -> checkPieces (ssFolder ss) (map FilePath indices) pieces req (ssMaxAge ss) (ssUseHash ss) >>= response
609+ Nothing -> checkPieces (ssFolder ss) (map FilePath indices) pieces req (ssMaxAge ss) (ssUseHash ss) (ssRedirectToIndex ss) >>= response
602610 where
603611 response cp = case cp of
604612 FileResponse file ch -> do
warp-static/warp.hs+1
@@ -52,6 +52,7 @@ main = do
5252 } $ middle $ staticApp defaultFileServerSettings
5353 { ssFolder = fileSystemLookup $ toFilePath docroot
5454 , ssIndices = if noindex then [] else map pack index
55+ , ssRedirectToIndex = false
5556 , ssListing = Just defaultListing
5657 , ssGetMimeType = return . mimeTypeByExt mimeMap defaultMimeType . fileName
5758 }