zhttp.service.client.ClientInboundHandler.scala Maven / Gradle / Ivy
package zhttp.service.client
import io.netty.channel.{ChannelHandlerContext, SimpleChannelInboundHandler}
import io.netty.handler.codec.http.{FullHttpRequest, FullHttpResponse}
import zhttp.http.Response
import zhttp.service.HttpRuntime
import zio.Promise
/**
* Handles HTTP response
*/
final class ClientInboundHandler[R](
zExec: HttpRuntime[R],
jReq: FullHttpRequest,
promise: Promise[Throwable, Response],
isWebSocket: Boolean,
) extends SimpleChannelInboundHandler[FullHttpResponse](true) {
override def channelActive(ctx: ChannelHandlerContext): Unit = {
if (isWebSocket) {
ctx.fireChannelActive(): Unit
} else {
ctx.writeAndFlush(jReq)
()
}
}
override def channelRead0(ctx: ChannelHandlerContext, msg: FullHttpResponse): Unit = {
msg.touch("handlers.ClientInboundHandler-channelRead0")
zExec.unsafeRunUninterruptible(ctx)(promise.succeed(Response.unsafeFromJResponse(msg)))
if (isWebSocket) {
ctx.fireChannelRead(msg.retain())
ctx.pipeline().remove(ctx.name()): Unit
}
}
override def exceptionCaught(ctx: ChannelHandlerContext, error: Throwable): Unit = {
zExec.unsafeRunUninterruptible(ctx)(promise.fail(error))
releaseRequest()
}
private def releaseRequest(): Unit = {
if (jReq.refCnt() > 0) {
jReq.release(jReq.refCnt()): Unit
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy