Search in sources :

Example 31 with LastHttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project async-http-client by AsyncHttpClient.

the class AsyncHttpClientHandler method channelRead.

@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel channel = ctx.channel();
    Object attribute = Channels.getAttribute(channel);
    try {
        if (attribute instanceof OnLastHttpContentCallback) {
            if (msg instanceof LastHttpContent) {
                ((OnLastHttpContentCallback) attribute).call();
            }
        } else if (attribute instanceof NettyResponseFuture) {
            NettyResponseFuture<?> future = (NettyResponseFuture<?>) attribute;
            future.touch();
            handleRead(channel, future, msg);
        } else if (attribute instanceof StreamedResponsePublisher) {
            StreamedResponsePublisher publisher = (StreamedResponsePublisher) attribute;
            publisher.future().touch();
            if (msg instanceof HttpContent) {
                ByteBuf content = ((HttpContent) msg).content();
                // Republish as a HttpResponseBodyPart
                if (content.isReadable()) {
                    HttpResponseBodyPart part = config.getResponseBodyPartFactory().newResponseBodyPart(content, false);
                    ctx.fireChannelRead(part);
                }
                if (msg instanceof LastHttpContent) {
                    // Remove the handler from the pipeline, this will trigger
                    // it to finish
                    ctx.pipeline().remove(publisher);
                    // Trigger a read, just in case the last read complete
                    // triggered no new read
                    ctx.read();
                    // Send the last content on to the protocol, so that it can
                    // conclude the cleanup
                    handleRead(channel, publisher.future(), msg);
                }
            } else {
                logger.info("Received unexpected message while expecting a chunk: " + msg);
                ctx.pipeline().remove(publisher);
                Channels.setDiscard(channel);
            }
        } else if (attribute != DiscardEvent.DISCARD) {
            // unhandled message
            logger.debug("Orphan channel {} with attribute {} received message {}, closing", channel, attribute, msg);
            Channels.silentlyCloseChannel(channel);
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}
Also used : Channel(io.netty.channel.Channel) OnLastHttpContentCallback(org.asynchttpclient.netty.OnLastHttpContentCallback) NettyResponseFuture(org.asynchttpclient.netty.NettyResponseFuture) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ByteBuf(io.netty.buffer.ByteBuf) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpResponseBodyPart(org.asynchttpclient.HttpResponseBodyPart)

Example 32 with LastHttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.

the class HttpClientOperations method onInboundNext.

@Override
protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        if (response.decoderResult().isFailure()) {
            onInboundError(response.decoderResult().cause());
            ReferenceCountUtil.release(msg);
            return;
        }
        if (HttpResponseStatus.CONTINUE.equals(response.status())) {
            is100Continue = true;
            ReferenceCountUtil.release(msg);
            return;
        }
        if (started) {
            if (log.isDebugEnabled()) {
                log.debug(format(channel(), "HttpClientOperations cannot proceed more than one response {}"), response.headers().toString());
            }
            ReferenceCountUtil.release(msg);
            return;
        }
        is100Continue = false;
        started = true;
        setNettyResponse(response);
        if (!isKeepAlive()) {
            markPersistent(false);
        }
        if (isInboundCancelled()) {
            ReferenceCountUtil.release(msg);
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug(format(channel(), "Received response (auto-read:{}) : {}"), channel().config().isAutoRead(), responseHeaders().entries().toString());
        }
        if (notRedirected(response)) {
            try {
                listener().onStateChange(this, HttpClientState.RESPONSE_RECEIVED);
            } catch (Exception e) {
                onInboundError(e);
                ReferenceCountUtil.release(msg);
                return;
            }
        } else {
            // when redirecting no need of manual reading
            channel().config().setAutoRead(true);
        }
        if (msg instanceof FullHttpResponse) {
            FullHttpResponse request = (FullHttpResponse) msg;
            if (request.content().readableBytes() > 0) {
                super.onInboundNext(ctx, msg);
            } else {
                request.release();
            }
            terminate();
        }
        return;
    }
    if (msg instanceof LastHttpContent) {
        if (is100Continue) {
            ReferenceCountUtil.release(msg);
            channel().read();
            return;
        }
        if (!started) {
            if (log.isDebugEnabled()) {
                log.debug(format(channel(), "HttpClientOperations received an incorrect end " + "delimiter (previously used connection?)"));
            }
            ReferenceCountUtil.release(msg);
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug(format(channel(), "Received last HTTP packet"));
        }
        if (msg != LastHttpContent.EMPTY_LAST_CONTENT) {
            if (redirecting != null) {
                ReferenceCountUtil.release(msg);
            } else {
                super.onInboundNext(ctx, msg);
            }
        }
        if (redirecting == null) {
            // EmitResult is ignored as it is guaranteed that there will be only one emission of LastHttpContent
            // Whether there are subscribers or the subscriber cancels is not of interest
            // Evaluated EmitResult: FAIL_TERMINATED, FAIL_OVERFLOW, FAIL_CANCELLED, FAIL_NON_SERIALIZED
            // FAIL_ZERO_SUBSCRIBER
            trailerHeaders.tryEmitValue(((LastHttpContent) msg).trailingHeaders());
        }
        // force auto read to enable more accurate close selection now inbound is done
        channel().config().setAutoRead(true);
        if (markSentBody()) {
            markPersistent(false);
        }
        terminate();
        return;
    }
    if (!started) {
        if (log.isDebugEnabled()) {
            if (msg instanceof ByteBufHolder) {
                msg = ((ByteBufHolder) msg).content();
            }
            log.debug(format(channel(), "HttpClientOperations received an incorrect chunk {} " + "(previously used connection?)"), msg);
        }
        ReferenceCountUtil.release(msg);
        return;
    }
    if (redirecting != null) {
        ReferenceCountUtil.release(msg);
        // when redirecting auto-read is set to true, no need of manual reading
        return;
    }
    super.onInboundNext(ctx, msg);
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ByteBufHolder(io.netty.buffer.ByteBufHolder) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) AbortedException(reactor.netty.channel.AbortedException) URISyntaxException(java.net.URISyntaxException) ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 33 with LastHttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.

