2 module Network.HTTP.Lucu.Postprocess
4 , completeUnconditionalHeaders
8 import Control.Concurrent.STM
12 import GHC.Conc (unsafeIOToSTM)
13 import Network.HTTP.Lucu.Abortion
14 import Network.HTTP.Lucu.Config
15 import Network.HTTP.Lucu.Headers
16 import Network.HTTP.Lucu.HttpVersion
17 import Network.HTTP.Lucu.Interaction
18 import Network.HTTP.Lucu.RFC1123DateTime
19 import Network.HTTP.Lucu.Request
20 import Network.HTTP.Lucu.Response
21 import Network.HTTP.Lucu.Utils
26 * Response が未設定なら、200 OK にする。
28 * ステータスが 2xx, 3xx, 4xx, 5xx のいずれでもなければ 500 にする。
30 * 405 Method Not Allowed なのに Allow ヘッダが無ければ 500 にする。
32 * 304 Not Modified 以外の 3xx なのに Location ヘッダが無ければ 500 に
35 * Content-Length があれば、それを削除する。Transfer-Encoding があって
38 * HTTP/1.1 であり、body を持つ事が出來る時、Transfer-Encoding を
41 * body を持つ事が出來る時、Content-Type が無ければデフォルト値にする。
42 出來ない時、HEAD でなければContent-Type, Etag, Last-Modified を削除
45 * body を持つ事が出來ない時、body 破棄フラグを立てる。
47 * Connection: close が設定されてゐる時、切斷フラグを立てる。
49 * 切斷フラグが立ってゐる時、Connection: close を設定する。
57 postprocess :: Interaction -> STM ()
60 do reqM <- readItr itr itrRequest id
61 res <- readItr itr itrResponse id
62 let sc = resStatus res
64 when (not $ any (\ p -> p sc) [isSuccessful, isRedirection, isError])
65 $ abortSTM InternalServerError []
66 $ Just ("The status code is not good for a final status: "
69 when (sc == MethodNotAllowed && getHeader "Allow" res == Nothing)
70 $ abortSTM InternalServerError []
71 $ Just ("The status was " ++ show sc ++ " but no Allow header.")
73 when (sc /= NotModified && isRedirection sc && getHeader "Location" res == Nothing)
74 $ abortSTM InternalServerError []
75 $ Just ("The status code was " ++ show sc ++ " but no Location header.")
77 when (reqM /= Nothing)
80 -- itrResponse の内容は relyOnRequest によって變へられてゐる可
82 do oldRes <- readItr itr itrResponse id
83 newRes <- unsafeIOToSTM
84 $ completeUnconditionalHeaders (itrConfig itr) oldRes
85 writeItr itr itrResponse newRes
87 relyOnRequest :: Interaction -> STM ()
90 do status <- readItr itr itrResponse resStatus
91 req <- readItr itr itrRequest fromJust
93 let reqVer = reqVersion req
94 canHaveBody = if reqMethod req == HEAD then
97 not (isInformational status ||
98 status == NoContent ||
99 status == ResetContent ||
100 status == NotModified )
102 updateRes itr $! deleteHeader "Content-Length"
103 updateRes itr $! deleteHeader "Transfer-Encoding"
105 cType <- readHeader itr "Content-Type"
106 when (cType == Nothing)
107 $ updateRes itr $ setHeader "Content-Type" defaultPageContentType
110 when (reqVer == HttpVersion 1 1)
111 $ do updateRes itr $! setHeader "Transfer-Encoding" "chunked"
112 writeItr itr itrWillChunkBody True
114 -- body 関連のヘッダを削除。但し HEAD なら Content-* は殘す
115 when (reqMethod req /= HEAD)
116 $ do updateRes itr $! deleteHeader "Content-Type"
117 updateRes itr $! deleteHeader "Etag"
118 updateRes itr $! deleteHeader "Last-Modified"
120 conn <- readHeader itr "Connection"
121 case fmap (map toLower) conn of
122 Just "close" -> writeItr itr itrWillClose True
125 willClose <- readItr itr itrWillClose id
127 $ updateRes itr $! setHeader "Connection" "close"
129 when (reqMethod req == HEAD || not canHaveBody)
130 $ writeTVar (itrWillDiscardBody itr) True
132 readHeader :: Interaction -> String -> STM (Maybe String)
134 = itr `seq` name `seq`
135 readItr itr itrResponse $ getHeader name
137 updateRes :: Interaction -> (Response -> Response) -> STM ()
138 updateRes itr updator
139 = itr `seq` updator `seq`
140 updateItr itr itrResponse updator
143 completeUnconditionalHeaders :: Config -> Response -> IO Response
144 completeUnconditionalHeaders conf res
145 = conf `seq` res `seq`
146 return res >>= compServer >>= compDate >>= return
149 = case getHeader "Server" res of
150 Nothing -> return $ addHeader "Server" (cnfServerSoftware conf) res
154 = case getHeader "Date" res of
155 Nothing -> do time <- getClockTime
156 return $ addHeader "Date" (formatHTTPDateTime time) res