{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell, CPP #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE PatternGuards #-}
module Network.Wai.Application.Static
(
staticApp
, defaultWebAppSettings
, webAppSettingsWithLookup
, defaultFileServerSettings
, embeddedSettings
, StaticSettings
, ssLookupFile
, ssMkRedirect
, ssGetMimeType
, ssListing
, ssIndices
, ssMaxAge
, ssRedirectToIndex
, ssAddTrailingSlash
, ss404Handler
) where
import Prelude hiding (FilePath)
import qualified Network.Wai as W
import qualified Network.HTTP.Types as H
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as S8
import qualified Data.ByteString.Lazy as L
import Data.ByteString.Lazy.Char8 ()
import Control.Monad.IO.Class (liftIO)
import Data.ByteString.Builder (toLazyByteString)
import Data.FileEmbed (embedFile)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Network.HTTP.Date (parseHTTPDate, epochTimeToHTTPDate, formatHTTPDate)
import WaiAppStatic.Types
import Util
import WaiAppStatic.Storage.Filesystem
import WaiAppStatic.Storage.Embedded
import Network.Mime (MimeType)
data StaticResponse =
Redirect Pieces (Maybe ByteString)
| RawRedirect ByteString
| NotFound
| FileResponse File H.ResponseHeaders
| NotModified
| SendContent MimeType L.ByteString
| WaiResponse W.Response
safeInit :: [a] -> [a]
safeInit [] = []
safeInit xs = init xs
filterButLast :: (a -> Bool) -> [a] -> [a]
filterButLast _ [] = []
filterButLast _ [x] = [x]
filterButLast f (x:xs)
| f x = x : filterButLast f xs
| otherwise = filterButLast f xs
serveFolder :: StaticSettings -> Pieces -> W.Request -> Folder -> IO StaticResponse
serveFolder StaticSettings {..} pieces req folder@Folder {..} =
case ssListing of
Just _ | Just path <- addTrailingSlash req, ssAddTrailingSlash ->
return $ RawRedirect path
Just listing -> do
builder <- listing pieces folder
return $ WaiResponse $ W.responseBuilder H.status200
[ ("Content-Type", "text/html; charset=utf-8")
] builder
Nothing -> return $ WaiResponse $ W.responseLBS H.status403
[ ("Content-Type", "text/plain")
] "Directory listings disabled"
addTrailingSlash :: W.Request -> Maybe ByteString
addTrailingSlash req
| S8.null rp = Just "/"
| S8.last rp == '/' = Nothing
| otherwise = Just $ S8.snoc rp '/'
where
rp = W.rawPathInfo req
checkPieces :: StaticSettings
-> Pieces
-> W.Request
-> IO StaticResponse
checkPieces _ pieces _ | any (T.null . fromPiece) $ safeInit pieces =
return $ Redirect (filterButLast (not . T.null . fromPiece) pieces) Nothing
checkPieces ss@StaticSettings {..} pieces req = do
res <- lookupResult
case res of
Left location -> return $ RawRedirect location
Right LRNotFound -> return NotFound
Right (LRFile file) -> serveFile ss req file
Right (LRFolder folder) -> serveFolder ss pieces req folder
where
lookupResult :: IO (Either ByteString LookupResult)
lookupResult = do
nonIndexResult <- ssLookupFile pieces
case nonIndexResult of
LRFile{} -> return $ Right nonIndexResult
_ -> do
eIndexResult <- lookupIndices (map (\ index -> dropLastIfNull pieces ++ [index]) ssIndices)
return $ case eIndexResult of
Left redirect -> Left redirect
Right indexResult -> case indexResult of
LRNotFound -> Right nonIndexResult
LRFile file | ssRedirectToIndex ->
let relPath =
case reverse pieces of
[] -> fromPiece $ fileName file
lastSegment:_ ->
case fromPiece lastSegment of
"" -> fromPiece $ fileName file
lastSegment' -> T.concat
[ lastSegment'
, "/"
, fromPiece $ fileName file
]
in Left $ TE.encodeUtf8 relPath
_ -> Right indexResult
lookupIndices :: [Pieces] -> IO (Either ByteString LookupResult)
lookupIndices (x : xs) = do
res <- ssLookupFile x
case res of
LRNotFound -> lookupIndices xs
_ -> return $ case (ssAddTrailingSlash, addTrailingSlash req) of
(True, Just redirect) -> Left redirect
_ -> Right res
lookupIndices [] = return $ Right LRNotFound
serveFile :: StaticSettings -> W.Request -> File -> IO StaticResponse
serveFile StaticSettings {..} req file
| ssUseHash = do
mHash <- fileGetHash file
case (mHash, lookup "if-none-match" $ W.requestHeaders req) of
(Just hash, Just lastHash) | hash == lastHash -> return NotModified
(Just hash, _) -> respond [("ETag", hash)]
(Nothing, _) -> lastMod
| otherwise = lastMod
where
mLastSent = lookup "if-modified-since" (W.requestHeaders req) >>= parseHTTPDate
lastMod =
case (fmap epochTimeToHTTPDate $ fileGetModified file, mLastSent) of
(Just mdate, Just lastSent)
| mdate == lastSent -> return NotModified
(Just mdate, _) -> respond [("last-modified", formatHTTPDate mdate)]
(Nothing, _) -> respond []
respond headers = return $ FileResponse file $ cacheControl ssMaxAge headers
cacheControl :: MaxAge -> (H.ResponseHeaders -> H.ResponseHeaders)
cacheControl maxage =
headerCacheControl . headerExpires
where
ccInt =
case maxage of
NoMaxAge -> Nothing
MaxAgeSeconds i -> Just i
MaxAgeForever -> Just oneYear
oneYear :: Int
oneYear = 60 * 60 * 24 * 365
headerCacheControl =
case ccInt of
Nothing -> id
Just i -> (:) ("Cache-Control", S8.append "public, max-age=" $ S8.pack $ show i)
headerExpires =
case maxage of
NoMaxAge -> id
MaxAgeSeconds _ -> id
MaxAgeForever -> (:) ("Expires", "Thu, 31 Dec 2037 23:55:55 GMT")
staticApp :: StaticSettings -> W.Application
staticApp set req = staticAppPieces set (W.pathInfo req) req
staticAppPieces :: StaticSettings -> [Text] -> W.Application
staticAppPieces _ _ req sendResponse
| notElem (W.requestMethod req) ["GET", "HEAD"] = sendResponse $ W.responseLBS
H.status405
[("Content-Type", "text/plain")]
"Only GET or HEAD is supported"
staticAppPieces _ [".hidden", "folder.png"] _ sendResponse = sendResponse $ W.responseLBS H.status200 [("Content-Type", "image/png")] $ L.fromChunks [$(embedFile "images/folder.png")]
staticAppPieces _ [".hidden", "haskell.png"] _ sendResponse = sendResponse $ W.responseLBS H.status200 [("Content-Type", "image/png")] $ L.fromChunks [$(embedFile "images/haskell.png")]
staticAppPieces ss rawPieces req sendResponse = liftIO $ do
case toPieces rawPieces of
Just pieces -> checkPieces ss pieces req >>= response
Nothing -> sendResponse $ W.responseLBS H.status403
[ ("Content-Type", "text/plain")
] "Forbidden"
where
response :: StaticResponse -> IO W.ResponseReceived
response (FileResponse file ch) = do
mimetype <- ssGetMimeType ss file
let headers = ("Content-Type", mimetype)
: ch
sendResponse $ fileToResponse file H.status200 headers
response NotModified =
sendResponse $ W.responseLBS H.status304 [] ""
response (SendContent mt lbs) = do
sendResponse $ W.responseLBS H.status200
[ ("Content-Type", mt)
] lbs
response (Redirect pieces' mHash) = do
let loc = ssMkRedirect ss pieces' $ L.toStrict $ toLazyByteString (H.encodePathSegments $ map fromPiece pieces')
let qString = case mHash of
Just hash -> replace "etag" (Just hash) (W.queryString req)
Nothing -> remove "etag" (W.queryString req)
sendResponse $ W.responseLBS H.status302
[ ("Content-Type", "text/plain")
, ("Location", S8.append loc $ H.renderQuery True qString)
] "Redirect"
response (RawRedirect path) =
sendResponse $ W.responseLBS H.status302
[ ("Content-Type", "text/plain")
, ("Location", path)
] "Redirect"
response NotFound = case (ss404Handler ss) of
Just app -> app req sendResponse
Nothing -> sendResponse $ W.responseLBS H.status404
[ ("Content-Type", "text/plain")
] "File not found"
response (WaiResponse r) = sendResponse r