the class Http2StreamBridgeServerHandler method write.

@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    if (msg instanceof ByteBuf) {
        // "FutureReturnValueIgnored" this is deliberate
        ctx.write(new DefaultHttpContent((ByteBuf) msg), promise);
    } else {
        // "FutureReturnValueIgnored" this is deliberate
        ChannelFuture f = ctx.write(msg, promise);
        if (msg instanceof LastHttpContent) {
            pendingResponse = false;
            f.addListener(this);
            ctx.read();
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 34 with LastHttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.

the class AbstractHttpClientMetricsHandler method write.

@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    if (msg instanceof HttpRequest) {
        method = ((HttpRequest) msg).method().name();
        ChannelOperations<?, ?> channelOps = ChannelOperations.get(ctx.channel());
        if (channelOps instanceof HttpClientOperations) {
            HttpClientOperations ops = (HttpClientOperations) channelOps;
            path = uriTagValue == null ? ops.path : uriTagValue.apply(ops.path);
            contextView = ops.currentContextView();
        }
        startWrite((HttpRequest) msg, ctx.channel().remoteAddress());
    }
    if (msg instanceof ByteBufHolder) {
        dataSent += ((ByteBufHolder) msg).content().readableBytes();
    } else if (msg instanceof ByteBuf) {
        dataSent += ((ByteBuf) msg).readableBytes();
    }
    if (msg instanceof LastHttpContent) {
        SocketAddress address = ctx.channel().remoteAddress();
        promise.addListener(future -> recordWrite(address));
    }
    // "FutureReturnValueIgnored" this is deliberate
    ctx.write(msg, promise);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) ByteBufHolder(io.netty.buffer.ByteBufHolder) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) SocketAddress(java.net.SocketAddress)

Example 35 with LastHttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.

the class HttpClientOperationsTest method addNamedDecoderReplaysLastHttp.

@Test
void addNamedDecoderReplaysLastHttp() {
    ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
    EmbeddedChannel channel = new EmbeddedChannel();
    new HttpClientOperations(() -> channel, ConnectionObserver.emptyListener(), ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT).addHandler("json", new JsonObjectDecoder());
    channel.writeInbound(new DefaultLastHttpContent(buf));
    assertThat(channel.pipeline().names()).first().isEqualTo("json$extractor");
    Object content = channel.readInbound();
    assertThat(content).isInstanceOf(ByteBuf.class);
    ((ByteBuf) content).release();
    content = channel.readInbound();
    assertThat(content).isInstanceOf(LastHttpContent.class);
    ((LastHttpContent) content).release();
    content = channel.readInbound();
    assertThat(content).isNull();
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) JsonObjectDecoder(io.netty.handler.codec.json.JsonObjectDecoder) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Test(org.junit.jupiter.api.Test)

Aggregations

LastHttpContent (io.netty.handler.codec.http.LastHttpContent)137 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)63 HttpContent (io.netty.handler.codec.http.HttpContent)55 ByteBuf (io.netty.buffer.ByteBuf)49 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)43 HttpResponse (io.netty.handler.codec.http.HttpResponse)42 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 Test (org.junit.Test)27 Test (org.junit.jupiter.api.Test)24 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)23 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)18 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)13 IOException (java.io.IOException)12 ChannelFuture (io.netty.channel.ChannelFuture)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)10 HttpObject (io.netty.handler.codec.http.HttpObject)10 JsonObjectDecoder (io.netty.handler.codec.json.JsonObjectDecoder)10 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)9 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9