Search in sources :

Example 36 with LastHttpContent

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

the class HttpTrafficHandler method write.

@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    // modify message on way out to add headers if needed
    if (msg instanceof HttpResponse) {
        final HttpResponse response = (HttpResponse) msg;
        nonInformationalResponse = !isInformational(response);
        // Assume the response writer knows if they can persist or not and sets isKeepAlive on the response
        boolean maxKeepAliveRequestsReached = maxKeepAliveRequests != -1 && HttpServerOperations.requestsCounter(ctx.channel()) == maxKeepAliveRequests;
        if (maxKeepAliveRequestsReached || !isKeepAlive(response) || !isSelfDefinedMessageLength(response)) {
            // No longer keep alive as the client can't tell when the message is done unless we close connection
            pendingResponses = 0;
            persistentConnection = false;
        }
        // Server might think it can keep connection alive, but we should fix response header if we know better
        if (!shouldKeepAlive()) {
            setKeepAlive(response, false);
        }
        if (response.status().equals(HttpResponseStatus.CONTINUE)) {
            // "FutureReturnValueIgnored" this is deliberate
            ctx.write(msg, promise);
            return;
        }
    }
    if (msg instanceof LastHttpContent) {
        if (!shouldKeepAlive()) {
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug(format(ctx.channel(), "Detected non persistent http " + "connection, preparing to close"), pendingResponses);
            }
            ctx.write(msg, promise.unvoid()).addListener(this).addListener(ChannelFutureListener.CLOSE);
            return;
        }
        ctx.write(msg, promise.unvoid()).addListener(this);
        if (!persistentConnection) {
            return;
        }
        if (nonInformationalResponse) {
            nonInformationalResponse = false;
            pendingResponses -= 1;
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug(format(ctx.channel(), "Decreasing pending responses, now {}"), pendingResponses);
            }
        }
        if (pipelined != null && !pipelined.isEmpty()) {
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug(format(ctx.channel(), "Draining next pipelined " + "request, pending response count: {}, queued: {}"), pendingResponses, pipelined.size());
            }
            ctx.executor().execute(this);
        } else {
            ctx.read();
        }
        return;
    }
    if (persistentConnection && pendingResponses == 0) {
        if (HttpServerOperations.log.isDebugEnabled()) {
            HttpServerOperations.log.debug(format(ctx.channel(), "Dropped HTTP content, " + "since response has been sent already: {}"), toPrettyHexDump(msg));
        }
        ReferenceCountUtil.release(msg);
        promise.setSuccess();
        return;
    }
    // "FutureReturnValueIgnored" this is deliberate
    ctx.write(msg, promise);
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 37 with LastHttpContent

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

