module Network.Wai.Application.Classic.File (
fileApp
) where
import Control.Monad.IO.Class (liftIO)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS hiding (unpack, pack)
import qualified Data.ByteString.Char8 as BS (unpack, pack)
import qualified Data.ByteString.Lazy.Char8 as BL (length)
import Network.HTTP.Types
import Network.Wai
import Network.Wai.Application.Classic.Field
import Network.Wai.Application.Classic.FileInfo
import Network.Wai.Application.Classic.MaybeIter
import Network.Wai.Application.Classic.Types
import Network.Wai.Application.Classic.Utils
fileApp :: AppSpec -> FileRoute -> Application
fileApp spec filei req = do
RspSpec st hdr body <- case method of
"GET" -> processGET req file ishtml rfile
"HEAD" -> processHEAD req file ishtml rfile
_ -> return notAllowed
let hdr'= addServer hdr
(response, mlen) = case body of
NoBody -> (responseLBS st hdr' "", Nothing)
BodyLBS bd ->
let len = fromIntegral $ BL.length bd
in (responseLBS st hdr' bd, Just len)
BodyFile afile rng ->
let (len, mfp) = case rng of
Entire bytes -> (bytes, Nothing)
Part skip bytes -> (bytes, Just (FilePart skip bytes))
hdr'' = addLength hdr' len
afile' = BS.unpack afile
in (ResponseFile st hdr'' afile' mfp, Just len)
liftIO $ logger spec req st mlen
return response
where
method = requestMethod req
path = pathinfoToFilePath req filei
file = addIndex spec path
ishtml = isHTML spec file
rfile = redirectPath spec path
addServer hdr = ("Server", softwareName spec) : hdr
addLength hdr len = ("Content-Length", BS.pack . show $ len) : hdr
type Lang = Maybe ByteString
langSuffixes :: Request -> [Lang]
langSuffixes req = map (Just . BS.cons 46) (languages req) ++ [Nothing, Just ".en"]
processGET :: Request -> ByteString -> Bool -> Maybe ByteString -> Rsp
processGET req file ishtml rfile = runAny [
tryGet req file ishtml
, tryRedirect req rfile
, just notFound
]
tryGet :: Request -> ByteString -> Bool -> MRsp
tryGet req file True = runAnyMaybe $ map (tryGetFile req file True) langs
where
langs = langSuffixes req
tryGet req file False = tryGetFile req file False Nothing
tryGetFile :: Request -> ByteString -> Bool -> Lang -> MRsp
tryGetFile req file ishtml mlang = do
let file' = maybe file (file +++) mlang
liftIO (fileInfo file') |>| \(size, mtime) -> do
let hdr = newHeader ishtml file mtime
Just pst = ifmodified req size mtime
||| ifunmodified req size mtime
||| ifrange req size mtime
||| unconditional req size mtime
case pst of
Full st
| st == statusOK -> just $ RspSpec statusOK hdr (BodyFile file' (Entire size))
| otherwise -> just $ RspSpec st hdr NoBody
Partial skip len -> just $ RspSpec statusPartialContent hdr (BodyFile file' (Part skip len))
processHEAD :: Request -> ByteString -> Bool -> Maybe ByteString -> Rsp
processHEAD req file ishtml rfile = runAny [
tryHead req file ishtml
, tryRedirect req rfile
, just notFound
]
tryHead :: Request -> ByteString -> Bool -> MRsp
tryHead req file True = runAnyMaybe $ map (tryHeadFile req file True) langs
where
langs = langSuffixes req
tryHead req file False= tryHeadFile req file False Nothing
tryHeadFile :: Request -> ByteString -> Bool -> Lang -> MRsp
tryHeadFile req file ishtml mlang = do
let file' = maybe file (file +++) mlang
liftIO (fileInfo file') |>| \(size, mtime) -> do
let hdr = newHeader ishtml file mtime
Just pst = ifmodified req size mtime
||| Just (Full statusOK)
case pst of
Full st -> just $ RspSpec st hdr NoBody
_ -> nothing
tryRedirect :: Request -> Maybe ByteString -> MRsp
tryRedirect _ Nothing = nothing
tryRedirect req (Just file) =
runAnyMaybe $ map (tryRedirectFile req file) langs
where
langs = langSuffixes req
tryRedirectFile :: Request -> ByteString -> Lang -> MRsp
tryRedirectFile req file mlang = do
let file' = maybe file (file +++) mlang
minfo <- liftIO $ fileInfo file'
case minfo of
Nothing -> nothing
Just _ -> just $ RspSpec statusMovedPermanently hdr NoBody
where
hdr = [("Location", redirectURL)]
redirectURL = "http://"
+++ serverName req
+++ ":"
+++ (BS.pack . show . serverPort) req
+++ rawPathInfo req
+++ "/"
notFound :: RspSpec
notFound = RspSpec statusNotFound textPlain (BodyLBS "Not Found\r\n")
notAllowed :: RspSpec
notAllowed = RspSpec statusNotAllowed textPlain (BodyLBS "Method Not Allowed\r\n")