Search in sources :

Example 21 with HttpServerCodec

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

the class Http2OrHttpHandler method configurePipeline.

@Override
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception {
    if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
        ctx.pipeline().addLast(Http2FrameCodecBuilder.forServer().build());
        ctx.pipeline().addLast(new Http2MultiplexHandler(new HelloWorldHttp2Handler()));
        return;
    }
    if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) {
        ctx.pipeline().addLast(new HttpServerCodec(), new HttpObjectAggregator(MAX_CONTENT_LENGTH), new HelloWorldHttp1Handler("ALPN Negotiation"));
        return;
    }
    throw new IllegalStateException("unknown protocol: " + protocol);
}
Also used : Http2MultiplexHandler(io.netty.handler.codec.http2.Http2MultiplexHandler) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HelloWorldHttp1Handler(io.netty.example.http2.helloworld.server.HelloWorldHttp1Handler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec)

Example 22 with HttpServerCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec in project zuul by Netflix.

the class PushMessageSenderInitializer method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    final ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(getPushMessageSender(pushConnectionRegistry));
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 23 with HttpServerCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec in project zuul by Netflix.

the class ClientRequestReceiverTest method multipleHostHeaders_setBadRequestStatus.

@Test
public void multipleHostHeaders_setBadRequestStatus() {
    ClientRequestReceiver receiver = new ClientRequestReceiver(null);
    EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestEncoder());
    PassportLoggingHandler loggingHandler = new PassportLoggingHandler(new DefaultRegistry());
    // Required for messages
    channel.attr(SourceAddressChannelHandler.ATTR_SERVER_LOCAL_PORT).set(1234);
    channel.pipeline().addLast(new HttpServerCodec());
    channel.pipeline().addLast(receiver);
    channel.pipeline().addLast(loggingHandler);
    HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/post");
    httpRequest.headers().add("Host", "foo.bar.com");
    httpRequest.headers().add("Host", "bar.foo.com");
    channel.writeOutbound(httpRequest);
    ByteBuf byteBuf = channel.readOutbound();
    channel.writeInbound(byteBuf);
    channel.readInbound();
    channel.close();
    HttpRequestMessage request = ClientRequestReceiver.getRequestFromChannel(channel);
    SessionContext context = request.getContext();
    assertEquals(StatusCategoryUtils.getStatusCategory(context), ZuulStatusCategory.FAILURE_CLIENT_BAD_REQUEST);
    assertEquals("Multiple Host headers", context.getError().getMessage());
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) SessionContext(com.netflix.zuul.context.SessionContext) ByteBuf(io.netty.buffer.ByteBuf) PassportLoggingHandler(com.netflix.zuul.netty.insights.PassportLoggingHandler) Test(org.junit.Test)

Example 24 with HttpServerCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec in project zuul by Netflix.

the class ClientRequestReceiverTest method maxHeaderSizeExceeded_setBadRequestStatus.

@Test
public void maxHeaderSizeExceeded_setBadRequestStatus() {
    int maxInitialLineLength = BaseZuulChannelInitializer.MAX_INITIAL_LINE_LENGTH.get();
    int maxHeaderSize = 10;
    int maxChunkSize = BaseZuulChannelInitializer.MAX_CHUNK_SIZE.get();
    ClientRequestReceiver receiver = new ClientRequestReceiver(null);
    EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestEncoder());
    PassportLoggingHandler loggingHandler = new PassportLoggingHandler(new DefaultRegistry());
    // Required for messages
    channel.attr(SourceAddressChannelHandler.ATTR_SERVER_LOCAL_PORT).set(1234);
    channel.pipeline().addLast(new HttpServerCodec(maxInitialLineLength, maxHeaderSize, maxChunkSize, false));
    channel.pipeline().addLast(receiver);
    channel.pipeline().addLast(loggingHandler);
    String str = "test-header-value";
    ByteBuf buf = Unpooled.buffer(1);
    HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/post", buf);
    for (int i = 0; i < 100; i++) {
        httpRequest.headers().add("test-header" + i, str);
    }
    channel.writeOutbound(httpRequest);
    ByteBuf byteBuf = channel.readOutbound();
    channel.writeInbound(byteBuf);
    channel.readInbound();
    channel.close();
    HttpRequestMessage request = ClientRequestReceiver.getRequestFromChannel(channel);
    assertEquals(StatusCategoryUtils.getStatusCategory(request.getContext()), ZuulStatusCategory.FAILURE_CLIENT_BAD_REQUEST);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ByteBuf(io.netty.buffer.ByteBuf) PassportLoggingHandler(com.netflix.zuul.netty.insights.PassportLoggingHandler) Test(org.junit.Test)

