From 5159fbc8d86f6afc61224738dbfd8a94c8d4840d Mon Sep 17 00:00:00 2001 From: ioleo Date: Fri, 22 Sep 2023 09:11:07 +0100 Subject: [PATCH] Update Handler.scala Fix #2372 --- zio-http/src/main/scala/zio/http/Handler.scala | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/zio-http/src/main/scala/zio/http/Handler.scala b/zio-http/src/main/scala/zio/http/Handler.scala index fd62831833..6163ad6e46 100644 --- a/zio-http/src/main/scala/zio/http/Handler.scala +++ b/zio-http/src/main/scala/zio/http/Handler.scala @@ -660,6 +660,16 @@ sealed trait Handler[-R, +Err, -In, +Out] { self => object Handler { + def asStreamBounded(request: Request, limit: Int): Handler[Any, Throwable, Any, Chunk[Byte]] = + Handler.fromZIO( + request.body.asStream + .chunks + .runFoldZIO(Chunk.empty[Byte])((acc, bytes) => + ZIO.succeed(acc ++ bytes) + .filterOrFail(_.sizeIs < limit)(new Exception("Too large input")) + ) + ) + /** * Attempts to create a Handler that succeeds with the provided value, * capturing all exceptions on it's way.