-output ∷ Lazy.ByteString → Resource ()
-{-# INLINE output #-}
-output str = outputChunk str *> driftTo Done
-
--- | Write a 'Lazy.ByteString' to the response body. This action can
--- be repeated as many times as you want. It is safe to apply
--- 'outputChunk' to an infinite string.
---
--- Note that you must first set the \"Content-Type\" response header
--- before applying this function. See: 'setContentType'
-outputChunk ∷ Lazy.ByteString → Resource ()
-outputChunk str
- = do driftTo DecidingBody
- itr ← getInteraction
- liftIO $ atomically
- $ do hasCType ← readTVar $ itrResponseHasCType itr
- unless hasCType
- $ abortSTM InternalServerError []
- $ Just "outputChunk: Content-Type has not been set."
- putTMVar (itrBodyToSend itr) (BB.fromLazyByteString str)
-
-driftTo ∷ InteractionState → Resource ()
-driftTo newState
- = do itr ← getInteraction
- liftIO $ atomically
- $ do oldState ← readTVar $ itrState itr
- if newState < oldState then
- throwStateError oldState newState
- else
- do let a = [oldState .. newState]
- b = tail a
- c = zip a b
- mapM_ (uncurry $ drift itr) c
- writeTVar (itrState itr) newState
- where
- throwStateError ∷ Monad m ⇒ InteractionState → InteractionState → m a
- throwStateError Done DecidingBody
- = fail "It makes no sense to output something after finishing outputs."
- throwStateError old new
- = fail ("state error: " ⧺ show old ⧺ " ==> " ⧺ show new)
-
- drift ∷ Interaction → InteractionState → InteractionState → STM ()
- drift (Interaction {..}) GettingBody _
- = writeTVar itrReqBodyWasteAll True
- drift itr DecidingHeader _
- = postprocess itr
- drift _ _ _
- = return ()