ContentBuilder and responseBuilder

This commit is contained in:
Michael Snoyman 2010-12-21 00:07:50 +02:00
parent 48e8b1d9ca
commit 8d7d5ce167
2 changed files with 8 additions and 7 deletions

View File

@ -70,15 +70,16 @@ import Test.HUnit hiding (Test)
#endif #endif
import Data.Enumerator (Enumerator) import Data.Enumerator (Enumerator)
import Blaze.ByteString.Builder (Builder) import Blaze.ByteString.Builder (Builder, fromByteString, fromLazyByteString)
import Data.Monoid (mempty)
data Content = ContentLBS L.ByteString data Content = ContentBuilder Builder
| ContentEnum (forall a. Enumerator Builder IO a) | ContentEnum (forall a. Enumerator Builder IO a)
| ContentFile FilePath | ContentFile FilePath
-- | Zero-length enumerator. -- | Zero-length enumerator.
emptyContent :: Content emptyContent :: Content
emptyContent = ContentLBS L.empty emptyContent = ContentBuilder mempty
-- | Anything which can be converted into 'Content'. Most of the time, you will -- | Anything which can be converted into 'Content'. Most of the time, you will
-- want to use the 'ContentEnum' constructor. An easier approach will be to use -- want to use the 'ContentEnum' constructor. An easier approach will be to use
@ -88,13 +89,13 @@ class ToContent a where
toContent :: a -> Content toContent :: a -> Content
instance ToContent B.ByteString where instance ToContent B.ByteString where
toContent = ContentLBS . L.fromChunks . return toContent = ContentBuilder . fromByteString
instance ToContent L.ByteString where instance ToContent L.ByteString where
toContent = ContentLBS toContent = ContentBuilder . fromLazyByteString
instance ToContent T.Text where instance ToContent T.Text where
toContent = toContent . Data.Text.Encoding.encodeUtf8 toContent = toContent . Data.Text.Encoding.encodeUtf8
instance ToContent Text where instance ToContent Text where
toContent = ContentLBS . Data.Text.Lazy.Encoding.encodeUtf8 toContent = toContent . Data.Text.Lazy.Encoding.encodeUtf8
instance ToContent String where instance ToContent String where
toContent = toContent . T.pack toContent = toContent . T.pack

View File

@ -339,7 +339,7 @@ toWaiApp' y key' segments env = do
hs''' = ("Content-Type", charsToBs ct) : hs'' hs''' = ("Content-Type", charsToBs ct) : hs''
return $ return $
case c of case c of
ContentLBS lbs -> W.ResponseLBS s hs''' lbs ContentBuilder b -> W.responseBuilder s hs''' b
ContentFile fp -> W.ResponseFile s hs''' fp ContentFile fp -> W.ResponseFile s hs''' fp
ContentEnum e -> W.ResponseEnumerator $ \iter -> ContentEnum e -> W.ResponseEnumerator $ \iter ->
run_ $ e $$ iter s hs''' run_ $ e $$ iter s hs'''