@@ -626,51 +626,55 @@ data THStatus = THStatus
627takeHeaders :: C.Sink ByteString (ResourceT IO) [ByteString]
627takeHeaders :: C.Sink ByteString (ResourceT IO) [ByteString]
628takeHeaders =
628takeHeaders =
629− C.sinkState (THStatus 0 id id) takeHeadersPush close
629+ C.Processing (push (THStatus 0 id id)) close
631− close _ = throwIO IncompleteHeaders
631+ close = throwIO IncompleteHeaders
633+ push (THStatus len lines prepend) bs
635+ | len > maxTotalHeaderLength = throwIO OverLargeHeader
638+ -- No newline find in this chunk. Add it to the prepend,
639+ -- update the length, and continue processing.
641+ let len' = len + bsLen
642+ prepend' = prepend . S.append bs
643+ status = THStatus len' lines prepend'
644+ in C.Processing (push status) close
645+ -- Found a newline at position end.
647+ let start = end + 1 -- start of next chunk
649+ -- There were some bytes before the newline, get them
650+ | end > 0 = prepend $ SU.unsafeTake (checkCR bs end) bs
651+ -- No bytes before the newline
652+ | otherwise = prepend S.empty
656+ let lines' = lines []
658+ rest = if start < bsLen
659+ then Just (SU.unsafeDrop start bs)
661+ in C.Done rest lines'
664+ let len' = len + start
665+ lines' = lines . (line:)
666+ status = THStatus len' lines' id
668+ -- more bytes in this chunk, push again
669+ then let bs' = SU.unsafeDrop start bs
671+ -- no more bytes in this chunk, ask for more
672+ else C.Processing (push status) close
675+ mnl = S.elemIndex 10 bs
632{-# INLINE takeHeaders #-}
676{-# INLINE takeHeaders #-}
634−takeHeadersPush :: THStatus
636− -> ResourceT IO (C.SinkStateResult THStatus ByteString [ByteString])
637−takeHeadersPush (THStatus len _ _ ) _
638− | len > maxTotalHeaderLength = throwIO OverLargeHeader
639−takeHeadersPush (THStatus len lines prepend) bs =
641− -- no newline. prepend entire bs to next line
643− let len' = len + bsLen
644− return $ C.StateProcessing $ THStatus len' lines (prepend . S.append bs)
648− line = prepend (if end > 0
649− then SU.unsafeTake (checkCR bs end) bs
654− let lines' = lines []
657− let rest = SU.unsafeDrop start bs
658− return $ C.StateDone (Just rest) lines'
659− else return $ C.StateDone Nothing lines'
662− let len' = len + start
663− lines' = lines . (:) line
666− let more = SU.unsafeDrop start bs
667− takeHeadersPush (THStatus len' lines' id) more
668− else return $ C.StateProcessing $ THStatus len' lines' id
671− mnl = S.elemIndex 10 bs
672−{-# INLINE takeHeadersPush #-}
674checkCR :: ByteString -> Int -> Int
678checkCR :: ByteString -> Int -> Int
675checkCR bs pos =
679checkCR bs pos =
676 let !p = pos - 1
680 let !p = pos - 1