Search in sources :

Example 61 with LastHttpContent

use of io.netty.handler.codec.http.LastHttpContent in project proxyee-down by monkeyWie.

the class SniffIntercept method beforeRequest.

@Override
public void beforeRequest(Channel clientChannel, HttpContent httpContent, HttpProxyInterceptPipeline pipeline) throws Exception {
    if (!matchFlag) {
        super.beforeRequest(clientChannel, httpContent, pipeline);
        return;
    }
    if (content != null) {
        ByteBuf temp = httpContent.content().slice();
        content.writeBytes(temp);
        if (httpContent instanceof LastHttpContent) {
            try {
                byte[] contentBts = new byte[content.readableBytes()];
                content.readBytes(contentBts);
                ((HttpRequestInfo) pipeline.getHttpRequest()).setContent(contentBts);
            } finally {
                ReferenceCountUtil.release(content);
            }
        }
    }
    pipeline.beforeRequest(clientChannel, httpContent);
}
Also used : HttpRequestInfo(org.pdown.core.entity.HttpRequestInfo) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 62 with LastHttpContent

use of 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 63 with LastHttpContent

use of 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 64 with LastHttpContent

use of 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 65 with LastHttpContent

use of 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 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 IOException (java.io.IOException)10 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)9 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9