Debian Codegraph @

Network/Wai/Application/Classic/File.hs

raw
1 {-# LANGUAGE OverloadedStrings, CPP #-}
2
3 module Network.Wai.Application.Classic.File (
4 fileApp
5 , redirectHeader
6 ) where
7
8 import Control.Applicative
9 import Data.ByteString (ByteString)
10 import Data.Maybe
11 import qualified Data.ByteString.Char8 as BS (concat)
12 import Network.HTTP.Types
13 import Network.Wai
14 import Network.Wai.Internal
15 import Network.Wai.Application.Classic.Field
16 import Network.Wai.Application.Classic.FileInfo
17 import Network.Wai.Application.Classic.Path
18 import Network.Wai.Application.Classic.Status
19 import Network.Wai.Application.Classic.Types
20 import Network.Wai.Handler.Warp (getFileInfo)
21
22 ----------------------------------------------------------------
23
24 data RspSpec = NoBody Status
25 | NoBodyHdr Status ResponseHeaders
26 | BodyFile Status ResponseHeaders FilePath
27 deriving (Eq,Show)
28
29 ----------------------------------------------------------------
30
31 data HandlerInfo = HandlerInfo FileAppSpec Request Path [Lang]
32
33 langSuffixes :: RequestHeaders -> [Lang]
34 langSuffixes hdr = map (\x -> (<.> x)) langs ++ [id, (<.> "en")]
35 where
36 langs = languages hdr
37
38 ----------------------------------------------------------------
39
40 {-|
41 Handle GET and HEAD for a static file.
42
43 If 'pathInfo' ends with \'/\', 'indexFile' is automatically
44 added. In this case, "Acceptable-Language:" is also handled. Suppose
45 'indexFile' is "index.html" and if the value is "ja,en", then
46 \"index.html.ja\", \"index.html.en\", and \"index.html\" are tried to be
47 opened in order.
48
49 If 'pathInfo' does not end with \'/\' and a corresponding index file
50 exist, redirection is specified in HTTP response.
51
52 Directory contents are NOT automatically listed. To list directory
53 contents, an index file must be created beforehand.
54
55 The following HTTP headers are handled: Acceptable-Language:,
56 If-Modified-Since:, Range:, If-Range:, If-Unmodified-Since:.
57 -}
58
59 fileApp :: ClassicAppSpec -> FileAppSpec -> FileRoute -> Application
60 fileApp cspec spec filei req respond = do
61 rspspec <- case method of
62 Right GET -> processGET hinfo ishtml rfile
63 Right HEAD -> processGET hinfo ishtml rfile
64 _ -> return notAllowed
65 response <- case rspspec of
66 NoBody st -> bodyStatus st
67 NoBodyHdr st hdr -> return $ responseLBS st hdr ""
68 BodyFile st hdr fl -> return $ ResponseFile st hdr fl Nothing
69 respond response
70 where
71 hinfo = HandlerInfo spec req file langs
72 method = parseMethod $ requestMethod req
73 path = pathinfoToFilePath req filei
74 file = addIndex spec path
75 ishtml = isHTML spec file
76 rfile = redirectPath spec path
77 langs = langSuffixes $ requestHeaders req
78 noBody st = return $ responseLBS st [] ""
79 bodyStatus st = getStatusInfo cspec req langs st
80 >>= statusBody st
81 statusBody st StatusNone = noBody st
82 statusBody st (StatusByteString bd) =
83 return $ responseLBS st hdr bd
84 where
85 hdr = textPlainHeader
86 statusBody st (StatusFile afile len) =
87 return $ ResponseFile st hdr fl mfp
88 where
89 mfp = Just (FilePart 0 len len)
90 fl = pathString afile
91 hdr = textHtmlHeader
92
93 ----------------------------------------------------------------
94
95 processGET :: HandlerInfo -> Bool -> Maybe Path -> IO RspSpec
96 processGET hinfo ishtml rfile = tryGet hinfo ishtml
97 <|> tryRedirect hinfo rfile
98 <|> return notFound
99
100 tryGet :: HandlerInfo -> Bool -> IO RspSpec
101 tryGet hinfo@(HandlerInfo _ _ _ langs) True =
102 runAnyOne $ map (tryGetFile hinfo True) langs
103 tryGet hinfo False = tryGetFile hinfo False id
104
105 tryGetFile :: HandlerInfo -> Bool -> Lang -> IO RspSpec
106 tryGetFile (HandlerInfo _ req file _) ishtml lang = do
107 let file' = pathString $ lang file
108 hdr = newHeader ishtml file
109 _ <- getFileInfo req file' -- expecting an error
110 return $ BodyFile ok200 hdr file'
111
112 ----------------------------------------------------------------
113
114 tryRedirect :: HandlerInfo -> Maybe Path -> IO RspSpec
115 tryRedirect _ Nothing = empty
116 tryRedirect (HandlerInfo spec req _ langs) (Just file) =
117 runAnyOne $ map (tryRedirectFile hinfo) langs
118 where
119 hinfo = HandlerInfo spec req file langs
120
121 tryRedirectFile :: HandlerInfo -> Lang -> IO RspSpec
122 tryRedirectFile (HandlerInfo _ req file _) lang = do
123 let file' = pathString $ lang file
124 _ <- getFileInfo req file' -- expecting an error
125 return $ NoBodyHdr movedPermanently301 hdr
126 where
127 hdr = redirectHeader req
128
129 redirectHeader :: Request -> ResponseHeaders
130 redirectHeader = locationHeader . redirectURL
131
132 redirectURL :: Request -> ByteString
133 redirectURL req = BS.concat [
134 -- Scheme must not be included because of no way to tell
135 -- http or https.
136 "//"
137 -- Host includes ":<port>" if it is not 80.
138 , host
139 , rawPathInfo req
140 , "/"
141 ]
142 where
143 host = fromMaybe "" $ requestHeaderHost req
144
145 ----------------------------------------------------------------
146
147 notFound :: RspSpec
148 notFound = NoBody notFound404
149
150 notAllowed :: RspSpec
151 notAllowed = NoBody methodNotAllowed405
152
153 ----------------------------------------------------------------
154
155 runAnyOne :: [IO a] -> IO a
156 runAnyOne = foldr (<|>) empty