@@ -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,28 +410,37 @@ 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
292430-- | Take one header or subheader line.
293−-- It makes sense to limit the maximum line length.
294−-- Apache's default is 8190 (http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestline)
295−-- so we're using that here as well.
296−takeLine :: Source -> IO (Maybe S.ByteString)
297−takeLine src =
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 =
298435 go ""
299436 where
300437 go front = do
301438 bs <- readSource src
302− when (S.length front + S.length bs > 8190) $ error "Header line length exceeds allowed maximum."
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 ()
303444 if S.null bs
304445 then close front
305446 else push front bs
@@ -311,32 +452,36 @@ takeLine src =
311452 then go $ front `S.append` x
312453 else do
313454 when (S.length y > 1) $ leftover src $ S.drop 1 y
314− return $ Just $ killCR $ front `S.append` x
315−
316−takeLines :: Source -> IO [S.ByteString]
317−takeLines src = do
318− 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
319480 case res of
320− Nothing -> return []
481+ Nothing -> return lines
321482 Just l
322− | S.null l -> return []
323− | otherwise -> do
324− ls <- takeLines src
325− return $ l : ls
326−
327−takeLines' :: Int -> Source -> IO [S.ByteString]
328−takeLines' a b = reverse <$> takeLines'' [] a b
329−
330−takeLines'' :: [S.ByteString] -> Int -> Source -> IO [S.ByteString]
331−takeLines'' lines maxLines src
332− | length lines > maxLines = error "Too many lines in mime/multipart header"
333− | otherwise = do
334− res <- takeLine src
335− case res of
336− Nothing -> return lines
337− Just l
338− | S.null l -> return lines
339− | otherwise -> takeLines'' (l:lines) maxLines src
483+ | S.null l -> return lines
484+ | otherwise -> takeLines'' (l:lines) lineLength maxLines src
340485
341486data Source = Source (IO S.ByteString) (IORef S.ByteString)
342487
@@ -355,53 +500,6 @@ readSource (Source f ref) = do
355500leftover :: Source -> S.ByteString -> IO ()
356501leftover (Source _ ref) bs = writeIORef ref bs
357502
358−parsePieces :: BackEnd y
359− -> S.ByteString
360− -> IO S.ByteString
361− -> (Either Param (File y) -> IO ())
362− -> IO ()
363−parsePieces sink bound rbody add =
364− mkSource rbody >>= loop
365− where
366− loop src = do
367− _boundLine <- takeLine src
368− res' <- takeLines src
369− unless (null res') $ do
370− let ls' = map parsePair res'
371− let x = do
372− cd <- lookup contDisp ls'
373− let ct = lookup contType ls'
374− let attrs = parseAttrs cd
375− name <- lookup "name" attrs
376− return (ct, name, lookup "filename" attrs)
377− case x of
378− Just (mct, name, Just filename) -> do
379− let ct = fromMaybe "application/octet-stream" mct
380− fi0 = FileInfo filename ct ()
381− ((wasFound, _fileSize), y) <- sinkTillBound' bound name fi0 sink src Nothing
382− add $ Right (name, fi0 { fileContent = y })
383− when wasFound (loop src)
384− Just (_ct, name, Nothing) -> do
385− let seed = id
386− let iter front bs = return $ front . (:) bs
387− ((wasFound, _fileSize), front) <- sinkTillBound bound iter seed src Nothing
388− let bs = S.concat $ front []
389− let x' = (name, bs)
390− add $ Left x'
391− when wasFound (loop src)
392− _ -> do
393− -- ignore this part
394− let seed = ()
395− iter () _ = return ()
396− ((wasFound, _fileSize), ()) <- sinkTillBound bound iter seed src Nothing
397− when wasFound (loop src)
398− where
399− contDisp = mk $ S8.pack "Content-Disposition"
400− contType = mk $ S8.pack "Content-Type"
401− parsePair s =
402− let (x, y) = breakDiscard 58 s -- colon
403− in (mk $ x, S.dropWhile (== 32) y) -- space
404−
405503parsePiecesEx :: ParseRequestBodyOptions
406504 -> BackEnd y
407505 -> S.ByteString
@@ -413,8 +511,9 @@ parsePiecesEx o sink bound rbody add =
413511 where
414512 loop :: Int -> Int -> Int -> Int64 -> Source -> IO ()
415513 loop numParms numFiles parmSize filesSize src = do
416− _boundLine <- takeLine src
417− res' <- takeLines' (prboMaxHeaderLines o) src
514+ _boundLine <- takeLine (prboMaxHeaderLineLength o) src
515+ res' <- takeLines' (prboMaxHeaderLineLength o)
516+ (prboMaxHeaderLines o) src
418517 unless (null res') $ do
419518 let ls' = map parsePair res'
420519 let x = do
@@ -425,30 +524,41 @@ parsePiecesEx o sink bound rbody add =
425524 return (ct, name, lookup "filename" attrs)
426525 case x of
427526 Just (mct, name, Just filename) -> do
428− when (S.length name > prboKeyLength o) $
429− error "Filename is too long"
430− when (numFiles >= prboMaxNumFiles o) $
431− 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 ()
432536 let ct = fromMaybe "application/octet-stream" mct
433537 fi0 = FileInfo filename ct ()
434− fs = catMaybes [ prboMaxFileSize o, prboMaxFilesSize o ]
538+ fs = catMaybes [ prboMaxFileSize o
539+ , subtract filesSize <$> prboMaxFilesSize o ]
435540 mfs = if fs == [] then Nothing else Just $ minimum fs
436541 ((wasFound, fileSize), y) <- sinkTillBound' bound name fi0 sink src mfs
437542 let newFilesSize = filesSize + fileSize
438543 add $ Right (name, fi0 { fileContent = y })
439544 when wasFound $ loop numParms (numFiles + 1) parmSize newFilesSize src
440545 Just (_ct, name, Nothing) -> do
441− when (S.length name > prboKeyLength o) $
442− 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 ()
443551 let seed = id
444552 let iter front bs = return $ front . (:) bs
445− ((wasFound, _fileSize), front) <- sinkTillBound bound iter seed src $
446− Just . fromIntegral . prboMaxParmsValueSize $ o
553+ ((wasFound, _fileSize), front) <- sinkTillBound bound iter seed src
554+ (fromIntegral <$> prboMaxParmsSize o)
447555 let bs = S.concat $ front []
448556 let x' = (name, bs)
449557 let newParmSize = parmSize + S.length name + S.length bs
450− when (newParmSize > prboMaxParmsValueSize o) $
451− 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 ()
452562 add $ Left x'
453563 when wasFound $ loop (numParms + 1) numFiles
454564 newParmSize filesSize src
@@ -472,7 +582,7 @@ data Bound = FoundBound S.ByteString S.ByteString
472582 deriving (Eq, Show)
473583
474584findBound :: S.ByteString -> S.ByteString -> Bound
475−findBound b bs = handleBreak $ Search.breakOn b bs
585+findBound b bs = handleBreak $ S.breakSubstring b bs
476586 where
477587 handleBreak (h, t)
478588 | S.null t = go [lowBound..S.length bs - 1]
@@ -501,8 +611,8 @@ sinkTillBound' :: S.ByteString
501611 -> Source
502612 -> Maybe Int64
503613 -> IO ((Bool, Int64), y)
504−sinkTillBound' bound name fi sink src max = do
505− (next, final) <- wrapTillBound bound src max
614+sinkTillBound' bound name fi sink src max' = do
615+ (next, final) <- wrapTillBound bound src max'
506616 y <- sink name fi next
507617 b <- final
508618 return (b, y)
@@ -513,7 +623,7 @@ wrapTillBound :: S.ByteString -- ^ bound
513623 -> Source
514624 -> Maybe Int64
515625 -> IO (IO S.ByteString, IO (Bool, Int64)) -- ^ Bool indicates if the bound was found
516−wrapTillBound bound src max = do
626+wrapTillBound bound src max' = do
517627 ref <- newIORef $ WTBWorking id
518628 sref <- newIORef (0 :: Int64)
519629 return (go ref sref, final ref sref)
@@ -532,18 +642,19 @@ wrapTillBound bound src max = do
532642 WTBDone _ -> return S.empty
533643 WTBWorking front -> do
534644 bs <- readSource src
535− cur <- atomicModifyIORef sref $ \ cur ->
645+ cur <- atomicModifyIORef' sref $ \ cur ->
536646 let new = cur + fromIntegral (S.length bs) in (new, new)
537− case max of
538− Just max' | cur > max' -> error "Maximum size exceeded"
539− _ -> return ()
647+ case max' of
648+ Just max'' | cur > max'' ->
649+ E.throwIO $ ConnectionError (UnknownErrorCode 413) "Payload Too Large"
650+ _ -> return ()
540651 if S.null bs
541652 then do
542653 writeIORef ref $ WTBDone False
543654 return $ front bs
544655 else push $ front bs
545656 where
546− push bs =
657+ push bs = do
547658 case findBound bound bs of
548659 FoundBound before after -> do
549660 let before' = killCRLF before
@@ -571,8 +682,8 @@ sinkTillBound :: S.ByteString
571682 -> Source
572683 -> Maybe Int64
573684 -> IO ((Bool, Int64), x)
574−sinkTillBound bound iter seed0 src max = do
575− (next, final) <- wrapTillBound bound src max
685+sinkTillBound bound iter seed0 src max' = do
686+ (next, final) <- wrapTillBound bound src max'
576687 let loop seed = do
577688 bs <- next
578689 if S.null bs