Example 25 with HttpServerCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec in project grpc-java by grpc.

the class ProtocolNegotiatorsTest method plaintextUpgradeNegotiator.

@Test
public void plaintextUpgradeNegotiator() throws Exception {
    LocalAddress addr = new LocalAddress("plaintextUpgradeNegotiator");
    UpgradeCodecFactory ucf = new UpgradeCodecFactory() {

        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            return new Http2ServerUpgradeCodec(FakeGrpcHttp2ConnectionHandler.newHandler());
        }
    };
    final HttpServerCodec serverCodec = new HttpServerCodec();
    final HttpServerUpgradeHandler serverUpgradeHandler = new HttpServerUpgradeHandler(serverCodec, ucf);
    Channel serverChannel = new ServerBootstrap().group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(serverCodec, serverUpgradeHandler);
        }
    }).bind(addr).sync().channel();
    FakeGrpcHttp2ConnectionHandler gh = FakeGrpcHttp2ConnectionHandler.newHandler();
    ProtocolNegotiator nego = ProtocolNegotiators.plaintextUpgrade();
    ChannelHandler ch = nego.newHandler(gh);
    WriteBufferingAndExceptionHandler wbaeh = new WriteBufferingAndExceptionHandler(ch);
    Channel channel = new Bootstrap().group(group).channel(LocalChannel.class).handler(wbaeh).register().sync().channel();
    ChannelFuture write = channel.writeAndFlush(NettyClientHandler.NOOP_MESSAGE);
    channel.connect(serverChannel.localAddress());
    boolean completed = gh.negotiated.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    if (!completed) {
        assertTrue("failed to negotiated", write.await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
        // sync should fail if we are in this block.
        write.sync();
        throw new AssertionError("neither wrote nor negotiated");
    }
    channel.close().sync();
    serverChannel.close();
    assertThat(gh.securityInfo).isNull();
    assertThat(gh.attrs.get(GrpcAttributes.ATTR_SECURITY_LEVEL)).isEqualTo(SecurityLevel.NONE);
    assertThat(gh.attrs.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)).isEqualTo(addr);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LocalAddress(io.netty.channel.local.LocalAddress) Http2ServerUpgradeCodec(io.netty.handler.codec.http2.Http2ServerUpgradeCodec) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ChannelHandler(io.netty.channel.ChannelHandler) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ClientTlsProtocolNegotiator(io.grpc.netty.ProtocolNegotiators.ClientTlsProtocolNegotiator) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) Test(org.junit.Test)

Aggregations

HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)54 ChannelPipeline (io.netty.channel.ChannelPipeline)35 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)30 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)11 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)11 HttpServerUpgradeHandler (io.netty.handler.codec.http.HttpServerUpgradeHandler)10 Test (org.junit.Test)9 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)8 Channel (io.netty.channel.Channel)7 ChannelHandler (io.netty.channel.ChannelHandler)7 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)7 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)7 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)6 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)6 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)6 WebSocketServerProtocolHandler (io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler)6 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)6 SocketChannel (io.netty.channel.socket.SocketChannel)4 HttpContentCompressor (io.netty.handler.codec.http.HttpContentCompressor)4