the class HttpTrafficHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (secure == null) {
        secure = ctx.channel().pipeline().get(SslHandler.class) != null;
    }
    if (remoteAddress == null) {
        remoteAddress = Optional.ofNullable(HAProxyMessageReader.resolveRemoteAddressFromProxyProtocol(ctx.channel())).orElse(ctx.channel().remoteAddress());
    }
    // read message and track if it was keepAlive
    if (msg instanceof HttpRequest) {
        IdleTimeoutHandler.removeIdleTimeoutHandler(ctx.pipeline());
        final HttpRequest request = (HttpRequest) msg;
        if (H2.equals(request.protocolVersion())) {
            sendDecodingFailures(new IllegalStateException("Unexpected request [" + request.method() + " " + request.uri() + " HTTP/2.0]"), msg);
            return;
        }
        if (persistentConnection) {
            pendingResponses += 1;
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug(format(ctx.channel(), "Increasing pending responses, now {}"), pendingResponses);
            }
            persistentConnection = isKeepAlive(request);
        } else {
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug(format(ctx.channel(), "Dropping pipelined HTTP request, " + "previous response requested connection close"));
            }
            ReferenceCountUtil.release(msg);
            return;
        }
        if (pendingResponses > 1) {
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug(format(ctx.channel(), "Buffering pipelined HTTP request, " + "pending response count: {}, queue: {}"), pendingResponses, pipelined != null ? pipelined.size() : 0);
            }
            overflow = true;
            doPipeline(ctx, msg);
            return;
        } else {
            overflow = false;
            DecoderResult decoderResult = request.decoderResult();
            if (decoderResult.isFailure()) {
                sendDecodingFailures(decoderResult.cause(), msg);
                return;
            }
            HttpServerOperations ops;
            try {
                ops = new HttpServerOperations(Connection.from(ctx.channel()), listener, request, compress, ConnectionInfo.from(ctx.channel(), request, secure, remoteAddress, forwardedHeaderHandler), cookieDecoder, cookieEncoder, formDecoderProvider, mapHandle, secure);
            } catch (RuntimeException e) {
                sendDecodingFailures(e, msg);
                return;
            }
            ops.bind();
            listener.onStateChange(ops, ConnectionObserver.State.CONFIGURED);
            ctx.fireChannelRead(msg);
            return;
        }
    } else if (persistentConnection && pendingResponses == 0) {
        if (msg instanceof LastHttpContent) {
            DecoderResult decoderResult = ((LastHttpContent) msg).decoderResult();
            if (decoderResult.isFailure()) {
                sendDecodingFailures(decoderResult.cause(), msg);
                return;
            }
            ctx.fireChannelRead(msg);
        } else {
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug(format(ctx.channel(), "Dropped HTTP content, " + "since response has been sent already: {}"), msg);
            }
            ReferenceCountUtil.release(msg);
        }
        ctx.read();
        return;
    } else if (overflow) {
        if (HttpServerOperations.log.isDebugEnabled()) {
            HttpServerOperations.log.debug(format(ctx.channel(), "Buffering pipelined HTTP content, " + "pending response count: {}, pending pipeline:{}"), pendingResponses, pipelined != null ? pipelined.size() : 0);
        }
        doPipeline(ctx, msg);
        return;
    }
    if (msg instanceof DecoderResultProvider) {
        DecoderResult decoderResult = ((DecoderResultProvider) msg).decoderResult();
        if (decoderResult.isFailure()) {
            sendDecodingFailures(decoderResult.cause(), msg);
            return;
        }
    }
    ctx.fireChannelRead(msg);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DecoderResultProvider(io.netty.handler.codec.DecoderResultProvider) DecoderResult(io.netty.handler.codec.DecoderResult) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 38 with LastHttpContent

use of org.apache.flink.shaded.netty4.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 39 with LastHttpContent

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

the class DefaultHttpTradeTestCase method testTradeForRequestAndLastContentSourceAndDumpFullRequests.

@Test
public final void testTradeForRequestAndLastContentSourceAndDumpFullRequests() throws IOException {
    final DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    final LastHttpContent lastcontent = new DefaultLastHttpContent(Nettys4Test.buildByteBuf(REQ_CONTENT));
    final EmbeddedChannel channel = new EmbeddedChannel();
    final HttpTrade trade = new DefaultHttpTrade(channel);
    writeToInboundAndFlush(channel, request);
    writeToInboundAndFlush(channel, lastcontent);
    // final Func0<FullHttpRequest> fullRequestBuilder =
    // trade.inboundHolder().fullOf(RxNettys.BUILD_FULL_REQUEST);
    // expected refCnt, request -- 1 + HttpMessageHolder -- 1, total refcnt is 2
    // TODO, why 4 & 5 refCnt ?
    callByteBufHolderBuilderOnceAndAssertDumpContentAndRefCnt(trade.inbound().compose(RxNettys.message2fullreq(trade)).toBlocking().single().unwrap(), REQ_CONTENT.getBytes(Charsets.UTF_8), 4);
    callByteBufHolderBuilderOnceAndAssertDumpContentAndRefCnt(trade.inbound().compose(RxNettys.message2fullreq(trade)).toBlocking().single().unwrap(), REQ_CONTENT.getBytes(Charsets.UTF_8), 5);
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 40 with LastHttpContent

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

the class Nettys method httpobjs2fullresp.

// retain when build fullresp
public static FullHttpResponse httpobjs2fullresp(final List<HttpObject> httpobjs) {
    if (httpobjs.size() > 0 && (httpobjs.get(0) instanceof HttpResponse) && (httpobjs.get(httpobjs.size() - 1) instanceof LastHttpContent)) {
        if (httpobjs.get(0) instanceof FullHttpResponse) {
            return ((FullHttpResponse) httpobjs.get(0)).retainedDuplicate();
        }
        final HttpResponse resp = (HttpResponse) httpobjs.get(0);
        final DefaultFullHttpResponse fullresp = new DefaultFullHttpResponse(resp.protocolVersion(), resp.status(), tobuf(httpobjs));
        fullresp.headers().add(resp.headers());
        // ? need update Content-Length header field ?
        return fullresp;
    } else {
        throw new RuntimeException("invalid HttpObjects");
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

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