@@ -62,7 +62,7 @@ recvRequest settings conn ii addr src = do
6262 te = idxhdr ! idxTransferEncoding
6363 handleExpect conn httpversion expect
6464 (rbody, remainingRef, bodyLength) <- bodyAndSource src cl te
65− rbody' <- timeoutBody th rbody
65+ rbody' <- timeoutBody remainingRef th rbody
6666 let req = Request {
6767 requestMethod = method
6868 , httpVersion = httpversion
@@ -138,10 +138,22 @@ isChunked _ = False
138138
139139----------------------------------------------------------------
140140
141−timeoutBody :: Timeout.Handle -> IO ByteString -> IO (IO ByteString)
142−timeoutBody timeoutHandle rbody = do
141+timeoutBody :: Maybe (I.IORef Int) -- ^ remaining
142+ -> Timeout.Handle
143+ -> IO ByteString
144+ -> IO (IO ByteString)
145+timeoutBody remainingRef timeoutHandle rbody = do
143146 isFirstRef <- I.newIORef True
144147
148+ let checkEmpty =
149+ case remainingRef of
150+ Nothing -> return . S.null
151+ Just ref -> \bs -> if S.null bs
152+ then return True
153+ else do
154+ x <- I.readIORef ref
155+ return $! x <= 0
156+
145157 return $ do
146158 isFirst <- I.readIORef isFirstRef
147159
@@ -158,7 +170,8 @@ timeoutBody timeoutHandle rbody = do
158170 -- because the application is not interested in more bytes, or
159171 -- because there is no more data available, pause the timeout
160172 -- handler again.
161− when (S.null bs) (Timeout.pause timeoutHandle)
173+ isEmpty <- checkEmpty bs
174+ when isEmpty (Timeout.pause timeoutHandle)
162175
163176 return bs
164177