Debian Codegraph @

Network/Wai/Application/Classic/Status.hs

raw
1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE CPP #-}
3
4 module Network.Wai.Application.Classic.Status (getStatusInfo) where
5
6 import Control.Applicative
7 import Control.Arrow
8 import Control.Exception
9 import qualified Data.ByteString.Lazy as BL
10 import Data.ByteString.Lazy.Char8 ()
11 import qualified Data.StaticHash as M
12 import Network.HTTP.Types
13 import Network.Wai (Request)
14 import Network.Wai.Application.Classic.Path
15 import Network.Wai.Application.Classic.Types
16 import Network.Wai.Handler.Warp
17
18 ----------------------------------------------------------------
19
20 getStatusInfo :: ClassicAppSpec -> Request -> [Lang] -> Status -> IO StatusInfo
21 getStatusInfo cspec req langs st = getStatusFile getF dir code langs
22 <|> getStatusBS code
23 <|> return StatusNone
24 where
25 dir = statusFileDir cspec
26 getF = getFileInfo req
27 code = statusCode st
28
29 ----------------------------------------------------------------
30
31 statusList :: [Status]
32 statusList = [
33 methodNotAllowed405 -- File
34 , notFound404 -- File
35 , internalServerError500 -- CGI
36 , badGateway502 -- RevProxy
37 ]
38
39 ----------------------------------------------------------------
40
41 statusBSMap :: M.StaticHash Int StatusInfo
42 statusBSMap = M.fromList $ map (statusCode &&& toRspBody) statusList
43 where
44 toRspBody s = StatusByteString $ BL.fromChunks [statusMessage s, "\r\n"]
45
46 getStatusBS :: Int -> IO StatusInfo
47 getStatusBS code = case M.lookup code statusBSMap of
48 Nothing -> throwIO $ userError "getStatusBS"
49 Just x -> return x
50
51 ----------------------------------------------------------------
52
53 statusFileMap :: M.StaticHash Int Path
54 statusFileMap = M.fromList $ map (statusCode &&& toPath) statusList
55 where
56 toPath s = fromString $ show (statusCode s) ++ ".html"
57
58 getStatusFile :: (FilePath -> IO FileInfo) -> Path -> Int -> [Lang] -> IO StatusInfo
59 getStatusFile getF dir code langs = tryFile mfiles
60 where
61 mfiles = case M.lookup code statusFileMap of
62 Nothing -> []
63 Just file -> map ($ (dir </> file)) langs
64 tryFile = foldr func empty
65 func f io = StatusFile f . fileInfoSize <$> getF f' <|> io
66 where
67 f' = pathString f
68
69 ----------------------------------------------------------------