Skip to content

Commit

Permalink
fix: request body issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed May 7, 2022
1 parent 10c760b commit c1d14f3
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions zio-http/src/main/scala/zhttp/service/Handler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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
}

/**
Expand Down

0 comments on commit c1d14f3

Please sign in to comment.