newtype and Source in WAI

This commit is contained in:
Snoyman 2010-02-26 10:40:05 +02:00
parent d2e88391a7
commit 70a06a8808
2 changed files with 12 additions and 6 deletions

View File

@ -38,7 +38,7 @@ module Yesod.Request
) where
import qualified Network.Wai as W
import qualified Network.Wai.Enumerator as WE
import qualified Network.Wai.Source as WS
import Data.Function.Predicate (equals)
import Yesod.Definitions
import Web.Encodings
@ -138,7 +138,7 @@ parseWaiRequest env session = do
rbHelper :: W.Request -> IO RequestBodyContents
rbHelper env = do
inputLBS <- WE.toLBS $ W.requestBody env -- FIXME
inputLBS <- WS.toLBS $ W.requestBody env -- FIXME
let clength = maybe "0" cs $ lookup W.ReqContentLength
$ W.requestHeaders env
let ctype = maybe "" cs $ lookup W.ReqContentType $ W.requestHeaders env

View File

@ -81,12 +81,15 @@ import Test.Framework (testGroup, Test)
#endif
data Content = ContentFile FilePath
| ContentEnum (forall a. W.Enumerator a)
| ContentEnum (forall a.
(a -> B.ByteString -> IO (Either a a))
-> a
-> IO (Either a a))
instance ConvertSuccess B.ByteString Content where
convertSuccess bs = ContentEnum $ \f a -> f a bs
instance ConvertSuccess L.ByteString Content where
convertSuccess = ContentEnum . WE.fromLBS
convertSuccess = swapEnum . WE.fromLBS
instance ConvertSuccess T.Text Content where
convertSuccess t = cs (cs t :: B.ByteString)
instance ConvertSuccess Text Content where
@ -103,7 +106,10 @@ type ChooseRep = [ContentType] -> IO (ContentType, Content)
-- | It would be nice to simplify 'Content' to the point where this is
-- unnecesary.
ioTextToContent :: IO Text -> Content
ioTextToContent t = ContentEnum $ WE.fromLBS' $ fmap DTLE.encodeUtf8 t
ioTextToContent = swapEnum . WE.fromLBS' . fmap DTLE.encodeUtf8
swapEnum :: W.Enumerator -> Content
swapEnum (W.Enumerator e) = ContentEnum e
hoToJsonContent :: HtmlObject -> Content
hoToJsonContent = cs . unJsonDoc . cs
@ -231,7 +237,7 @@ responseToWaiResponse (Response sc hs ct c) = do
let hs'' = (W.ContentType, cs ct) : hs'
return $ W.Response sc hs'' $ case c of
ContentFile fp -> Left fp
ContentEnum e -> Right e
ContentEnum e -> Right $ W.Enumerator e
#if TEST
runContent :: Content -> IO L.ByteString