~ubuntu-branches/ubuntu/quantal/haskell-wai-extra/quantal

« back to all changes in this revision

Viewing changes to Network/Wai/Handler/Helper.hs

  • Committer: Package Import Robot
  • Author(s): Clint Adams
  • Date: 2011-10-08 23:41:41 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20111008234141-4irb49p6argz4a3g
Tags: 0.4.3-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
module Network.Wai.Handler.Helper
2
 
    ( requestBodyHandle
3
 
    , requestBodyFunc
4
 
    ) where
5
 
 
6
 
import System.IO (Handle)
7
 
import qualified Data.ByteString as B
8
 
import Data.ByteString.Lazy.Internal (defaultChunkSize)
9
 
import Network.Wai (Source (..))
10
 
 
11
 
requestBodyHandle :: Handle -> Int -> Source
12
 
requestBodyHandle h =
13
 
    requestBodyFunc go
14
 
  where
15
 
    go i = Just `fmap` B.hGet h (min i defaultChunkSize)
16
 
 
17
 
requestBodyFunc :: (Int -> IO (Maybe B.ByteString)) -> Int -> Source
18
 
requestBodyFunc _ 0 = Source $ return Nothing
19
 
requestBodyFunc h len = Source $ do
20
 
    mbs <- h len
21
 
    case mbs of
22
 
        Nothing -> return Nothing
23
 
        Just bs -> do
24
 
            let newLen = len - B.length bs
25
 
            return $ Just (bs, requestBodyFunc h $ max 0 newLen)