- relyOnRequest ∷ STM ()
- relyOnRequest
- = do status ← readItr itr itrResponse resStatus
- req ← readItr itr itrRequest fromJust
-
- let reqVer = reqVersion req
- canHaveBody = if reqMethod req ≡ HEAD then
- False
- else
- not (isInformational status ∨
- status ≡ NoContent ∨
- status ≡ ResetContent ∨
- status ≡ NotModified )
-
- updateRes $ deleteHeader "Content-Length"
- updateRes $ deleteHeader "Transfer-Encoding"
-
- cType ← readHeader "Content-Type"
- when (cType ≡ Nothing)
- $ updateRes $ setHeader "Content-Type" defaultPageContentType
-
- if canHaveBody then
- when (reqVer ≡ HttpVersion 1 1)
- $ do updateRes $ setHeader "Transfer-Encoding" "chunked"
- writeItr itr itrWillChunkBody True
- else
- -- body 関連のヘッダを削除。但し HEAD なら Content-* は殘す
- when (reqMethod req /= HEAD)
- $ do updateRes $ deleteHeader "Content-Type"
- updateRes $ deleteHeader "Etag"
- updateRes $ deleteHeader "Last-Modified"
-
- conn ← readHeader "Connection"
- case conn of
- Nothing → return ()
- Just value → when (A.toCIAscii value ≡ "close")
- $ writeItr itr itrWillClose True
-
- willClose ← readItr itr itrWillClose id
- when willClose
- $ updateRes $ setHeader "Connection" "close"
-
- when (reqMethod req ≡ HEAD ∨ not canHaveBody)
- $ writeTVar (itrWillDiscardBody itr) True
-
- readHeader ∷ CIAscii → STM (Maybe Ascii)
- readHeader = readItr itr itrResponse ∘ getHeader
-
- updateRes ∷ (Response → Response) → STM ()
- updateRes = updateItr itr itrResponse
+ go ∷ Response → STM ()
+ go res@(Response {..})
+ = do unless (any (\ p → p resStatus) [ isSuccessful
+ , isRedirection
+ , isError
+ ])
+ $ abort'
+ $ A.toAsciiBuilder "Inappropriate status code for a response: "
+ ⊕ printStatusCode resStatus
+
+ when ( resStatus ≡ MethodNotAllowed ∧
+ hasHeader "Allow" res )
+ $ abort'
+ $ A.toAsciiBuilder "The status was "
+ ⊕ printStatusCode resStatus
+ ⊕ A.toAsciiBuilder " but no \"Allow\" header."
+
+ when ( resStatus ≢ NotModified ∧
+ isRedirection resStatus ∧
+ hasHeader "Location" res )
+ $ abort'
+ $ A.toAsciiBuilder "The status code was "
+ ⊕ printStatusCode resStatus
+ ⊕ A.toAsciiBuilder " but no Location header."
+
+ abort' ∷ AsciiBuilder → STM ()
+ abort' = abortSTM InternalServerError []
+ ∘ Just
+ ∘ A.toText
+ ∘ A.fromAsciiBuilder
+
+postprocessWithRequest ∷ Interaction → Request → STM ()
+postprocessWithRequest itr@(Interaction {..}) (Request {..})
+ = do willDiscardBody ← readTVar itrWillDiscardBody
+ canHaveBody ← if willDiscardBody then
+ return False
+ else
+ resCanHaveBody <$> readTVar itrResponse
+
+ updateRes itr
+ $ deleteHeader "Content-Length"
+ ∘ deleteHeader "Transfer-Encoding"
+
+ if canHaveBody then
+ do when (reqVersion ≡ HttpVersion 1 1)
+ $ do writeHeader itr "Transfer-Encoding" (Just "chunked")
+ writeTVar itrWillChunkBody True
+ writeDefaultPageIfNeeded itr
+ else
+ do writeTVar itrWillDiscardBody True
+ -- These headers make sense for HEAD requests even
+ -- when there won't be a response entity body.
+ when (reqMethod ≢ HEAD)
+ $ updateRes itr
+ $ deleteHeader "Content-Type"
+ ∘ deleteHeader "Etag"
+ ∘ deleteHeader "Last-Modified"
+
+ hasConnClose ← (≡ Just "close") <$> readCIHeader itr "Connection"
+ willClose ← readTVar itrWillClose
+ when (hasConnClose ∧ (¬) willClose)
+ $ writeTVar itrWillClose True
+ when ((¬) hasConnClose ∧ willClose)
+ $ writeHeader itr "Connection" (Just "close")
+
+writeDefaultPageIfNeeded ∷ Interaction → STM ()
+writeDefaultPageIfNeeded itr@(Interaction {..})
+ = do resHasCType ← readTVar itrResponseHasCType
+ unless resHasCType
+ $ do writeHeader itr "Content-Type" (Just defaultPageContentType)
+ res ← readTVar itrResponse
+ let page = getDefaultPage itrConfig itrRequest res
+ putTMVar itrBodyToSend (BB.fromLazyText page)
+
+writeHeader ∷ Interaction → CIAscii → Maybe Ascii → STM ()
+{-# INLINE writeHeader #-}
+writeHeader itr k v
+ = case v of
+ Just v' → updateRes itr $ setHeader k v'
+ Nothing → updateRes itr $ deleteHeader k
+
+readCIHeader ∷ Interaction → CIAscii → STM (Maybe CIAscii)
+{-# INLINE readCIHeader #-}
+readCIHeader (Interaction {..}) k
+ = getCIHeader k <$> readTVar itrResponse
+
+updateRes ∷ Interaction → (Response → Response) → STM ()
+{-# INLINE updateRes #-}
+updateRes (Interaction {..}) f
+ = do old ← readTVar itrResponse
+ writeTVar itrResponse (f old)
+
+updateResIO ∷ Interaction → (Response → IO Response) → STM ()
+{-# INLINE updateResIO #-}
+updateResIO (Interaction {..}) f
+ = do old ← readTVar itrResponse
+ new ← unsafeIOToSTM $ f old
+ writeTVar itrResponse new