diff --git a/zio-http/src/main/scala/zhttp/service/Handler.scala b/zio-http/src/main/scala/zhttp/service/Handler.scala index b49d545733..f74e1f77f3 100644 --- a/zio-http/src/main/scala/zhttp/service/Handler.scala +++ b/zio-http/src/main/scala/zhttp/service/Handler.scala @@ -51,19 +51,20 @@ private[zhttp] final case class Handler[R]( case throwable: Throwable => resWriter.write(throwable, jReq) } case jReq: HttpRequest => - if (canHaveBody(jReq)) { - ctx.channel().config().setAutoRead(false): Unit - } + val hasBody = canHaveBody(jReq) + if (hasBody) ctx.channel().config().setAutoRead(false): Unit try unsafeRun( jReq, app, new Request { - override def data: HttpData = HttpData.UnsafeAsync(callback => - ctx - .pipeline() - .addAfter(HTTP_REQUEST_HANDLER, HTTP_CONTENT_HANDLER, new RequestBodyHandler(callback)): Unit, - ) + override def data: HttpData = if (hasBody) asyncData else HttpData.empty + private final def asyncData = + HttpData.UnsafeAsync(callback => + ctx + .pipeline() + .addAfter(HTTP_REQUEST_HANDLER, HTTP_CONTENT_HANDLER, new RequestBodyHandler(callback)): Unit, + ) override def headers: Headers = Headers.make(jReq.headers()) @@ -101,9 +102,10 @@ private[zhttp] final case class Handler[R]( } } - private def canHaveBody(req: HttpRequest): Boolean = req.method() match { - case HttpMethod.GET | HttpMethod.HEAD | HttpMethod.OPTIONS | HttpMethod.TRACE => false - case _ => true + private def canHaveBody(req: HttpRequest): Boolean = { + req.method() == HttpMethod.TRACE || + req.headers().get(HttpHeaderNames.CONTENT_LENGTH) != null || + req.headers().get(HttpHeaderNames.TRANSFER_ENCODING) != null } /**