module Network.Wai.Conduit
(
sourceRequestBody
, responseSource
, responseRawSource
, module Network.Wai
) where
import Network.Wai
import Data.Conduit
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.ByteString (ByteString)
import qualified Data.ByteString as S
import Control.Monad (unless)
import Network.HTTP.Types
import Data.ByteString.Builder (Builder)
import Data.IORef
import qualified Data.Conduit.List as CL
sourceRequestBody :: MonadIO m => Request -> Source m ByteString
sourceRequestBody req =
loop
where
go = liftIO (requestBody req)
loop = do
bs <- go
unless (S.null bs) $ do
yield bs
loop
responseSource :: Status -> ResponseHeaders -> Source IO (Flush Builder) -> Response
responseSource s hs src = responseStream s hs $ \send flush ->
src $$ CL.mapM_ (\mbuilder ->
case mbuilder of
Chunk b -> send b
Flush -> flush)
responseRawSource :: (MonadIO m, MonadIO n)
=> (Source m ByteString -> Sink ByteString n () -> IO ())
-> Response
-> Response
responseRawSource app =
responseRaw app'
where
app' recv send =
app src sink
where
src = do
bs <- liftIO recv
unless (S.null bs) $ do
yield bs
src
sink = CL.mapM_ $ liftIO . send