Search in sources :

Example 96 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project aws-sdk-java-v2 by aws.

the class LastHttpContentSwallowerTest method testOtherHttpObjectRead_doesNotSwallowObject.

@Test
public void testOtherHttpObjectRead_doesNotSwallowObject() {
    HttpContent content = mock(HttpContent.class);
    lastHttpContentSwallower.channelRead0(mockCtx, content);
    verify(mockCtx).fireChannelRead(eq(content));
}
Also used : HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) Test(org.junit.jupiter.api.Test)

Example 97 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project aws-sdk-java-v2 by aws.

the class PublisherAdapterTest method errorOccurred_shouldInvokeResponseHandler.

@Test
public void errorOccurred_shouldInvokeResponseHandler() {
    RuntimeException exception = new RuntimeException("boom");
    Flowable<HttpContent> testPublisher = Flowable.error(exception);
    StreamedHttpResponse streamedHttpResponse = new DefaultStreamedHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.ACCEPTED, testPublisher);
    ResponseHandler.PublisherAdapter publisherAdapter = new ResponseHandler.PublisherAdapter(streamedHttpResponse, ctx, requestContext, executeFuture);
    TestSubscriber subscriber = new TestSubscriber();
    publisherAdapter.subscribe(subscriber);
    verify(ctx, times(0)).read();
    verify(ctx).close();
    assertThat(subscriber.errorOccurred).isEqualTo(true);
    verify(channelPool).release(channel);
    assertThat(executeFuture).isCompletedExceptionally();
    verify(responseHandler).onError(exception);
}
Also used : DefaultStreamedHttpResponse(software.amazon.awssdk.http.nio.netty.internal.nrs.DefaultStreamedHttpResponse) StreamedHttpResponse(software.amazon.awssdk.http.nio.netty.internal.nrs.StreamedHttpResponse) DefaultStreamedHttpResponse(software.amazon.awssdk.http.nio.netty.internal.nrs.DefaultStreamedHttpResponse) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) Test(org.junit.Test)

Example 98 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project aws-sdk-java-v2 by aws.

the class HttpToHttp2OutboundAdapter method write.

/**
 * Handles conversion of {@link HttpMessage} and {@link HttpContent} to HTTP/2 frames.
 */
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    if (!(msg instanceof HttpMessage || msg instanceof HttpContent)) {
        ctx.write(msg, promise);
        return;
    }
    boolean release = true;
    SimpleChannelPromiseAggregator promiseAggregator = new SimpleChannelPromiseAggregator(promise, ctx.channel(), ctx.executor());
    try {
        boolean endStream = false;
        if (msg instanceof HttpMessage) {
            HttpMessage httpMsg = (HttpMessage) msg;
            // Convert and write the headers.
            Http2Headers http2Headers = HttpConversionUtil.toHttp2Headers(httpMsg, false);
            endStream = msg instanceof FullHttpMessage && !((FullHttpMessage) msg).content().isReadable();
            ctx.write(new DefaultHttp2HeadersFrame(http2Headers), promiseAggregator.newPromise());
        }
        if (!endStream && msg instanceof HttpContent) {
            boolean isLastContent = false;
            HttpHeaders trailers = EmptyHttpHeaders.INSTANCE;
            Http2Headers http2Trailers = EmptyHttp2Headers.INSTANCE;
            if (msg instanceof LastHttpContent) {
                isLastContent = true;
                // Convert any trailing headers.
                LastHttpContent lastContent = (LastHttpContent) msg;
                trailers = lastContent.trailingHeaders();
                http2Trailers = HttpConversionUtil.toHttp2Headers(trailers, false);
            }
            // Write the data
            ByteBuf content = ((HttpContent) msg).content();
            endStream = isLastContent && trailers.isEmpty();
            release = false;
            ctx.write(new DefaultHttp2DataFrame(content, endStream), promiseAggregator.newPromise());
            if (!trailers.isEmpty()) {
                // Write trailing headers.
                ctx.write(new DefaultHttp2HeadersFrame(http2Trailers, true), promiseAggregator.newPromise());
            }
            ctx.flush();
        }
    } catch (Throwable t) {
        promiseAggregator.setFailure(t);
    } finally {
        if (release) {
            ReferenceCountUtil.release(msg);
        }
        promiseAggregator.doneAllocatingPromises();
    }
}
Also used : DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) EmptyHttpHeaders(io.netty.handler.codec.http.EmptyHttpHeaders) DefaultHttp2DataFrame(io.netty.handler.codec.http2.DefaultHttp2DataFrame) EmptyHttp2Headers(io.netty.handler.codec.http2.EmptyHttp2Headers) Http2Headers(io.netty.handler.codec.http2.Http2Headers) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) HttpMessage(io.netty.handler.codec.http.HttpMessage) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) 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)

