Search in sources :

Example 96 with DefaultHttp2Headers

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

the class HelloWorldHttp2Handler method sendResponse.

/**
 * Sends a "Hello World" DATA frame to the client.
 */
private static void sendResponse(ChannelHandlerContext ctx, Http2FrameStream stream, ByteBuf payload) {
    // Send a frame for the response status
    Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
    ctx.write(new DefaultHttp2HeadersFrame(headers).stream(stream));
    ctx.write(new DefaultHttp2DataFrame(payload, true).stream(stream));
}
Also used : DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) DefaultHttp2DataFrame(io.netty.handler.codec.http2.DefaultHttp2DataFrame) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers)

Example 97 with DefaultHttp2Headers

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

the class HelloWorldHttp2Handler method sendResponse.

/**
 * Sends a "Hello World" DATA frame to the client.
 */
private void sendResponse(ChannelHandlerContext ctx, int streamId, ByteBuf payload) {
    // Send a frame for the response status
    Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
    encoder().writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise());
    encoder().writeData(ctx, streamId, payload, 0, true, ctx.newPromise());
// no need to call flush as channelReadComplete(...) will take care of it.
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers)

Example 98 with DefaultHttp2Headers

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

the class Http2FrameClient method main.

public static void main(String[] args) throws Exception {
    final EventLoopGroup clientWorkerGroup = new NioEventLoopGroup();
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        final SslProvider provider = SslProvider.isAlpnSupported(SslProvider.OPENSSL) ? SslProvider.OPENSSL : SslProvider.JDK;
        sslCtx = SslContextBuilder.forClient().sslProvider(provider).ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE).trustManager(InsecureTrustManagerFactory.INSTANCE).applicationProtocolConfig(new ApplicationProtocolConfig(Protocol.ALPN, SelectorFailureBehavior.NO_ADVERTISE, SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1)).build();
    } else {
        sslCtx = null;
    }
    try {
        final Bootstrap b = new Bootstrap();
        b.group(clientWorkerGroup);
        b.channel(NioSocketChannel.class);
        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.remoteAddress(HOST, PORT);
        b.handler(new Http2ClientFrameInitializer(sslCtx));
        // Start the client.
        final Channel channel = b.connect().syncUninterruptibly().channel();
        System.out.println("Connected to [" + HOST + ':' + PORT + ']');
        final Http2ClientStreamFrameResponseHandler streamFrameResponseHandler = new Http2ClientStreamFrameResponseHandler();
        final Http2StreamChannelBootstrap streamChannelBootstrap = new Http2StreamChannelBootstrap(channel);
        final Http2StreamChannel streamChannel = streamChannelBootstrap.open().syncUninterruptibly().getNow();
        streamChannel.pipeline().addLast(streamFrameResponseHandler);
        // Send request (a HTTP/2 HEADERS frame - with ':method = GET' in this case)
        final DefaultHttp2Headers headers = new DefaultHttp2Headers();
        headers.method("GET");
        headers.path(PATH);
        headers.scheme(SSL ? "https" : "http");
        final Http2HeadersFrame headersFrame = new DefaultHttp2HeadersFrame(headers, true);
        streamChannel.writeAndFlush(headersFrame);
        System.out.println("Sent HTTP/2 GET request to " + PATH);
        // Wait for the responses (or for the latch to expire), then clean up the connections
        if (!streamFrameResponseHandler.responseSuccessfullyCompleted()) {
            System.err.println("Did not get HTTP/2 response in expected time.");
        }
        System.out.println("Finished HTTP/2 request, will close the connection.");
        // Wait until the connection is closed.
        channel.close().syncUninterruptibly();
    } finally {
        clientWorkerGroup.shutdownGracefully();
    }
}
Also used : DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) Http2HeadersFrame(io.netty.handler.codec.http2.Http2HeadersFrame) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel) ApplicationProtocolConfig(io.netty.handler.ssl.ApplicationProtocolConfig) DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Http2StreamChannelBootstrap(io.netty.handler.codec.http2.Http2StreamChannelBootstrap) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) Bootstrap(io.netty.bootstrap.Bootstrap) Http2StreamChannelBootstrap(io.netty.handler.codec.http2.Http2StreamChannelBootstrap) SslProvider(io.netty.handler.ssl.SslProvider) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel)

Example 99 with DefaultHttp2Headers

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

the class Http2MultiplexTest method writeUnknownFrame.

// TODO(buchgr): Flush from child channel
// TODO(buchgr): ChildChannel.childReadComplete()
// TODO(buchgr): GOAWAY Logic
// TODO(buchgr): Test ChannelConfig.setMaxMessagesPerRead
@Test
public void writeUnknownFrame() {
    Http2StreamChannel childChannel = newOutboundStream(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            ctx.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()));
            ctx.writeAndFlush(new DefaultHttp2UnknownFrame((byte) 99, new Http2Flags()));
            ctx.fireChannelActive();
        }
    });
    assertTrue(childChannel.isActive());
    parentChannel.runPendingTasks();
    verify(frameWriter).writeFrame(eq(codec.ctx), eq((byte) 99), eqStreamId(childChannel), any(Http2Flags.class), any(ByteBuf.class), any(ChannelPromise.class));
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) Http2TestUtil.anyChannelPromise(io.netty.handler.codec.http2.Http2TestUtil.anyChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 100 with DefaultHttp2Headers

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

the class InboundHttp2ToHttpAdapterTest method clientRequestSingleHeaderNonAsciiShouldThrow.

@Test
public void clientRequestSingleHeaderNonAsciiShouldThrow() throws Exception {
    boostrapEnv(1, 1, 1);
    final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")).scheme(new AsciiString("https")).authority(new AsciiString("example.org")).path(new AsciiString("/some/path/resource2")).add(new AsciiString("çã".getBytes(CharsetUtil.UTF_8)), new AsciiString("Ãã".getBytes(CharsetUtil.UTF_8)));
    runInChannel(clientChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            clientHandler.encoder().writeHeaders(ctxClient(), 3, http2Headers, 0, true, newPromiseClient());
            clientChannel.flush();
        }
    });
    awaitResponses();
    assertTrue(isStreamError(clientException));
}
Also used : Http2CodecUtil.getEmbeddedHttp2Exception(io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception) Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) AsciiString(io.netty.util.AsciiString) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)72 Http2Headers (io.netty.handler.codec.http2.Http2Headers)58 AsciiString (io.netty.util.AsciiString)56 ByteBuf (io.netty.buffer.ByteBuf)54 Test (org.junit.Test)49 Test (org.junit.jupiter.api.Test)32 ChannelFuture (io.netty.channel.ChannelFuture)27 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)19 ChannelPromise (io.netty.channel.ChannelPromise)17 Channel (io.netty.channel.Channel)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)15 List (java.util.List)15 ChannelInitializer (io.netty.channel.ChannelInitializer)14 ChannelPipeline (io.netty.channel.ChannelPipeline)14 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)14 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)14 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)14 ApplicationProtocolNames (io.netty.handler.ssl.ApplicationProtocolNames)14 ApplicationProtocolNegotiationHandler (io.netty.handler.ssl.ApplicationProtocolNegotiationHandler)14