@@ -11,6 +11,7 @@ module Network.Wai.Parse
1111 , RequestBodyType (..)
1212 , getRequestBodyType
1313 , sinkRequestBody
14+ , sinkRequestBodyEx
1415 , BackEnd
1516 , lbsBackEnd
1617 , tempFileBackEnd
@@ -19,6 +20,24 @@ module Network.Wai.Parse
1920 , File
2021 , FileInfo (..)
2122 , parseContentType
23+ , ParseRequestBodyOptions
24+ , defaultParseRequestBodyOptions
25+ , noLimitParseRequestBodyOptions
26+ , parseRequestBodyEx
27+ , setMaxRequestKeyLength
28+ , clearMaxRequestKeyLength
29+ , setMaxRequestNumFiles
30+ , clearMaxRequestNumFiles
31+ , setMaxRequestFileSize
32+ , clearMaxRequestFileSize
33+ , setMaxRequestFilesSize
34+ , clearMaxRequestFilesSize
35+ , setMaxRequestParmsSize
36+ , clearMaxRequestParmsSize
37+ , setMaxHeaderLines
38+ , clearMaxHeaderLines
39+ , setMaxHeaderLineLength
40+ , clearMaxHeaderLineLength
2241#if TEST
2342 , Bound (..)
2443 , findBound
@@ -29,24 +48,31 @@ module Network.Wai.Parse
2948#endif
3049 ) where
3150
32−import qualified Data.ByteString.Search as Search
51+import qualified Control.Exception as E
3352import qualified Data.ByteString as S
3453import qualified Data.ByteString.Lazy as L
3554import qualified Data.ByteString.Char8 as S8
3655import Data.Word (Word8)
37−import Data.Maybe (fromMaybe)
56+import Data.Int (Int64)
57+import Data.Maybe (catMaybes, fromMaybe)
3858import Data.List (sortBy)
3959import Data.Function (on, fix)
4060import System.Directory (removeFile, getTemporaryDirectory)
4161import System.IO (hClose, openBinaryTempFile)
62+import System.IO.Error (isDoesNotExistError)
4263import Network.Wai
4364import qualified Network.HTTP.Types as H
44−import Control.Monad (when, unless)
65+import Control.Applicative ((<$>))
66+import Control.Exception (catchJust)
67+import Control.Monad (when, unless, guard)
4568import Control.Monad.Trans.Resource (allocate, release, register, InternalState, runInternalState)
4669import Data.IORef
4770import Network.HTTP.Types (hContentType)
71+import Network.HTTP2( HTTP2Error (..), ErrorCodeId (..) )
4872import Data.CaseInsensitive (mk)
4973
74+import Prelude hiding (lines)
75+
5076breakDiscard :: Word8 -> S.ByteString -> (S.ByteString, S.ByteString)
5177breakDiscard w s =
5278 let (x, y) = S.break (== w) s
@@ -103,11 +129,9 @@ tempFileBackEndOpts :: IO FilePath -- ^ get temporary directory
103129 -> ignored2
104130 -> IO S.ByteString
105131 -> IO FilePath
106−tempFileBackEndOpts getTmpDir pattern internalState _ _ popper = do
107− (key, (fp, h)) <- flip runInternalState internalState $ allocate (do
108− tempDir <- getTmpDir
109− openBinaryTempFile tempDir pattern) (\(_, h) -> hClose h)
110− _ <- runInternalState (register $ removeFile fp) internalState
132+tempFileBackEndOpts getTmpDir pattrn internalState _ _ popper = do
133+ (key, (fp, h)) <- flip runInternalState internalState $ allocate it (hClose . snd)
134+ _ <- runInternalState (register $ removeFileQuiet fp) internalState
111135 fix $ \loop -> do
112136 bs <- popper
113137 unless (S.null bs) $ do
@@ -115,6 +139,150 @@ tempFileBackEndOpts getTmpDir pattern internalState _ _ popper = do
115139 loop
116140 release key
117141 return fp
142+ where
143+ it = do
144+ tempDir <- getTmpDir
145+ openBinaryTempFile tempDir pattrn
146+ removeFileQuiet fp = catchJust (guard . isDoesNotExistError)
147+ (removeFile fp)
148+ (const $ return ())
149+
150+-- | A data structure that describes the behavior of
151+-- the parseRequestBodyEx function.
152+--
153+-- @since 3.0.16.0
154+data ParseRequestBodyOptions = ParseRequestBodyOptions
155+ { -- | The maximum length of a filename
156+ prboKeyLength :: Maybe Int
157+ , -- | The maximum number of files.
158+ prboMaxNumFiles :: Maybe Int
159+ , -- | The maximum filesize per file.
160+ prboMaxFileSize :: Maybe Int64
161+ , -- | The maximum total filesize.
162+ prboMaxFilesSize :: Maybe Int64
163+ , -- | The maximum size of the sum of all parameters
164+ prboMaxParmsSize :: Maybe Int
165+ , -- | The maximum header lines per mime/multipart entry
166+ prboMaxHeaderLines :: Maybe Int
167+ , -- | The maximum header line length per mime/multipart entry
168+ prboMaxHeaderLineLength :: Maybe Int }
169+
170+-- | Set the maximum length of a filename.
171+--
172+-- @since 3.0.16.0
173+setMaxRequestKeyLength :: Int -> ParseRequestBodyOptions -> ParseRequestBodyOptions
174+setMaxRequestKeyLength l p = p { prboKeyLength=Just l }
175+
176+-- | Do not limit the length of filenames.
177+--
178+-- @since 3.0.16.0
179+clearMaxRequestKeyLength :: ParseRequestBodyOptions -> ParseRequestBodyOptions
180+clearMaxRequestKeyLength p = p { prboKeyLength=Nothing }
181+
182+-- | Set the maximum number of files per request.
183+--
184+-- @since 3.0.16.0
185+setMaxRequestNumFiles :: Int -> ParseRequestBodyOptions -> ParseRequestBodyOptions
186+setMaxRequestNumFiles l p = p { prboMaxNumFiles=Just l }
187+
188+-- | Do not limit the maximum number of files per request.
189+--
190+-- @since 3.0.16.0
191+clearMaxRequestNumFiles :: ParseRequestBodyOptions -> ParseRequestBodyOptions
192+clearMaxRequestNumFiles p = p { prboMaxNumFiles=Nothing }
193+
194+-- | Set the maximum filesize per file (in bytes).
195+--
196+-- @since 3.0.16.0
197+setMaxRequestFileSize :: Int64 -> ParseRequestBodyOptions -> ParseRequestBodyOptions
198+setMaxRequestFileSize l p = p { prboMaxFileSize=Just l }
199+
200+-- | Do not limit the maximum filesize per file.
201+--
202+-- @since 3.0.16.0
203+clearMaxRequestFileSize :: ParseRequestBodyOptions -> ParseRequestBodyOptions
204+clearMaxRequestFileSize p = p { prboMaxFileSize=Nothing }
205+
206+-- | Set the maximum size of all files per request.
207+--
208+-- @since 3.0.16.0
209+setMaxRequestFilesSize :: Int64 -> ParseRequestBodyOptions -> ParseRequestBodyOptions
210+setMaxRequestFilesSize l p = p { prboMaxFilesSize=Just l }
211+
212+-- | Do not limit the maximum size of all files per request.
213+--
214+-- @since 3.0.16.0
215+clearMaxRequestFilesSize :: ParseRequestBodyOptions -> ParseRequestBodyOptions
216+clearMaxRequestFilesSize p = p { prboMaxFilesSize=Nothing }
217+
218+-- | Set the maximum size of the sum of all parameters.
219+--
220+-- @since 3.0.16.0
221+setMaxRequestParmsSize :: Int -> ParseRequestBodyOptions -> ParseRequestBodyOptions
222+setMaxRequestParmsSize l p = p { prboMaxParmsSize=Just l }
223+
224+-- | Do not limit the maximum size of the sum of all parameters.
225+--
226+-- @since 3.0.16.0
227+clearMaxRequestParmsSize :: ParseRequestBodyOptions -> ParseRequestBodyOptions
228+clearMaxRequestParmsSize p = p { prboMaxParmsSize=Nothing }
229+
230+-- | Set the maximum header lines per mime/multipart entry.
231+--
232+-- @since 3.0.16.0
233+setMaxHeaderLines :: Int -> ParseRequestBodyOptions -> ParseRequestBodyOptions
234+setMaxHeaderLines l p = p { prboMaxHeaderLines=Just l }
235+
236+-- | Do not limit the maximum header lines per mime/multipart entry.
237+--
238+-- @since 3.0.16.0
239+clearMaxHeaderLines:: ParseRequestBodyOptions -> ParseRequestBodyOptions
240+clearMaxHeaderLines p = p { prboMaxHeaderLines=Nothing }
241+
242+-- | Set the maximum header line length per mime/multipart entry.
243+--
244+-- @since 3.0.16.0
245+setMaxHeaderLineLength :: Int -> ParseRequestBodyOptions -> ParseRequestBodyOptions
246+setMaxHeaderLineLength l p = p { prboMaxHeaderLineLength=Just l }
247+
248+-- | Do not limit the maximum header lines per mime/multipart entry.
249+--
250+-- @since 3.0.16.0
251+clearMaxHeaderLineLength :: ParseRequestBodyOptions -> ParseRequestBodyOptions
252+clearMaxHeaderLineLength p = p { prboMaxHeaderLineLength=Nothing }
253+
254+-- | A reasonable default set of parsing options.
255+-- Maximum key/filename length: 32 bytes;
256+-- maximum files: 10; filesize unlimited; maximum
257+-- size for parameters: 64kbytes; maximum number of header
258+-- lines: 32 bytes (applies only to headers of a mime/multipart message);
259+-- maximum header line length: Apache's default for that is 8190 bytes
260+-- (http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestline)
261+-- so we're using that here as well.
262+--
263+-- @since 3.0.16.0
264+defaultParseRequestBodyOptions :: ParseRequestBodyOptions
265+defaultParseRequestBodyOptions = ParseRequestBodyOptions
266+ { prboKeyLength=Just 32
267+ , prboMaxNumFiles=Just 10
268+ , prboMaxFileSize=Nothing
269+ , prboMaxFilesSize=Nothing
270+ , prboMaxParmsSize=Just 65336
271+ , prboMaxHeaderLines=Just 32
272+ , prboMaxHeaderLineLength=Just 8190 }
273+
274+-- | Do not impose any memory limits.
275+--
276+-- @since 3.0.21.0
277+noLimitParseRequestBodyOptions :: ParseRequestBodyOptions
278+noLimitParseRequestBodyOptions = ParseRequestBodyOptions
279+ { prboKeyLength=Nothing
280+ , prboMaxNumFiles=Nothing
281+ , prboMaxFileSize=Nothing
282+ , prboMaxFilesSize=Nothing
283+ , prboMaxParmsSize=Nothing
284+ , prboMaxHeaderLines=Nothing
285+ , prboMaxHeaderLineLength=Nothing }
118286
119287-- | Information on an uploaded file.
120288data FileInfo c = FileInfo
@@ -137,8 +305,16 @@ type BackEnd a = S.ByteString -- ^ parameter name
137305 -> IO S.ByteString
138306 -> IO a
139307
140−data RequestBodyType = UrlEncoded | Multipart S.ByteString
308+-- | The mimetype of the http body.
309+-- Depending on whether just parameters or parameters and files
310+-- are passed, one or the other mimetype should be used.
311+data RequestBodyType
312+ = -- | application/x-www-form-urlencoded (parameters only)
313+ UrlEncoded
314+ | -- | multipart/form-data (parameters and files)
315+ Multipart S.ByteString
141316
317+-- | Get the mimetype of the body of an http request.
142318getRequestBodyType :: Request -> Maybe RequestBodyType
143319getRequestBodyType req = do
144320 ctype' <- lookup hContentType $ requestHeaders req
@@ -151,7 +327,7 @@ getRequestBodyType req = do
151327-- | Parse a content type value, turning a single @ByteString@ into the actual
152328-- content type and a list of pairs of attributes.
153329--
154−-- Since 1.3.2
330+-- @since 1.3.2
155331parseContentType :: S.ByteString -> (S.ByteString, [(S.ByteString, S.ByteString)])
156332parseContentType a = do
157333 let (ctype, b) = S.break (== semicolon) a
@@ -161,6 +337,9 @@ parseContentType a = do
161337 semicolon = 59
162338 equals = 61
163339 space = 32
340+ dq s = if S.length s > 2 && S.head s == 34 && S.last s == 34 -- quote
341+ then S.tail $ S.init s
342+ else s
164343 goAttrs front bs
165344 | S.null bs = front []
166345 | otherwise =
@@ -169,79 +348,140 @@ parseContentType a = do
169348 goAttr bs =
170349 let (k, v') = S.break (== equals) bs
171350 v = S.drop 1 v'
172− in (strip k, strip v)
351+ in (strip k, dq $ strip v)
173352 strip = S.dropWhile (== space) . fst . S.breakEnd (/= space)
174353
354+-- | Parse the body of an HTTP request.
355+-- See parseRequestBodyEx for details.
356+-- Note: This function does not limit the memory it allocates.
357+-- When dealing with untrusted data (as is usually the case when
358+-- receiving input from the internet), it is recommended to
359+-- use the 'parseRequestBodyEx' function instead.
175360parseRequestBody :: BackEnd y
176361 -> Request
177362 -> IO ([Param], [File y])
178−parseRequestBody s r =
363+parseRequestBody = parseRequestBodyEx noLimitParseRequestBodyOptions
364+
365+-- | Parse the body of an HTTP request, limit resource usage.
366+-- The HTTP body can contain both parameters and files.
367+-- This function will return a list of key,value pairs
368+-- for all parameters, and a list of key,a pairs
369+-- for filenames. The a depends on the used backend that
370+-- is responsible for storing the received files.
371+parseRequestBodyEx :: ParseRequestBodyOptions
372+ -> BackEnd y
373+ -> Request
374+ -> IO ([Param], [File y])
375+parseRequestBodyEx o s r =
179376 case getRequestBodyType r of
180377 Nothing -> return ([], [])
181− Just rbt -> sinkRequestBody s rbt (requestBody r)
378+ Just rbt -> sinkRequestBodyEx o s rbt (requestBody r)
182379
183380sinkRequestBody :: BackEnd y
184381 -> RequestBodyType
185382 -> IO S.ByteString
186383 -> IO ([Param], [File y])
187−sinkRequestBody s r body = do
188− ref <- newIORef (id, id)
384+sinkRequestBody = sinkRequestBodyEx noLimitParseRequestBodyOptions
385+
386+-- |
387+--
388+-- @since 3.0.16.0
389+sinkRequestBodyEx :: ParseRequestBodyOptions
390+ -> BackEnd y
391+ -> RequestBodyType
392+ -> IO S.ByteString
393+ -> IO ([Param], [File y])
394+sinkRequestBodyEx o s r body = do
395+ ref <- newIORef ([], [])
189396 let add x = atomicModifyIORef ref $ \(y, z) ->
190397 case x of
191− Left y' -> ((y . (y':), z), ())
192− Right z' -> ((y, z . (z':)), ())
193− conduitRequestBody s r body add
194− (x, y) <- readIORef ref
195− return (x [], y [])
196−
197−conduitRequestBody :: BackEnd y
198− -> RequestBodyType
199− -> IO S.ByteString
200− -> (Either Param (File y) -> IO ())
201− -> IO ()
202−conduitRequestBody _ UrlEncoded rbody add = do
398+ Left y' -> ((y':y, z), ())
399+ Right z' -> ((y, z':z), ())
400+ conduitRequestBodyEx o s r body add
401+ (\(a, b) -> (reverse a, reverse b)) <$> readIORef ref
402+
403+conduitRequestBodyEx :: ParseRequestBodyOptions
404+ -> BackEnd y
405+ -> RequestBodyType
406+ -> IO S.ByteString
407+ -> (Either Param (File y) -> IO ())
408+ -> IO ()
409+conduitRequestBodyEx o _ UrlEncoded rbody add = do
203410 -- NOTE: in general, url-encoded data will be in a single chunk.
204411 -- Therefore, I'm optimizing for the usual case by sticking with
205412 -- strict byte strings here.
206− let loop front = do
413+ let loop size front = do
207414 bs <- rbody
208415 if S.null bs
209416 then return $ S.concat $ front []
210− else loop $ front . (bs:)
211− bs <- loop id
417+ else do
418+ let newsize = size + S.length bs
419+ case prboMaxParmsSize o of
420+ Just maxSize -> when (newsize > maxSize) $
421+ error "Maximum size of parameters exceeded"
422+ Nothing -> return ()
423+ loop newsize $ front . (bs:)
424+ bs <- loop 0 id
212425 mapM_ (add . Left) $ H.parseSimpleQuery bs
213−conduitRequestBody backend (Multipart bound) rbody add =
214− parsePieces backend (S8.pack "--" `S.append` bound) rbody add
426+conduitRequestBodyEx o backend (Multipart bound) rbody add =
427+ parsePiecesEx o backend (S8.pack "--" `S.append` bound) rbody add
428+
215429
216−takeLine :: Source -> IO (Maybe S.ByteString)
217−takeLine src =
218− go id
430+-- | Take one header or subheader line.
431+-- Since: 3.0.26
432+-- Throw 431 if headers too large.
433+takeLine :: Maybe Int -> Source -> IO (Maybe S.ByteString)
434+takeLine maxlen src =
435+ go ""
219436 where
220437 go front = do
221438 bs <- readSource src
439+ case maxlen of
440+ Just maxlen' -> when (S.length front > maxlen') $
441+ E.throwIO $ ConnectionError (UnknownErrorCode 431)
442+ "Request Header Fields Too Large"
443+ Nothing -> return ()
222444 if S.null bs
223445 then close front
224446 else push front bs
225447
226− close front = leftover src (front S.empty) >> return Nothing
448+ close front = leftover src front >> return Nothing
227449 push front bs = do
228− let (x, y) = S.break (== 10) $ front bs -- LF
450+ let (x, y) = S.break (== 10) bs -- LF
229451 in if S.null y
230− then go $ S.append x
452+ then go $ front `S.append` x
231453 else do
232454 when (S.length y > 1) $ leftover src $ S.drop 1 y
233− return $ Just $ killCR x
234−
235−takeLines :: Source -> IO [S.ByteString]
236−takeLines src = do
237− res <- takeLine src
455+ let res = front `S.append` x
456+ case maxlen of
457+ Just maxlen' -> when (S.length res > maxlen') $
458+ E.throwIO $ ConnectionError (UnknownErrorCode 431)
459+ "Request Header Fields Too Large"
460+ Nothing -> return ()
461+ return $ Just $ killCR $ res
462+
463+takeLines' :: Maybe Int -> Maybe Int -> Source -> IO [S.ByteString]
464+takeLines' lineLength maxLines source =
465+ reverse <$> takeLines'' [] lineLength maxLines source
466+
467+takeLines''
468+ :: [S.ByteString]
469+ -> Maybe Int
470+ -> Maybe Int
471+ -> Source
472+ -> IO [S.ByteString]
473+takeLines'' lines lineLength maxLines src = do
474+ case maxLines of
475+ Just maxLines' ->
476+ when (length lines > maxLines') $
477+ error "Too many lines in mime/multipart header"
478+ Nothing -> return ()
479+ res <- takeLine lineLength src
238480 case res of
239− Nothing -> return []
481+ Nothing -> return lines
240482 Just l
241− | S.null l -> return []
242− | otherwise -> do
243− ls <- takeLines src
244− return $ l : ls
483+ | S.null l -> return lines
484+ | otherwise -> takeLines'' (l:lines) lineLength maxLines src
245485
246486data Source = Source (IO S.ByteString) (IORef S.ByteString)
247487
@@ -260,17 +500,20 @@ readSource (Source f ref) = do
260500leftover :: Source -> S.ByteString -> IO ()
261501leftover (Source _ ref) bs = writeIORef ref bs
262502
263−parsePieces :: BackEnd y
264− -> S.ByteString
265− -> IO S.ByteString
266− -> (Either Param (File y) -> IO ())
267− -> IO ()
268−parsePieces sink bound rbody add =
269− mkSource rbody >>= loop
503+parsePiecesEx :: ParseRequestBodyOptions
504+ -> BackEnd y
505+ -> S.ByteString
506+ -> IO S.ByteString
507+ -> (Either Param (File y) -> IO ())
508+ -> IO ()
509+parsePiecesEx o sink bound rbody add =
510+ mkSource rbody >>= loop 0 0 0 0
270511 where
271− loop src = do
272− _boundLine <- takeLine src
273− res' <- takeLines src
512+ loop :: Int -> Int -> Int -> Int64 -> Source -> IO ()
513+ loop numParms numFiles parmSize filesSize src = do
514+ _boundLine <- takeLine (prboMaxHeaderLineLength o) src
515+ res' <- takeLines' (prboMaxHeaderLineLength o)
516+ (prboMaxHeaderLines o) src
274517 unless (null res') $ do
275518 let ls' = map parsePair res'
276519 let x = do
@@ -281,25 +524,50 @@ parsePieces sink bound rbody add =
281524 return (ct, name, lookup "filename" attrs)
282525 case x of
283526 Just (mct, name, Just filename) -> do
527+ case prboKeyLength o of
528+ Just maxKeyLength ->
529+ when (S.length name > maxKeyLength) $
530+ error "Filename is too long"
531+ Nothing -> return ()
532+ case prboMaxNumFiles o of
533+ Just maxFiles -> when (numFiles >= maxFiles) $
534+ error "Maximum number of files exceeded"
535+ Nothing -> return ()
284536 let ct = fromMaybe "application/octet-stream" mct
285537 fi0 = FileInfo filename ct ()
286− (wasFound, y) <- sinkTillBound' bound name fi0 sink src
538+ fs = catMaybes [ prboMaxFileSize o
539+ , subtract filesSize <$> prboMaxFilesSize o ]
540+ mfs = if fs == [] then Nothing else Just $ minimum fs
541+ ((wasFound, fileSize), y) <- sinkTillBound' bound name fi0 sink src mfs
542+ let newFilesSize = filesSize + fileSize
287543 add $ Right (name, fi0 { fileContent = y })
288− when wasFound (loop src)
544+ when wasFound $ loop numParms (numFiles + 1) parmSize newFilesSize src
289545 Just (_ct, name, Nothing) -> do
546+ case prboKeyLength o of
547+ Just maxKeyLength ->
548+ when (S.length name > maxKeyLength) $
549+ error "Parameter name is too long"
550+ Nothing -> return ()
290551 let seed = id
291552 let iter front bs = return $ front . (:) bs
292− (wasFound, front) <- sinkTillBound bound iter seed src
553+ ((wasFound, _fileSize), front) <- sinkTillBound bound iter seed src
554+ (fromIntegral <$> prboMaxParmsSize o)
293555 let bs = S.concat $ front []
294556 let x' = (name, bs)
557+ let newParmSize = parmSize + S.length name + S.length bs
558+ case prboMaxParmsSize o of
559+ Just maxParmSize -> when (newParmSize > maxParmSize) $
560+ error "Maximum size of parameters exceeded"
561+ Nothing -> return ()
295562 add $ Left x'
296− when wasFound (loop src)
563+ when wasFound $ loop (numParms + 1) numFiles
564+ newParmSize filesSize src
297565 _ -> do
298566 -- ignore this part
299567 let seed = ()
300568 iter () _ = return ()
301− (wasFound, ()) <- sinkTillBound bound iter seed src
302− when wasFound (loop src)
569+ ((wasFound, _fileSize), ()) <- sinkTillBound bound iter seed src Nothing
570+ when wasFound $ loop numParms numFiles parmSize filesSize src
303571 where
304572 contDisp = mk $ S8.pack "Content-Disposition"
305573 contType = mk $ S8.pack "Content-Type"
@@ -307,13 +575,14 @@ parsePieces sink bound rbody add =
307575 let (x, y) = breakDiscard 58 s -- colon
308576 in (mk $ x, S.dropWhile (== 32) y) -- space
309577
578+
310579data Bound = FoundBound S.ByteString S.ByteString
311580 | NoBound
312581 | PartialBound
313582 deriving (Eq, Show)
314583
315584findBound :: S.ByteString -> S.ByteString -> Bound
316−findBound b bs = handleBreak $ Search.breakOn b bs
585+findBound b bs = handleBreak $ S.breakSubstring b bs
317586 where
318587 handleBreak (h, t)
319588 | S.null t = go [lowBound..S.length bs - 1]
@@ -340,9 +609,10 @@ sinkTillBound' :: S.ByteString
340609 -> FileInfo ()
341610 -> BackEnd y
342611 -> Source
343− -> IO (Bool, y)
344−sinkTillBound' bound name fi sink src = do
345− (next, final) <- wrapTillBound bound src
612+ -> Maybe Int64
613+ -> IO ((Bool, Int64), y)
614+sinkTillBound' bound name fi sink src max' = do
615+ (next, final) <- wrapTillBound bound src max'
346616 y <- sink name fi next
347617 b <- final
348618 return (b, y)
@@ -351,30 +621,40 @@ data WTB = WTBWorking (S.ByteString -> S.ByteString)
351621 | WTBDone Bool
352622wrapTillBound :: S.ByteString -- ^ bound
353623 -> Source
354− -> IO (IO S.ByteString, IO Bool) -- ^ Bool indicates if the bound was found
355−wrapTillBound bound src = do
624+ -> Maybe Int64
625+ -> IO (IO S.ByteString, IO (Bool, Int64)) -- ^ Bool indicates if the bound was found
626+wrapTillBound bound src max' = do
356627 ref <- newIORef $ WTBWorking id
357− return (go ref, final ref)
628+ sref <- newIORef (0 :: Int64)
629+ return (go ref sref, final ref sref)
358630 where
359− final ref = do
631+ final ref sref = do
360632 x <- readIORef ref
361633 case x of
362634 WTBWorking _ -> error "wrapTillBound did not finish"
363− WTBDone y -> return y
635+ WTBDone y -> do
636+ siz <- readIORef sref
637+ return (y, siz)
364638
365− go ref = do
639+ go ref sref = do
366640 state <- readIORef ref
367641 case state of
368642 WTBDone _ -> return S.empty
369643 WTBWorking front -> do
370644 bs <- readSource src
645+ cur <- atomicModifyIORef' sref $ \ cur ->
646+ let new = cur + fromIntegral (S.length bs) in (new, new)
647+ case max' of
648+ Just max'' | cur > max'' ->
649+ E.throwIO $ ConnectionError (UnknownErrorCode 413) "Payload Too Large"
650+ _ -> return ()
371651 if S.null bs
372652 then do
373653 writeIORef ref $ WTBDone False
374654 return $ front bs
375655 else push $ front bs
376656 where
377− push bs =
657+ push bs = do
378658 case findBound bound bs of
379659 FoundBound before after -> do
380660 let before' = killCRLF before
@@ -390,19 +670,20 @@ wrapTillBound bound src = do
390670 else (bs, id)
391671 writeIORef ref $ WTBWorking front'
392672 if S.null toEmit
393− then go ref
673+ then go ref sref
394674 else return toEmit
395675 PartialBound -> do
396676 writeIORef ref $ WTBWorking $ S.append bs
397− go ref
677+ go ref sref
398678
399679sinkTillBound :: S.ByteString
400680 -> (x -> S.ByteString -> IO x)
401681 -> x
402682 -> Source
403− -> IO (Bool, x)
404−sinkTillBound bound iter seed0 src = do
405− (next, final) <- wrapTillBound bound src
683+ -> Maybe Int64
684+ -> IO ((Bool, Int64), x)
685+sinkTillBound bound iter seed0 src max' = do
686+ (next, final) <- wrapTillBound bound src max'
406687 let loop seed = do
407688 bs <- next
408689 if S.null bs