on master help sign in
Request body is an Enumerator
commitfb2cbb75abae035080e43fe82b3889dd347b2fde authorMichael Snoyman <michael@snoyman.com> dateThu 28 Jan 2010, 19:59 (17 years ago) parent8618fea
3 files changed+12−21
Network/Wai.hs+1−1 Network/Wai/Handler/SimpleServer.hs+8−6 test.hs+3−14
Network/Wai.hs+1−1
@@ -246,7 +246,7 @@ data Request = Request
246246 , serverPort :: Int
247247 , httpHeaders :: [(RequestHeader, B.ByteString)]
248248 , urlScheme :: UrlScheme
249− , requestBody :: IO (Maybe B.ByteString)
249+ , requestBody :: forall a. Enumerator a
250250 , errorHandler :: String -> IO ()
251251 , remoteHost :: B.ByteString
252252 }
Network/Wai/Handler/SimpleServer.hs+8−6
@@ -134,14 +134,16 @@ parseRequest port lines' handle remoteHost' = do
134134 , remoteHost = B8.pack remoteHost'
135135 }
136136
137−requestBodyHandle :: Handle -> MVar Int -> IO (Maybe BS.ByteString)
138−requestBodyHandle h mlen = modifyMVar mlen helper where
139− helper :: Int -> IO (Int, Maybe BS.ByteString)
140− helper 0 = return (0, Nothing)
141− helper len = do
137+requestBodyHandle :: Handle -> MVar Int -> Enumerator a
138+requestBodyHandle h mlen iter accum = modifyMVar mlen (helper accum) where
139+ helper a 0 = return (0, a)
140+ helper a len = do
142141 bs <- BS.hGet h len
143142 let newLen = len - BS.length bs
144− return (newLen, Just bs)
143+ ea' <- iter a bs
144+ case ea' of
145+ Left a' -> return (newLen, a')
146+ Right a' -> helper a' newLen
145147
146148parseFirst :: (StringLike s, MonadFailure InvalidRequest m) =>
147149 s
test.hs+3−14
@@ -1,7 +1,7 @@
1+{-# LANGUAGE Rank2Types #-}
12import Network.Wai
23import Network.Wai.Handler.SimpleServer
34import qualified Data.ByteString.Char8 as B8
4−import qualified Data.ByteString as B
55
66main :: IO ()
77main = putStrLn "http://localhost:3000/" >> run 3000 app
@@ -18,23 +18,12 @@ indexResponse = return Response
1818 , body = index
1919 }
2020
21−postResponse :: IO (Maybe B.ByteString) -> IO Response
21+postResponse :: (forall a. Enumerator a) -> IO Response
2222postResponse rb = return Response
2323 { status = Status200
2424 , headers = [(ContentType, B8.pack "text/plain")]
25− , body = Right $ postBody rb
25+ , body = Right rb
2626 }
2727
2828index :: Either FilePath a
2929index = Left "index.html"
30−
31−postBody :: IO (Maybe B.ByteString) -> Enumerator a
32−postBody req f a = do
33− mbs <- req
34− case mbs of
35− Nothing -> return a
36− Just bs -> do
37− res <- f a bs
38− case res of
39− Left a' -> return a'
40− Right a' -> postBody req f a'