1 module Network.HTTP.Lucu.Preprocess
6 import Control.Concurrent.STM
8 import qualified Data.ByteString as Strict (ByteString)
9 import qualified Data.ByteString.Char8 as C8 hiding (ByteString)
12 import Network.HTTP.Lucu.Config
13 import Network.HTTP.Lucu.Headers
14 import Network.HTTP.Lucu.HttpVersion
15 import Network.HTTP.Lucu.Interaction
16 import Network.HTTP.Lucu.Request
17 import Network.HTTP.Lucu.Response
24 [1] HTTP/1.0 ならば Config を使って補完
25 [2] HTTP/1.1 ならば Host ヘッダで補完。Host が無ければ 400。
27 * Expect: に問題があった場合は 417 Expectation Failed に設定。
28 100-continue 以外のものは全部 417 に。
30 * Transfer-Encoding: に問題があったら 501 Not Implemented にする。具
31 体的には、identity でも chunked でもなければ 501 Not Implemented に
34 * メソッドが GET, HEAD, POST, PUT, DELETE の何れでもない場合は 501
37 * HTTP/1.0 でも HTTP/1.1 でもないリクエストに對しては 505 HTTP
38 Version Not Supported を返す。
40 * POST または PUT に Content-Length も Transfer-Encoding も無い時は、
41 411 Length Required にする。
43 * Content-Length の値が數値でなかったり負だったりしたら 400 Bad
46 * willDiscardBody その他の變數を設定する。
50 preprocess :: Interaction -> STM ()
53 do req <- readItr itr itrRequest fromJust
55 let reqVer = reqVersion req
57 if reqVer /= HttpVersion 1 0 &&
58 reqVer /= HttpVersion 1 1 then
60 do setStatus HttpVersionNotSupported
61 writeItr itr itrWillClose True
64 -- HTTP/1.0 では Keep-Alive できない
65 do when (reqVer == HttpVersion 1 0)
66 $ writeItr itr itrWillClose True
73 HEAD -> writeItr itr itrWillDiscardBody True
74 POST -> writeItr itr itrRequestHasBody True
75 PUT -> writeItr itr itrRequestHasBody True
77 _ -> setStatus NotImplemented
81 setStatus :: StatusCode -> STM ()
84 updateItr itr itrResponse
89 completeAuthority :: Request -> STM ()
92 when (uriAuthority (reqURI req) == Nothing)
93 $ if reqVersion req == HttpVersion 1 0 then
94 -- HTTP/1.0 なので Config から補完
95 do let conf = itrConfig itr
96 host = cnfServerHost conf
97 port = case cnfServerPort conf of
98 PortNumber n -> Just (fromIntegral n :: Int)
103 Just n -> Just $ ":" ++ show n
106 Just str -> updateAuthority host (C8.pack str)
107 -- FIXME: このエラーの原因は、listen してゐるソ
108 -- ケットが INET でない故にポート番號が分からな
109 -- い事だが、その事をどうにかして通知した方が良
111 Nothing -> setStatus InternalServerError
113 do case getHeader (C8.pack "Host") req of
114 Just str -> let (host, portStr) = parseHost str
115 in updateAuthority host portStr
116 Nothing -> setStatus BadRequest
119 parseHost :: Strict.ByteString -> (Strict.ByteString, Strict.ByteString)
120 parseHost = C8.break (== ':')
123 updateAuthority :: Strict.ByteString -> Strict.ByteString -> STM ()
124 updateAuthority host portStr
125 = host `seq` portStr `seq`
126 updateItr itr itrRequest
127 $! \ (Just req) -> Just req {
128 reqURI = let uri = reqURI req
130 uriAuthority = Just URIAuth {
132 , uriRegName = C8.unpack host
133 , uriPort = C8.unpack portStr
139 preprocessHeader :: Request -> STM ()
142 do case getHeader (C8.pack "Expect") req of
144 Just value -> if value `noCaseEq` C8.pack "100-continue" then
145 writeItr itr itrExpectedContinue True
147 setStatus ExpectationFailed
149 case getHeader (C8.pack "Transfer-Encoding") req of
151 Just value -> if value `noCaseEq` C8.pack "identity" then
154 if value `noCaseEq` C8.pack "chunked" then
155 writeItr itr itrRequestIsChunked True
157 setStatus NotImplemented
159 case getHeader (C8.pack "Content-Length") req of
161 Just value -> if C8.all isDigit value then
162 do let Just (len, _) = C8.readInt value
163 writeItr itr itrReqChunkLength $ Just len
164 writeItr itr itrReqChunkRemaining $ Just len
168 case getHeader (C8.pack "Connection") req of
170 Just value -> if value `noCaseEq` C8.pack "close" then
171 writeItr itr itrWillClose True