on master help sign in
Request body is an Enumerator
fb2cbb7Michael Snoyman
Network/Wai.hs+1−1
@@ -246,7 +246,7 @@ data Request = Request
246 , serverPort :: Int
246 , serverPort :: Int
247 , httpHeaders :: [(RequestHeader, B.ByteString)]
247 , httpHeaders :: [(RequestHeader, B.ByteString)]
248 , urlScheme :: UrlScheme
248 , urlScheme :: UrlScheme
249− , requestBody :: IO (Maybe B.ByteString)
249+ , requestBody :: forall a. Enumerator a
250 , errorHandler :: String -> IO ()
250 , errorHandler :: String -> IO ()
251 , remoteHost :: B.ByteString
251 , remoteHost :: B.ByteString
252 }
252 }
Network/Wai/Handler/SimpleServer.hs+8−6
@@ -134,14 +134,16 @@ parseRequest port lines' handle remoteHost' = do
134 , remoteHost = B8.pack remoteHost'
134 , remoteHost = B8.pack remoteHost'
135 }
135 }
136
136
137−requestBodyHandle :: Handle -> MVar Int -> IO (Maybe BS.ByteString)
137+requestBodyHandle :: Handle -> MVar Int -> Enumerator a
138−requestBodyHandle h mlen = modifyMVar mlen helper where
138+requestBodyHandle h mlen iter accum = modifyMVar mlen (helper accum) where
139− helper :: Int -> IO (Int, Maybe BS.ByteString)
140− helper 0 = return (0, Nothing)
139+ helper a 0 = return (0, a)
141− helper len = do
140+ helper a len = do
142 bs <- BS.hGet h len
141 bs <- BS.hGet h len
143 let newLen = len - BS.length bs
142 let newLen = len - BS.length bs
143+ ea' <- iter a bs
144+ case ea' of
144− return (newLen, Just bs)
145+ Left a' -> return (newLen, a')
146+ Right a' -> helper a' newLen
145
147
146parseFirst :: (StringLike s, MonadFailure InvalidRequest m) =>
148parseFirst :: (StringLike s, MonadFailure InvalidRequest m) =>
147 s
149 s
test.hs+3−14
@@ -1,7 +1,7 @@
1+{-# LANGUAGE Rank2Types #-}
1import Network.Wai
2import Network.Wai
2import Network.Wai.Handler.SimpleServer
3import Network.Wai.Handler.SimpleServer
3import qualified Data.ByteString.Char8 as B8
4import qualified Data.ByteString.Char8 as B8
4−import qualified Data.ByteString as B
5
5
6main :: IO ()
6main :: IO ()
7main = putStrLn "http://localhost:3000/" >> run 3000 app
7main = putStrLn "http://localhost:3000/" >> run 3000 app
@@ -18,23 +18,12 @@ indexResponse = return Response
18 , body = index
18 , body = index
19 }
19 }
20
20
21−postResponse :: IO (Maybe B.ByteString) -> IO Response
21+postResponse :: (forall a. Enumerator a) -> IO Response
22postResponse rb = return Response
22postResponse rb = return Response
23 { status = Status200
23 { status = Status200
24 , headers = [(ContentType, B8.pack "text/plain")]
24 , headers = [(ContentType, B8.pack "text/plain")]
25− , body = Right $ postBody rb
25+ , body = Right rb
26 }
26 }
27
27
28index :: Either FilePath a
28index :: Either FilePath a
29index = Left "index.html"
29index = 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'