Search in sources :

Example 81 with LastHttpContent

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

the class DefaultSignalClient method assembleOutgoing.

private Outgoing assembleOutgoing(final HttpRequest request, final BodyForm body, final Attachment[] attachments) throws Exception {
    if (0 == attachments.length) {
        final LastHttpContent lastContent = buildLastContent(request, body);
        return new Outgoing(Observable.<Object>just(request, lastContent), lastContent.content().readableBytes(), new Action0() {

            @Override
            public void call() {
                ReferenceCountUtil.release(request);
                ReferenceCountUtil.release(lastContent);
            }
        });
    } else {
        // Use the PostBody encoder
        final HttpPostRequestEncoder postRequestEncoder = new HttpPostRequestEncoder(_DATA_FACTORY, request, true, CharsetUtil.UTF_8, // true => multipart
        EncoderMode.HTML5);
        final long signalSize = addSignalToMultipart(postRequestEncoder, body);
        final long attachmentSize = addAttachmentsToMultipart(postRequestEncoder, attachments);
        final long total = signalSize + attachmentSize;
        // finalize request
        final HttpRequest request4send = postRequestEncoder.finalizeRequest();
        final Action0 toRelease = new Action0() {

            @Override
            public void call() {
                ReferenceCountUtil.release(request4send);
                RxNettys.releaseObjects(postRequestEncoder.getBodyListAttributes());
            }
        };
        return postRequestEncoder.isChunked() ? new Outgoing(Observable.<Object>just(request4send, postRequestEncoder), total, toRelease) : new Outgoing(Observable.<Object>just(request4send), total, toRelease);
    }
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) Action0(rx.functions.Action0) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 82 with LastHttpContent

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

the class HttpOperationsTest method httpAndJsonDecoders.

@Test
public void httpAndJsonDecoders() {
    EmbeddedChannel channel = new EmbeddedChannel();
    NettyContext testContext = () -> channel;
    ChannelHandler handler = new JsonObjectDecoder(true);
    testContext.addHandlerLast("foo", handler);
    HttpOperations.autoAddHttpExtractor(testContext, "foo", handler);
    String json1 = "[{\"some\": 1} , {\"valu";
    String json2 = "e\": true, \"test\": 1}]";
    Object[] content = new Object[3];
    content[0] = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    content[1] = new DefaultHttpContent(Unpooled.copiedBuffer(json1, CharsetUtil.UTF_8));
    content[2] = new DefaultLastHttpContent(Unpooled.copiedBuffer(json2, CharsetUtil.UTF_8));
    channel.writeInbound(content);
    Object t = channel.readInbound();
    assertThat(t, instanceOf(HttpResponse.class));
    assertThat(t, not(instanceOf(HttpContent.class)));
    t = channel.readInbound();
    assertThat(t, instanceOf(ByteBuf.class));
    assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"some\": 1}"));
    ((ByteBuf) t).release();
    t = channel.readInbound();
    assertThat(t, instanceOf(ByteBuf.class));
    assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"value\": true, \"test\": 1}"));
    ((ByteBuf) t).release();
    t = channel.readInbound();
    assertThat(t, is(LastHttpContent.EMPTY_LAST_CONTENT));
    ((LastHttpContent) t).release();
    t = channel.readInbound();
    assertThat(t, nullValue());
}
Also used : DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) JsonObjectDecoder(io.netty.handler.codec.json.JsonObjectDecoder) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelHandler(io.netty.channel.ChannelHandler) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) NettyContext(reactor.ipc.netty.NettyContext) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) Test(org.junit.Test)

Example 83 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
public void addNamedDecoderReplaysLastHttp() throws Exception {
    ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
    EmbeddedChannel channel = new EmbeddedChannel();
    HttpClientOperations ops = new HttpClientOperations(channel, (response, request) -> null, handler);
    ops.addHandler("json", new JsonObjectDecoder());
    channel.writeInbound(new DefaultLastHttpContent(buf));
    assertThat(channel.pipeline().names().iterator().next(), is("json$extractor"));
    Object content = channel.readInbound();
    assertThat(content, instanceOf(ByteBuf.class));
    ((ByteBuf) content).release();
    content = channel.readInbound();
    assertThat(content, instanceOf(LastHttpContent.class));
    ((LastHttpContent) content).release();
    assertThat(channel.readInbound(), nullValue());
}
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.Test)

Example 84 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 addNamedEncoderReplaysLastHttp.

@Test
public void addNamedEncoderReplaysLastHttp() throws Exception {
    ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
    EmbeddedChannel channel = new EmbeddedChannel();
    HttpClientOperations ops = new HttpClientOperations(channel, (response, request) -> null, handler);
    ops.addHandler("json", new JsonObjectDecoder());
    channel.writeInbound(new DefaultLastHttpContent(buf));
    assertThat(channel.pipeline().names().iterator().next(), is("json$extractor"));
    Object content = channel.readInbound();
    assertThat(content, instanceOf(ByteBuf.class));
    ((ByteBuf) content).release();
    content = channel.readInbound();
    assertThat(content, instanceOf(LastHttpContent.class));
    ((LastHttpContent) content).release();
    assertThat(channel.readInbound(), nullValue());
}
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.Test)

Example 85 with LastHttpContent

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

the class HttpServerHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    // modify message on way out to add headers if needed
    if (msg instanceof HttpResponse) {
        final HttpResponse response = (HttpResponse) msg;
        trackResponse(response);
        // Assume the response writer knows if they can persist or not and sets isKeepAlive on the response
        if (!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)) {
            ctx.write(msg, promise);
            return;
        }
    }
    if (msg instanceof LastHttpContent) {
        if (!shouldKeepAlive()) {
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug("Detected non persistent http " + "connection," + " " + "preparing to close", pendingResponses);
            }
            promise.addListener(ChannelFutureListener.CLOSE);
            ctx.write(msg, promise);
            return;
        }
        ctx.write(msg, promise).addListener(new TerminateHttpHandler(ctx.channel()));
        if (!persistentConnection) {
            return;
        }
        if (mustRecycleEncoder) {
            mustRecycleEncoder = false;
            pendingResponses -= 1;
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug("Decreasing pending responses, now " + "{}", pendingResponses);
            }
        }
        if (pipelined != null && !pipelined.isEmpty()) {
            if (HttpServerOperations.log.isDebugEnabled()) {
                HttpServerOperations.log.debug("draining next pipelined " + "request," + " pending response count: {}, queued: " + "{}", pendingResponses, pipelined.size());
            }
            ctx.executor().execute(this);
        } else {
            ctx.read();
        }
        return;
    }
    ctx.write(msg, promise);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) 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