on master help sign in
strictRequestBody
commitdc92ea724ca3f33bc531ce086ba8830a34c64670 authorMichael Snoyman <michael@snoyman.com> dateTue 22 Jul 2014, 08:20 (12 years ago) parentfc2a2f6
3 files changed+27−1
wai/Network/Wai.hs+15 wai/test/Network/WaiSpec.hs+11 wai/wai.cabal+1−1
wai/Network/Wai.hs+15
@@ -59,6 +59,7 @@ module Network.Wai
5959 , requestBodyLength
6060 , requestHeaderHost
6161 , requestHeaderRange
62+ , strictRequestBody
6263 , lazyRequestBody
6364 -- * Response
6465 , Response
@@ -278,6 +279,20 @@ defaultRequest = Request
278279 , requestHeaderRange = Nothing
279280 }
280281
282+-- | Get the request body as a lazy ByteString. However, do /not/ use any lazy
283+-- I\/O, instead reading the entire body into memory strictly.
284+--
285+-- Since 3.0.1
286+strictRequestBody :: Request -> IO L.ByteString
287+strictRequestBody req =
288+ loop id
289+ where
290+ loop front = do
291+ bs <- requestBody req
292+ if B.null bs
293+ then return $ front LI.Empty
294+ else loop (front . LI.Chunk bs)
295+
281296-- | Get the request body as a lazy ByteString. This uses lazy I\/O under the
282297-- surface, and therefore all typical warnings regarding lazy I/O apply.
283298--
wai/test/Network/WaiSpec.hs+11
@@ -71,3 +71,14 @@ spec = do
7171 }
7272 _ <- lazyRequestBody req
7373 return ()
74+ describe "strictRequestBody" $ do
75+ prop "works" $ \chunks -> do
76+ ref <- newIORef $ map S.pack $ filter (not . null) chunks
77+ let req = Request
78+ { requestBody = atomicModifyIORef ref $ \bss ->
79+ case bss of
80+ [] -> ([], S.empty)
81+ x:y -> (y, x)
82+ }
83+ body <- strictRequestBody req
84+ body `shouldBe` L.fromChunks (map S.pack chunks)
wai/wai.cabal+1−1
@@ -1,5 +1,5 @@
11Name: wai
2−Version: 3.0.0.2
2+Version: 3.0.1
33Synopsis: Web Application Interface.
44Description: Provides a common protocol for communication between web applications and web servers.
55License: MIT