@@ -11,6 +11,7 @@ module Network.Wai.Parse
1111 , RequestBodyType (..)
1212 , getRequestBodyType
1313 , sinkRequestBody
14+ , sinkRequestBodyEx
1415 , BackEnd
1516 , lbsBackEnd
1617 , tempFileBackEnd
@@ -19,8 +20,24 @@ module Network.Wai.Parse
1920 , File
2021 , FileInfo (..)
2122 , parseContentType
22− , ParseRequestBodyOptions (..)
23+ , ParseRequestBodyOptions
24+ , defaultParseRequestBodyOptions
25+ , noLimitParseRequestBodyOptions
2326 , 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
2441#if TEST
2542 , Bound (..)
2643 , findBound
@@ -31,11 +48,10 @@ module Network.Wai.Parse
3148#endif
3249 ) where
3350
34−import qualified Data.ByteString.Search as Search
51+import qualified Control.Exception as E
3552import qualified Data.ByteString as S
3653import qualified Data.ByteString.Lazy as L
3754import qualified Data.ByteString.Char8 as S8
38−import Data.Default (Default, def)
3955import Data.Word (Word8)
4056import Data.Int (Int64)
4157import Data.Maybe (catMaybes, fromMaybe)
@@ -43,15 +59,20 @@ import Data.List (sortBy)
4359import Data.Function (on, fix)
4460import System.Directory (removeFile, getTemporaryDirectory)
4561import System.IO (hClose, openBinaryTempFile)
62+import System.IO.Error (isDoesNotExistError)
4663import Network.Wai
4764import qualified Network.HTTP.Types as H
4865import Control.Applicative ((<$>))
49−import Control.Monad (when, unless)
66+import Control.Exception (catchJust)
67+import Control.Monad (when, unless, guard)
5068import Control.Monad.Trans.Resource (allocate, release, register, InternalState, runInternalState)
5169import Data.IORef
5270import Network.HTTP.Types (hContentType)
71+import Network.HTTP2( HTTP2Error (..), ErrorCodeId (..) )
5372import Data.CaseInsensitive (mk)
5473
74+import Prelude hiding (lines)
75+
5576breakDiscard :: Word8 -> S.ByteString -> (S.ByteString, S.ByteString)
5677breakDiscard w s =
5778 let (x, y) = S.break (== w) s
@@ -110,7 +131,7 @@ tempFileBackEndOpts :: IO FilePath -- ^ get temporary directory
110131 -> IO FilePath
111132tempFileBackEndOpts getTmpDir pattrn internalState _ _ popper = do
112133 (key, (fp, h)) <- flip runInternalState internalState $ allocate it (hClose . snd)
113− _ <- runInternalState (register $ removeFile fp) internalState
134+ _ <- runInternalState (register $ removeFileQuiet fp) internalState
114135 fix $ \loop -> do
115136 bs <- popper
116137 unless (S.null bs) $ do
@@ -122,31 +143,146 @@ tempFileBackEndOpts getTmpDir pattrn internalState _ _ popper = do
122143 it = do
123144 tempDir <- getTmpDir
124145 openBinaryTempFile tempDir pattrn
146+ removeFileQuiet fp = catchJust (guard . isDoesNotExistError)
147+ (removeFile fp)
148+ (const $ return ())
125149
126150-- | A data structure that describes the behavior of
127151-- the parseRequestBodyEx function.
152+--
153+-- @since 3.0.16.0
128154data ParseRequestBodyOptions = ParseRequestBodyOptions
129155 { -- | The maximum length of a filename
130− prboKeyLength :: Int
156+ prboKeyLength :: Maybe Int
131157 , -- | The maximum number of files.
132− prboMaxNumFiles :: Int
158+ prboMaxNumFiles :: Maybe Int
133159 , -- | The maximum filesize per file.
134160 prboMaxFileSize :: Maybe Int64
135161 , -- | The maximum total filesize.
136162 prboMaxFilesSize :: Maybe Int64
137− , -- | The maximum size of the sum of all parameters values
138− prboMaxParmsValueSize :: Int
139− , -- | The maximum header lines per entry
140− prboMaxHeaderLines :: Int }
141−
142−instance Default ParseRequestBodyOptions where
143− def = ParseRequestBodyOptions
144− { prboKeyLength=32
145− , prboMaxNumFiles=10
146− , prboMaxFileSize=Nothing
147− , prboMaxFilesSize=Nothing
148− , prboMaxParmsValueSize=65336
149− , prboMaxHeaderLines=32 }
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 }
150286
151287-- | Information on an uploaded file.
152288data FileInfo c = FileInfo
@@ -169,8 +305,16 @@ type BackEnd a = S.ByteString -- ^ parameter name
169305 -> IO S.ByteString
170306 -> IO a
171307
172−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
173316
317+-- | Get the mimetype of the body of an http request.
174318getRequestBodyType :: Request -> Maybe RequestBodyType
175319getRequestBodyType req = do
176320 ctype' <- lookup hContentType $ requestHeaders req
@@ -183,7 +327,7 @@ getRequestBodyType req = do
183327-- | Parse a content type value, turning a single @ByteString@ into the actual
184328-- content type and a list of pairs of attributes.
185329--
186−-- Since 1.3.2
330+-- @since 1.3.2
187331parseContentType :: S.ByteString -> (S.ByteString, [(S.ByteString, S.ByteString)])
188332parseContentType a = do
189333 let (ctype, b) = S.break (== semicolon) a
@@ -193,6 +337,9 @@ parseContentType a = do
193337 semicolon = 59
194338 equals = 61
195339 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
196343 goAttrs front bs
197344 | S.null bs = front []
198345 | otherwise =
@@ -201,17 +348,26 @@ parseContentType a = do
201348 goAttr bs =
202349 let (k, v') = S.break (== equals) bs
203350 v = S.drop 1 v'
204− in (strip k, strip v)
351+ in (strip k, dq $ strip v)
205352 strip = S.dropWhile (== space) . fst . S.breakEnd (/= space)
206353
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.
207360parseRequestBody :: BackEnd y
208361 -> Request
209362 -> IO ([Param], [File y])
210−parseRequestBody s r =
211− case getRequestBodyType r of
212− Nothing -> return ([], [])
213− Just rbt -> sinkRequestBody s rbt (requestBody r)
214−
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.
215371parseRequestBodyEx :: ParseRequestBodyOptions
216372 -> BackEnd y
217373 -> Request
@@ -225,16 +381,11 @@ sinkRequestBody :: BackEnd y
225381 -> RequestBodyType
226382 -> IO S.ByteString
227383 -> IO ([Param], [File y])
228−sinkRequestBody s r body = do
229− ref <- newIORef (id, id)
230− let add x = atomicModifyIORef ref $ \(y, z) ->
231− case x of
232− Left y' -> ((y . (y':), z), ())
233− Right z' -> ((y, z . (z':)), ())
234− conduitRequestBody s r body add
235− (x, y) <- readIORef ref
236− return (x [], y [])
384+sinkRequestBody = sinkRequestBodyEx noLimitParseRequestBodyOptions
237385
386+-- |
387+--
388+-- @since 3.0.16.0
238389sinkRequestBodyEx :: ParseRequestBodyOptions
239390 -> BackEnd y
240391 -> RequestBodyType
@@ -247,26 +398,7 @@ sinkRequestBodyEx o s r body = do
247398 Left y' -> ((y':y, z), ())
248399 Right z' -> ((y, z':z), ())
249400 conduitRequestBodyEx o s r body add
250− readIORef ref
251−
252−conduitRequestBody :: BackEnd y
253− -> RequestBodyType
254− -> IO S.ByteString
255− -> (Either Param (File y) -> IO ())
256− -> IO ()
257−conduitRequestBody _ UrlEncoded rbody add = do
258− -- NOTE: in general, url-encoded data will be in a single chunk.
259− -- Therefore, I'm optimizing for the usual case by sticking with
260− -- strict byte strings here.
261− let loop front = do
262− bs <- rbody
263− if S.null bs
264− then return $ S.concat $ front []
265− else loop $ front . (bs:)
266− bs <- loop id
267− mapM_ (add . Left) $ H.parseSimpleQuery bs
268−conduitRequestBody backend (Multipart bound) rbody add =
269− parsePieces backend (S8.pack "--" `S.append` bound) rbody add
401+ (\(a, b) -> (reverse a, reverse b)) <$> readIORef ref
270402
271403conduitRequestBodyEx :: ParseRequestBodyOptions
272404 -> BackEnd y
@@ -278,60 +410,78 @@ conduitRequestBodyEx o _ UrlEncoded rbody add = do
278410 -- NOTE: in general, url-encoded data will be in a single chunk.
279411 -- Therefore, I'm optimizing for the usual case by sticking with
280412 -- strict byte strings here.
281− let loop front = do
413+ let loop size front = do
282414 bs <- rbody
283415 if S.null bs
284416 then return $ S.concat $ front []
285− else loop $ front . (bs:)
286− 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
287425 mapM_ (add . Left) $ H.parseSimpleQuery bs
288426conduitRequestBodyEx o backend (Multipart bound) rbody add =
289427 parsePiecesEx o backend (S8.pack "--" `S.append` bound) rbody add
290428
291429
292−takeLine :: Source -> IO (Maybe S.ByteString)
293−takeLine src =
294− 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 ""
295436 where
296437 go front = do
297438 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 ()
298444 if S.null bs
299445 then close front
300446 else push front bs
301447
302− close front = leftover src (front S.empty) >> return Nothing
448+ close front = leftover src front >> return Nothing
303449 push front bs = do
304− let (x, y) = S.break (== 10) $ front bs -- LF
450+ let (x, y) = S.break (== 10) bs -- LF
305451 in if S.null y
306− then go $ S.append x
452+ then go $ front `S.append` x
307453 else do
308454 when (S.length y > 1) $ leftover src $ S.drop 1 y
309− return $ Just $ killCR x
310−
311−takeLines :: Source -> IO [S.ByteString]
312−takeLines src = do
313− 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
314480 case res of
315− Nothing -> return []
481+ Nothing -> return lines
316482 Just l
317− | S.null l -> return []
318− | otherwise -> do
319− ls <- takeLines src
320− return $ l : ls
321−
322−takeLines' :: Int -> Source -> IO [S.ByteString]
323−takeLines' a b = reverse <$> takeLines'' [] a b
324−
325−takeLines'' :: [S.ByteString] -> Int -> Source -> IO [S.ByteString]
326−takeLines'' lines maxLines src
327− | length lines > maxLines = error "Too many lines in mime/multipart header"
328− | otherwise = do
329− res <- takeLine src
330− case res of
331− Nothing -> return lines
332− Just l
333− | S.null l -> return lines
334− | otherwise -> takeLines'' (l:lines) maxLines src
483+ | S.null l -> return lines
484+ | otherwise -> takeLines'' (l:lines) lineLength maxLines src
335485
336486data Source = Source (IO S.ByteString) (IORef S.ByteString)
337487
@@ -350,53 +500,6 @@ readSource (Source f ref) = do
350500leftover :: Source -> S.ByteString -> IO ()
351501leftover (Source _ ref) bs = writeIORef ref bs
352502
353−parsePieces :: BackEnd y
354− -> S.ByteString
355− -> IO S.ByteString
356− -> (Either Param (File y) -> IO ())
357− -> IO ()
358−parsePieces sink bound rbody add =
359− mkSource rbody >>= loop
360− where
361− loop src = do
362− _boundLine <- takeLine src
363− res' <- takeLines src
364− unless (null res') $ do
365− let ls' = map parsePair res'
366− let x = do
367− cd <- lookup contDisp ls'
368− let ct = lookup contType ls'
369− let attrs = parseAttrs cd
370− name <- lookup "name" attrs
371− return (ct, name, lookup "filename" attrs)
372− case x of
373− Just (mct, name, Just filename) -> do
374− let ct = fromMaybe "application/octet-stream" mct
375− fi0 = FileInfo filename ct ()
376− ((wasFound, _fileSize), y) <- sinkTillBound' bound name fi0 sink src Nothing
377− add $ Right (name, fi0 { fileContent = y })
378− when wasFound (loop src)
379− Just (_ct, name, Nothing) -> do
380− let seed = id
381− let iter front bs = return $ front . (:) bs
382− ((wasFound, _fileSize), front) <- sinkTillBound bound iter seed src Nothing
383− let bs = S.concat $ front []
384− let x' = (name, bs)
385− add $ Left x'
386− when wasFound (loop src)
387− _ -> do
388− -- ignore this part
389− let seed = ()
390− iter () _ = return ()
391− ((wasFound, _fileSize), ()) <- sinkTillBound bound iter seed src Nothing
392− when wasFound (loop src)
393− where
394− contDisp = mk $ S8.pack "Content-Disposition"
395− contType = mk $ S8.pack "Content-Type"
396− parsePair s =
397− let (x, y) = breakDiscard 58 s -- colon
398− in (mk $ x, S.dropWhile (== 32) y) -- space
399−
400503parsePiecesEx :: ParseRequestBodyOptions
401504 -> BackEnd y
402505 -> S.ByteString
@@ -408,8 +511,9 @@ parsePiecesEx o sink bound rbody add =
408511 where
409512 loop :: Int -> Int -> Int -> Int64 -> Source -> IO ()
410513 loop numParms numFiles parmSize filesSize src = do
411− _boundLine <- takeLine src
412− res' <- takeLines' (prboMaxHeaderLines o) src
514+ _boundLine <- takeLine (prboMaxHeaderLineLength o) src
515+ res' <- takeLines' (prboMaxHeaderLineLength o)
516+ (prboMaxHeaderLines o) src
413517 unless (null res') $ do
414518 let ls' = map parsePair res'
415519 let x = do
@@ -420,30 +524,41 @@ parsePiecesEx o sink bound rbody add =
420524 return (ct, name, lookup "filename" attrs)
421525 case x of
422526 Just (mct, name, Just filename) -> do
423− when (S.length name > prboKeyLength o) $
424− error "Filename is too long"
425− when (numFiles >= prboMaxNumFiles o) $
426− error "Maximum number of files exceeded"
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 ()
427536 let ct = fromMaybe "application/octet-stream" mct
428537 fi0 = FileInfo filename ct ()
429− fs = catMaybes [ prboMaxFileSize o, prboMaxFilesSize o ]
538+ fs = catMaybes [ prboMaxFileSize o
539+ , subtract filesSize <$> prboMaxFilesSize o ]
430540 mfs = if fs == [] then Nothing else Just $ minimum fs
431541 ((wasFound, fileSize), y) <- sinkTillBound' bound name fi0 sink src mfs
432542 let newFilesSize = filesSize + fileSize
433543 add $ Right (name, fi0 { fileContent = y })
434544 when wasFound $ loop numParms (numFiles + 1) parmSize newFilesSize src
435545 Just (_ct, name, Nothing) -> do
436− when (S.length name > prboKeyLength o) $
437− error "Parameter name is too long"
546+ case prboKeyLength o of
547+ Just maxKeyLength ->
548+ when (S.length name > maxKeyLength) $
549+ error "Parameter name is too long"
550+ Nothing -> return ()
438551 let seed = id
439552 let iter front bs = return $ front . (:) bs
440− ((wasFound, _fileSize), front) <- sinkTillBound bound iter seed src $
441− Just . fromIntegral . prboMaxParmsValueSize $ o
553+ ((wasFound, _fileSize), front) <- sinkTillBound bound iter seed src
554+ (fromIntegral <$> prboMaxParmsSize o)
442555 let bs = S.concat $ front []
443556 let x' = (name, bs)
444557 let newParmSize = parmSize + S.length name + S.length bs
445− when (newParmSize > prboMaxParmsValueSize o) $
446− error "Maximum size of parameters exceeded"
558+ case prboMaxParmsSize o of
559+ Just maxParmSize -> when (newParmSize > maxParmSize) $
560+ error "Maximum size of parameters exceeded"
561+ Nothing -> return ()
447562 add $ Left x'
448563 when wasFound $ loop (numParms + 1) numFiles
449564 newParmSize filesSize src
@@ -467,7 +582,7 @@ data Bound = FoundBound S.ByteString S.ByteString
467582 deriving (Eq, Show)
468583
469584findBound :: S.ByteString -> S.ByteString -> Bound
470−findBound b bs = handleBreak $ Search.breakOn b bs
585+findBound b bs = handleBreak $ S.breakSubstring b bs
471586 where
472587 handleBreak (h, t)
473588 | S.null t = go [lowBound..S.length bs - 1]
@@ -496,8 +611,8 @@ sinkTillBound' :: S.ByteString
496611 -> Source
497612 -> Maybe Int64
498613 -> IO ((Bool, Int64), y)
499−sinkTillBound' bound name fi sink src max = do
500− (next, final) <- wrapTillBound bound src max
614+sinkTillBound' bound name fi sink src max' = do
615+ (next, final) <- wrapTillBound bound src max'
501616 y <- sink name fi next
502617 b <- final
503618 return (b, y)
@@ -508,7 +623,7 @@ wrapTillBound :: S.ByteString -- ^ bound
508623 -> Source
509624 -> Maybe Int64
510625 -> IO (IO S.ByteString, IO (Bool, Int64)) -- ^ Bool indicates if the bound was found
511−wrapTillBound bound src max = do
626+wrapTillBound bound src max' = do
512627 ref <- newIORef $ WTBWorking id
513628 sref <- newIORef (0 :: Int64)
514629 return (go ref sref, final ref sref)
@@ -527,18 +642,19 @@ wrapTillBound bound src max = do
527642 WTBDone _ -> return S.empty
528643 WTBWorking front -> do
529644 bs <- readSource src
530− cur <- atomicModifyIORef sref $ \ cur ->
645+ cur <- atomicModifyIORef' sref $ \ cur ->
531646 let new = cur + fromIntegral (S.length bs) in (new, new)
532− case max of
533− Just max' | cur > max' -> error "Maximum size exceeded"
534− _ -> return ()
647+ case max' of
648+ Just max'' | cur > max'' ->
649+ E.throwIO $ ConnectionError (UnknownErrorCode 413) "Payload Too Large"
650+ _ -> return ()
535651 if S.null bs
536652 then do
537653 writeIORef ref $ WTBDone False
538654 return $ front bs
539655 else push $ front bs
540656 where
541− push bs =
657+ push bs = do
542658 case findBound bound bs of
543659 FoundBound before after -> do
544660 let before' = killCRLF before
@@ -566,8 +682,8 @@ sinkTillBound :: S.ByteString
566682 -> Source
567683 -> Maybe Int64
568684 -> IO ((Bool, Int64), x)
569−sinkTillBound bound iter seed0 src max = do
570− (next, final) <- wrapTillBound bound src max
685+sinkTillBound bound iter seed0 src max' = do
686+ (next, final) <- wrapTillBound bound src max'
571687 let loop seed = do
572688 bs <- next
573689 if S.null bs