use of io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE in project ignite-3 by apache.
the class RestApiHandler method channelRead0.
/**
* {@inheritDoc}
*/
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
CompletableFuture<DefaultFullHttpResponse> responseFuture = router.route(request).map(route -> {
var response = new RestApiHttpResponse(new DefaultHttpResponse(HttpVersion.HTTP_1_1, OK));
return route.handle(request, response).thenApply(resp -> {
ByteBuf content = resp.content() != null ? Unpooled.wrappedBuffer(resp.content()) : new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
return new DefaultFullHttpResponse(resp.protocolVersion(), resp.status(), content, resp.headers(), EmptyHttpHeaders.INSTANCE);
});
}).orElseGet(() -> CompletableFuture.completedFuture(new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.NOT_FOUND)));
responseFuture.whenCompleteAsync((response, e) -> {
if (e != null) {
exceptionCaught(ctx, e);
return;
}
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
boolean keepAlive = HttpUtil.isKeepAlive(request);
if (keepAlive) {
if (!request.protocolVersion().isKeepAliveDefault()) {
response.headers().set(CONNECTION, KEEP_ALIVE);
}
} else {
response.headers().set(CONNECTION, CLOSE);
}
ChannelFuture f = ctx.writeAndFlush(response);
if (!keepAlive) {
f.addListener(ChannelFutureListener.CLOSE);
}
}, ctx.executor());
}
Aggregations