-{- Postprocess は body を補完した後で實行する事 -}
-
-postprocess :: Interaction -> STM ()
-postprocess itr
- = do res <- readItr itr itrResponse id
-
- when (res == Nothing)
- $ setStatus itr InternalServerError
-
- when (itrRequest itr /= Nothing)
- $ relyOnRequest itr
-
- do oldRes <- readItr itr itrResponse id
- newRes <- unsafeIOToSTM $ completeUnconditionalHeaders $ fromJust oldRes
- writeItr itr itrResponse $ Just newRes
+postprocess ∷ Interaction → STM ()
+postprocess !itr
+ = do reqM ← readItr itr itrRequest id
+ res ← readItr itr itrResponse id
+ let sc = resStatus res
+
+ unless (any (\ p → p sc) [isSuccessful, isRedirection, isError])
+ $ abortSTM InternalServerError []
+ $ Just
+ $ A.toText ( "The status code is not good for a final status of a response: "
+ ⊕ printStatusCode sc )
+
+ when (sc ≡ MethodNotAllowed ∧ getHeader "Allow" res ≡ Nothing)
+ $ abortSTM InternalServerError []
+ $ Just ("The status was " ++ show sc ++ " but no Allow header.")
+
+ when (sc /= NotModified ∧ isRedirection sc ∧ getHeader (C8.pack "Location") res ≡ Nothing)
+ $ abortSTM InternalServerError []
+ $ Just ("The status code was " ++ show sc ++ " but no Location header.")
+
+ when (reqM /= Nothing) relyOnRequest
+
+ -- itrResponse の内容は relyOnRequest によって變へられてゐる可
+ -- 能性が高い。
+ do oldRes ← readItr itr itrResponse id
+ newRes ← unsafeIOToSTM
+ $ completeUnconditionalHeaders (itrConfig itr) oldRes
+ writeItr itr itrResponse newRes