on master help sign in
wai-conduit
commitc2eff63080b3ec6a335b299c8333b669228194cf authorMichael Snoyman <michael@snoyman.com> dateSun 1 Jun 2014, 08:05 (12 years ago) parent4148613
5 files changed+114
sources.txt+1 wai-conduit/LICENSE+20 wai-conduit/Network/Wai/Conduit.hs+67 wai-conduit/Setup.hs+2 wai-conduit/wai-conduit.cabal+24
sources.txt+1
@@ -9,4 +9,5 @@
99./wai-frontend-monadcgi
1010./wai-websockets
1111./wai-eventsource
12+./wai-conduit
1213./mime-types
wai-conduit/LICENSEnew file+20
@@ -0,0 +1,20 @@
1+Copyright (c) 2014 Michael Snoyman
2+
3+Permission is hereby granted, free of charge, to any person obtaining
4+a copy of this software and associated documentation files (the
5+"Software"), to deal in the Software without restriction, including
6+without limitation the rights to use, copy, modify, merge, publish,
7+distribute, sublicense, and/or sell copies of the Software, and to
8+permit persons to whom the Software is furnished to do so, subject to
9+the following conditions:
10+
11+The above copyright notice and this permission notice shall be included
12+in all copies or substantial portions of the Software.
13+
14+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wai-conduit/Network/Wai/Conduit.hsnew file+67
@@ -0,0 +1,67 @@
1+-- | A light-weight wrapper around @Network.Wai@ to provide easy conduit support.
2+module Network.Wai.Conduit
3+ ( -- * Request body
4+ sourceRequestBody
5+ -- * Response body
6+ , responseSource
7+ , responseRawSource
8+ -- * Re-export
9+ , module Network.Wai
10+ ) where
11+
12+import Network.Wai
13+import Data.Conduit
14+import Control.Monad.IO.Class (MonadIO, liftIO)
15+import Data.ByteString (ByteString)
16+import qualified Data.ByteString as S
17+import Control.Monad (unless)
18+import Network.HTTP.Types
19+import Blaze.ByteString.Builder (Builder)
20+import Data.IORef
21+import qualified Data.Conduit.List as CL
22+
23+-- | Stream the request body.
24+--
25+-- Since 3.0.0
26+sourceRequestBody :: MonadIO m => Request -> Source m ByteString
27+sourceRequestBody req =
28+ loop
29+ where
30+ go = liftIO (requestBody req)
31+
32+ loop = do
33+ bs <- go
34+ unless (S.null bs) $ do
35+ yield bs
36+ loop
37+
38+-- | Create an HTTP response out of a @Source@.
39+--
40+-- Since 3.0.0
41+responseSource :: Status -> ResponseHeaders -> Source IO (Flush Builder) -> Response
42+responseSource s hs src = responseStream s hs $ \send flush ->
43+ src $$ CL.mapM_ (\mbuilder ->
44+ case mbuilder of
45+ Chunk b -> send b
46+ Flush -> flush)
47+
48+-- | Create a raw response using a @Source@ and @Sink@ to represent the input
49+-- and output, respectively.
50+--
51+-- Since 3.0.0
52+responseRawSource :: (MonadIO m, MonadIO n)
53+ => (Source m ByteString -> Sink ByteString n () -> IO ())
54+ -> Response
55+ -> Response
56+responseRawSource app backup =
57+ responseRaw app' backup
58+ where
59+ app' recv send =
60+ app src sink
61+ where
62+ src = do
63+ bs <- liftIO recv
64+ unless (S.null bs) $ do
65+ yield bs
66+ src
67+ sink = CL.mapM_ $ liftIO . send
wai-conduit/Setup.hsnew file+2
@@ -0,0 +1,2 @@
1+import Distribution.Simple
2+main = defaultMain
wai-conduit/wai-conduit.cabalnew file+24
@@ -0,0 +1,24 @@
1+name: wai-conduit
2+version: 3.0.0
3+synopsis: conduit wrappers for WAI
4+description: Since version 3.0.0, WAI has no built-in streaming data abstraction.
5+ This library provides similar functionality to what existed in WAI 2.x.
6+homepage: https://github.com/yesodweb/wai
7+license: MIT
8+license-file: LICENSE
9+author: Michael Snoyman
10+maintainer: michael@snoyman.com
11+category: Web, Conduit
12+build-type: Simple
13+cabal-version: >=1.10
14+
15+library
16+ exposed-modules: Network.Wai.Conduit
17+ build-depends: base >= 4 && < 5
18+ , wai >= 3.0 && < 3.1
19+ , conduit
20+ , transformers
21+ , bytestring
22+ , http-types
23+ , blaze-builder
24+ default-language: Haskell2010