Search in sources :

Example 21 with ByteBufHolder

use of io.netty.buffer.ByteBufHolder in project reactor-netty by reactor.

the class AccessLogHandlerH1 method write.

@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    if (msg instanceof HttpResponse) {
        final HttpResponse response = (HttpResponse) msg;
        final HttpResponseStatus status = response.status();
        if (status.equals(HttpResponseStatus.CONTINUE)) {
            // "FutureReturnValueIgnored" this is deliberate
            ctx.write(msg, promise);
            return;
        }
        final boolean chunked = HttpUtil.isTransferEncodingChunked(response);
        accessLogArgProvider.response(response).chunked(chunked);
        if (!chunked) {
            accessLogArgProvider.contentLength(HttpUtil.getContentLength(response, -1));
        }
        ChannelOperations<?, ?> ops = ChannelOperations.get(ctx.channel());
        if (ops instanceof HttpInfos) {
            accessLogArgProvider.cookies(((HttpInfos) ops).cookies());
        }
    }
    if (msg instanceof LastHttpContent) {
        accessLogArgProvider.increaseContentLength(((LastHttpContent) msg).content().readableBytes());
        ctx.write(msg, promise.unvoid()).addListener(future -> {
            if (future.isSuccess()) {
                AccessLog log = accessLog.apply(accessLogArgProvider);
                if (log != null) {
                    log.log();
                }
                accessLogArgProvider.clear();
            }
        });
        return;
    }
    if (msg instanceof ByteBuf) {
        accessLogArgProvider.increaseContentLength(((ByteBuf) msg).readableBytes());
    }
    if (msg instanceof ByteBufHolder) {
        accessLogArgProvider.increaseContentLength(((ByteBufHolder) msg).content().readableBytes());
    }
    // "FutureReturnValueIgnored" this is deliberate
    ctx.write(msg, promise);
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) HttpResponse(io.netty.handler.codec.http.HttpResponse) ByteBufHolder(io.netty.buffer.ByteBufHolder) HttpInfos(reactor.netty.http.HttpInfos) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBufHolder (io.netty.buffer.ByteBufHolder)21 ByteBuf (io.netty.buffer.ByteBuf)13 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)5 Test (org.junit.jupiter.api.Test)5 ChannelFuture (io.netty.channel.ChannelFuture)4 DnsRawRecord (io.netty.handler.codec.dns.DnsRawRecord)4 DnsRecord (io.netty.handler.codec.dns.DnsRecord)4 HttpResponse (io.netty.handler.codec.http.HttpResponse)4 DefaultByteBufHolder (io.netty.buffer.DefaultByteBufHolder)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)3 HttpRequest (io.netty.handler.codec.http.HttpRequest)3 ArrayList (java.util.ArrayList)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 EmptyByteBuf (io.netty.buffer.EmptyByteBuf)2 DnsRecordType (io.netty.handler.codec.dns.DnsRecordType)2 Future (io.netty.util.concurrent.Future)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 URISyntaxException (java.net.URISyntaxException)2