Search in sources :

Example 26 with Http2ConnectionEncoder

use of io.netty.handler.codec.http2.Http2ConnectionEncoder in project netty by netty.

the class HttpToHttp2ConnectionHandler 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 {
        Http2ConnectionEncoder encoder = encoder();
        boolean endStream = false;
        if (msg instanceof HttpMessage) {
            final HttpMessage httpMsg = (HttpMessage) msg;
            // Provide the user the opportunity to specify the streamId
            currentStreamId = getStreamId(httpMsg.headers());
            // Convert and write the headers.
            Http2Headers http2Headers = HttpConversionUtil.toHttp2Headers(httpMsg, validateHeaders);
            endStream = msg instanceof FullHttpMessage && !((FullHttpMessage) msg).content().isReadable();
            writeHeaders(ctx, encoder, currentStreamId, httpMsg.headers(), http2Headers, endStream, promiseAggregator);
        }
        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.
                final LastHttpContent lastContent = (LastHttpContent) msg;
                trailers = lastContent.trailingHeaders();
                http2Trailers = HttpConversionUtil.toHttp2Headers(trailers, validateHeaders);
            }
            // Write the data
            final ByteBuf content = ((HttpContent) msg).content();
            endStream = isLastContent && trailers.isEmpty();
            release = false;
            encoder.writeData(ctx, currentStreamId, content, 0, endStream, promiseAggregator.newPromise());
            if (!trailers.isEmpty()) {
                // Write trailing headers.
                writeHeaders(ctx, encoder, currentStreamId, trailers, http2Trailers, true, promiseAggregator);
            }
        }
    } catch (Throwable t) {
        onError(ctx, t);
        promiseAggregator.setFailure(t);
    } finally {
        if (release) {
            ReferenceCountUtil.release(msg);
        }
        promiseAggregator.doneAllocatingPromises();
    }
}
Also used : EmptyHttpHeaders(io.netty.handler.codec.http.EmptyHttpHeaders) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) SimpleChannelPromiseAggregator(io.netty.handler.codec.http2.Http2CodecUtil.SimpleChannelPromiseAggregator) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) HttpMessage(io.netty.handler.codec.http.HttpMessage) 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)

Aggregations

Http2ConnectionEncoder (io.netty.handler.codec.http2.Http2ConnectionEncoder)22 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)19 ChannelFuture (io.netty.channel.ChannelFuture)18 Context (io.vertx.core.Context)17 Test (org.junit.Test)17 Http2Exception (io.netty.handler.codec.http2.Http2Exception)16 Http2FrameAdapter (io.netty.handler.codec.http2.Http2FrameAdapter)16 ByteBuf (io.netty.buffer.ByteBuf)15 Channel (io.netty.channel.Channel)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)15 Http2Connection (io.netty.handler.codec.http2.Http2Connection)15 Http2ConnectionDecoder (io.netty.handler.codec.http2.Http2ConnectionDecoder)15 Http2ConnectionHandler (io.netty.handler.codec.http2.Http2ConnectionHandler)15 ChannelPipeline (io.netty.channel.ChannelPipeline)14 DefaultHttp2Connection (io.netty.handler.codec.http2.DefaultHttp2Connection)14 Http2Headers (io.netty.handler.codec.http2.Http2Headers)14 Bootstrap (io.netty.bootstrap.Bootstrap)13 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)13 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)13 EventLoopGroup (io.netty.channel.EventLoopGroup)12