Example 99 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project aws-sdk-java-v2 by aws.

the class HttpStreamsHandler method unbufferedWrite.

protected void unbufferedWrite(final ChannelHandlerContext ctx, final Outgoing out) {
    if (out.message instanceof FullHttpMessage) {
        // Forward as is
        ctx.writeAndFlush(out.message, out.promise);
        out.promise.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                executeInEventLoop(ctx, new Runnable() {

                    @Override
                    public void run() {
                        sentOutMessage(ctx);
                        outgoing.remove();
                        flushNext(ctx);
                    }
                });
            }
        });
    } else if (out.message instanceof StreamedHttpMessage) {
        StreamedHttpMessage streamed = (StreamedHttpMessage) out.message;
        HandlerSubscriber<HttpContent> subscriber = new HandlerSubscriber<HttpContent>(ctx.executor()) {

            @Override
            protected void error(Throwable error) {
                out.promise.tryFailure(error);
                ctx.close();
            }

            @Override
            protected void complete() {
                executeInEventLoop(ctx, new Runnable() {

                    @Override
                    public void run() {
                        completeBody(ctx);
                    }
                });
            }
        };
        sendLastHttpContent = true;
        // DON'T pass the promise through, create a new promise instead.
        ctx.writeAndFlush(out.message);
        ctx.pipeline().addAfter(ctx.name(), ctx.name() + "-body-subscriber", subscriber);
        subscribeSubscriberToStream(streamed, subscriber);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) ChannelFutureListener(io.netty.channel.ChannelFutureListener) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 100 with HttpContent

use of io.netty.handler.codec.http.HttpContent in project aws-sdk-java-v2 by aws.

the class HttpStreamsClientHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpResponse && awaiting100Continue != null && withServer == 0) {
        HttpResponse response = (HttpResponse) msg;
        if (response.status().equals(HttpResponseStatus.CONTINUE)) {
            super.subscribeSubscriberToStream(awaiting100ContinueMessage, awaiting100Continue);
            awaiting100Continue = null;
            awaiting100ContinueMessage = null;
            if (msg instanceof FullHttpResponse) {
                ReferenceCountUtil.release(msg);
            } else {
                ignoreResponseBody = true;
            }
        } else {
            awaiting100ContinueMessage.subscribe(new CancelledSubscriber<HttpContent>());
            awaiting100ContinueMessage = null;
            awaiting100Continue.onSubscribe(new NoOpSubscription());
            awaiting100Continue.onComplete();
            awaiting100Continue = null;
            super.channelRead(ctx, msg);
        }
    } else if (ignoreResponseBody && msg instanceof HttpContent) {
        ReferenceCountUtil.release(msg);
        if (msg instanceof LastHttpContent) {
            ignoreResponseBody = false;
        }
    } else {
        super.channelRead(ctx, msg);
    }
}
Also used : FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Aggregations

HttpContent (io.netty.handler.codec.http.HttpContent)267 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)197 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)85 ByteBuf (io.netty.buffer.ByteBuf)77 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)76 Test (org.junit.Test)65 HttpResponse (io.netty.handler.codec.http.HttpResponse)64 HttpRequest (io.netty.handler.codec.http.HttpRequest)52 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)43 ArrayList (java.util.ArrayList)37 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)33 HttpObject (io.netty.handler.codec.http.HttpObject)33 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)29 IOException (java.io.IOException)29 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)26 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)25 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)23 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)21 List (java.util.List)20 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)19